From b84e47344b4305783190cb16ebdfc12f4346395d Mon Sep 17 00:00:00 2001 From: Adam R Grey Date: Wed, 10 Jan 2024 21:21:31 -0500 Subject: [PATCH] db in the right place; fix QR code math --- Behavior/Behaver.cs | 19 +++++++++---------- Behavior/LinkMe.cs | 4 +--- Behavior/QRify.cs | 4 ++-- ConsoleService.cs | 1 + Program.cs | 2 +- .../TwitchInterface/TwitchInterface.cs | 8 ++++---- 6 files changed, 18 insertions(+), 20 deletions(-) diff --git a/Behavior/Behaver.cs b/Behavior/Behaver.cs index f837224..e75de05 100644 --- a/Behavior/Behaver.cs +++ b/Behavior/Behaver.cs @@ -10,14 +10,11 @@ using System.Collections.Generic; public class Behaver { - private ChattingContext _db; private List SelfAccounts { get; set; } = new List(); private User SelfUser { get; set; } public static List Behaviors { get; private set; } = new List(); internal Behaver() { - _db = new ChattingContext(); - var subtypes = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(domainAssembly => domainAssembly.GetTypes()) .Where(type => type.IsSubclassOf(typeof(Behavior)) && !type.IsAbstract && @@ -64,25 +61,27 @@ public class Behaver internal bool IsSelf(Guid AccountId) { - var acc = _db.Accounts.Find(AccountId); + var db = new ChattingContext(); + var acc = db.Accounts.Find(AccountId); return SelfAccounts.Any(acc => acc.Id == AccountId); } public void MarkSelf(Account selfAccount) { + var db = new ChattingContext(); if(SelfUser == null) { SelfUser = selfAccount.IsUser; } else if (SelfUser != selfAccount.IsUser) { - CollapseUsers(SelfUser, selfAccount.IsUser); + CollapseUsers(SelfUser, selfAccount.IsUser, db); } - SelfAccounts = _db.Accounts.Where(a => a.IsUser == SelfUser).ToList(); + SelfAccounts = db.Accounts.Where(a => a.IsUser == SelfUser).ToList(); } - public bool CollapseUsers(User primary, User secondary) + public bool CollapseUsers(User primary, User secondary, ChattingContext db) { Console.WriteLine($"{secondary.Id} is being consumed into {primary.Id}"); primary.Accounts.AddRange(secondary.Accounts); @@ -94,7 +93,7 @@ public class Behaver Console.WriteLine("accounts transferred"); try { - _db.SaveChanges(); + db.SaveChangesAsync().Wait(); } catch(Exception e) { @@ -105,11 +104,11 @@ public class Behaver Console.WriteLine("saved"); - _db.Users.Remove(secondary); + db.Users.Remove(secondary); Console.WriteLine("old account cleaned up"); try { - _db.SaveChanges(); + db.SaveChangesAsync().Wait(); } catch(Exception e) { diff --git a/Behavior/LinkMe.cs b/Behavior/LinkMe.cs index b6c012f..841e6b6 100644 --- a/Behavior/LinkMe.cs +++ b/Behavior/LinkMe.cs @@ -41,13 +41,11 @@ public class LinkClose : Behavior public override string Description => "the second half of LinkMe - this is confirmation that you are the other one"; - private ChattingContext _db; private string _pw; private Account _primary; public LinkClose(string pw, Account primary) { - _db = new ChattingContext(); _pw = pw; _primary = primary; } @@ -74,7 +72,7 @@ public class LinkClose : Behavior return true; } - if(Behaver.Instance.CollapseUsers(_primary.IsUser, secondary)) + if(Behaver.Instance.CollapseUsers(_primary.IsUser, secondary, new ChattingContext())) { await message.Channel.SendMessage("done :)"); } diff --git a/Behavior/QRify.cs b/Behavior/QRify.cs index 56ad7f2..2e81f6a 100644 --- a/Behavior/QRify.cs +++ b/Behavior/QRify.cs @@ -42,10 +42,10 @@ public class QRify : Behavior File.WriteAllText($"tmp/qr{todaysnumber}.svg", qrCodeAsSvg); if (ExternalProcess.GoPlz("convert", $"tmp/qr{todaysnumber}.svg tmp/qr{todaysnumber}.png")) { - if(message.Channel.EffectivePermissions.MaxAttachmentBytes < (ulong)(new System.IO.FileInfo($"tmp/qr{todaysnumber}.png").Length)) + if(message.Channel.EffectivePermissions.MaxAttachmentBytes >= (ulong)(new System.IO.FileInfo($"tmp/qr{todaysnumber}.png").Length)) await message.Channel.SendFile($"tmp/qr{todaysnumber}.png", null); else - await message.Channel.SendMessage("resulting qr image 2 big 4 here"); + await message.Channel.SendMessage($"resulting qr image 2 big 4 here ({(ulong)(new System.IO.FileInfo($"tmp/qr{todaysnumber}.png").Length)} / {message.Channel.EffectivePermissions.MaxAttachmentBytes})"); File.Delete($"tmp/qr{todaysnumber}.svg"); File.Delete($"tmp/qr{todaysnumber}.png"); } diff --git a/ConsoleService.cs b/ConsoleService.cs index 8698ae7..15a62b1 100644 --- a/ConsoleService.cs +++ b/ConsoleService.cs @@ -40,6 +40,7 @@ namespace vassago await t.Init(tc); ProtocolInterfaces.ProtocolList.twitchs.Add(t); } + Console.WriteLine("survived initting"); } public Task StopAsync(CancellationToken cancellationToken) diff --git a/Program.cs b/Program.cs index c0b75ad..ab7ea22 100644 --- a/Program.cs +++ b/Program.cs @@ -7,7 +7,7 @@ var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllersWithViews(); builder.Services.AddSingleton(); builder.Services.AddDbContext(options => - options.UseNpgsql(builder.Configuration.GetConnectionString("ChattingContext") ),ServiceLifetime.Transient); + options.UseNpgsql(builder.Configuration.GetConnectionString("ChattingContext") )); var app = builder.Build(); diff --git a/ProtocolInterfaces/TwitchInterface/TwitchInterface.cs b/ProtocolInterfaces/TwitchInterface/TwitchInterface.cs index 949f9a2..533db08 100644 --- a/ProtocolInterfaces/TwitchInterface/TwitchInterface.cs +++ b/ProtocolInterfaces/TwitchInterface/TwitchInterface.cs @@ -115,10 +115,10 @@ public class TwitchInterface var m = UpsertMessage(e.WhisperMessage); m.Channel.ChannelType = vassago.Models.Enumerations.ChannelType.DM; m.MentionsMe = Regex.IsMatch(e.WhisperMessage.Message?.ToLower(), $"\\b@{e.WhisperMessage.BotUsername.ToLower()}\\b"); - _db.SaveChanges(); + await _db.SaveChangesAsync(); await Behaver.Instance.ActOn(m); - _db.SaveChanges(); + await _db.SaveChangesAsync(); } private async void Client_OnMessageReceivedAsync(object sender, OnMessageReceivedArgs e) @@ -134,10 +134,10 @@ public class TwitchInterface var m = UpsertMessage(e.ChatMessage); m.MentionsMe = Regex.IsMatch(e.ChatMessage.Message?.ToLower(), $"@{e.ChatMessage.BotUsername.ToLower()}\\b") || e.ChatMessage.ChatReply?.ParentUserLogin == e.ChatMessage.BotUsername; - _db.SaveChanges(); + await _db.SaveChangesAsync(); await Behaver.Instance.ActOn(m); - _db.SaveChanges(); + await _db.SaveChangesAsync(); } private async void Client_OnConnected(object sender, OnConnectedArgs e)