rememberer. but it doesn't remember.
Some checks failed
gitea.arg.rip/vassago/pipeline/head There was a failure building this commit

FUCK this is what I get for saying I like entity framework. *adds thing* *hits save* "(unrelated shit) is already here, why are you trying to add it again you dumbass?" FUCK IF I KNOW, you're supposed to be straightening this shit out!
This commit is contained in:
adam 2025-02-07 17:00:29 -05:00
parent 03fdb56190
commit 740471d105
6 changed files with 145 additions and 94 deletions

View File

@ -72,55 +72,31 @@ public class Behaver
public void MarkSelf(Account selfAccount) public void MarkSelf(Account selfAccount)
{ {
var db = new ChattingContext();
if(SelfUser == null) if(SelfUser == null)
{ {
SelfUser = selfAccount.IsUser; SelfUser = selfAccount.IsUser;
} }
else if (SelfUser != selfAccount.IsUser) else if (SelfUser != selfAccount.IsUser)
{ {
CollapseUsers(SelfUser, selfAccount.IsUser, db); CollapseUsers(SelfUser, selfAccount.IsUser);
} }
SelfAccounts = db.Accounts.Where(a => a.IsUser == SelfUser).ToList(); SelfAccounts = Rememberer.SearchAccounts(a => a.IsUser == SelfUser);
db.SaveChanges(); Rememberer.RememberAccount(selfAccount);
} }
public bool CollapseUsers(User primary, User secondary, ChattingContext db) public bool CollapseUsers(User primary, User secondary)
{ {
Console.WriteLine($"{secondary.Id} is being consumed into {primary.Id}"); if(primary.Accounts == null)
primary.Accounts = new List<Account>();
if(secondary.Accounts != null)
primary.Accounts.AddRange(secondary.Accounts); primary.Accounts.AddRange(secondary.Accounts);
foreach(var a in secondary.Accounts) foreach(var a in secondary.Accounts)
{ {
a.IsUser = primary; a.IsUser = primary;
} }
secondary.Accounts.Clear(); secondary.Accounts.Clear();
Console.WriteLine("accounts transferred"); Rememberer.ForgetUser(secondary);
try Rememberer.RememberUser(primary);
{
db.SaveChangesAsync().Wait();
}
catch(Exception e)
{
Console.WriteLine("First save exception.");
Console.Error.WriteLine(e);
return false;
}
Console.WriteLine("saved");
db.Users.Remove(secondary);
Console.WriteLine("old account cleaned up");
try
{
db.SaveChangesAsync().Wait();
}
catch(Exception e)
{
Console.WriteLine("Second save exception.");
Console.Error.WriteLine(e);
return false;
}
Console.WriteLine("saved, again, separately");
return true; return true;
} }
} }

View File

@ -72,7 +72,7 @@ public class LinkClose : Behavior
return true; return true;
} }
if(Behaver.Instance.CollapseUsers(_primary.IsUser, secondary, new ChattingContext())) if(Behaver.Instance.CollapseUsers(_primary.IsUser, secondary))
{ {
await message.Channel.SendMessage("done :)"); await message.Channel.SendMessage("done :)");
} }

View File

@ -39,7 +39,6 @@ namespace vassago
await t.Init(tc); await t.Init(tc);
ProtocolInterfaces.ProtocolList.twitchs.Add(t); ProtocolInterfaces.ProtocolList.twitchs.Add(t);
} }
Console.WriteLine("survived initting");
} }
public Task StopAsync(CancellationToken cancellationToken) public Task StopAsync(CancellationToken cancellationToken)

View File

@ -23,7 +23,6 @@ public class DiscordInterface
private static SemaphoreSlim discordChannelSetup = new SemaphoreSlim(1, 1); private static SemaphoreSlim discordChannelSetup = new SemaphoreSlim(1, 1);
private Channel protocolAsChannel; private Channel protocolAsChannel;
public async Task Init(string token) public async Task Init(string token)
{ {
await SetupDiscordChannel(); await SetupDiscordChannel();
@ -47,8 +46,7 @@ public class DiscordInterface
try try
{ {
var db = new ChattingContext(); protocolAsChannel = Rememberer.SearchChannel(c => c.ParentChannel == null && c.Protocol == PROTOCOL);
protocolAsChannel = db.Channels.FirstOrDefault(c => c.ParentChannel == null && c.Protocol == PROTOCOL);
if (protocolAsChannel == null) if (protocolAsChannel == null)
{ {
protocolAsChannel = new Channel() protocolAsChannel = new Channel()
@ -64,11 +62,15 @@ public class DiscordInterface
Protocol = PROTOCOL, Protocol = PROTOCOL,
SubChannels = new List<Channel>() SubChannels = new List<Channel>()
}; };
protocolAsChannel.SendMessage = (t) => { throw new InvalidOperationException($"discord itself cannot accept text"); };
protocolAsChannel.SendFile = (f, t) => { throw new InvalidOperationException($"discord itself cannot send file"); };
db.Channels.Add(protocolAsChannel);
db.SaveChanges();
} }
else
{
Console.WriteLine($"discord, channel with id {protocolAsChannel.Id}, already exists");
}
protocolAsChannel.DisplayName = "discord (itself)";
protocolAsChannel.SendMessage = (t) => { throw new InvalidOperationException($"protocol isn't a real channel, cannot accept text"); };
protocolAsChannel.SendFile = (f, t) => { throw new InvalidOperationException($"protocol isn't a real channel, cannot send file"); };
Rememberer.RememberChannel(protocolAsChannel);
} }
finally finally
{ {
@ -110,11 +112,8 @@ public class DiscordInterface
private async Task SelfConnected() private async Task SelfConnected()
{ {
var db = new ChattingContext();
var selfAccount = UpsertAccount(client.CurrentUser, protocolAsChannel); var selfAccount = UpsertAccount(client.CurrentUser, protocolAsChannel);
selfAccount.DisplayName = client.CurrentUser.Username; selfAccount.DisplayName = client.CurrentUser.Username;
await db.SaveChangesAsync();
Behaver.Instance.MarkSelf(selfAccount); Behaver.Instance.MarkSelf(selfAccount);
} }
@ -137,7 +136,6 @@ public class DiscordInterface
} }
await Behaver.Instance.ActOn(m); await Behaver.Instance.ActOn(m);
m.ActedOn = true; // for its own ruposess it might act on it later, but either way, fuck it, we checked. m.ActedOn = true; // for its own ruposess it might act on it later, but either way, fuck it, we checked.
} }
private void UserJoined(SocketGuildUser arg) private void UserJoined(SocketGuildUser arg)
@ -185,31 +183,26 @@ public class DiscordInterface
} }
internal vassago.Models.Attachment UpsertAttachment(IAttachment dAttachment) internal vassago.Models.Attachment UpsertAttachment(IAttachment dAttachment)
{ {
var a = Rememberer.SearchAttachment(ai => ai.ExternalId == dAttachment.Id);
var db = new ChattingContext();
var a = db.Attachments.FirstOrDefault(ai => ai.ExternalId == dAttachment.Id);
if (a == null) if (a == null)
{ {
a = new vassago.Models.Attachment(); a = new vassago.Models.Attachment();
db.Attachments.Add(a);
} }
a.ContentType = dAttachment.ContentType; a.ContentType = dAttachment.ContentType;
a.Description = dAttachment.Description; a.Description = dAttachment.Description;
a.Filename = dAttachment.Filename; a.Filename = dAttachment.Filename;
a.Size = dAttachment.Size; a.Size = dAttachment.Size;
a.Source = new Uri(dAttachment.Url); a.Source = new Uri(dAttachment.Url);
db.SaveChanges(); Rememberer.RememberAttachment(a);
return a; return a;
} }
internal Message UpsertMessage(IUserMessage dMessage) internal Message UpsertMessage(IUserMessage dMessage)
{ {
var db = new ChattingContext(); var m = Rememberer.SearchMessage(mi => mi.ExternalId == dMessage.Id.ToString() && mi.Protocol == PROTOCOL);
var m = db.Messages.FirstOrDefault(mi => mi.ExternalId == dMessage.Id.ToString() && mi.Protocol == PROTOCOL);
if (m == null) if (m == null)
{ {
m = new Message(); m = new Message();
m.Protocol = PROTOCOL; m.Protocol = PROTOCOL;
db.Messages.Add(m);
} }
m.Attachments = m.Attachments ?? new List<vassago.Models.Attachment>(); m.Attachments = m.Attachments ?? new List<vassago.Models.Attachment>();
if (dMessage.Attachments?.Any() == true) if (dMessage.Attachments?.Any() == true)
@ -225,7 +218,7 @@ public class DiscordInterface
m.Timestamp = dMessage.EditedTimestamp ?? dMessage.CreatedAt; m.Timestamp = dMessage.EditedTimestamp ?? dMessage.CreatedAt;
m.Channel = UpsertChannel(dMessage.Channel); m.Channel = UpsertChannel(dMessage.Channel);
m.Author = UpsertAccount(dMessage.Author, m.Channel); m.Author = UpsertAccount(dMessage.Author, m.Channel);
if(dMessage.Channel is IGuildChannel) if (dMessage.Channel is IGuildChannel)
{ {
m.Author.DisplayName = (dMessage.Author as IGuildUser).DisplayName;//discord forgot how display names work. m.Author.DisplayName = (dMessage.Author as IGuildUser).DisplayName;//discord forgot how display names work.
} }
@ -234,18 +227,16 @@ public class DiscordInterface
m.Reply = (t) => { return dMessage.ReplyAsync(t); }; m.Reply = (t) => { return dMessage.ReplyAsync(t); };
m.React = (e) => { return attemptReact(dMessage, e); }; m.React = (e) => { return attemptReact(dMessage, e); };
db.SaveChanges(); Rememberer.RememberChannel(m.Channel);
return m; return m;
} }
internal Channel UpsertChannel(IMessageChannel channel) internal Channel UpsertChannel(IMessageChannel channel)
{ {
Channel c = Rememberer.SearchChannel(ci => ci.ExternalId == channel.Id.ToString() && ci.Protocol == PROTOCOL);
var db = new ChattingContext();
Channel c = db.Channels.FirstOrDefault(ci => ci.ExternalId == channel.Id.ToString() && ci.Protocol == PROTOCOL);
if (c == null) if (c == null)
{ {
c = new Channel(); c = new Channel();
db.Channels.Add(c); Console.WriteLine($"adding channel {channel.Name}");
} }
c.DisplayName = channel.Name; c.DisplayName = channel.Name;
@ -255,8 +246,7 @@ public class DiscordInterface
c.Protocol = PROTOCOL; c.Protocol = PROTOCOL;
if (channel is IGuildChannel) if (channel is IGuildChannel)
{ {
c.ParentChannel = UpsertChannel((channel as IGuildChannel).Guild, db); UpsertChannel((channel as IGuildChannel).Guild);
c.ParentChannel.SubChannels.Add(c);
} }
else if (channel is IPrivateChannel) else if (channel is IPrivateChannel)
{ {
@ -267,39 +257,51 @@ public class DiscordInterface
c.ParentChannel = protocolAsChannel; c.ParentChannel = protocolAsChannel;
Console.Error.WriteLine($"trying to upsert channel {channel.Id}/{channel.Name}, but it's neither guildchannel nor private channel. shrug.jpg"); Console.Error.WriteLine($"trying to upsert channel {channel.Id}/{channel.Name}, but it's neither guildchannel nor private channel. shrug.jpg");
} }
c.SubChannels = c.SubChannels ?? new List<Channel>();
c.SendMessage = (t) => { return channel.SendMessageAsync(t); };
c.SendFile = (f, t) => { return channel.SendFileAsync(f, t); };
switch(c.ChannelType) switch (c.ChannelType)
{ {
case vassago.Models.Enumerations.ChannelType.DM: case vassago.Models.Enumerations.ChannelType.DM:
c.DisplayName = "DM: " + (channel as IPrivateChannel).Recipients?.FirstOrDefault(u => u.Id != client.CurrentUser.Id).Username; c.DisplayName = "DM: " + (channel as IPrivateChannel).Recipients?.FirstOrDefault(u => u.Id != client.CurrentUser.Id).Username;
break; break;
} }
db.SaveChanges(); Rememberer.RememberChannel(c);
return c;
Channel parentChannel = null;
if (channel is IGuildChannel)
{
parentChannel = Rememberer.SearchChannel(c => c.ExternalId == (channel as IGuildChannel).Guild.Id.ToString() && c.Protocol == PROTOCOL);
} }
internal Channel UpsertChannel(IGuild channel, ChattingContext db = null) else if (channel is IPrivateChannel)
{ {
db = db ?? new ChattingContext(); parentChannel = protocolAsChannel;
Console.WriteLine($"upserting *guild*: {channel.Id}");
Channel c = db.Channels.FirstOrDefault(ci => ci.ExternalId == channel.Id.ToString() && ci.Protocol == PROTOCOL);
if (c == null)
{
Console.WriteLine($"don't have one already. Creating.");
c = new Channel();
db.Channels.Add(c);
Console.WriteLine($"upserting channel {channel.Name} from discord, have to create a new one in the DB");
} }
else else
{ {
Console.WriteLine($"found one."); parentChannel = protocolAsChannel;
Console.Error.WriteLine($"trying to upsert channel {channel.Id}/{channel.Name}, but it's neither guildchannel nor private channel. shrug.jpg");
}
if (parentChannel.SubChannels == null)
parentChannel.SubChannels = new List<Channel>();
parentChannel.SubChannels.Add(c);
Rememberer.RememberChannel(parentChannel);
c.SendMessage = (t) => { return channel.SendMessageAsync(t); };
c.SendFile = (f, t) => { return channel.SendFileAsync(f, t); };
return c;
}
internal Channel UpsertChannel(IGuild channel)
{
Channel c = Rememberer.SearchChannel(ci => ci.ExternalId == channel.Id.ToString() && ci.Protocol == PROTOCOL);
if (c == null)
{
c = new Channel();
Rememberer.RememberChannel(c);
} }
c.DisplayName = channel.Name; c.DisplayName = channel.Name;
c.ExternalId = channel.Id.ToString(); c.ExternalId = channel.Id.ToString();
c.ChannelType = vassago.Models.Enumerations.ChannelType.Normal; c.ChannelType = vassago.Models.Enumerations.ChannelType.OU;
c.Messages = c.Messages ?? new List<Message>(); c.Messages = c.Messages ?? new List<Message>();
c.Protocol = protocolAsChannel.Protocol; c.Protocol = protocolAsChannel.Protocol;
c.ParentChannel = protocolAsChannel; c.ParentChannel = protocolAsChannel;
@ -308,17 +310,15 @@ public class DiscordInterface
c.SendMessage = (t) => { throw new InvalidOperationException($"channel {channel.Name} is guild; cannot accept text"); }; c.SendMessage = (t) => { throw new InvalidOperationException($"channel {channel.Name} is guild; cannot accept text"); };
c.SendFile = (f, t) => { throw new InvalidOperationException($"channel {channel.Name} is guild; send file"); }; c.SendFile = (f, t) => { throw new InvalidOperationException($"channel {channel.Name} is guild; send file"); };
db.SaveChanges(); Rememberer.RememberChannel(c);
return c; return c;
} }
internal Account UpsertAccount(IUser user, Channel inChannel) internal Account UpsertAccount(IUser user, Channel inChannel)
{ {
var db = new ChattingContext(); var acc = Rememberer.SearchAccount(ui => ui.ExternalId == user.Id.ToString() && ui.SeenInChannel.Id == inChannel.Id);
var acc = db.Accounts.FirstOrDefault(ui => ui.ExternalId == user.Id.ToString() && ui.SeenInChannel.Id == inChannel.Id);
if (acc == null) if (acc == null)
{ {
acc = new Account(); acc = new Account();
db.Accounts.Add(acc);
} }
acc.Username = user.Username; acc.Username = user.Username;
acc.ExternalId = user.Id.ToString(); acc.ExternalId = user.Id.ToString();
@ -326,21 +326,19 @@ public class DiscordInterface
acc.Protocol = PROTOCOL; acc.Protocol = PROTOCOL;
acc.SeenInChannel = inChannel; acc.SeenInChannel = inChannel;
acc.IsUser = db.Users.FirstOrDefault(u => u.Accounts.Any(a => a.ExternalId == acc.ExternalId && a.Protocol == acc.Protocol)); acc.IsUser = Rememberer.SearchUser(u => u.Accounts.Any(a => a.ExternalId == acc.ExternalId && a.Protocol == acc.Protocol));
if(acc.IsUser == null) //db.Users.FirstOrDefault(u => u.Accounts.Any(a => a.ExternalId == acc.ExternalId && a.Protocol == acc.Protocol));
if (acc.IsUser == null)
{ {
acc.IsUser = new User() { Accounts = new List<Account>() { acc } }; acc.IsUser = new User() { Accounts = new List<Account>() { acc } };
db.Users.Add(acc.IsUser);
} }
db.SaveChanges(); Rememberer.RememberAccount(acc);
return acc; return acc;
} }
private Task attemptReact(IUserMessage msg, string e) private Task attemptReact(IUserMessage msg, string e)
{ {
var c = Rememberer.SearchChannel(c => c.ExternalId == msg.Channel.Id.ToString());// db.Channels.FirstOrDefault(c => c.ExternalId == msg.Channel.Id.ToString());
var db = new ChattingContext();
var c = db.Channels.FirstOrDefault(c => c.ExternalId == msg.Channel.Id.ToString());
//var preferredEmote = c.EmoteOverrides?[e] ?? e; //TODO: emote overrides //var preferredEmote = c.EmoteOverrides?[e] ?? e; //TODO: emote overrides
var preferredEmote = e; var preferredEmote = e;
if (Emoji.TryParse(preferredEmote, out Emoji emoji)) if (Emoji.TryParse(preferredEmote, out Emoji emoji))

79
Rememberer.cs Normal file
View File

@ -0,0 +1,79 @@
namespace vassago;
using System.Linq.Expressions;
using vassago.Models;
public static class Rememberer
{
public static Account SearchAccount(Expression<Func<Account, bool>> predicate)
{
return (new ChattingContext()).Accounts.FirstOrDefault(predicate);
}
public static List<Account> SearchAccounts(Expression<Func<Account, bool>> predicate)
{
return (new ChattingContext()).Accounts.Where(predicate).ToList();
}
public static Attachment SearchAttachment(Expression<Func<Attachment, bool>> predicate)
{
return (new ChattingContext()).Attachments.FirstOrDefault(predicate);
}
public static Channel SearchChannel(Expression<Func<Channel, bool>> predicate)
{
return (new ChattingContext()).Channels.FirstOrDefault(predicate);
}
public static Message SearchMessage(Expression<Func<Message, bool>> predicate)
{
return (new ChattingContext()).Messages.FirstOrDefault(predicate);
}
public static User SearchUser(Expression<Func<User, bool>> predicate)
{
return (new ChattingContext()).Users.FirstOrDefault(predicate);
}
public static void RememberAccount(Account toRemember)
{
var db = new ChattingContext();
if (toRemember.Id == Guid.Empty)
db.Accounts.Add(toRemember);
db.SaveChanges();
}
public static void RememberAttachment(Attachment toRemember)
{
var db = new ChattingContext();
if (toRemember.Id == Guid.Empty)
db.Attachments.Add(toRemember);
db.SaveChanges();
}
public static void RememberChannel(Channel toRemember)
{
var db = new ChattingContext();
if (toRemember.Id == Guid.Empty)
db.Channels.Add(toRemember);
db.SaveChanges();
}
public static void RememberMessage(Message toRemember)
{
var db = new ChattingContext();
if (toRemember.Id == Guid.Empty)
{
db.Messages.Add(toRemember);
}
db.SaveChanges();
}
public static void RememberUser(User toRemember)
{
var db = new ChattingContext();
if (toRemember.Id == Guid.Empty)
db.Users.Add(toRemember);
db.SaveChanges();
}
public static void ForgetUser(User toForget)
{
var db = new ChattingContext();
db.Users.Remove(toForget);
db.SaveChanges();
}
}

View File

@ -166,7 +166,6 @@ public class HomeController : Controller
} }
private void serializeUser(ref StringBuilder sb, ref List<Account> allAccounts, User currentUser) private void serializeUser(ref StringBuilder sb, ref List<Account> allAccounts, User currentUser)
{ {
Console.WriteLine(currentUser);
sb.Append($"{{\"text\": \"<a href=\\\"{Url.ActionLink(action: "Details", controller: "Users", values: new {id = currentUser.Id})}\\\">"); sb.Append($"{{\"text\": \"<a href=\\\"{Url.ActionLink(action: "Details", controller: "Users", values: new {id = currentUser.Id})}\\\">");
sb.Append(currentUser.DisplayName); sb.Append(currentUser.DisplayName);
sb.Append("</a>\", "); sb.Append("</a>\", ");