Skip to content

WASM #1024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 46 commits into
base: main
Choose a base branch
from
Open

WASM #1024

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
8b1865a
Remove reading of 'developmentSettings.json' at boot
calledude Mar 31, 2024
7163a2f
Migrate to new blazor conventions
calledude Apr 1, 2024
81b24d2
Add WASM client project
calledude Jun 28, 2024
3d8d968
Add Modix.Web.Shared project
calledude Jun 28, 2024
c1cf8d6
Parse selected guild closer to the actual usage in ClaimsMiddleware
calledude Jun 28, 2024
145ffc1
Read data from API
calledude Jun 28, 2024
328b3a9
Reimplement the MudBlazor stuff in Wasm MainLayout
calledude Jun 28, 2024
ec2f29d
WIP possibly working auth
calledude Aug 17, 2024
ef9d0e3
Flow claims to wasm client as well
calledude Aug 17, 2024
a6e8981
Move navbar related components over to the wasm project
calledude Aug 17, 2024
f80d557
Stop using CascadingAuthenticationState
calledude Aug 17, 2024
a50eaca
Move Commands page over to the wasm project
calledude Aug 17, 2024
29d30ad
Remove Newtonsoft serializer
calledude Aug 17, 2024
caa4001
Include AvatarHash claim
calledude Aug 17, 2024
3389899
Move Stats page over to the wasm project
calledude Aug 17, 2024
ed0a9ff
Return user to the page they were trying to visit before getting chal…
calledude Aug 17, 2024
3bc8194
Move UserLookup page over to the wasm project
calledude Aug 17, 2024
db00804
Move new projects into the src folder to conform to the new repositor…
calledude Aug 17, 2024
12201b8
Replace the old Error page with the new blazor page variant
calledude Aug 17, 2024
926772a
Move Tags page over to the wasm project (pending auth fix)
calledude Aug 20, 2024
f4028fc
Reintroduce ModixController to get rid of redundant code, also fixes …
calledude Aug 20, 2024
ee2f8d8
Remove default pages
calledude Aug 20, 2024
64ee8f4
Cache roles for 10 minutes
calledude Aug 20, 2024
b6986c5
Move Promotions page over to the wasm project
calledude Aug 24, 2024
8c55ec6
Move CreatePromotion page over to the wasm project
calledude Aug 24, 2024
dc5c0ea
Move Infractions page over to the wasm project
calledude Aug 29, 2024
853a9fd
Move DeletedMessages page over to the wasm project
calledude Aug 29, 2024
fd8ef15
Implement guild option endpoint to fix guild selection
calledude Aug 31, 2024
23af1d7
Move Configuration/Channels page over to the wasm project
calledude Aug 31, 2024
d2447cb
Move Configuration/Roles page over to the wasm project
calledude Aug 31, 2024
e584bb1
Create common models project
calledude Sep 1, 2024
0034e8d
Switch over to using AuthorizationClaim enum from newly created model…
calledude Sep 1, 2024
d7054f2
Move Configuration/Claims page over to the wasm project
calledude Sep 1, 2024
b280394
Remove/fix TODOs
calledude Sep 1, 2024
c7475f4
Remove SelectedGuild from Routes params in favor of using user claim
calledude Sep 1, 2024
f316322
Disable SSR for now
calledude Sep 1, 2024
3812d54
Fixup authorization in controllers
calledude Sep 1, 2024
2d7c628
Move DesignatedChannelType to Modix.Models project
calledude Sep 1, 2024
8339951
Move PromotionCampaignOutcome/PromotionSentiment to Modix.Models project
calledude Sep 1, 2024
73bb395
Move DesignatedRoleType to Modix.Models project
calledude Sep 1, 2024
a5b33aa
Move InfractionType to Modix.Models project
calledude Sep 1, 2024
d852a9e
Move PromotionCampaignEntityExtensions to Modix.Models project
calledude Sep 1, 2024
882fb4b
Refactor table query models in Infractions/DeletedMessages
calledude Sep 1, 2024
ceaae6a
Dispose HttpResponseMessage
calledude Sep 1, 2024
83ce852
Stop using CascadingAuthenticationState (components will use Authenti…
calledude Sep 1, 2024
9c421fd
Fix paging in DeletedMessages/Infractions
calledude Sep 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Reintroduce ModixController to get rid of redundant code, also fixes …
…internal authentication towards the AuthenticationService
  • Loading branch information
calledude committed Nov 15, 2024
commit f4028fcbe1d10ef88a3be1986900d5affded16a6
33 changes: 7 additions & 26 deletions src/Modix.Web/Controllers/AutocompleteController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,39 @@
using Discord.WebSocket;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modix.Controllers;
using Modix.Data.Utilities;
using Modix.Services.Core;
using Modix.Services.Utilities;
using Modix.Web.Models;
using Modix.Web.Shared.Models.Common;

namespace Modix.Web.Controllers;

[Route("~/api/autocomplete")]
[ApiController]
[Authorize]
public class AutocompleteController : ControllerBase
public class AutocompleteController : ModixController
{
private readonly DiscordSocketClient _discordSocketClient;
private readonly IUserService _userService;

public AutocompleteController(DiscordSocketClient discordSocketClient, IUserService userService)
public AutocompleteController(DiscordSocketClient discordSocketClient, IUserService userService, Modix.Services.Core.IAuthorizationService authorizationService)
: base(discordSocketClient, authorizationService)
{
_discordSocketClient = discordSocketClient;
_userService = userService;
}


[HttpGet("users/{query}")]
public async Task<IEnumerable<ModixUser>> AutocompleteUsers(string query)
public async Task<IEnumerable<ModixUser>> AutocompleteUsersAsync(string query)
{
// TODO: Move this to a base class like ModixController?
var guildCookie = Request.Cookies[CookieConstants.SelectedGuild];

SocketGuild guildToSearch;
if (!string.IsNullOrWhiteSpace(guildCookie))
{
var guildId = ulong.Parse(guildCookie);
guildToSearch = _discordSocketClient.GetGuild(guildId);
}
else
{
guildToSearch = _discordSocketClient.Guilds.First();
}

if (guildToSearch?.Users is null)
return [];

var result = guildToSearch.Users
var result = UserGuild.Users
.Where(d => d.Username.OrdinalContains(query) || d.Id.ToString() == query)
.Take(10)
.Select(FromIGuildUser);

if (!result.Any() && ulong.TryParse(query, out var userId))
{
var user = await _userService.GetUserInformationAsync(guildToSearch.Id, userId);
var user = await _userService.GetUserInformationAsync(UserGuild.Id, userId);

if (user is not null)
{
Expand All @@ -63,7 +45,6 @@ public async Task<IEnumerable<ModixUser>> AutocompleteUsers(string query)
return result;
}


public static ModixUser FromIGuildUser(IGuildUser user) => new()
{
Name = user.GetDisplayName(),
Expand Down
34 changes: 9 additions & 25 deletions src/Modix.Web/Controllers/GuildStatsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Discord.WebSocket;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modix.Controllers;
using Modix.Services.Core;
using Modix.Services.GuildStats;
using Modix.Web.Models;
using Modix.Web.Shared.Models.Stats;
Expand All @@ -11,43 +13,25 @@ namespace Modix.Web.Controllers;
[Route("~/api")]
[ApiController]
[Authorize]
public class GuildStatsController : ControllerBase
public class GuildStatsController : ModixController
{
private readonly IGuildStatService _guildStatService;
private readonly DiscordSocketClient _discordSocketClient;

public GuildStatsController(IGuildStatService guildStatService, DiscordSocketClient discordSocketClient)
public GuildStatsController(IGuildStatService guildStatService, DiscordSocketClient discordSocketClient, Modix.Services.Core.IAuthorizationService authorizationService)
: base(discordSocketClient, authorizationService)
{
_guildStatService = guildStatService;
_discordSocketClient = discordSocketClient;
}

[HttpGet("guildstats")]
public async Task<GuildStatData> GuildStats()
public async Task<GuildStatData> GuildStatsAsync()
{
// TODO: Move this to a base class like ModixController?
var guildCookie = Request.Cookies[CookieConstants.SelectedGuild];

SocketGuild guildToSearch;
if (!string.IsNullOrWhiteSpace(guildCookie))
{
var guildId = ulong.Parse(guildCookie);
guildToSearch = _discordSocketClient.GetGuild(guildId);
}
else
{
guildToSearch = _discordSocketClient.Guilds.First();
}


var userId = ulong.Parse(User.FindFirst(d => d.Type == ClaimTypes.NameIdentifier)?.Value);

var roleCounts = await _guildStatService.GetGuildMemberDistributionAsync(guildToSearch);
var messageCounts = await _guildStatService.GetTopMessageCounts(guildToSearch, userId);
var roleCounts = await _guildStatService.GetGuildMemberDistributionAsync(UserGuild);
var messageCounts = await _guildStatService.GetTopMessageCounts(UserGuild, SocketUser.Id);

var guildRoleCounts = roleCounts.Select(x => new GuildRoleMemberCount(x.Name, x.Count, x.Color));
var topUserMessageCounts = messageCounts.Select(x => new PerUserMessageCount(x.Username, x.Discriminator, x.Rank, x.MessageCount, x.IsCurrentUser));

return new GuildStatData(guildToSearch.Name, [..guildRoleCounts], [..topUserMessageCounts]);
return new GuildStatData(UserGuild.Name, [..guildRoleCounts], [..topUserMessageCounts]);
}
}
71 changes: 71 additions & 0 deletions src/Modix.Web/Controllers/ModixController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#nullable enable
using System.Security.Claims;
using Discord.WebSocket;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Modix.Web.Models;

namespace Modix.Controllers;

[Authorize]
public class ModixController : Controller
{
protected DiscordSocketClient DiscordSocketClient { get; private set; }
protected SocketGuildUser SocketUser { get; private set; }
protected SocketGuild UserGuild => SocketUser.Guild;

protected Services.Core.IAuthorizationService ModixAuth { get; private set; }

public ModixController(DiscordSocketClient client, Services.Core.IAuthorizationService modixAuth)
{
DiscordSocketClient = client;
ModixAuth = modixAuth;
}


public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
if (!DiscordSocketClient.Guilds.Any())
return;

if (User is null)
{
await HttpContext.ChallengeAsync();
return;
}

var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (!ulong.TryParse(userId, out var userSnowflake))
{
await HttpContext.ChallengeAsync();
return;
}

var guildCookie = Request.Cookies[CookieConstants.SelectedGuild];
SocketGuild guildToSearch;

if (!string.IsNullOrWhiteSpace(guildCookie))
{
var guildId = ulong.Parse(guildCookie);
guildToSearch = DiscordSocketClient.GetGuild(guildId);
}
else
{
guildToSearch = DiscordSocketClient.Guilds.First();
}

SocketUser = guildToSearch.GetUser(userSnowflake);

if (SocketUser is null)
{
await HttpContext.ChallengeAsync();
return;
}

await ModixAuth.OnAuthenticatedAsync(SocketUser.Id, SocketUser.Guild.Id, [.. SocketUser.Roles.Select(x => x.Id) ]);

await next();
}
}
27 changes: 5 additions & 22 deletions src/Modix.Web/Controllers/RolesController.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,25 @@
using Discord.WebSocket;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modix.Web.Models;
using Modix.Controllers;
using Modix.Web.Shared.Models.Common;

namespace Modix.Web.Controllers;

[Route("~/api/roles")]
[ApiController]
[Authorize]
public class RolesController : ControllerBase
public class RolesController : ModixController
{
private readonly DiscordSocketClient _discordSocketClient;

public RolesController(DiscordSocketClient discordSocketClient)
public RolesController(DiscordSocketClient discordSocketClient, Modix.Services.Core.IAuthorizationService authorizationService)
: base(discordSocketClient, authorizationService)
{
_discordSocketClient = discordSocketClient;
}

[HttpGet]
public async Task<Dictionary<ulong, RoleInformation>> GetRoles()
{
// TODO: Move this to a base class like ModixController?

var guildCookie = Request.Cookies[CookieConstants.SelectedGuild];

SocketGuild guildToSearch;
if (!string.IsNullOrWhiteSpace(guildCookie))
{
var guildId = ulong.Parse(guildCookie);
guildToSearch = _discordSocketClient.GetGuild(guildId);
}
else
{
guildToSearch = _discordSocketClient.Guilds.First();
}

return guildToSearch.Roles
return UserGuild.Roles
.Select(x => new RoleInformation(x.Id, x.Name, x.Color.ToString()))
.ToDictionary(x => x.Id);
}
Expand Down
48 changes: 7 additions & 41 deletions src/Modix.Web/Controllers/TagsController.cs
Original file line number Diff line number Diff line change
@@ -1,51 +1,34 @@
using System.Diagnostics;
using System.Security.Claims;
using Discord.WebSocket;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modix.Controllers;
using Modix.Data.Models.Core;
using Modix.Data.Models.Tags;
using Modix.Services.Tags;
using Modix.Web.Models;
using Modix.Web.Shared.Models.Tags;

namespace Modix.Web.Controllers;

[Route("~/api/tags")]
[ApiController]
[Authorize]
public class TagsController : ControllerBase
public class TagsController : ModixController
{
private readonly ITagService _tagService;
private readonly DiscordSocketClient _discordSocketClient;

public TagsController(ITagService tagService, DiscordSocketClient discordSocketClient)
public TagsController(ITagService tagService, DiscordSocketClient discordSocketClient, Modix.Services.Core.IAuthorizationService authorizationService)
: base(discordSocketClient, authorizationService)
{
_tagService = tagService;
_discordSocketClient = discordSocketClient;
}

[HttpGet]
public async Task<IEnumerable<TagData>> GetTagsAsync()
{
// TODO: Move this to a base class like ModixController?

var guildCookie = Request.Cookies[CookieConstants.SelectedGuild];

SocketGuild guildToSearch;
if (!string.IsNullOrWhiteSpace(guildCookie))
{
var guildId = ulong.Parse(guildCookie);
guildToSearch = _discordSocketClient.GetGuild(guildId);
}
else
{
guildToSearch = _discordSocketClient.Guilds.First();
}

var summaries = await _tagService.GetSummariesAsync(new TagSearchCriteria
{
GuildId = guildToSearch.Id,
GuildId = UserGuild.Id,
});

return summaries.Select(CreateFromSummary);
Expand All @@ -57,25 +40,8 @@ public async Task<IActionResult> CreateTagAsync([FromBody] TagCreationData tagCr
{
try
{
// TODO: Move this to a base class like ModixController?

var guildCookie = Request.Cookies[CookieConstants.SelectedGuild];

SocketGuild guildToSearch;
if (!string.IsNullOrWhiteSpace(guildCookie))
{
var guildId = ulong.Parse(guildCookie);
guildToSearch = _discordSocketClient.GetGuild(guildId);
}
else
{
guildToSearch = _discordSocketClient.Guilds.First();
}

var userId = ulong.Parse(User.FindFirst(ClaimTypes.NameIdentifier)?.Value);

await _tagService.CreateTagAsync(guildToSearch.Id, userId, tagCreationData.Name, tagCreationData.Content);
var createdTag = await _tagService.GetTagAsync(guildToSearch.Id, tagCreationData.Name);
await _tagService.CreateTagAsync(UserGuild.Id, SocketUser.Id, tagCreationData.Name, tagCreationData.Content);
var createdTag = await _tagService.GetTagAsync(UserGuild.Id, tagCreationData.Name);

var tagSummary = CreateFromSummary(createdTag);

Expand Down
Loading