forked from adam/discord-bot-shtik
rememberer is now a singleton, channels retrieve accounts
All checks were successful
gitea.arg.rip/vassago/pipeline/head This commit looks good
All checks were successful
gitea.arg.rip/vassago/pipeline/head This commit looks good
see 57
This commit is contained in:
parent
736fc3643b
commit
a4c2ff1ea4
29
Behaver.cs
29
Behaver.cs
@ -17,6 +17,7 @@ public class Behaver
|
||||
private List<Account> SelfAccounts { get; set; } = new List<Account>();
|
||||
private User SelfUser { get; set; }
|
||||
public static List<vassago.Behavior.Behavior> Behaviors { get; private set; } = new List<vassago.Behavior.Behavior>();
|
||||
private static Rememberer r = Rememberer.Instance;
|
||||
internal Behaver()
|
||||
{
|
||||
var subtypes = AppDomain.CurrentDomain.GetAssemblies()
|
||||
@ -44,7 +45,7 @@ public class Behaver
|
||||
public async Task<bool> ActOn(Message message)
|
||||
{
|
||||
//TODO: this is yet another hit to the database, and a big one. cache them in memory! there needs to be a feasibly-viewable amount, anyway.
|
||||
var matchingUACs = Rememberer.MatchUACs(message);
|
||||
var matchingUACs = r.MatchUACs(message);
|
||||
message.TranslatedContent = message.Content;
|
||||
foreach (var uacMatch in matchingUACs)
|
||||
{
|
||||
@ -79,7 +80,7 @@ public class Behaver
|
||||
message.ActedOn = true;
|
||||
behaviorsActedOn.Add("generic question fallback");
|
||||
}
|
||||
Rememberer.RememberMessage(message);
|
||||
r.RememberMessage(message);
|
||||
ForwardToKafka(message, behaviorsActedOn, matchingUACs);
|
||||
return message.ActedOn;
|
||||
}
|
||||
@ -117,7 +118,7 @@ public class Behaver
|
||||
|
||||
internal bool IsSelf(Guid AccountId)
|
||||
{
|
||||
var acc = Rememberer.SearchAccount(a => a.Id == AccountId);
|
||||
var acc = r.SearchAccount(a => a.Id == AccountId);
|
||||
|
||||
return SelfAccounts.Any(acc => acc.Id == AccountId);
|
||||
}
|
||||
@ -132,8 +133,8 @@ public class Behaver
|
||||
{
|
||||
CollapseUsers(SelfUser, selfAccount.IsUser);
|
||||
}
|
||||
SelfAccounts = Rememberer.SearchAccounts(a => a.IsUser == SelfUser);
|
||||
Rememberer.RememberAccount(selfAccount);
|
||||
SelfAccounts = r.SearchAccounts(a => a.IsUser == SelfUser);
|
||||
r.RememberAccount(selfAccount);
|
||||
}
|
||||
|
||||
public bool CollapseUsers(User primary, User secondary)
|
||||
@ -147,18 +148,18 @@ public class Behaver
|
||||
a.IsUser = primary;
|
||||
}
|
||||
secondary.Accounts.Clear();
|
||||
var uacs = Rememberer.SearchUACs(u => u.Users.FirstOrDefault(u => u.Id == secondary.Id) != null);
|
||||
var uacs = r.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);
|
||||
r.RememberUAC(uac);
|
||||
}
|
||||
}
|
||||
Rememberer.ForgetUser(secondary);
|
||||
Rememberer.RememberUser(primary);
|
||||
r.ForgetUser(secondary);
|
||||
r.RememberUser(primary);
|
||||
return true;
|
||||
}
|
||||
private ProtocolInterface fetchInterface(Channel ch)
|
||||
@ -177,7 +178,7 @@ public class Behaver
|
||||
}
|
||||
public async Task<int> SendMessage(Guid channelId, string text)
|
||||
{
|
||||
var channel = Rememberer.ChannelDetail(channelId);
|
||||
var channel = r.ChannelDetail(channelId);
|
||||
if (channel == null)
|
||||
return 404;
|
||||
var iprotocol = fetchInterface(channel);
|
||||
@ -189,7 +190,7 @@ public class Behaver
|
||||
public async Task<int> React(Guid messageId, string reaction)
|
||||
{
|
||||
Console.WriteLine($"sanity check: behaver is reacting, {messageId}, {reaction}");
|
||||
var message = Rememberer.MessageDetail(messageId);
|
||||
var message = r.MessageDetail(messageId);
|
||||
if (message == null)
|
||||
{
|
||||
Console.Error.WriteLine($"message {messageId} not found");
|
||||
@ -212,7 +213,7 @@ public class Behaver
|
||||
}
|
||||
public async Task<int> Reply(Guid messageId, string text)
|
||||
{
|
||||
var message = Rememberer.MessageDetail(messageId);
|
||||
var message = r.MessageDetail(messageId);
|
||||
if (message == null)
|
||||
{
|
||||
Console.WriteLine($"message {messageId} not found");
|
||||
@ -228,7 +229,7 @@ public class Behaver
|
||||
}
|
||||
public async Task<int> SendFile(Guid channelId, string path, string accompanyingText)
|
||||
{
|
||||
var channel = Rememberer.ChannelDetail(channelId);
|
||||
var channel = r.ChannelDetail(channelId);
|
||||
if (channel == null)
|
||||
return 404;
|
||||
var iprotocol = fetchInterface(channel);
|
||||
@ -239,7 +240,7 @@ public class Behaver
|
||||
}
|
||||
public async Task<int> SendFile(Guid channelId, string base64dData, string filename, string accompanyingText)
|
||||
{
|
||||
var channel = Rememberer.ChannelDetail(channelId);
|
||||
var channel = r.ChannelDetail(channelId);
|
||||
if (channel == null)
|
||||
return 404;
|
||||
var iprotocol = fetchInterface(channel);
|
||||
|
@ -12,6 +12,7 @@ public abstract class Behavior
|
||||
{
|
||||
//recommendation: set up your UACs in your constructor.
|
||||
public abstract Task<bool> ActOn(Message message);
|
||||
protected static Rememberer rememberer = Rememberer.Instance;
|
||||
|
||||
public virtual bool ShouldAct(Message message, List<UAC> matchedUACs)
|
||||
{
|
||||
|
@ -16,7 +16,7 @@ public class Ripcord: Behavior
|
||||
|
||||
public Ripcord()
|
||||
{
|
||||
myUAC = Rememberer.SearchUAC(uac => uac.OwnerId == uacID);
|
||||
myUAC = rememberer.SearchUAC(uac => uac.OwnerId == uacID);
|
||||
if (myUAC == null)
|
||||
{
|
||||
myUAC = new()
|
||||
@ -26,7 +26,7 @@ public class Ripcord: Behavior
|
||||
Description = @"matching this means you can tell the bot to shutdown, now"
|
||||
};
|
||||
}
|
||||
Rememberer.RememberUAC(myUAC);
|
||||
rememberer.RememberUAC(myUAC);
|
||||
}
|
||||
public override bool ShouldAct(Message message, List<UAC> matchedUACs)
|
||||
{
|
||||
|
@ -16,7 +16,7 @@ public class TwitchSummon : Behavior
|
||||
|
||||
public TwitchSummon()
|
||||
{
|
||||
myUAC = Rememberer.SearchUAC(uac => uac.OwnerId == uacID);
|
||||
myUAC = rememberer.SearchUAC(uac => uac.OwnerId == uacID);
|
||||
if (myUAC == null)
|
||||
{
|
||||
myUAC = new()
|
||||
@ -26,7 +26,7 @@ public class TwitchSummon : Behavior
|
||||
Description = @"matching this means you can summon the bot <i>to</i> <b>any</b> twitch channel"
|
||||
};
|
||||
}
|
||||
Rememberer.RememberUAC(myUAC);
|
||||
rememberer.RememberUAC(myUAC);
|
||||
}
|
||||
internal static TwitchInterface.TwitchInterface getAnyTwitchInterface()
|
||||
{
|
||||
|
@ -37,7 +37,7 @@ public class Webhook : Behavior
|
||||
Console.WriteLine($"{kvp[0]}: {kvp[1]}");
|
||||
}
|
||||
var changed = false;
|
||||
var myUAC = Rememberer.SearchUAC(uac => uac.OwnerId == conf.uacID);
|
||||
var myUAC = rememberer.SearchUAC(uac => uac.OwnerId == conf.uacID);
|
||||
if (myUAC == null)
|
||||
{
|
||||
myUAC = new()
|
||||
@ -47,7 +47,7 @@ public class Webhook : Behavior
|
||||
Description = conf.Description
|
||||
};
|
||||
changed = true;
|
||||
Rememberer.RememberUAC(myUAC);
|
||||
rememberer.RememberUAC(myUAC);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -63,7 +63,7 @@ public class Webhook : Behavior
|
||||
}
|
||||
}
|
||||
if (changed)
|
||||
Rememberer.RememberUAC(myUAC);
|
||||
rememberer.RememberUAC(myUAC);
|
||||
}
|
||||
}
|
||||
|
||||
@ -88,7 +88,7 @@ public class Webhook : Behavior
|
||||
{
|
||||
var webhookableMessageContent = message.Content.Substring(message.Content.IndexOf(triggerTarget) + triggerTarget.Length + 1);
|
||||
Console.WriteLine($"webhookable content: {webhookableMessageContent}");
|
||||
var uacConf = Rememberer.SearchUAC(uac => uac.OwnerId == wh.uacID);
|
||||
var uacConf = rememberer.SearchUAC(uac => uac.OwnerId == wh.uacID);
|
||||
if (uacConf.Users.Contains(message.Author.IsUser) || uacConf.Channels.Contains(message.Channel) || uacConf.AccountInChannels.Contains(message.Author))
|
||||
{
|
||||
Console.WriteLine("webhook UAC passed, preparing WebhookActionOrder");
|
||||
|
@ -28,6 +28,7 @@ public class DiscordInterface : ProtocolInterface
|
||||
private static readonly SemaphoreSlim discordChannelSetup = new(1, 1);
|
||||
private Channel protocolAsChannel;
|
||||
public override Channel SelfChannel { get => protocolAsChannel; }
|
||||
private static Rememberer r = Rememberer.Instance;
|
||||
|
||||
public async Task Init(string config)
|
||||
{
|
||||
@ -53,7 +54,7 @@ public class DiscordInterface : ProtocolInterface
|
||||
|
||||
try
|
||||
{
|
||||
protocolAsChannel = Rememberer.SearchChannel(c => c.ParentChannel == null && c.Protocol == Protocol);
|
||||
protocolAsChannel = r.SearchChannel(c => c.ParentChannel == null && c.Protocol == Protocol);
|
||||
if (protocolAsChannel == null)
|
||||
{
|
||||
protocolAsChannel = new Channel()
|
||||
@ -75,7 +76,7 @@ public class DiscordInterface : ProtocolInterface
|
||||
Console.WriteLine($"discord, channel with id {protocolAsChannel.Id}, already exists");
|
||||
}
|
||||
protocolAsChannel.DisplayName = "discord (itself)";
|
||||
protocolAsChannel = Rememberer.RememberChannel(protocolAsChannel);
|
||||
protocolAsChannel = r.RememberChannel(protocolAsChannel);
|
||||
Console.WriteLine($"protocol as channel addeed; {protocolAsChannel}");
|
||||
}
|
||||
finally
|
||||
@ -192,7 +193,7 @@ public class DiscordInterface : ProtocolInterface
|
||||
}
|
||||
internal static vassago.Models.Attachment UpsertAttachment(IAttachment dAttachment)
|
||||
{
|
||||
var a = Rememberer.SearchAttachment(ai => ai.ExternalId == dAttachment.Id)
|
||||
var a = r.SearchAttachment(ai => ai.ExternalId == dAttachment.Id)
|
||||
?? new vassago.Models.Attachment();
|
||||
|
||||
a.ContentType = dAttachment.ContentType;
|
||||
@ -200,12 +201,12 @@ public class DiscordInterface : ProtocolInterface
|
||||
a.Filename = dAttachment.Filename;
|
||||
a.Size = dAttachment.Size;
|
||||
a.Source = new Uri(dAttachment.Url);
|
||||
Rememberer.RememberAttachment(a);
|
||||
r.RememberAttachment(a);
|
||||
return a;
|
||||
}
|
||||
internal Message UpsertMessage(IUserMessage dMessage)
|
||||
{
|
||||
var m = Rememberer.SearchMessage(mi => mi.ExternalId == dMessage.Id.ToString() && mi.Protocol == Protocol)
|
||||
var m = r.SearchMessage(mi => mi.ExternalId == dMessage.Id.ToString() && mi.Protocol == Protocol)
|
||||
?? new()
|
||||
{
|
||||
Protocol = Protocol
|
||||
@ -231,13 +232,13 @@ public class DiscordInterface : ProtocolInterface
|
||||
m.MentionsMe = (dMessage.Author.Id != client.CurrentUser.Id
|
||||
&& (dMessage.MentionedUserIds?.FirstOrDefault(muid => muid == client.CurrentUser.Id) > 0));
|
||||
|
||||
Rememberer.RememberMessage(m);
|
||||
r.RememberMessage(m);
|
||||
Console.WriteLine($"received message; author: {m.Author.DisplayName}, {m.Author.Id}. messageid:{m.Id}");
|
||||
return m;
|
||||
}
|
||||
internal Channel UpsertChannel(IMessageChannel channel)
|
||||
{
|
||||
Channel c = Rememberer.SearchChannel(ci => ci.ExternalId == channel.Id.ToString() && ci.Protocol == Protocol);
|
||||
Channel c = r.SearchChannel(ci => ci.ExternalId == channel.Id.ToString() && ci.Protocol == Protocol);
|
||||
if (c == null)
|
||||
{
|
||||
Console.WriteLine($"couldn't find channel under protocol {Protocol} with externalId {channel.Id.ToString()}");
|
||||
@ -290,7 +291,7 @@ public class DiscordInterface : ProtocolInterface
|
||||
Channel parentChannel = null;
|
||||
if (channel is IGuildChannel)
|
||||
{
|
||||
parentChannel = Rememberer.SearchChannel(c => c.ExternalId == (channel as IGuildChannel).Guild.Id.ToString() && c.Protocol == Protocol);
|
||||
parentChannel = r.SearchChannel(c => c.ExternalId == (channel as IGuildChannel).Guild.Id.ToString() && c.Protocol == Protocol);
|
||||
if (parentChannel is null)
|
||||
{
|
||||
Console.Error.WriteLine("why am I still null?");
|
||||
@ -311,7 +312,7 @@ public class DiscordInterface : ProtocolInterface
|
||||
parentChannel.SubChannels.Add(c);
|
||||
}
|
||||
|
||||
c = Rememberer.RememberChannel(c);
|
||||
c = r.RememberChannel(c);
|
||||
|
||||
//Console.WriteLine($"no one knows how to make good tooling. c.users.first, which needs client currentuser id tostring. c: {c}, c.Users {c.Users}, client: {client}, client.CurrentUser: {client.CurrentUser}, client.currentUser.Id: {client.CurrentUser.Id}");
|
||||
var selfAccountInChannel = c.Users?.FirstOrDefault(a => a.ExternalId == client.CurrentUser.Id.ToString());
|
||||
@ -324,7 +325,7 @@ public class DiscordInterface : ProtocolInterface
|
||||
}
|
||||
internal Channel UpsertChannel(IGuild channel)
|
||||
{
|
||||
Channel c = Rememberer.SearchChannel(ci => ci.ExternalId == channel.Id.ToString() && ci.Protocol == Protocol);
|
||||
Channel c = r.SearchChannel(ci => ci.ExternalId == channel.Id.ToString() && ci.Protocol == Protocol);
|
||||
if (c == null)
|
||||
{
|
||||
Console.WriteLine($"couldn't find channel under protocol {Protocol} with externalId {channel.Id.ToString()}");
|
||||
@ -340,11 +341,11 @@ public class DiscordInterface : ProtocolInterface
|
||||
c.SubChannels ??= [];
|
||||
c.MaxAttachmentBytes = channel.MaxUploadLimit;
|
||||
|
||||
return Rememberer.RememberChannel(c);
|
||||
return r.RememberChannel(c);
|
||||
}
|
||||
internal static Account UpsertAccount(IUser discordUser, Channel inChannel)
|
||||
{
|
||||
var acc = Rememberer.SearchAccount(ui => ui.ExternalId == discordUser.Id.ToString() && ui.SeenInChannel.Id == inChannel.Id);
|
||||
var acc = r.SearchAccount(ui => ui.ExternalId == discordUser.Id.ToString() && ui.SeenInChannel.Id == inChannel.Id);
|
||||
Console.WriteLine($"upserting account, retrieved {acc?.Id}.");
|
||||
if (acc != null)
|
||||
{
|
||||
@ -352,7 +353,7 @@ public class DiscordInterface : ProtocolInterface
|
||||
}
|
||||
acc ??= new Account()
|
||||
{
|
||||
IsUser = Rememberer.SearchUser(u => u.Accounts.Any(a => a.ExternalId == discordUser.Id.ToString() && a.Protocol == Protocol))
|
||||
IsUser = r.SearchUser(u => u.Accounts.Any(a => a.ExternalId == discordUser.Id.ToString() && a.Protocol == Protocol))
|
||||
?? new User()
|
||||
};
|
||||
|
||||
@ -372,12 +373,12 @@ public class DiscordInterface : ProtocolInterface
|
||||
{
|
||||
Console.WriteLine($"channel has {inChannel.Users.Count} accounts");
|
||||
}
|
||||
Rememberer.RememberAccount(acc);
|
||||
r.RememberAccount(acc);
|
||||
inChannel.Users ??= [];
|
||||
if (!inChannel.Users.Contains(acc))
|
||||
{
|
||||
inChannel.Users.Add(acc);
|
||||
Rememberer.RememberChannel(inChannel);
|
||||
r.RememberChannel(inChannel);
|
||||
}
|
||||
return acc;
|
||||
}
|
||||
@ -385,7 +386,7 @@ public class DiscordInterface : ProtocolInterface
|
||||
private static async Task<int> AttemptReact(IUserMessage msg, string e)
|
||||
{
|
||||
Console.WriteLine("discord attempting to react");
|
||||
var c = Rememberer.SearchChannel(c => c.ExternalId == msg.Channel.Id.ToString());// db.Channels.FirstOrDefault(c => c.ExternalId == msg.Channel.Id.ToString());
|
||||
var c = r.SearchChannel(c => c.ExternalId == msg.Channel.Id.ToString());// db.Channels.FirstOrDefault(c => c.ExternalId == msg.Channel.Id.ToString());
|
||||
//var preferredEmote = c.EmoteOverrides?[e] ?? e; //TODO: emote overrides
|
||||
var preferredEmote = e;
|
||||
if (Emoji.TryParse(preferredEmote, out Emoji emoji))
|
||||
|
@ -21,6 +21,7 @@ public class TwitchInterface : ProtocolInterface
|
||||
private Channel protocolAsChannel;
|
||||
public override Channel SelfChannel { get => protocolAsChannel;}
|
||||
private Account selfAccountInProtocol;
|
||||
private static Rememberer r = Rememberer.Instance;
|
||||
TwitchClient client;
|
||||
|
||||
private async Task SetupTwitchChannel()
|
||||
@ -29,7 +30,7 @@ public class TwitchInterface : ProtocolInterface
|
||||
|
||||
try
|
||||
{
|
||||
protocolAsChannel = Rememberer.SearchChannel(c => c.ParentChannel == null && c.Protocol == Protocol);
|
||||
protocolAsChannel = r.SearchChannel(c => c.ParentChannel == null && c.Protocol == Protocol);
|
||||
if (protocolAsChannel == null)
|
||||
{
|
||||
protocolAsChannel = new Channel()
|
||||
@ -46,7 +47,7 @@ public class TwitchInterface : ProtocolInterface
|
||||
SubChannels = []
|
||||
};
|
||||
protocolAsChannel.DisplayName = "twitch (itself)";
|
||||
protocolAsChannel = Rememberer.RememberChannel(protocolAsChannel);
|
||||
protocolAsChannel = r.RememberChannel(protocolAsChannel);
|
||||
Console.WriteLine($"protocol as channle added; {protocolAsChannel}");
|
||||
}
|
||||
else
|
||||
@ -139,7 +140,7 @@ public class TwitchInterface : ProtocolInterface
|
||||
private Account UpsertAccount(string username, Channel inChannel)
|
||||
{
|
||||
//Console.WriteLine($"upserting twitch account. username: {username}. inChannel: {inChannel?.Id}");
|
||||
var acc = Rememberer.SearchAccount(ui => ui.ExternalId == username && ui.SeenInChannel.ExternalId == inChannel.ExternalId);
|
||||
var acc = r.SearchAccount(ui => ui.ExternalId == username && ui.SeenInChannel.ExternalId == inChannel.ExternalId);
|
||||
// Console.WriteLine($"upserting twitch account, retrieved {acc?.Id}.");
|
||||
if (acc != null)
|
||||
{
|
||||
@ -147,7 +148,7 @@ public class TwitchInterface : ProtocolInterface
|
||||
}
|
||||
acc ??= new Account()
|
||||
{
|
||||
IsUser = Rememberer.SearchUser(
|
||||
IsUser = r.SearchUser(
|
||||
u => u.Accounts.Any(a => a.ExternalId == username && a.Protocol == Protocol))
|
||||
?? new vassago.Models.User()
|
||||
};
|
||||
@ -168,19 +169,19 @@ public class TwitchInterface : ProtocolInterface
|
||||
// {
|
||||
// Console.WriteLine($"channel has {inChannel.Users.Count} accounts");
|
||||
// }
|
||||
Rememberer.RememberAccount(acc);
|
||||
r.RememberAccount(acc);
|
||||
inChannel.Users ??= [];
|
||||
if (!inChannel.Users.Contains(acc))
|
||||
{
|
||||
inChannel.Users.Add(acc);
|
||||
Rememberer.RememberChannel(inChannel);
|
||||
r.RememberChannel(inChannel);
|
||||
}
|
||||
return acc;
|
||||
}
|
||||
|
||||
private Channel UpsertChannel(string channelName)
|
||||
{
|
||||
Channel c = Rememberer.SearchChannel(ci => ci.ExternalId == channelName
|
||||
Channel c = r.SearchChannel(ci => ci.ExternalId == channelName
|
||||
&& ci.Protocol == Protocol);
|
||||
if (c == null)
|
||||
{
|
||||
@ -198,7 +199,7 @@ public class TwitchInterface : ProtocolInterface
|
||||
c.Protocol = Protocol;
|
||||
c.ParentChannel = protocolAsChannel;
|
||||
c.SubChannels = c.SubChannels ?? new List<Channel>();
|
||||
c = Rememberer.RememberChannel(c);
|
||||
c = r.RememberChannel(c);
|
||||
|
||||
var selfAccountInChannel = c.Users?.FirstOrDefault(a => a.ExternalId == selfAccountInProtocol.ExternalId);
|
||||
if (selfAccountInChannel == null)
|
||||
@ -210,7 +211,7 @@ public class TwitchInterface : ProtocolInterface
|
||||
}
|
||||
private Channel UpsertDMChannel(string whisperWith)
|
||||
{
|
||||
Channel c = Rememberer.SearchChannel(ci => ci.ExternalId == $"w_{whisperWith}"
|
||||
Channel c = r.SearchChannel(ci => ci.ExternalId == $"w_{whisperWith}"
|
||||
&& ci.Protocol == Protocol);
|
||||
if (c == null)
|
||||
{
|
||||
@ -228,7 +229,7 @@ public class TwitchInterface : ProtocolInterface
|
||||
c.Protocol = Protocol;
|
||||
c.ParentChannel = protocolAsChannel;
|
||||
c.SubChannels = c.SubChannels ?? new List<Channel>();
|
||||
c = Rememberer.RememberChannel(c);
|
||||
c = r.RememberChannel(c);
|
||||
|
||||
var selfAccountInChannel = c.Users.FirstOrDefault(a => a.ExternalId == selfAccountInProtocol.ExternalId);
|
||||
if (selfAccountInChannel == null)
|
||||
@ -244,7 +245,7 @@ public class TwitchInterface : ProtocolInterface
|
||||
//none of the features we care about are on it!
|
||||
private Message UpsertMessage(ChatMessage chatMessage)
|
||||
{
|
||||
var m = Rememberer.SearchMessage(mi => mi.ExternalId == chatMessage.Id && mi.Protocol == Protocol)
|
||||
var m = r.SearchMessage(mi => mi.ExternalId == chatMessage.Id && mi.Protocol == Protocol)
|
||||
?? new()
|
||||
{
|
||||
Protocol = Protocol,
|
||||
@ -255,7 +256,7 @@ public class TwitchInterface : ProtocolInterface
|
||||
m.Channel = UpsertChannel(chatMessage.Channel);
|
||||
m.Author = UpsertAccount(chatMessage.Username, m.Channel);
|
||||
m.MentionsMe = Regex.IsMatch(m.Content?.ToLower(), $"@\\b{selfAccountInProtocol.Username.ToLower()}\\b");
|
||||
Rememberer.RememberMessage(m);
|
||||
r.RememberMessage(m);
|
||||
return m;
|
||||
}
|
||||
//n.b., I see you future adam. "we should unify these, they're redundant".
|
||||
@ -264,7 +265,7 @@ public class TwitchInterface : ProtocolInterface
|
||||
private Message UpsertMessage(WhisperMessage whisperMessage)
|
||||
{
|
||||
//WhisperMessage.Id corresponds to chatMessage.Id. \*eye twitch*
|
||||
var m = Rememberer.SearchMessage(mi => mi.ExternalId == whisperMessage.MessageId && mi.Protocol == Protocol)
|
||||
var m = r.SearchMessage(mi => mi.ExternalId == whisperMessage.MessageId && mi.Protocol == Protocol)
|
||||
?? new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
@ -276,7 +277,7 @@ public class TwitchInterface : ProtocolInterface
|
||||
m.Channel = UpsertDMChannel(whisperMessage.Username);
|
||||
m.Author = UpsertAccount(whisperMessage.Username, m.Channel);
|
||||
m.MentionsMe = Regex.IsMatch(m.Content?.ToLower(), $"@\\b{selfAccountInProtocol.Username.ToLower()}\\b");
|
||||
Rememberer.RememberMessage(m);
|
||||
r.RememberMessage(m);
|
||||
return m;
|
||||
}
|
||||
|
||||
|
111
Rememberer.cs
111
Rememberer.cs
@ -4,18 +4,38 @@ using System.Linq.Expressions;
|
||||
using vassago.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
public static class Rememberer
|
||||
public class Rememberer
|
||||
{
|
||||
private readonly SemaphoreSlim dbAccessSemaphore = new(1, 1);
|
||||
private readonly ChattingContext db = new();
|
||||
private List<Channel> channels;
|
||||
private bool channelCacheDirty = true;
|
||||
private Rememberer() { }
|
||||
private static Rememberer _instance = null;
|
||||
public static Rememberer Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
private static readonly SemaphoreSlim dbAccessSemaphore = new(1, 1);
|
||||
private static readonly ChattingContext db = new();
|
||||
private static List<Channel> channels;
|
||||
private static bool channelCacheDirty = true;
|
||||
|
||||
private static void cacheChannels()
|
||||
lock (instantiationLock)
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
_instance = new Rememberer();
|
||||
}
|
||||
}
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
private static readonly object instantiationLock = new();
|
||||
|
||||
private void cacheChannels()
|
||||
{
|
||||
dbAccessSemaphore.Wait();
|
||||
channels = db.Channels.ToList();
|
||||
Console.WriteLine($"caching channels. {channels.Count} channels retrieved");
|
||||
foreach (Channel ch in channels)
|
||||
{
|
||||
if (ch.ParentChannelId != null)
|
||||
@ -27,17 +47,13 @@ public static class Rememberer
|
||||
ch.ParentChannel.SubChannels.Add(ch);
|
||||
}
|
||||
}
|
||||
if (ch.Messages?.Count > 0)
|
||||
{
|
||||
Console.WriteLine($"{ch.DisplayName} got {ch.Messages.Count} messages");
|
||||
}
|
||||
ch.SubChannels ??= [];
|
||||
}
|
||||
channelCacheDirty = false;
|
||||
dbAccessSemaphore.Release();
|
||||
}
|
||||
|
||||
public static Account SearchAccount(Expression<Func<Account, bool>> predicate)
|
||||
public Account SearchAccount(Expression<Func<Account, bool>> predicate)
|
||||
{
|
||||
Account toReturn;
|
||||
dbAccessSemaphore.Wait();
|
||||
@ -45,7 +61,7 @@ public static class Rememberer
|
||||
dbAccessSemaphore.Release();
|
||||
return toReturn;
|
||||
}
|
||||
public static List<Account> SearchAccounts(Expression<Func<Account, bool>> predicate)
|
||||
public List<Account> SearchAccounts(Expression<Func<Account, bool>> predicate)
|
||||
{
|
||||
List<Account> toReturn;
|
||||
dbAccessSemaphore.Wait();
|
||||
@ -53,7 +69,7 @@ public static class Rememberer
|
||||
dbAccessSemaphore.Release();
|
||||
return toReturn;
|
||||
}
|
||||
public static Attachment SearchAttachment(Expression<Func<Attachment, bool>> predicate)
|
||||
public Attachment SearchAttachment(Expression<Func<Attachment, bool>> predicate)
|
||||
{
|
||||
Attachment toReturn;
|
||||
dbAccessSemaphore.Wait();
|
||||
@ -61,13 +77,13 @@ public static class Rememberer
|
||||
dbAccessSemaphore.Release();
|
||||
return toReturn;
|
||||
}
|
||||
public static Channel SearchChannel(Func<Channel, bool> predicate)
|
||||
public Channel SearchChannel(Func<Channel, bool> predicate)
|
||||
{
|
||||
if (channelCacheDirty)
|
||||
Task.Run(() => cacheChannels()).Wait();
|
||||
return channels.FirstOrDefault(predicate);
|
||||
}
|
||||
public static Message SearchMessage(Expression<Func<Message, bool>> predicate)
|
||||
public Message SearchMessage(Expression<Func<Message, bool>> predicate)
|
||||
{
|
||||
Message toReturn;
|
||||
dbAccessSemaphore.Wait();
|
||||
@ -75,7 +91,7 @@ public static class Rememberer
|
||||
dbAccessSemaphore.Release();
|
||||
return toReturn;
|
||||
}
|
||||
public static List<Message> SearchMessages(Expression<Func<Message, bool>> predicate)
|
||||
public List<Message> SearchMessages(Expression<Func<Message, bool>> predicate)
|
||||
{
|
||||
List<Message> toReturn;
|
||||
dbAccessSemaphore.Wait();
|
||||
@ -83,7 +99,7 @@ public static class Rememberer
|
||||
dbAccessSemaphore.Release();
|
||||
return toReturn;
|
||||
}
|
||||
public static User SearchUser(Expression<Func<User, bool>> predicate)
|
||||
public User SearchUser(Expression<Func<User, bool>> predicate)
|
||||
{
|
||||
User toReturn;
|
||||
dbAccessSemaphore.Wait();
|
||||
@ -91,7 +107,7 @@ public static class Rememberer
|
||||
dbAccessSemaphore.Release();
|
||||
return toReturn;
|
||||
}
|
||||
public static void RememberAccount(Account toRemember)
|
||||
public void RememberAccount(Account toRemember)
|
||||
{
|
||||
dbAccessSemaphore.Wait();
|
||||
toRemember.IsUser ??= new User { Accounts = [toRemember] };
|
||||
@ -99,7 +115,7 @@ public static class Rememberer
|
||||
db.SaveChanges();
|
||||
dbAccessSemaphore.Release();
|
||||
}
|
||||
public static void RememberAttachment(Attachment toRemember)
|
||||
public void RememberAttachment(Attachment toRemember)
|
||||
{
|
||||
dbAccessSemaphore.Wait();
|
||||
toRemember.Message ??= new Message() { Attachments = [toRemember] };
|
||||
@ -107,7 +123,7 @@ public static class Rememberer
|
||||
db.SaveChanges();
|
||||
dbAccessSemaphore.Release();
|
||||
}
|
||||
public static Channel RememberChannel(Channel toRemember)
|
||||
public Channel RememberChannel(Channel toRemember)
|
||||
{
|
||||
if (channelCacheDirty)
|
||||
Task.Run(() => cacheChannels()).Wait(); //so we always do 2 db trips?
|
||||
@ -119,7 +135,7 @@ public static class Rememberer
|
||||
cacheChannels();
|
||||
return toRemember;
|
||||
}
|
||||
public static void RememberMessage(Message toRemember)
|
||||
public void RememberMessage(Message toRemember)
|
||||
{
|
||||
dbAccessSemaphore.Wait();
|
||||
toRemember.Channel ??= new();
|
||||
@ -133,21 +149,21 @@ public static class Rememberer
|
||||
db.SaveChanges();
|
||||
dbAccessSemaphore.Release();
|
||||
}
|
||||
public static void RememberUser(User toRemember)
|
||||
public void RememberUser(User toRemember)
|
||||
{
|
||||
dbAccessSemaphore.Wait();
|
||||
db.Users.Update(toRemember);
|
||||
db.SaveChanges();
|
||||
dbAccessSemaphore.Release();
|
||||
}
|
||||
public static void ForgetAccount(Account toForget)
|
||||
public void ForgetAccount(Account toForget)
|
||||
{
|
||||
var user = toForget.IsUser;
|
||||
var usersOnlyAccount = user.Accounts?.Count == 1;
|
||||
|
||||
if (usersOnlyAccount)
|
||||
{
|
||||
Rememberer.ForgetUser(user);
|
||||
ForgetUser(user);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -157,14 +173,14 @@ public static class Rememberer
|
||||
dbAccessSemaphore.Release();
|
||||
}
|
||||
}
|
||||
public static void ForgetAttachment(Attachment toForget)
|
||||
public void ForgetAttachment(Attachment toForget)
|
||||
{
|
||||
dbAccessSemaphore.Wait();
|
||||
db.Attachments.Remove(toForget);
|
||||
db.SaveChanges();
|
||||
dbAccessSemaphore.Release();
|
||||
}
|
||||
public static void ForgetChannel(Channel toForget)
|
||||
public void ForgetChannel(Channel toForget)
|
||||
{
|
||||
if (toForget.SubChannels?.Count > 0)
|
||||
{
|
||||
@ -187,28 +203,28 @@ public static class Rememberer
|
||||
channelCacheDirty = true;
|
||||
cacheChannels();
|
||||
}
|
||||
public static void ForgetMessage(Message toForget)
|
||||
public void ForgetMessage(Message toForget)
|
||||
{
|
||||
dbAccessSemaphore.Wait();
|
||||
db.Messages.Remove(toForget);
|
||||
db.SaveChanges();
|
||||
dbAccessSemaphore.Release();
|
||||
}
|
||||
public static void ForgetUAC(UAC toForget)
|
||||
public void ForgetUAC(UAC toForget)
|
||||
{
|
||||
dbAccessSemaphore.Wait();
|
||||
db.UACs.Remove(toForget);
|
||||
db.SaveChanges();
|
||||
dbAccessSemaphore.Release();
|
||||
}
|
||||
public static void ForgetUser(User toForget)
|
||||
public void ForgetUser(User toForget)
|
||||
{
|
||||
dbAccessSemaphore.Wait();
|
||||
db.Users.Remove(toForget);
|
||||
db.SaveChanges();
|
||||
dbAccessSemaphore.Release();
|
||||
}
|
||||
public static List<Account> AccountsOverview()
|
||||
public List<Account> AccountsOverview()
|
||||
{
|
||||
List<Account> toReturn;
|
||||
dbAccessSemaphore.Wait();
|
||||
@ -216,13 +232,16 @@ public static class Rememberer
|
||||
dbAccessSemaphore.Release();
|
||||
return toReturn;
|
||||
}
|
||||
public static List<Channel> ChannelsOverview()
|
||||
///<summary>
|
||||
///intentionally does not include Users; to help search for orphaned accounts.
|
||||
///</summary>
|
||||
public List<Channel> ChannelsOverview()
|
||||
{
|
||||
if (channelCacheDirty)
|
||||
Task.Run(() => cacheChannels()).Wait();
|
||||
return channels;
|
||||
return channels.ToList();
|
||||
}
|
||||
public static Account AccountDetail(Guid Id)
|
||||
public Account AccountDetail(Guid Id)
|
||||
{
|
||||
Account toReturn;
|
||||
dbAccessSemaphore.Wait();
|
||||
@ -230,7 +249,7 @@ public static class Rememberer
|
||||
dbAccessSemaphore.Release();
|
||||
return toReturn;
|
||||
}
|
||||
public static Attachment AttachmentDetail(Guid Id)
|
||||
public Attachment AttachmentDetail(Guid Id)
|
||||
{
|
||||
Attachment toReturn;
|
||||
dbAccessSemaphore.Wait();
|
||||
@ -238,16 +257,18 @@ public static class Rememberer
|
||||
dbAccessSemaphore.Release();
|
||||
return toReturn;
|
||||
}
|
||||
public static Channel ChannelDetail(Guid Id, bool messages = false)
|
||||
public Channel ChannelDetail(Guid Id, bool accounts = true, bool messages = false)
|
||||
{
|
||||
if (channelCacheDirty)
|
||||
Task.Run(() => cacheChannels()).Wait();
|
||||
var ch = channels.Find(c => c.Id == Id);
|
||||
if(accounts)
|
||||
ch.Users = SearchAccounts(a => a.SeenInChannel == ch);
|
||||
if (messages)
|
||||
ch.Messages = SearchMessages(m => m.ChannelId == ch.Id);
|
||||
return ch;
|
||||
}
|
||||
public static Message MessageDetail(Guid Id)
|
||||
public Message MessageDetail(Guid Id)
|
||||
{
|
||||
Message toReturn;
|
||||
dbAccessSemaphore.Wait();
|
||||
@ -256,7 +277,7 @@ public static class Rememberer
|
||||
dbAccessSemaphore.Release();
|
||||
return toReturn;
|
||||
}
|
||||
public static UAC UACDetail(Guid Id)
|
||||
public UAC UACDetail(Guid Id)
|
||||
{
|
||||
UAC toReturn;
|
||||
dbAccessSemaphore.Wait();
|
||||
@ -264,7 +285,7 @@ public static class Rememberer
|
||||
dbAccessSemaphore.Release();
|
||||
return toReturn;
|
||||
}
|
||||
public static User UserDetail(Guid Id)
|
||||
public User UserDetail(Guid Id)
|
||||
{
|
||||
User toReturn;
|
||||
dbAccessSemaphore.Wait();
|
||||
@ -272,7 +293,7 @@ public static class Rememberer
|
||||
dbAccessSemaphore.Release();
|
||||
return toReturn;
|
||||
}
|
||||
public static List<User> UsersOverview()
|
||||
public List<User> UsersOverview()
|
||||
{
|
||||
List<User> toReturn;
|
||||
dbAccessSemaphore.Wait();
|
||||
@ -280,7 +301,7 @@ public static class Rememberer
|
||||
dbAccessSemaphore.Release();
|
||||
return toReturn;
|
||||
}
|
||||
public static List<UAC> UACsOverview()
|
||||
public List<UAC> UACsOverview()
|
||||
{
|
||||
List<UAC> toReturn;
|
||||
dbAccessSemaphore.Wait();
|
||||
@ -288,7 +309,7 @@ public static class Rememberer
|
||||
dbAccessSemaphore.Release();
|
||||
return toReturn;
|
||||
}
|
||||
public static UAC SearchUAC(Expression<Func<UAC, bool>> predicate)
|
||||
public UAC SearchUAC(Expression<Func<UAC, bool>> predicate)
|
||||
{
|
||||
UAC toReturn;
|
||||
dbAccessSemaphore.Wait();
|
||||
@ -297,7 +318,7 @@ public static class Rememberer
|
||||
dbAccessSemaphore.Release();
|
||||
return toReturn;
|
||||
}
|
||||
public static List<UAC> MatchUACs(Message message)
|
||||
public List<UAC> MatchUACs(Message message)
|
||||
{
|
||||
var msgId = message.Id;
|
||||
var accId = message.Author.Id;
|
||||
@ -308,7 +329,7 @@ public static class Rememberer
|
||||
|| uac.Users.FirstOrDefault(usr => usr.Id == usrId) != null
|
||||
|| uac.Channels.FirstOrDefault(ch => ch.Id == chId) != null);
|
||||
}
|
||||
public static List<UAC> SearchUACs(Expression<Func<UAC, bool>> predicate)
|
||||
public List<UAC> SearchUACs(Expression<Func<UAC, bool>> predicate)
|
||||
{
|
||||
List<UAC> toReturn;
|
||||
dbAccessSemaphore.Wait();
|
||||
@ -317,7 +338,7 @@ public static class Rememberer
|
||||
dbAccessSemaphore.Release();
|
||||
return toReturn;
|
||||
}
|
||||
public static void RememberUAC(UAC toRemember)
|
||||
public void RememberUAC(UAC toRemember)
|
||||
{
|
||||
dbAccessSemaphore.Wait();
|
||||
db.Update(toRemember);
|
||||
|
@ -10,17 +10,17 @@ namespace vassago.WebInterface.Controllers;
|
||||
|
||||
public class ChannelsController() : Controller
|
||||
{
|
||||
private static Rememberer r = Rememberer.Instance;
|
||||
public IActionResult Details(Guid id)
|
||||
{
|
||||
var allChannels = Rememberer.ChannelsOverview();
|
||||
if (allChannels == null)
|
||||
return Problem("no channels.");
|
||||
|
||||
var channel = allChannels.FirstOrDefault(u => u.Id == id);
|
||||
var channel = r.ChannelDetail(id);
|
||||
if (channel == null)
|
||||
{
|
||||
return Problem($"couldn't find channle {id}");
|
||||
}
|
||||
else {
|
||||
Console.WriteLine($"details.cshtml will have a channel; {channel}.");
|
||||
}
|
||||
var walker = channel;
|
||||
while (walker != null)
|
||||
{
|
||||
|
@ -12,6 +12,7 @@ namespace vassago.Controllers;
|
||||
public class HomeController : Controller
|
||||
{
|
||||
private readonly ILogger<HomeController> _logger;
|
||||
private static Rememberer r = Rememberer.Instance;
|
||||
|
||||
public HomeController(ILogger<HomeController> logger)
|
||||
{
|
||||
@ -20,14 +21,14 @@ public class HomeController : Controller
|
||||
|
||||
public IActionResult Index()
|
||||
{
|
||||
var allAccounts = Rememberer.AccountsOverview();
|
||||
var allChannels = Rememberer.ChannelsOverview();
|
||||
var allAccounts = r.AccountsOverview();
|
||||
var allChannels = r.ChannelsOverview();
|
||||
Console.WriteLine($"accounts: {allAccounts?.Count ?? 0}, channels: {allChannels?.Count ?? 0}");
|
||||
var sb = new StringBuilder();
|
||||
sb.Append('[');
|
||||
|
||||
//UACs
|
||||
var allUACs = Rememberer.UACsOverview();
|
||||
var allUACs = r.UACsOverview();
|
||||
var first = true;
|
||||
if(allUACs.Any())
|
||||
{
|
||||
@ -58,13 +59,13 @@ public class HomeController : Controller
|
||||
}
|
||||
|
||||
//users
|
||||
var users = Rememberer.UsersOverview();
|
||||
var users = r.UsersOverview();
|
||||
if(users.Any())
|
||||
{
|
||||
sb.Append(",{text: \"users\", expanded:true, nodes: [");
|
||||
first=true;
|
||||
//refresh list; we'll be knocking them out again in serializeUser
|
||||
allAccounts = Rememberer.AccountsOverview();
|
||||
allAccounts = r.AccountsOverview();
|
||||
foreach(var user in users)
|
||||
{
|
||||
if (first)
|
||||
@ -83,7 +84,7 @@ public class HomeController : Controller
|
||||
//type error, e is not defined
|
||||
//channels
|
||||
sb.Append(",{text: \"channels\", expanded:true, nodes: [");
|
||||
var topLevelChannels = Rememberer.ChannelsOverview().Where(x => x.ParentChannel == null).ToList();
|
||||
var topLevelChannels = r.ChannelsOverview().Where(x => x.ParentChannel == null).ToList();
|
||||
first = true;
|
||||
foreach (var topLevelChannel in topLevelChannels)
|
||||
{
|
||||
|
@ -10,11 +10,11 @@ public class UACsController() : Controller
|
||||
{
|
||||
public IActionResult Index()
|
||||
{
|
||||
return View(Rememberer.UACsOverview());
|
||||
return View(Rememberer.Instance.UACsOverview());
|
||||
}
|
||||
public IActionResult Details(Guid id)
|
||||
{
|
||||
return View(Rememberer.SearchUAC(uac => uac.Id == id));
|
||||
return View(Rememberer.Instance.SearchUAC(uac => uac.Id == id));
|
||||
}
|
||||
|
||||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
||||
|
@ -11,6 +11,7 @@ namespace vassago.Controllers.api;
|
||||
public class AccountsController : ControllerBase
|
||||
{
|
||||
private readonly ILogger<AccountsController> _logger;
|
||||
private static Rememberer r = Rememberer.Instance;
|
||||
|
||||
public AccountsController(ILogger<AccountsController> logger)
|
||||
{
|
||||
@ -29,14 +30,14 @@ public class AccountsController : ControllerBase
|
||||
public IActionResult UnlinkUser([FromBody] extraSpecialObjectReadGlorifiedTupleFor_UnlinkUser req)
|
||||
{
|
||||
var acc_guid = req.acc_guid;
|
||||
var accFromDb = Rememberer.SearchAccount(acc => acc.Id == acc_guid);
|
||||
var accFromDb = r.SearchAccount(acc => acc.Id == acc_guid);
|
||||
if (accFromDb == null)
|
||||
{
|
||||
var err = $"attempt to unlink user for acc {acc_guid}, not found";
|
||||
_logger.LogError(err);
|
||||
return NotFound(err);
|
||||
}
|
||||
var userFromDb = Rememberer.SearchUser(c => c.Id == accFromDb.IsUser.Id);
|
||||
var userFromDb = r.SearchUser(c => c.Id == accFromDb.IsUser.Id);
|
||||
if (userFromDb == null)
|
||||
{
|
||||
var err = $"attempt to unlink user for {acc_guid}, doesn't have a user";
|
||||
@ -46,7 +47,7 @@ public class AccountsController : ControllerBase
|
||||
|
||||
accFromDb.IsUser = null;
|
||||
|
||||
Rememberer.RememberAccount(accFromDb);
|
||||
r.RememberAccount(accFromDb);
|
||||
return Ok(accFromDb);
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ namespace vassago.Controllers.api;
|
||||
public class RemembererController : ControllerBase
|
||||
{
|
||||
private readonly ILogger<RemembererController> _logger;
|
||||
private static Rememberer r = Rememberer.Instance;
|
||||
|
||||
public RemembererController(ILogger<RemembererController> logger)
|
||||
{
|
||||
@ -22,42 +23,42 @@ public class RemembererController : ControllerBase
|
||||
[Produces("application/json")]
|
||||
public Account CreateAccount(Guid id)
|
||||
{
|
||||
return Rememberer.AccountDetail(id);
|
||||
return r.AccountDetail(id);
|
||||
}
|
||||
[HttpPut]
|
||||
[Route("Attachment")]
|
||||
[Produces("application/json")]
|
||||
public Attachment CreateAttachment(Guid id)
|
||||
{
|
||||
return Rememberer.AttachmentDetail(id);
|
||||
return r.AttachmentDetail(id);
|
||||
}
|
||||
[HttpPut]
|
||||
[Route("Channels")]
|
||||
[Produces("application/json")]
|
||||
public Channel CreateChannel(Guid id)
|
||||
{
|
||||
return Rememberer.ChannelDetail(id);
|
||||
return r.ChannelDetail(id);
|
||||
}
|
||||
[HttpPut]
|
||||
[Route("Message")]
|
||||
[Produces("application/json")]
|
||||
public Message CreateMessage(Guid id)
|
||||
{
|
||||
return Rememberer.MessageDetail(id);
|
||||
return r.MessageDetail(id);
|
||||
}
|
||||
[HttpPut]
|
||||
[Route("UAC")]
|
||||
[Produces("application/json")]
|
||||
public UAC CreateUAC(Guid id)
|
||||
{
|
||||
return Rememberer.UACDetail(id);
|
||||
return r.UACDetail(id);
|
||||
}
|
||||
[HttpPut]
|
||||
[Route("User")]
|
||||
[Produces("application/json")]
|
||||
public User CreateUser(Guid id)
|
||||
{
|
||||
return Rememberer.UserDetail(id);
|
||||
return r.UserDetail(id);
|
||||
}
|
||||
//Read
|
||||
[HttpGet]
|
||||
@ -65,42 +66,42 @@ public class RemembererController : ControllerBase
|
||||
[Produces("application/json")]
|
||||
public Account GetAccount(Guid id)
|
||||
{
|
||||
return Rememberer.AccountDetail(id);
|
||||
return r.AccountDetail(id);
|
||||
}
|
||||
[HttpGet]
|
||||
[Route("Attachment")]
|
||||
[Produces("application/json")]
|
||||
public Attachment GetAttachment(Guid id)
|
||||
{
|
||||
return Rememberer.AttachmentDetail(id);
|
||||
return r.AttachmentDetail(id);
|
||||
}
|
||||
[HttpGet]
|
||||
[Route("Channels")]
|
||||
[Produces("application/json")]
|
||||
public Channel GetChannel(Guid id)
|
||||
{
|
||||
return Rememberer.ChannelDetail(id);
|
||||
return r.ChannelDetail(id);
|
||||
}
|
||||
[HttpGet]
|
||||
[Route("Message")]
|
||||
[Produces("application/json")]
|
||||
public Message GetMessage(Guid id)
|
||||
{
|
||||
return Rememberer.MessageDetail(id);
|
||||
return r.MessageDetail(id);
|
||||
}
|
||||
[HttpGet]
|
||||
[Route("UAC")]
|
||||
[Produces("application/json")]
|
||||
public UAC GetUAC(Guid id)
|
||||
{
|
||||
return Rememberer.UACDetail(id);
|
||||
return r.UACDetail(id);
|
||||
}
|
||||
[HttpGet]
|
||||
[Route("User")]
|
||||
[Produces("application/json")]
|
||||
public User GetUser(Guid id)
|
||||
{
|
||||
return Rememberer.UserDetail(id);
|
||||
return r.UserDetail(id);
|
||||
}
|
||||
//Update
|
||||
[HttpPatch]
|
||||
@ -108,7 +109,7 @@ public class RemembererController : ControllerBase
|
||||
[Produces("application/json")]
|
||||
public IActionResult Patch([FromBody] Channel channel)
|
||||
{
|
||||
var fromDb = Rememberer.ChannelDetail(channel.Id);
|
||||
var fromDb = r.ChannelDetail(channel.Id);
|
||||
if (fromDb == null)
|
||||
{
|
||||
_logger.LogError($"attempt to update channel {channel.Id}, not found");
|
||||
@ -121,7 +122,7 @@ public class RemembererController : ControllerBase
|
||||
//settable values: lewdness filter level, meanness filter level. maybe i could decorate them...
|
||||
fromDb.LewdnessFilterLevel = channel.LewdnessFilterLevel;
|
||||
fromDb.MeannessFilterLevel = channel.MeannessFilterLevel;
|
||||
Rememberer.RememberChannel(fromDb);
|
||||
r.RememberChannel(fromDb);
|
||||
return Ok(fromDb);
|
||||
}
|
||||
//Delete
|
||||
@ -130,13 +131,13 @@ public class RemembererController : ControllerBase
|
||||
[Produces("application/json")]
|
||||
public IActionResult DeleteAccount(Guid id)
|
||||
{
|
||||
var fromDb = Rememberer.AccountDetail(id);
|
||||
var fromDb = r.AccountDetail(id);
|
||||
if (fromDb == null)
|
||||
{
|
||||
_logger.LogError($"attempt to delete account {id}, not found");
|
||||
return NotFound();
|
||||
}
|
||||
Rememberer.ForgetAccount(fromDb);
|
||||
r.ForgetAccount(fromDb);
|
||||
return Ok();
|
||||
}
|
||||
[HttpDelete]
|
||||
@ -144,13 +145,13 @@ public class RemembererController : ControllerBase
|
||||
[Produces("application/json")]
|
||||
public IActionResult DeleteAttachment(Guid id)
|
||||
{
|
||||
var fromDb = Rememberer.AttachmentDetail(id);
|
||||
var fromDb = r.AttachmentDetail(id);
|
||||
if (fromDb == null)
|
||||
{
|
||||
_logger.LogError($"attempt to delete attachment {id}, not found");
|
||||
return NotFound();
|
||||
}
|
||||
Rememberer.ForgetAttachment(fromDb);
|
||||
r.ForgetAttachment(fromDb);
|
||||
return Ok();
|
||||
}
|
||||
[HttpDelete]
|
||||
@ -158,14 +159,14 @@ public class RemembererController : ControllerBase
|
||||
[Produces("application/json")]
|
||||
public IActionResult DeleteChannel(Guid id)
|
||||
{
|
||||
var fromDb = Rememberer.ChannelDetail(id);
|
||||
var fromDb = r.ChannelDetail(id);
|
||||
_logger.LogDebug($"delete channel {id}");
|
||||
if (fromDb == null)
|
||||
{
|
||||
_logger.LogError($"attempt to delete channel {id}, not found");
|
||||
return NotFound();
|
||||
}
|
||||
Rememberer.ForgetChannel(fromDb);
|
||||
r.ForgetChannel(fromDb);
|
||||
_logger.LogDebug($"delete channel {id} success");
|
||||
return Ok();
|
||||
}
|
||||
@ -174,13 +175,13 @@ public class RemembererController : ControllerBase
|
||||
[Produces("application/json")]
|
||||
public IActionResult DeleteMessage(Guid id)
|
||||
{
|
||||
var fromDb = Rememberer.MessageDetail(id);
|
||||
var fromDb = r.MessageDetail(id);
|
||||
if (fromDb == null)
|
||||
{
|
||||
_logger.LogError($"attempt to delete message {id}, not found");
|
||||
return NotFound();
|
||||
}
|
||||
Rememberer.ForgetMessage(fromDb);
|
||||
r.ForgetMessage(fromDb);
|
||||
return Ok();
|
||||
}
|
||||
[HttpDelete]
|
||||
@ -188,13 +189,13 @@ public class RemembererController : ControllerBase
|
||||
[Produces("application/json")]
|
||||
public IActionResult DeleteUAC(Guid id)
|
||||
{
|
||||
var fromDb = Rememberer.UACDetail(id);
|
||||
var fromDb = r.UACDetail(id);
|
||||
if (fromDb == null)
|
||||
{
|
||||
_logger.LogError($"attempt to delete uac {id}, not found");
|
||||
return NotFound();
|
||||
}
|
||||
Rememberer.ForgetUAC(fromDb);
|
||||
r.ForgetUAC(fromDb);
|
||||
return Ok();
|
||||
}
|
||||
[HttpDelete]
|
||||
@ -202,13 +203,13 @@ public class RemembererController : ControllerBase
|
||||
[Produces("application/json")]
|
||||
public IActionResult DeleteUser(Guid id)
|
||||
{
|
||||
var fromDb = Rememberer.UserDetail(id);
|
||||
var fromDb = r.UserDetail(id);
|
||||
if (fromDb == null)
|
||||
{
|
||||
_logger.LogError($"attempt to delete user {id}, not found");
|
||||
return NotFound();
|
||||
}
|
||||
Rememberer.ForgetUser(fromDb);
|
||||
r.ForgetUser(fromDb);
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ namespace vassago.Controllers.api;
|
||||
public class UACController : ControllerBase
|
||||
{
|
||||
private readonly ILogger<UACController> _logger;
|
||||
private static Rememberer r = Rememberer.Instance;
|
||||
|
||||
public UACController(ILogger<UACController> logger)
|
||||
{
|
||||
@ -31,14 +32,14 @@ public class UACController : ControllerBase
|
||||
{
|
||||
var uac_guid = req.uac_guid;
|
||||
var channel_guid = req.channel_guid;
|
||||
var uacFromDb = Rememberer.SearchUAC(uac => uac.Id == uac_guid);
|
||||
var uacFromDb = r.SearchUAC(uac => uac.Id == uac_guid);
|
||||
if (uacFromDb == null)
|
||||
{
|
||||
var err = $"attempt to link channel for uac {uac_guid}, not found";
|
||||
_logger.LogError(err);
|
||||
return NotFound(err);
|
||||
}
|
||||
var channelFromDb = Rememberer.SearchChannel(c => c.Id == channel_guid);
|
||||
var channelFromDb = r.SearchChannel(c => c.Id == channel_guid);
|
||||
if (channelFromDb == null)
|
||||
{
|
||||
var err = $"attempt to link channel for channel {channel_guid}, not found";
|
||||
@ -52,7 +53,7 @@ public class UACController : ControllerBase
|
||||
return BadRequest("channel already linked");
|
||||
}
|
||||
uacFromDb.Channels.Add(channelFromDb);
|
||||
Rememberer.RememberUAC(uacFromDb);
|
||||
r.RememberUAC(uacFromDb);
|
||||
return Ok(uacFromDb);
|
||||
}
|
||||
public class extraSpecialObjectReadGlorifiedTupleFor_LinkUser
|
||||
@ -67,13 +68,13 @@ public class UACController : ControllerBase
|
||||
{
|
||||
var uac_guid = req.uac_guid;
|
||||
var user_guid = req.user_guid;
|
||||
var uacFromDb = Rememberer.SearchUAC(uac => uac.Id == uac_guid);
|
||||
var uacFromDb = r.SearchUAC(uac => uac.Id == uac_guid);
|
||||
if (uacFromDb == null)
|
||||
{
|
||||
_logger.LogError($"attempt to link channal for uac {uac_guid}, not found");
|
||||
return NotFound();
|
||||
}
|
||||
var userFromDb = Rememberer.SearchUser(c => c.Id == user_guid);
|
||||
var userFromDb = r.SearchUser(c => c.Id == user_guid);
|
||||
if (userFromDb == null)
|
||||
{
|
||||
_logger.LogError($"attempt to link user for user {user_guid}, not found");
|
||||
@ -86,7 +87,7 @@ public class UACController : ControllerBase
|
||||
return BadRequest("user already linked");
|
||||
}
|
||||
uacFromDb.Users.Add(userFromDb);
|
||||
Rememberer.RememberUAC(uacFromDb);
|
||||
r.RememberUAC(uacFromDb);
|
||||
return Ok(uacFromDb);
|
||||
}
|
||||
public class extraSpecialObjectReadGlorifiedTupleFor_LinkAccount
|
||||
@ -101,13 +102,13 @@ public class UACController : ControllerBase
|
||||
{
|
||||
var uac_guid = req.uac_guid;
|
||||
var account_guid = req.account_guid;
|
||||
var uacFromDb = Rememberer.SearchUAC(uac => uac.Id == uac_guid);
|
||||
var uacFromDb = r.SearchUAC(uac => uac.Id == uac_guid);
|
||||
if (uacFromDb == null)
|
||||
{
|
||||
_logger.LogError($"attempt to link channal for uac {uac_guid}, not found");
|
||||
return NotFound();
|
||||
}
|
||||
var accountFromDb = Rememberer.SearchAccount(c => c.Id == account_guid);
|
||||
var accountFromDb = r.SearchAccount(c => c.Id == account_guid);
|
||||
if (accountFromDb == null)
|
||||
{
|
||||
_logger.LogError($"attempt to link account for user {account_guid}, not found");
|
||||
@ -120,7 +121,7 @@ public class UACController : ControllerBase
|
||||
return BadRequest("account already linked");
|
||||
}
|
||||
uacFromDb.AccountInChannels.Add(accountFromDb);
|
||||
Rememberer.RememberUAC(uacFromDb);
|
||||
r.RememberUAC(uacFromDb);
|
||||
return Ok(uacFromDb);
|
||||
}
|
||||
[HttpPatch]
|
||||
@ -130,13 +131,13 @@ public class UACController : ControllerBase
|
||||
{
|
||||
var uac_guid = req.uac_guid;
|
||||
var user_guid = req.user_guid;
|
||||
var uacFromDb = Rememberer.SearchUAC(uac => uac.Id == uac_guid);
|
||||
var uacFromDb = r.SearchUAC(uac => uac.Id == uac_guid);
|
||||
if (uacFromDb == null)
|
||||
{
|
||||
_logger.LogError($"attempt to unlink uac for uac {uac_guid}, not found");
|
||||
return NotFound();
|
||||
}
|
||||
var userFromDb = Rememberer.SearchUser(c => c.Id == user_guid);
|
||||
var userFromDb = r.SearchUser(c => c.Id == user_guid);
|
||||
if (userFromDb == null)
|
||||
{
|
||||
_logger.LogError($"attempt to unlink user for user {user_guid}, not found");
|
||||
@ -149,7 +150,7 @@ public class UACController : ControllerBase
|
||||
return BadRequest("user not linked");
|
||||
}
|
||||
uacFromDb.Users.Remove(userFromDb);
|
||||
Rememberer.RememberUAC(uacFromDb);
|
||||
r.RememberUAC(uacFromDb);
|
||||
return Ok(uacFromDb);
|
||||
}
|
||||
[HttpPatch]
|
||||
@ -159,13 +160,13 @@ public class UACController : ControllerBase
|
||||
{
|
||||
var uac_guid = req.uac_guid;
|
||||
var account_guid = req.account_guid;
|
||||
var uacFromDb = Rememberer.SearchUAC(uac => uac.Id == uac_guid);
|
||||
var uacFromDb = r.SearchUAC(uac => uac.Id == uac_guid);
|
||||
if (uacFromDb == null)
|
||||
{
|
||||
_logger.LogError($"attempt to unlink uac for uac {uac_guid}, not found");
|
||||
return NotFound();
|
||||
}
|
||||
var accountFromDb = Rememberer.SearchAccount(a => a.Id == account_guid);
|
||||
var accountFromDb = r.SearchAccount(a => a.Id == account_guid);
|
||||
if (accountFromDb == null)
|
||||
{
|
||||
_logger.LogError($"attempt to unlink account for user {account_guid}, not found");
|
||||
@ -178,7 +179,7 @@ public class UACController : ControllerBase
|
||||
return BadRequest("account not linked");
|
||||
}
|
||||
uacFromDb.AccountInChannels.Remove(accountFromDb);
|
||||
Rememberer.RememberUAC(uacFromDb);
|
||||
r.RememberUAC(uacFromDb);
|
||||
return Ok(uacFromDb);
|
||||
}
|
||||
[HttpPatch]
|
||||
@ -188,13 +189,13 @@ public class UACController : ControllerBase
|
||||
{
|
||||
var uac_guid = req.uac_guid;
|
||||
var channel_guid = req.channel_guid;
|
||||
var uacFromDb = Rememberer.SearchUAC(uac => uac.Id == uac_guid);
|
||||
var uacFromDb = r.SearchUAC(uac => uac.Id == uac_guid);
|
||||
if (uacFromDb == null)
|
||||
{
|
||||
_logger.LogError($"attempt to unlink channal for uac {uac_guid}, not found");
|
||||
return NotFound();
|
||||
}
|
||||
var channelFromDb = Rememberer.SearchChannel(c => c.Id == channel_guid);
|
||||
var channelFromDb = r.SearchChannel(c => c.Id == channel_guid);
|
||||
if (channelFromDb == null)
|
||||
{
|
||||
_logger.LogError($"attempt to unlink user for user {channel_guid}, not found");
|
||||
@ -207,7 +208,7 @@ public class UACController : ControllerBase
|
||||
return BadRequest("user not linked");
|
||||
}
|
||||
uacFromDb.Channels.Remove(channelFromDb);
|
||||
Rememberer.RememberUAC(uacFromDb);
|
||||
r.RememberUAC(uacFromDb);
|
||||
return Ok(uacFromDb);
|
||||
}
|
||||
[HttpPut]
|
||||
@ -216,7 +217,7 @@ public class UACController : ControllerBase
|
||||
public IActionResult CreateForChannels(Guid Id)
|
||||
{
|
||||
_logger.LogDebug($"made it to controller. creating for channel {Id}");
|
||||
var targetChannel = Rememberer.ChannelDetail(Id);
|
||||
var targetChannel = r.ChannelDetail(Id);
|
||||
if (targetChannel == null)
|
||||
{
|
||||
return NotFound();
|
||||
@ -225,8 +226,8 @@ public class UACController : ControllerBase
|
||||
{
|
||||
Channels = [targetChannel]
|
||||
};
|
||||
Rememberer.RememberUAC(newUAC);
|
||||
Rememberer.RememberChannel(targetChannel);
|
||||
r.RememberUAC(newUAC);
|
||||
r.RememberChannel(targetChannel);
|
||||
return Ok(newUAC.Id);
|
||||
}
|
||||
[HttpPut]
|
||||
@ -235,7 +236,7 @@ public class UACController : ControllerBase
|
||||
public IActionResult AddTranslation(Guid Id)
|
||||
{
|
||||
_logger.LogDebug($"made it to controller. creating translation for uac {Id}");
|
||||
var uacFromDb = Rememberer.SearchUAC(uac => uac.Id == Id);
|
||||
var uacFromDb = r.SearchUAC(uac => uac.Id == Id);
|
||||
if (uacFromDb == null)
|
||||
{
|
||||
_logger.LogError($"attempt to create translation for uac {Id}, not found");
|
||||
@ -243,7 +244,7 @@ public class UACController : ControllerBase
|
||||
}
|
||||
uacFromDb.Translations ??= [];
|
||||
uacFromDb.Translations.Add(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
|
||||
Rememberer.RememberUAC(uacFromDb);
|
||||
r.RememberUAC(uacFromDb);
|
||||
return Ok(uacFromDb.Translations.Count);
|
||||
}
|
||||
[HttpPut]
|
||||
@ -252,7 +253,7 @@ public class UACController : ControllerBase
|
||||
public IActionResult AddCommandAlteration(Guid Id)
|
||||
{
|
||||
_logger.LogDebug($"made it to controller. creating command alteration for uac {Id}");
|
||||
var uacFromDb = Rememberer.SearchUAC(uac => uac.Id == Id);
|
||||
var uacFromDb = r.SearchUAC(uac => uac.Id == Id);
|
||||
if (uacFromDb == null)
|
||||
{
|
||||
_logger.LogError($"attempt to create command alteration for uac {Id}, not found");
|
||||
@ -260,7 +261,7 @@ public class UACController : ControllerBase
|
||||
}
|
||||
uacFromDb.CommandAlterations ??= [];
|
||||
uacFromDb.CommandAlterations.Add(Guid.NewGuid().ToString(), Guid.NewGuid().ToString());
|
||||
Rememberer.RememberUAC(uacFromDb);
|
||||
r.RememberUAC(uacFromDb);
|
||||
return Ok(uacFromDb.CommandAlterations.Count);
|
||||
}
|
||||
|
||||
|
@ -189,7 +189,8 @@
|
||||
|
||||
sb.Append("[{text: \"accounts\", \"expanded\":true, nodes: [");
|
||||
var first = true;
|
||||
foreach (var acc in ThisChannel.Users?.OrderBy(a => a?.SeenInChannel?.LineageSummary))
|
||||
Console.WriteLine($"{ThisChannel.DisplayName} has {ThisChannel.Users?.Count ?? 0} users.");
|
||||
foreach (var acc in ThisChannel.Users?.OrderBy(a => a?.SeenInChannel?.LineageSummary ?? " ERROR"))
|
||||
{
|
||||
if(!first)
|
||||
sb.Append(',');
|
||||
|
Loading…
Reference in New Issue
Block a user