users controller, and some other fixes

This commit is contained in:
Adam R Grey 2023-07-04 20:40:37 -04:00
parent 4a60e53798
commit e9ddcd237c
7 changed files with 91 additions and 16 deletions

View File

@ -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<UsersController> _logger;
private readonly ChattingContext _db;
public UsersController(ILogger<UsersController> logger, ChattingContext db)
{
_logger = logger;
_db = db;
}
public async Task<IActionResult> 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 });
}
}

View File

@ -13,6 +13,8 @@ public class ChattingContext : DbContext
public DbSet<Account> Accounts { get; set; }
public DbSet<User> Users { get; set; }
public ChattingContext(DbContextOptions<ChattingContext> 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.

View File

@ -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<IHostedService, vassago.ConsoleService>();
builder.Services.AddDbContext<ChattingContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("ChattingContext")));
var app = builder.Build();

View File

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

View File

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

View File

@ -2,11 +2,11 @@
ViewData["Title"] = "Home Page";
}
<div>
connected accounts?
<a href="Users">Users</a>
</div>
<div>
list of Users
<a href="Accounts">Accounts</a>
</div>
<div>
list of channels
<a href="Channels">Channels</a>
</div>

40
Views/Users/Index.cshtml Normal file
View File

@ -0,0 +1,40 @@
@model IEnumerable<User>
@{
ViewData["Title"] = "Users";
}
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Id)
</th>
<th>
@Html.DisplayNameFor(model => model.PermissionTags)
</th>
<th>
number of associated accounts
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.PermissionTags)
</td>
<td>
@Html.DisplayFor(modelItem => item.Accounts.Count)x
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>