From e0283e87f5c0e6885a40be72c0493a5e8af32058 Mon Sep 17 00:00:00 2001 From: adam Date: Thu, 1 May 2025 11:18:02 -0400 Subject: [PATCH] probably fix twitchsummon --- Behaver.cs | 20 +++++-- Behavior/TwitchUnsummon.cs | 2 +- .../TwitchInterface/TwitchInterface.cs | 52 ++++++++++--------- Rememberer.cs | 11 +++- 4 files changed, 54 insertions(+), 31 deletions(-) diff --git a/Behaver.cs b/Behaver.cs index 977e5f2..db4b4ca 100644 --- a/Behaver.cs +++ b/Behaver.cs @@ -20,7 +20,7 @@ public class Behaver var subtypes = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(domainAssembly => domainAssembly.GetTypes()) .Where(type => type.IsSubclassOf(typeof(vassago.Behavior.Behavior)) && !type.IsAbstract && - type.GetCustomAttributes(typeof(StaticPlzAttribute),false)?.Any() == true) + type.GetCustomAttributes(typeof(StaticPlzAttribute), false)?.Any() == true) .ToList(); foreach (var subtype in subtypes) @@ -72,7 +72,7 @@ public class Behaver public void MarkSelf(Account selfAccount) { - if(SelfUser == null) + if (SelfUser == null) { SelfUser = selfAccount.IsUser; } @@ -86,15 +86,25 @@ public class Behaver public bool CollapseUsers(User primary, User secondary) { - if(primary.Accounts == null) + if (primary.Accounts == null) primary.Accounts = new List(); - if(secondary.Accounts != null) + if (secondary.Accounts != null) primary.Accounts.AddRange(secondary.Accounts); - foreach(var a in secondary.Accounts) + foreach (var a in secondary.Accounts) { a.IsUser = primary; } secondary.Accounts.Clear(); + var uacs = Rememberer.SearchUACs(u => u.Users.FirstOrDefault(u => u.Id == secondary.Id) != null); + if (uacs.Count() > 0) + { + foreach (var uac in uacs) + { + uac.Users.RemoveAll(u => u.Id == secondary.Id); + uac.Users.Add(primary); + Rememberer.RememberUAC(uac); + } + } Rememberer.ForgetUser(secondary); Rememberer.RememberUser(primary); return true; diff --git a/Behavior/TwitchUnsummon.cs b/Behavior/TwitchUnsummon.cs index b5f75af..d6e336b 100644 --- a/Behavior/TwitchUnsummon.cs +++ b/Behavior/TwitchUnsummon.cs @@ -14,7 +14,7 @@ public class TwitchDismiss : Behavior public override bool ShouldAct(Message message) { var ti = ProtocolInterfaces.ProtocolList.twitchs.FirstOrDefault(); - Console.WriteLine($"TwitchDismiss checking. menions me? {message.MentionsMe}"); + // Console.WriteLine($"TwitchDismiss checking. menions me? {message.MentionsMe}"); if(message.MentionsMe && (Regex.IsMatch(message.Content.ToLower(), "\\bbegone\\b") || Regex.IsMatch(message.Content.ToLower(), "\\bfuck off\\b"))) { diff --git a/ProtocolInterfaces/TwitchInterface/TwitchInterface.cs b/ProtocolInterfaces/TwitchInterface/TwitchInterface.cs index 92b598d..a83246a 100644 --- a/ProtocolInterfaces/TwitchInterface/TwitchInterface.cs +++ b/ProtocolInterfaces/TwitchInterface/TwitchInterface.cs @@ -15,7 +15,7 @@ namespace vassago.TwitchInterface; internal class unifiedTwitchMessage { - public unifiedTwitchMessage(ChatMessage chatMessage){} + public unifiedTwitchMessage(ChatMessage chatMessage) { } } public class TwitchInterface @@ -110,7 +110,7 @@ public class TwitchInterface //data eived Console.WriteLine($"#{e.ChatMessage.Channel}[{DateTime.Now}][{e.ChatMessage.DisplayName} [id={e.ChatMessage.Username}]][msg id: {e.ChatMessage.Id}] {e.ChatMessage.Message}"); - //translate to internal, upsert + //translate to internal, upsert var m = UpsertMessage(e.ChatMessage); m.Reply = (t) => { return Task.Run(() => { client.SendReply(e.ChatMessage.Channel, e.ChatMessage.Id, t); }); }; m.Channel.ChannelType = vassago.Models.Enumerations.ChannelType.Normal; @@ -142,14 +142,15 @@ public class TwitchInterface private Account UpsertAccount(string username, Channel inChannel) { - Console.WriteLine($"upserting twitch account. username: {username}. inChannel: {inChannel?.Id}"); + //Console.WriteLine($"upserting twitch account. username: {username}. inChannel: {inChannel?.Id}"); var acc = Rememberer.SearchAccount(ui => ui.ExternalId == username && ui.SeenInChannel.ExternalId == inChannel.ExternalId); - Console.WriteLine($"upserting twitch account, retrieved {acc?.Id}."); + // Console.WriteLine($"upserting twitch account, retrieved {acc?.Id}."); if (acc != null) { Console.WriteLine($"acc's usser: {acc.IsUser?.Id}"); } - acc ??= new Account() { + acc ??= new Account() + { IsUser = Rememberer.SearchUser( u => u.Accounts.Any(a => a.ExternalId == username && a.Protocol == PROTOCOL)) ?? new vassago.Models.User() @@ -161,16 +162,16 @@ public class TwitchInterface acc.Protocol = PROTOCOL; acc.SeenInChannel = inChannel; - Console.WriteLine($"we asked rememberer to search for acc's user. {acc.IsUser?.Id}"); - if (acc.IsUser != null) - { - Console.WriteLine($"user has record of {acc.IsUser.Accounts?.Count ?? 0} accounts"); - } + // Console.WriteLine($"we asked rememberer to search for acc's user. {acc.IsUser?.Id}"); + // if (acc.IsUser != null) + // { + // Console.WriteLine($"user has record of {acc.IsUser.Accounts?.Count ?? 0} accounts"); + // } acc.IsUser ??= new vassago.Models.User() { Accounts = [acc] }; - if (inChannel.Users?.Count > 0) - { - Console.WriteLine($"channel has {inChannel.Users.Count} accounts"); - } + // if (inChannel.Users?.Count > 0) + // { + // Console.WriteLine($"channel has {inChannel.Users.Count} accounts"); + // } Rememberer.RememberAccount(acc); inChannel.Users ??= []; if (!inChannel.Users.Contains(acc)) @@ -187,7 +188,7 @@ public class TwitchInterface && ci.Protocol == PROTOCOL); if (c == null) { - Console.WriteLine($"couldn't find channel under protocol {PROTOCOL} with externalId {channelName}"); + // Console.WriteLine($"couldn't find channel under protocol {PROTOCOL} with externalId {channelName}"); c = new Channel() { Users = [] @@ -206,7 +207,7 @@ public class TwitchInterface c = Rememberer.RememberChannel(c); var selfAccountInChannel = c.Users?.FirstOrDefault(a => a.ExternalId == selfAccountInProtocol.ExternalId); - if(selfAccountInChannel == null) + if (selfAccountInChannel == null) { selfAccountInChannel = UpsertAccount(selfAccountInProtocol.Username, c); } @@ -215,11 +216,11 @@ public class TwitchInterface } private Channel UpsertDMChannel(string whisperWith) { -Channel c = Rememberer.SearchChannel(ci => ci.ExternalId == $"w_{whisperWith}" - && ci.Protocol == PROTOCOL); + Channel c = Rememberer.SearchChannel(ci => ci.ExternalId == $"w_{whisperWith}" + && ci.Protocol == PROTOCOL); if (c == null) { - Console.WriteLine($"couldn't find channel under protocol {PROTOCOL}, whisper with {whisperWith}"); + // Console.WriteLine($"couldn't find channel under protocol {PROTOCOL}, whisper with {whisperWith}"); c = new Channel() { Users = [] @@ -233,29 +234,32 @@ Channel c = Rememberer.SearchChannel(ci => ci.ExternalId == $"w_{whisperWith}" c.Protocol = PROTOCOL; c.ParentChannel = protocolAsChannel; c.SubChannels = c.SubChannels ?? new List(); - c.SendMessage = (t) => { return Task.Run(() => { + c.SendMessage = (t) => + { + return Task.Run(() => + { try { client.SendWhisper(whisperWith, t); } - catch(Exception e) + catch (Exception e) { Console.Error.WriteLine(e); } }); - }; + }; c.SendFile = (f, t) => { throw new InvalidOperationException($"twitch cannot send files"); }; c = Rememberer.RememberChannel(c); var selfAccountInChannel = c.Users.FirstOrDefault(a => a.ExternalId == selfAccountInProtocol.ExternalId); - if(selfAccountInChannel == null) + if (selfAccountInChannel == null) { selfAccountInChannel = UpsertAccount(selfAccountInChannel.Username, c); } return c; - } + } //n.b., I see you future adam. "we should unify these, they're redundant". //ah, but that's the trick, they aren't! twitchlib has a common base class, but diff --git a/Rememberer.cs b/Rememberer.cs index 704cb55..b3da02d 100644 --- a/Rememberer.cs +++ b/Rememberer.cs @@ -179,7 +179,16 @@ public static class Rememberer dbAccessSemaphore.Release(); return toReturn; } - public static void RememberUAC(UAC toRemember) + public static List SearchUACs(Expression> predicate) + { + List toReturn; + dbAccessSemaphore.Wait(); + toReturn = db.UACs.Include(uac => uac.Users).Include(uac => uac.Channels).Include(uac => uac.AccountInChannels) + .Where(predicate).ToList(); + dbAccessSemaphore.Release(); + return toReturn; + } + public static void RememberUAC(UAC toRemember) { dbAccessSemaphore.Wait(); db.Update(toRemember);