From e9ddcd237c61f2f64b563cb291c430e17b1f2f34 Mon Sep 17 00:00:00 2001 From: Adam R Grey Date: Tue, 4 Jul 2023 20:40:37 -0400 Subject: [PATCH] users controller, and some other fixes --- Controllers/UsersController.cs | 31 ++++++++++++++ Models/ChattingContext.cs | 2 + Program.cs | 5 +++ .../DiscordInterface/DiscordInterface.cs | 12 +++--- .../TwitchInterface/TwitchInterface.cs | 11 +++-- Views/Home/Index.cshtml | 6 +-- Views/Users/Index.cshtml | 40 +++++++++++++++++++ 7 files changed, 91 insertions(+), 16 deletions(-) create mode 100644 Controllers/UsersController.cs create mode 100644 Views/Users/Index.cshtml diff --git a/Controllers/UsersController.cs b/Controllers/UsersController.cs new file mode 100644 index 0000000..f8c3fe4 --- /dev/null +++ b/Controllers/UsersController.cs @@ -0,0 +1,31 @@ +using System.Diagnostics; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using vassago.Models; + +namespace vassago.Controllers; + +public class UsersController : Controller +{ + private readonly ILogger _logger; + private readonly ChattingContext _db; + + public UsersController(ILogger logger, ChattingContext db) + { + _logger = logger; + _db = db; + } + + public async Task Index(string searchString) + { + return _db.Users != null ? + View(await _db.Users.Include(u => u.Accounts).ToListAsync()) : + Problem("Entity set '_db.Users' is null."); + } + + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] + public IActionResult Error() + { + return View(new ErrorPageViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); + } +} \ No newline at end of file diff --git a/Models/ChattingContext.cs b/Models/ChattingContext.cs index cfe3532..259a005 100644 --- a/Models/ChattingContext.cs +++ b/Models/ChattingContext.cs @@ -13,6 +13,8 @@ public class ChattingContext : DbContext public DbSet Accounts { get; set; } public DbSet Users { get; set; } + public ChattingContext(DbContextOptions options) : base(options) { } + public ChattingContext() : base() { } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseNpgsql(Shared.DBConnectionString) .EnableSensitiveDataLogging(true); //who the fuck is looking at log output but not allowed to see it? this should be on by default. diff --git a/Program.cs b/Program.cs index 21fca45..583ec92 100644 --- a/Program.cs +++ b/Program.cs @@ -1,8 +1,13 @@ +using Microsoft.EntityFrameworkCore; +using vassago.Models; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllersWithViews(); builder.Services.AddSingleton(); +builder.Services.AddDbContext(options => + options.UseNpgsql(builder.Configuration.GetConnectionString("ChattingContext"))); var app = builder.Build(); diff --git a/ProtocolInterfaces/DiscordInterface/DiscordInterface.cs b/ProtocolInterfaces/DiscordInterface/DiscordInterface.cs index 49bbb7c..c68a00d 100644 --- a/ProtocolInterfaces/DiscordInterface/DiscordInterface.cs +++ b/ProtocolInterfaces/DiscordInterface/DiscordInterface.cs @@ -117,8 +117,7 @@ public class DiscordInterface private async Task SelfConnected() { - var selfUser = UpsertAccount(client.CurrentUser); - selfUser.SeenInChannel = protocolAsChannel; + var selfUser = UpsertAccount(client.CurrentUser, protocolAsChannel.Id); selfUser.DisplayName = client.CurrentUser.Username; await _db.SaveChangesAsync(); @@ -154,8 +153,7 @@ public class DiscordInterface var guild = UpsertChannel(arg.Guild); var defaultChannel = UpsertChannel(arg.Guild.DefaultChannel); defaultChannel.ParentChannel = guild; - var u = UpsertAccount(arg); - u.SeenInChannel = guild; + var u = UpsertAccount(arg, guild.Id); u.DisplayName = arg.DisplayName; } private async Task ButtonHandler(SocketMessageComponent component) @@ -230,7 +228,7 @@ public class DiscordInterface m.ExternalId = dMessage.Id.ToString(); m.Timestamp = dMessage.EditedTimestamp ?? dMessage.CreatedAt; m.Channel = UpsertChannel(dMessage.Channel); - m.Author = UpsertAccount(dMessage.Author); + m.Author = UpsertAccount(dMessage.Author, m.Channel.Id); m.Author.SeenInChannel = m.Channel; if(dMessage.Channel is IGuildChannel) { @@ -299,10 +297,10 @@ public class DiscordInterface c.SendFile = (f, t) => { throw new InvalidOperationException($"channel {channel.Name} is guild; send file"); }; return c; } - internal Account UpsertAccount(IUser user) + internal Account UpsertAccount(IUser user, Guid inChannel) { var hadToAdd = false; - var acc = _db.Accounts.FirstOrDefault(ui => ui.ExternalId == user.Id.ToString() && ui.Protocol == PROTOCOL); + var acc = _db.Accounts.FirstOrDefault(ui => ui.ExternalId == user.Id.ToString() && ui.SeenInChannel.Id == inChannel); if (acc == null) { acc = new Account(); diff --git a/ProtocolInterfaces/TwitchInterface/TwitchInterface.cs b/ProtocolInterfaces/TwitchInterface/TwitchInterface.cs index 1ff9aef..200be27 100644 --- a/ProtocolInterfaces/TwitchInterface/TwitchInterface.cs +++ b/ProtocolInterfaces/TwitchInterface/TwitchInterface.cs @@ -121,8 +121,7 @@ public class TwitchInterface private async void Client_OnConnected(object sender, OnConnectedArgs e) { - var selfUser = UpsertAccount(e.BotUsername); - selfUser.SeenInChannel = protocolAsChannel; + var selfUser = UpsertAccount(e.BotUsername, protocolAsChannel.Id); await _db.SaveChangesAsync(); Behaver.Instance.Selves.Add(selfUser); @@ -140,10 +139,10 @@ public class TwitchInterface Console.WriteLine($"{e.DateTime.ToString()}: {e.BotUsername} - {e.Data}"); } - private Account UpsertAccount(string username) + private Account UpsertAccount(string username, Guid inChannel) { var hadToAdd = false; - var acc = _db.Accounts.FirstOrDefault(ui => ui.ExternalId == username); + var acc = _db.Accounts.FirstOrDefault(ui => ui.ExternalId == username && ui.SeenInChannel.Id == inChannel); if (acc == null) { acc = new Account(); @@ -221,7 +220,7 @@ public class TwitchInterface m.Content = chatMessage.Message; m.ExternalId = chatMessage.Id; m.Channel = UpsertChannel(chatMessage.Channel); - m.Author = UpsertAccount(chatMessage.Username); + m.Author = UpsertAccount(chatMessage.Username, m.Channel.Id); m.Author.SeenInChannel = m.Channel; m.Reply = (t) => { return Task.Run(() => { client.SendReply(chatMessage.Channel, chatMessage.Id, t); }); }; @@ -242,7 +241,7 @@ public class TwitchInterface m.ExternalId = whisperMessage.MessageId; m.Channel = UpsertDMChannel(whisperMessage.Username); m.Channel.IsDM = true; - m.Author = UpsertAccount(whisperMessage.Username); + m.Author = UpsertAccount(whisperMessage.Username, m.Channel.Id); m.Author.SeenInChannel = m.Channel; m.Reply = (t) => { return Task.Run(() => {client.SendWhisper(whisperMessage.Username, t); });}; diff --git a/Views/Home/Index.cshtml b/Views/Home/Index.cshtml index 700e096..88462b9 100644 --- a/Views/Home/Index.cshtml +++ b/Views/Home/Index.cshtml @@ -2,11 +2,11 @@ ViewData["Title"] = "Home Page"; }
- connected accounts? + Users
- list of Users + Accounts
- list of channels + Channels
diff --git a/Views/Users/Index.cshtml b/Views/Users/Index.cshtml new file mode 100644 index 0000000..24ebb70 --- /dev/null +++ b/Views/Users/Index.cshtml @@ -0,0 +1,40 @@ +@model IEnumerable +@{ + ViewData["Title"] = "Users"; +} + + + + + + + + + + + @foreach (var item in Model) { + + + + + + + } + +
+ @Html.DisplayNameFor(model => model.Id) + + @Html.DisplayNameFor(model => model.PermissionTags) + + number of associated accounts +
+ @Html.DisplayFor(modelItem => item.Id) + + @Html.DisplayFor(modelItem => item.PermissionTags) + + @Html.DisplayFor(modelItem => item.Accounts.Count)x + + Edit | + Details | + Delete +