diff --git a/ConsoleService.cs b/ConsoleService.cs index c01c663..a54c815 100644 --- a/ConsoleService.cs +++ b/ConsoleService.cs @@ -4,6 +4,7 @@ namespace vassago using vassago; using vassago.Models; using vassago.TwitchInterface; + using vassago.ProtocolInterfaces.DiscordInterface; internal class ConsoleService : IHostedService { @@ -27,7 +28,7 @@ namespace vassago if (DiscordTokens?.Any() ?? false) foreach (var dt in DiscordTokens) { - var d = new DiscordInterface.DiscordInterface(); + var d = new DiscordInterface(); await d.Init(dt); ProtocolInterfaces.ProtocolList.discords.Add(d); } diff --git a/ProtocolInterfaces/DiscordInterface/DiscordInterface.cs b/ProtocolInterfaces/DiscordInterface/DiscordInterface.cs index 5bb23ea..875b286 100644 --- a/ProtocolInterfaces/DiscordInterface/DiscordInterface.cs +++ b/ProtocolInterfaces/DiscordInterface/DiscordInterface.cs @@ -13,14 +13,14 @@ using Microsoft.EntityFrameworkCore; using System.Threading; using System.Reactive.Linq; -namespace vassago.DiscordInterface; +namespace vassago.ProtocolInterfaces.DiscordInterface; public class DiscordInterface { internal const string PROTOCOL = "discord"; internal DiscordSocketClient client; private bool eventsSignedUp = false; - private static SemaphoreSlim discordChannelSetup = new SemaphoreSlim(1, 1); + private static readonly SemaphoreSlim discordChannelSetup = new(1, 1); private Channel protocolAsChannel; public async Task Init(string token) @@ -33,8 +33,8 @@ public class DiscordInterface Console.WriteLine(msg.ToString()); return Task.CompletedTask; }; - client.Connected += SelfConnected; - client.Ready += ClientReady; + client.Connected += () => Task.Run(SelfConnected); + client.Ready += () => Task.Run(ClientReady); await client.LoginAsync(TokenType.Bot, token); await client.StartAsync(); @@ -60,7 +60,7 @@ public class DiscordInterface ReactionsPossible = true, ExternalId = null, Protocol = PROTOCOL, - SubChannels = new List() + SubChannels = [] }; } else @@ -87,7 +87,7 @@ public class DiscordInterface client.MessageReceived += MessageReceived; // _client.MessageUpdated += - //client.UserJoined += UserJoined; + client.UserJoined += UserJoined; client.SlashCommandExecuted += SlashCommandHandler; //client.ChannelCreated += // _client.ChannelDestroyed += @@ -110,7 +110,7 @@ public class DiscordInterface } } - private async Task SelfConnected() + private void SelfConnected() { var selfAccount = UpsertAccount(client.CurrentUser, protocolAsChannel); selfAccount.DisplayName = client.CurrentUser.Username; @@ -119,12 +119,13 @@ public class DiscordInterface private async Task MessageReceived(SocketMessage messageParam) { - var suMessage = messageParam as SocketUserMessage; - if (suMessage == null) + if(messageParam is not SocketUserMessage) { Console.WriteLine($"{messageParam.Content}, but not a user message"); return; } + var suMessage = messageParam as SocketUserMessage; + Console.WriteLine($"#{suMessage.Channel}[{DateTime.Now}][{suMessage.Author.Username} [id={suMessage.Author.Id}]][msg id: {suMessage.Id}] {suMessage.Content}"); var m = UpsertMessage(suMessage); @@ -138,22 +139,14 @@ public class DiscordInterface 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 Task UserJoined(SocketGuildUser arg) { var guild = UpsertChannel(arg.Guild); var defaultChannel = UpsertChannel(arg.Guild.DefaultChannel); defaultChannel.ParentChannel = guild; var u = UpsertAccount(arg, guild); u.DisplayName = arg.DisplayName; - } - private async Task ButtonHandler(SocketMessageComponent component) - { - switch (component.Data.CustomId) - { - case "custom-id": - await component.RespondAsync($"{component.User.Mention}, it's been here the whole time!"); - break; - } + return null; } internal static async Task SlashCommandHandler(SocketSlashCommand command) { @@ -181,13 +174,11 @@ public class DiscordInterface break; } } - internal vassago.Models.Attachment UpsertAttachment(IAttachment dAttachment) + internal static vassago.Models.Attachment UpsertAttachment(IAttachment dAttachment) { - var a = Rememberer.SearchAttachment(ai => ai.ExternalId == dAttachment.Id); - if (a == null) - { - a = new vassago.Models.Attachment(); - } + var a = Rememberer.SearchAttachment(ai => ai.ExternalId == dAttachment.Id) + ?? new vassago.Models.Attachment(); + a.ContentType = dAttachment.ContentType; a.Description = dAttachment.Description; a.Filename = dAttachment.Filename; @@ -198,16 +189,16 @@ public class DiscordInterface } internal Message UpsertMessage(IUserMessage dMessage) { - var m = Rememberer.SearchMessage(mi => mi.ExternalId == dMessage.Id.ToString() && mi.Protocol == PROTOCOL); - if (m == null) + var m = Rememberer.SearchMessage(mi => mi.ExternalId == dMessage.Id.ToString() && mi.Protocol == PROTOCOL) + ?? new() + { + Protocol = PROTOCOL + }; + + + if (dMessage.Attachments?.Count > 0) { - m = new Message(); - m.Protocol = PROTOCOL; - } - m.Attachments = m.Attachments ?? new List(); - if (dMessage.Attachments?.Any() == true) - { - m.Attachments = new List(); + m.Attachments = []; foreach (var da in dMessage.Attachments) { m.Attachments.Add(UpsertAttachment(da)); @@ -226,7 +217,7 @@ public class DiscordInterface && (dMessage.MentionedUserIds?.FirstOrDefault(muid => muid == client.CurrentUser.Id) > 0)); m.Reply = (t) => { return dMessage.ReplyAsync(t); }; - m.React = (e) => { return attemptReact(dMessage, e); }; + m.React = (e) => { return AttemptReact(dMessage, e); }; Rememberer.RememberChannel(m.Channel); return m; } @@ -242,7 +233,7 @@ public class DiscordInterface c.DisplayName = channel.Name; c.ExternalId = channel.Id.ToString(); c.ChannelType = (channel is IPrivateChannel) ? vassago.Models.Enumerations.ChannelType.DM : vassago.Models.Enumerations.ChannelType.Normal; - c.Messages = c.Messages ?? new List(); + c.Messages ??= []; c.Protocol = PROTOCOL; if (channel is IGuildChannel) { @@ -270,7 +261,6 @@ public class DiscordInterface if (channel is IGuildChannel) { parentChannel = Rememberer.SearchChannel(c => c.ExternalId == (channel as IGuildChannel).Guild.Id.ToString() && c.Protocol == PROTOCOL); - } else if (channel is IPrivateChannel) { @@ -281,8 +271,7 @@ public class DiscordInterface 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(); + parentChannel.SubChannels ??= []; parentChannel.SubChannels.Add(c); Rememberer.RememberChannel(parentChannel); @@ -302,10 +291,10 @@ public class DiscordInterface c.DisplayName = channel.Name; c.ExternalId = channel.Id.ToString(); c.ChannelType = vassago.Models.Enumerations.ChannelType.OU; - c.Messages = c.Messages ?? new List(); + c.Messages ??= []; c.Protocol = protocolAsChannel.Protocol; c.ParentChannel = protocolAsChannel; - c.SubChannels = c.SubChannels ?? new List(); + c.SubChannels ??= []; c.MaxAttachmentBytes = channel.MaxUploadLimit; c.SendMessage = (t) => { throw new InvalidOperationException($"channel {channel.Name} is guild; cannot accept text"); }; @@ -313,13 +302,11 @@ public class DiscordInterface Rememberer.RememberChannel(c); return c; } - internal Account UpsertAccount(IUser user, Channel inChannel) + internal static Account UpsertAccount(IUser user, Channel inChannel) { var acc = Rememberer.SearchAccount(ui => ui.ExternalId == user.Id.ToString() && ui.SeenInChannel.Id == inChannel.Id); - if (acc == null) - { - acc = new Account(); - } + acc ??= new Account(); + acc.Username = user.Username; acc.ExternalId = user.Id.ToString(); acc.IsBot = user.IsBot || user.IsWebhook; @@ -328,15 +315,14 @@ public class DiscordInterface acc.IsUser = Rememberer.SearchUser(u => u.Accounts.Any(a => a.ExternalId == acc.ExternalId && a.Protocol == acc.Protocol)); //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() { acc } }; - } + + acc.IsUser ??= new User() { Accounts = [ acc ] }; + Rememberer.RememberAccount(acc); return acc; } - private Task attemptReact(IUserMessage msg, string e) + private static 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 preferredEmote = c.EmoteOverrides?[e] ?? e; //TODO: emote overrides diff --git a/ProtocolInterfaces/DiscordInterface/SlashCommandsHelper.cs b/ProtocolInterfaces/DiscordInterface/SlashCommandsHelper.cs index 95ad279..a1cba8f 100644 --- a/ProtocolInterfaces/DiscordInterface/SlashCommandsHelper.cs +++ b/ProtocolInterfaces/DiscordInterface/SlashCommandsHelper.cs @@ -7,7 +7,7 @@ using Discord.WebSocket; using Discord; using Discord.Net; -namespace vassago.DiscordInterface +namespace vassago.ProtocolInterfaces.DiscordInterface { public static class SlashCommandsHelper { diff --git a/WebInterface/Controllers/ChannelsController.cs b/WebInterface/Controllers/ChannelsController.cs index 93b4b75..8e45a82 100644 --- a/WebInterface/Controllers/ChannelsController.cs +++ b/WebInterface/Controllers/ChannelsController.cs @@ -4,35 +4,29 @@ using System.Text; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using vassago.Models; +using vassago.WebInterface.Models; -namespace vassago.Controllers; +namespace vassago.WebInterface.Controllers; -public class ChannelsController : Controller +public class ChannelsController(ChattingContext db) : Controller { - private readonly ILogger _logger; - private readonly ChattingContext _db; + private ChattingContext Database => db; - public ChannelsController(ILogger logger, ChattingContext db) + public IActionResult Index() { - _logger = logger; - _db = db; - } - - public async Task Index(string searchString) - { - return _db.Channels != null ? - View(_db.Channels.Include(u => u.ParentChannel).ToList().OrderBy(c => c.LineageSummary)) : + return Database.Channels != null ? + View(Database.Channels.Include(u => u.ParentChannel).ToList().OrderBy(c => c.LineageSummary)) : Problem("Entity set '_db.Channels' is null."); } public async Task Details(Guid id) { - if(_db.Channels == null) + if(Database.Channels == null) return Problem("Entity set '_db.Channels' is null."); //"but adam", says the strawman, "why load *every* channel and walk your way up? surely there's a .Load command that works or something." //eh. I checked. Not really. You could make an SQL view that recurses its way up, meh idk how. You could just eagerly load *every* related object... //but that would take in all the messages. //realistically I expect this will have less than 1MB of total "channels", and several GB of total messages per (text) channel. - var AllChannels = await _db.Channels + var AllChannels = await Database.Channels .Include(u => u.SubChannels) .Include(u => u.Users) .Include(u => u.ParentChannel) @@ -46,7 +40,7 @@ public class ChannelsController : Controller walker = walker.ParentChannel; } var sb = new StringBuilder(); - sb.Append("["); + sb.Append('['); sb.Append($"{{text: \"{channel.SubChannels?.Count}\", nodes: ["); var first=true; foreach(var subChannel in channel.SubChannels) diff --git a/WebInterface/Controllers/HomeController.cs b/WebInterface/Controllers/HomeController.cs index e048ac4..6515e07 100644 --- a/WebInterface/Controllers/HomeController.cs +++ b/WebInterface/Controllers/HomeController.cs @@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.FileSystemGlobbing.Internal.PathSegments; using vassago.Models; +using vassago.WebInterface.Models; namespace vassago.Controllers; diff --git a/WebInterface/Controllers/UsersController.cs b/WebInterface/Controllers/UsersController.cs index ae37360..f18ba0a 100644 --- a/WebInterface/Controllers/UsersController.cs +++ b/WebInterface/Controllers/UsersController.cs @@ -2,37 +2,31 @@ using System.Diagnostics; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using vassago.Models; +using vassago.WebInterface.Models; -namespace vassago.Controllers; +namespace vassago.WebInterface.Controllers; -public class UsersController : Controller +public class UsersController(ChattingContext db) : Controller { - private readonly ILogger _logger; - private readonly ChattingContext _db; + private ChattingContext Database => db; - public UsersController(ILogger logger, ChattingContext db) + public async Task Index() { - _logger = logger; - _db = db; - } - - public async Task Index(string searchString) - { - return _db.Users != null ? - View(await _db.Users.Include(u => u.Accounts).ToListAsync()) : + return Database.Users != null ? + View(await Database.Users.Include(u => u.Accounts).ToListAsync()) : Problem("Entity set '_db.Users' is null."); } public async Task Details(Guid id) { - var user = await _db.Users + var user = await Database.Users .Include(u => u.Accounts) .FirstAsync(u => u.Id == id); - var allTheChannels = await _db.Channels.ToListAsync(); + var allTheChannels = await Database.Channels.ToListAsync(); foreach(var acc in user.Accounts) { acc.SeenInChannel = allTheChannels.FirstOrDefault(c => c.Id == acc.SeenInChannel.Id); } - return _db.Users != null ? + return Database.Users != null ? View(user) : Problem("Entity set '_db.Users' is null."); } diff --git a/WebInterface/Controllers/ErrorPageViewModel.cs b/WebInterface/Models/ErrorPageViewModel.cs similarity index 54% rename from WebInterface/Controllers/ErrorPageViewModel.cs rename to WebInterface/Models/ErrorPageViewModel.cs index def8bca..a013dc1 100644 --- a/WebInterface/Controllers/ErrorPageViewModel.cs +++ b/WebInterface/Models/ErrorPageViewModel.cs @@ -1,8 +1,8 @@ -namespace vassago.Models; +namespace vassago.WebInterface.Models; public class ErrorPageViewModel { - public string? RequestId { get; set; } + public string RequestId { get; set; } public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); } diff --git a/WebInterface/Views/Shared/Error.cshtml b/WebInterface/Views/Shared/Error.cshtml index 941bdaa..8b6606d 100644 --- a/WebInterface/Views/Shared/Error.cshtml +++ b/WebInterface/Views/Shared/Error.cshtml @@ -1,4 +1,4 @@ -@model ErrorPageViewModel +@model vassago.WebInterface.Models.ErrorPageViewModel @{ ViewData["Title"] = "Error"; }