db in the right place; fix QR code math

This commit is contained in:
Adam R Grey 2024-01-10 21:21:31 -05:00
parent c5f9ae2c6b
commit b84e47344b
6 changed files with 18 additions and 20 deletions

View File

@ -10,14 +10,11 @@ using System.Collections.Generic;
public class Behaver
{
private ChattingContext _db;
private List<Account> SelfAccounts { get; set; } = new List<Account>();
private User SelfUser { get; set; }
public static List<Behavior> Behaviors { get; private set; } = new List<Behavior>();
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)
{

View File

@ -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 :)");
}

View File

@ -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");
}

View File

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

View File

@ -7,7 +7,7 @@ var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
builder.Services.AddSingleton<IHostedService, vassago.ConsoleService>();
builder.Services.AddDbContext<ChattingContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("ChattingContext") ),ServiceLifetime.Transient);
options.UseNpgsql(builder.Configuration.GetConnectionString("ChattingContext") ));
var app = builder.Build();

View File

@ -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)