diff --git a/Catalogger.Backend/Api/GuildsController.Ignores.cs b/Catalogger.Backend/Api/GuildsController.Ignores.cs index ea9e647..f9cc106 100644 --- a/Catalogger.Backend/Api/GuildsController.Ignores.cs +++ b/Catalogger.Backend/Api/GuildsController.Ignores.cs @@ -13,6 +13,8 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +using System.Net; +using Catalogger.Backend.Api.Middleware; using Microsoft.AspNetCore.Mvc; using Remora.Discord.API; using Remora.Discord.API.Abstractions.Objects; @@ -104,4 +106,41 @@ public partial class GuildsController return NoContent(); } + + [HttpPut("key-roles/{roleId}")] + public async Task AddKeyRoleAsync(string id, ulong roleId) + { + var (guildId, _) = await ParseGuildAsync(id); + var guildConfig = await guildRepository.GetAsync(guildId); + + if (roleCache.GuildRoles(guildId).All(r => r.ID.Value != roleId)) + throw new ApiError(HttpStatusCode.BadRequest, ErrorCode.BadRequest, "Role not found"); + + if (guildConfig.KeyRoles.Contains(roleId)) + throw new ApiError( + HttpStatusCode.BadRequest, + ErrorCode.BadRequest, + "Role is already a key role" + ); + + await guildRepository.AddKeyRoleAsync(guildId, DiscordSnowflake.New(roleId)); + return NoContent(); + } + + [HttpDelete("key-roles/{roleId}")] + public async Task RemoveKeyRoleAsync(string id, ulong roleId) + { + var (guildId, _) = await ParseGuildAsync(id); + var guildConfig = await guildRepository.GetAsync(guildId); + + if (!guildConfig.KeyRoles.Contains(roleId)) + throw new ApiError( + HttpStatusCode.BadRequest, + ErrorCode.BadRequest, + "Role is already not a key role" + ); + + await guildRepository.RemoveKeyRoleAsync(guildId, DiscordSnowflake.New(roleId)); + return NoContent(); + } } diff --git a/Catalogger.Backend/Api/GuildsController.cs b/Catalogger.Backend/Api/GuildsController.cs index 560456f..e1a52b0 100644 --- a/Catalogger.Backend/Api/GuildsController.cs +++ b/Catalogger.Backend/Api/GuildsController.cs @@ -19,6 +19,7 @@ using Catalogger.Backend.Cache; using Catalogger.Backend.Cache.InMemoryCache; using Catalogger.Backend.Database; using Catalogger.Backend.Database.Repositories; +using Catalogger.Backend.Extensions; using Catalogger.Backend.Services; using Microsoft.AspNetCore.Mvc; using Remora.Discord.API; @@ -91,6 +92,16 @@ public partial class GuildsController( .Select(ToChannel) )); + var roles = roleCache + .GuildRoles(guildId) + .OrderByDescending(r => r.Position) + .Select(r => new GuildRole( + r.ID.ToString(), + r.Name, + r.Position, + r.Colour.ToPrettyString() + )); + return Ok( new GuildResponse( guild.Id, @@ -98,7 +109,9 @@ public partial class GuildsController( guild.IconUrl, categories, channelsWithoutCategories, - guildConfig.Channels + roles, + guildConfig.Channels, + guildConfig.KeyRoles ) ); } @@ -122,13 +135,17 @@ public partial class GuildsController( string IconUrl, IEnumerable Categories, IEnumerable ChannelsWithoutCategory, - Database.Models.Guild.ChannelConfig Config + IEnumerable Roles, + Database.Models.Guild.ChannelConfig Config, + ulong[] KeyRoles ); private record GuildCategory(string Id, string Name, IEnumerable Channels); private record GuildChannel(string Id, string Name, bool CanLogTo, bool CanRedirectFrom); + private record GuildRole(string Id, string Name, int Position, string Colour); + [Authorize] [HttpPatch] [ProducesResponseType(statusCode: StatusCodes.Status200OK)] diff --git a/Catalogger.Frontend/src/lib/api.ts b/Catalogger.Frontend/src/lib/api.ts index 9db5318..a52aa7c 100644 --- a/Catalogger.Frontend/src/lib/api.ts +++ b/Catalogger.Frontend/src/lib/api.ts @@ -77,7 +77,9 @@ export type FullGuild = { icon_url: string; categories: GuildCategory[]; channels_without_category: GuildChannel[]; + roles: GuildRole[]; config: GuildConfig; + key_roles: string[]; }; export type GuildCategory = { @@ -93,6 +95,13 @@ export type GuildChannel = { can_redirect_from: boolean; }; +export type GuildRole = { + id: string; + name: string; + position: string; + colour: string; +}; + export type CurrentUser = { user: User; guilds: PartialGuild[]; diff --git a/Catalogger.Frontend/src/routes/dash/[guildId]/key-roles/+page.svelte b/Catalogger.Frontend/src/routes/dash/[guildId]/key-roles/+page.svelte index 6f940a6..dea7dfa 100644 --- a/Catalogger.Frontend/src/routes/dash/[guildId]/key-roles/+page.svelte +++ b/Catalogger.Frontend/src/routes/dash/[guildId]/key-roles/+page.svelte @@ -1,7 +1,108 @@

Key roles

-

This page is still under construction!

+

+ Key roles are logged separately from other roles, and also log who added + or removed the role. Useful for moderator roles. +

+ +
+ + +
+ +
+ +
+ +
+

Current key roles

+ + + {#each keyRoles as r (r.id)} + + + + {r.name} + + + + {/each} + +