more details, more links, fixed a bug where it wouldn't give itself a seen-in-channel

This commit is contained in:
Adam R Grey 2024-06-10 16:24:30 -04:00
parent e364b47c0f
commit 4bd51721b6
4 changed files with 21 additions and 20 deletions

View File

@ -114,7 +114,7 @@ public class DiscordInterface
private async Task SelfConnected()
{
var selfAccount = UpsertAccount(client.CurrentUser, protocolAsChannel.Id);
var selfAccount = UpsertAccount(client.CurrentUser, protocolAsChannel);
selfAccount.DisplayName = client.CurrentUser.Username;
await _db.SaveChangesAsync();
@ -150,7 +150,7 @@ public class DiscordInterface
var guild = UpsertChannel(arg.Guild);
var defaultChannel = UpsertChannel(arg.Guild.DefaultChannel);
defaultChannel.ParentChannel = guild;
var u = UpsertAccount(arg, guild.Id);
var u = UpsertAccount(arg, guild);
u.DisplayName = arg.DisplayName;
}
private async Task ButtonHandler(SocketMessageComponent component)
@ -226,8 +226,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.Channel.Id);
m.Author.SeenInChannel = m.Channel;
m.Author = UpsertAccount(dMessage.Author, m.Channel);
if(dMessage.Channel is IGuildChannel)
{
m.Author.DisplayName = (dMessage.Author as IGuildUser).DisplayName;//discord forgot how display names work.
@ -301,9 +300,9 @@ public class DiscordInterface
c.SendFile = (f, t) => { throw new InvalidOperationException($"channel {channel.Name} is guild; send file"); };
return c;
}
internal Account UpsertAccount(IUser user, Guid inChannel)
internal Account UpsertAccount(IUser user, Channel inChannel)
{
var acc = _db.Accounts.FirstOrDefault(ui => ui.ExternalId == user.Id.ToString() && ui.SeenInChannel.Id == inChannel);
var acc = _db.Accounts.FirstOrDefault(ui => ui.ExternalId == user.Id.ToString() && ui.SeenInChannel.Id == inChannel.Id);
if (acc == null)
{
acc = new Account();
@ -313,6 +312,7 @@ public class DiscordInterface
acc.ExternalId = user.Id.ToString();
acc.IsBot = user.IsBot || user.IsWebhook;
acc.Protocol = PROTOCOL;
acc.SeenInChannel = inChannel;
acc.IsUser = _db.Users.FirstOrDefault(u => u.Accounts.Any(a => a.ExternalId == acc.ExternalId && a.Protocol == acc.Protocol));
if(acc.IsUser == null)

View File

@ -26,7 +26,7 @@ public class ChannelsController : Controller
{
if(_db.Channels == null)
return Problem("Entity set '_db.Channels' is null.");
var channel = await _db.Channels.Include(u => u.ParentChannel).FirstAsync(u => u.Id == id);
var channel = await _db.Channels.Include(u => u.SubChannels).Include(u => u.Users).Include(u => u.ParentChannel).FirstAsync(u => u.Id == id);
var walker = channel;
while(walker != null)
{

View File

@ -2,6 +2,7 @@
using System.Text;
using System.Web;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.FileSystemGlobbing.Internal.PathSegments;
using vassago.Models;
@ -21,7 +22,7 @@ public class HomeController : Controller
public IActionResult Index()
{
var allAccounts = _db.Accounts.ToList();
var allChannels = _db.Channels.ToList();
var allChannels = _db.Channels.Include(c => c.Users).ToList();
var sb = new StringBuilder();
sb.Append("[");
sb.Append("{text: \"channels\", nodes: [");
@ -41,11 +42,11 @@ public class HomeController : Controller
serializeChannel(ref sb, ref allChannels, ref allAccounts, topLevelChannel);
}
sb.Append("]},");
sb.Append("]}");
if (allChannels.Any())
{
sb.Append("{text: \"orphaned channels\", nodes: [");
sb.Append(",{text: \"orphaned channels\", nodes: [");
first = true;
while (true)
{
@ -63,11 +64,11 @@ public class HomeController : Controller
break;
}
}
sb.Append("]},");
sb.Append("]}");
}
if (allAccounts.Any())
{
sb.Append("{text: \"channelless accounts\", nodes: [");
sb.Append(",{text: \"channelless accounts\", nodes: [");
first = true;
foreach (var acc in allAccounts)
{
@ -84,13 +85,9 @@ public class HomeController : Controller
sb.Append("]}");
}
var users = _db.Users.ToList();
if(topLevelChannels.Any() && users.Any())
{
sb.Append(',');
}
if(users.Any())
{
sb.Append("{text: \"users\", nodes: [");
sb.Append(",{text: \"users\", nodes: [");
first=true;
//refresh list; we'll be knocking them out again in serializeUser
allAccounts = _db.Accounts.ToList();
@ -117,7 +114,7 @@ public class HomeController : Controller
allChannels.Remove(currentChannel);
//"but adam", you say, "there's an href attribute, why make a link?" because that makes the entire bar a link, and trying to expand the node will probably click the link
sb.Append($"{{\"text\": \"<a href=\\\"{Url.ActionLink(action: "Details", controller: "Channels", values: new {id = currentChannel.Id})}\\\">{currentChannel.DisplayName}</a>\"");
var theseAccounts = allAccounts.Where(a => a.SeenInChannel?.Id == currentChannel.Id);
var theseAccounts = allAccounts.Where(a => a.SeenInChannel?.Id == currentChannel.Id).ToList();
allAccounts.RemoveAll(a => a.SeenInChannel?.Id == currentChannel.Id);
var first = true;
if (currentChannel.SubChannels != null || theseAccounts != null)
@ -140,7 +137,6 @@ public class HomeController : Controller
}
if (theseAccounts != null)
{
sb.Append(',');
}
}
@ -170,7 +166,11 @@ public class HomeController : Controller
}
private void serializeUser(ref StringBuilder sb, ref List<Account> allAccounts, User currentUser)
{
sb.Append($"{{\"text\": \"{currentUser.DisplayName}\", ");
sb.Append($"{{\"text\": " +
$"\"<a href=\\\"{Url.ActionLink(action: "Details", controller: "Users", values: new {id = currentUser.Id})}\\\">"
+ currentUser.DisplayName +
"</a>\", ");
// \"{currentUser.DisplayName}\", ");
var ownedAccounts = allAccounts.Where(a => a.IsUser == currentUser);
sb.Append("nodes: [");
sb.Append($"{{\"text\": \"owned accounts:\", \"expanded\":true, \"nodes\": [");

View File

@ -8,6 +8,7 @@ tree above.
<script type="text/javascript">
function getTree() {
var tree = @Html.Raw(ViewData["treeString"]);
console.log(tree);
return tree;
}