forked from adam/discord-bot-shtik
users controller, and some other fixes
This commit is contained in:
parent
4a60e53798
commit
e9ddcd237c
31
Controllers/UsersController.cs
Normal file
31
Controllers/UsersController.cs
Normal 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 });
|
||||||
|
}
|
||||||
|
}
|
@ -13,6 +13,8 @@ public class ChattingContext : DbContext
|
|||||||
public DbSet<Account> Accounts { get; set; }
|
public DbSet<Account> Accounts { get; set; }
|
||||||
public DbSet<User> Users { get; set; }
|
public DbSet<User> Users { get; set; }
|
||||||
|
|
||||||
|
public ChattingContext(DbContextOptions<ChattingContext> options) : base(options) { }
|
||||||
|
public ChattingContext() : base() { }
|
||||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||||
=> optionsBuilder.UseNpgsql(Shared.DBConnectionString)
|
=> 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.
|
.EnableSensitiveDataLogging(true); //who the fuck is looking at log output but not allowed to see it? this should be on by default.
|
||||||
|
@ -1,8 +1,13 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using vassago.Models;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
// Add services to the container.
|
// Add services to the container.
|
||||||
builder.Services.AddControllersWithViews();
|
builder.Services.AddControllersWithViews();
|
||||||
builder.Services.AddSingleton<IHostedService, vassago.ConsoleService>();
|
builder.Services.AddSingleton<IHostedService, vassago.ConsoleService>();
|
||||||
|
builder.Services.AddDbContext<ChattingContext>(options =>
|
||||||
|
options.UseNpgsql(builder.Configuration.GetConnectionString("ChattingContext")));
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
@ -117,8 +117,7 @@ public class DiscordInterface
|
|||||||
|
|
||||||
private async Task SelfConnected()
|
private async Task SelfConnected()
|
||||||
{
|
{
|
||||||
var selfUser = UpsertAccount(client.CurrentUser);
|
var selfUser = UpsertAccount(client.CurrentUser, protocolAsChannel.Id);
|
||||||
selfUser.SeenInChannel = protocolAsChannel;
|
|
||||||
selfUser.DisplayName = client.CurrentUser.Username;
|
selfUser.DisplayName = client.CurrentUser.Username;
|
||||||
|
|
||||||
await _db.SaveChangesAsync();
|
await _db.SaveChangesAsync();
|
||||||
@ -154,8 +153,7 @@ public class DiscordInterface
|
|||||||
var guild = UpsertChannel(arg.Guild);
|
var guild = UpsertChannel(arg.Guild);
|
||||||
var defaultChannel = UpsertChannel(arg.Guild.DefaultChannel);
|
var defaultChannel = UpsertChannel(arg.Guild.DefaultChannel);
|
||||||
defaultChannel.ParentChannel = guild;
|
defaultChannel.ParentChannel = guild;
|
||||||
var u = UpsertAccount(arg);
|
var u = UpsertAccount(arg, guild.Id);
|
||||||
u.SeenInChannel = guild;
|
|
||||||
u.DisplayName = arg.DisplayName;
|
u.DisplayName = arg.DisplayName;
|
||||||
}
|
}
|
||||||
private async Task ButtonHandler(SocketMessageComponent component)
|
private async Task ButtonHandler(SocketMessageComponent component)
|
||||||
@ -230,7 +228,7 @@ public class DiscordInterface
|
|||||||
m.ExternalId = dMessage.Id.ToString();
|
m.ExternalId = dMessage.Id.ToString();
|
||||||
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.Author = UpsertAccount(dMessage.Author, m.Channel.Id);
|
||||||
m.Author.SeenInChannel = m.Channel;
|
m.Author.SeenInChannel = m.Channel;
|
||||||
if(dMessage.Channel is IGuildChannel)
|
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"); };
|
c.SendFile = (f, t) => { throw new InvalidOperationException($"channel {channel.Name} is guild; send file"); };
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
internal Account UpsertAccount(IUser user)
|
internal Account UpsertAccount(IUser user, Guid inChannel)
|
||||||
{
|
{
|
||||||
var hadToAdd = false;
|
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)
|
if (acc == null)
|
||||||
{
|
{
|
||||||
acc = new Account();
|
acc = new Account();
|
||||||
|
@ -121,8 +121,7 @@ public class TwitchInterface
|
|||||||
|
|
||||||
private async void Client_OnConnected(object sender, OnConnectedArgs e)
|
private async void Client_OnConnected(object sender, OnConnectedArgs e)
|
||||||
{
|
{
|
||||||
var selfUser = UpsertAccount(e.BotUsername);
|
var selfUser = UpsertAccount(e.BotUsername, protocolAsChannel.Id);
|
||||||
selfUser.SeenInChannel = protocolAsChannel;
|
|
||||||
|
|
||||||
await _db.SaveChangesAsync();
|
await _db.SaveChangesAsync();
|
||||||
Behaver.Instance.Selves.Add(selfUser);
|
Behaver.Instance.Selves.Add(selfUser);
|
||||||
@ -140,10 +139,10 @@ public class TwitchInterface
|
|||||||
Console.WriteLine($"{e.DateTime.ToString()}: {e.BotUsername} - {e.Data}");
|
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 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)
|
if (acc == null)
|
||||||
{
|
{
|
||||||
acc = new Account();
|
acc = new Account();
|
||||||
@ -221,7 +220,7 @@ public class TwitchInterface
|
|||||||
m.Content = chatMessage.Message;
|
m.Content = chatMessage.Message;
|
||||||
m.ExternalId = chatMessage.Id;
|
m.ExternalId = chatMessage.Id;
|
||||||
m.Channel = UpsertChannel(chatMessage.Channel);
|
m.Channel = UpsertChannel(chatMessage.Channel);
|
||||||
m.Author = UpsertAccount(chatMessage.Username);
|
m.Author = UpsertAccount(chatMessage.Username, m.Channel.Id);
|
||||||
m.Author.SeenInChannel = m.Channel;
|
m.Author.SeenInChannel = m.Channel;
|
||||||
|
|
||||||
m.Reply = (t) => { return Task.Run(() => { client.SendReply(chatMessage.Channel, chatMessage.Id, t); }); };
|
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.ExternalId = whisperMessage.MessageId;
|
||||||
m.Channel = UpsertDMChannel(whisperMessage.Username);
|
m.Channel = UpsertDMChannel(whisperMessage.Username);
|
||||||
m.Channel.IsDM = true;
|
m.Channel.IsDM = true;
|
||||||
m.Author = UpsertAccount(whisperMessage.Username);
|
m.Author = UpsertAccount(whisperMessage.Username, m.Channel.Id);
|
||||||
m.Author.SeenInChannel = m.Channel;
|
m.Author.SeenInChannel = m.Channel;
|
||||||
|
|
||||||
m.Reply = (t) => { return Task.Run(() => {client.SendWhisper(whisperMessage.Username, t); });};
|
m.Reply = (t) => { return Task.Run(() => {client.SendWhisper(whisperMessage.Username, t); });};
|
||||||
|
@ -2,11 +2,11 @@
|
|||||||
ViewData["Title"] = "Home Page";
|
ViewData["Title"] = "Home Page";
|
||||||
}
|
}
|
||||||
<div>
|
<div>
|
||||||
connected accounts?
|
<a href="Users">Users</a>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
list of Users
|
<a href="Accounts">Accounts</a>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
list of channels
|
<a href="Channels">Channels</a>
|
||||||
</div>
|
</div>
|
||||||
|
40
Views/Users/Index.cshtml
Normal file
40
Views/Users/Index.cshtml
Normal 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>
|
Loading…
Reference in New Issue
Block a user