From 1c43beb82f0a6c872f614d7ae5a13913d09c08aa Mon Sep 17 00:00:00 2001 From: sam Date: Sat, 19 Oct 2024 23:27:57 +0200 Subject: [PATCH] attempt to add ignored channels page --- Catalogger.Backend/Api/AuthController.cs | 11 +++ Catalogger.Backend/Api/GuildsController.cs | 32 ++++++-- Catalogger.Backend/Database/Models/Guild.cs | 2 +- .../src/lib/components/Navbar.svelte | 33 ++++++-- Catalogger.Frontend/src/lib/util.ts | 77 +++++++++++++++++++ Catalogger.Frontend/src/routes/+layout.svelte | 2 +- .../src/routes/callback/+page.svelte | 9 ++- .../src/routes/dash/+page.svelte | 32 +++++--- .../src/routes/dash/[guildId]/+layout.svelte | 77 +++++++++---------- .../src/routes/dash/[guildId]/+page.svelte | 2 + .../dash/[guildId]/ChannelSelect.svelte | 6 +- .../[guildId]/ignored-channels/+page.svelte | 23 ++++++ .../dash/[guildId]/redirects/+page.svelte | 56 +------------- 13 files changed, 238 insertions(+), 124 deletions(-) create mode 100644 Catalogger.Frontend/src/lib/util.ts create mode 100644 Catalogger.Frontend/src/routes/dash/[guildId]/ignored-channels/+page.svelte diff --git a/Catalogger.Backend/Api/AuthController.cs b/Catalogger.Backend/Api/AuthController.cs index 9651bfa..153a611 100644 --- a/Catalogger.Backend/Api/AuthController.cs +++ b/Catalogger.Backend/Api/AuthController.cs @@ -49,6 +49,17 @@ public class AuthController( return Redirect(url); } + [HttpGet("add-guild/{id}")] + public IActionResult AddGuild(ulong id) + { + var url = + $"https://discord.com/oauth2/authorize?client_id={config.Discord.ApplicationId}" + + "&permissions=537250993&scope=bot+applications.commands" + + $"&guild_id={id}"; + + return Redirect(url); + } + [HttpPost("callback")] [ProducesResponseType(statusCode: StatusCodes.Status200OK)] public async Task CallbackAsync([FromBody] CallbackRequest req) diff --git a/Catalogger.Backend/Api/GuildsController.cs b/Catalogger.Backend/Api/GuildsController.cs index e34575d..a793f27 100644 --- a/Catalogger.Backend/Api/GuildsController.cs +++ b/Catalogger.Backend/Api/GuildsController.cs @@ -18,6 +18,7 @@ using Catalogger.Backend.Api.Middleware; using Catalogger.Backend.Cache.InMemoryCache; using Catalogger.Backend.Database; using Catalogger.Backend.Database.Queries; +using Catalogger.Backend.Database.Redis; using Microsoft.AspNetCore.Mvc; using Remora.Discord.API; using Remora.Discord.API.Abstractions.Objects; @@ -30,16 +31,10 @@ public partial class GuildsController( Config config, DatabaseContext db, ChannelCache channelCache, + RedisService redisService, DiscordRequestService discordRequestService ) : ApiControllerBase { - public IActionResult AddGuild(ulong id) => - Redirect( - $"https://discord.com/oauth2/authorize?client_id={config.Discord.ApplicationId}" - + "&permissions=537250993&scope=bot%20applications.commands" - + $"&guild_id={id}" - ); - private async Task<(Snowflake GuildId, Guild Guild)> ParseGuildAsync(string id) { var guilds = await discordRequestService.GetGuildsAsync(CurrentToken); @@ -135,6 +130,28 @@ public partial class GuildsController( .ToList(); var guildConfig = await db.GetGuildAsync(guildId); + if (req.IgnoredChannels != null) + { + var categories = channelCache + .GuildChannels(guildId) + .Where(c => c.Type is ChannelType.GuildCategory) + .ToList(); + + if ( + req.IgnoredChannels.Any(cId => + guildChannels.All(c => c.ID.Value != cId) + && categories.All(c => c.ID.Value != cId) + ) + ) + throw new ApiError( + HttpStatusCode.BadRequest, + ErrorCode.BadRequest, + "One or more ignored channels are unknown" + ); + + guildConfig.Channels.IgnoredChannels = req.IgnoredChannels.ToList(); + } + // i love repeating myself wheeeeee if ( req.GuildUpdate == null @@ -295,6 +312,7 @@ public partial class GuildsController( } public record ChannelRequest( + ulong[]? IgnoredChannels = null, ulong? GuildUpdate = null, ulong? GuildEmojisUpdate = null, ulong? GuildRoleCreate = null, diff --git a/Catalogger.Backend/Database/Models/Guild.cs b/Catalogger.Backend/Database/Models/Guild.cs index bb91cdc..4ed7836 100644 --- a/Catalogger.Backend/Database/Models/Guild.cs +++ b/Catalogger.Backend/Database/Models/Guild.cs @@ -58,7 +58,7 @@ public class Guild public class ChannelConfig { - public List IgnoredChannels { get; init; } = []; + public List IgnoredChannels { get; set; } = []; public List IgnoredUsers { get; init; } = []; public Dictionary> IgnoredUsersPerChannel { get; init; } = []; public Dictionary Redirects { get; init; } = []; diff --git a/Catalogger.Frontend/src/lib/components/Navbar.svelte b/Catalogger.Frontend/src/lib/components/Navbar.svelte index af4dcfe..4d786cd 100644 --- a/Catalogger.Frontend/src/lib/components/Navbar.svelte +++ b/Catalogger.Frontend/src/lib/components/Navbar.svelte @@ -1,9 +1,9 @@ +

Log channels

+
diff --git a/Catalogger.Frontend/src/routes/dash/[guildId]/ChannelSelect.svelte b/Catalogger.Frontend/src/routes/dash/[guildId]/ChannelSelect.svelte index 2d2ea4f..3df4a92 100644 --- a/Catalogger.Frontend/src/routes/dash/[guildId]/ChannelSelect.svelte +++ b/Catalogger.Frontend/src/routes/dash/[guildId]/ChannelSelect.svelte @@ -5,14 +5,18 @@ type Option = { label: string; value: string }; export let options: Array; + export let multiple = false; + export let max: number | undefined = undefined; export let placeholder: string = "Select a channel"; - export let value: string | null; + export let value: string | string[] | null; + import { Label } from "@sveltestrap/sveltestrap"; + import type { PageData } from "./$types"; + import { makeFullOptions } from "$lib/util"; + import ChannelSelect from "../ChannelSelect.svelte"; + + export let data: PageData; + + $: ignored = data.guild.config.ignored_channels; + $: options = makeFullOptions(data.guild, ignored); + + +

Ignored channels

+ +

+ Messages from ignored channels will not be logged. Note that this does not + ignore channel update events, any changes to the channel will still be logged. +

+ +
+ + +
diff --git a/Catalogger.Frontend/src/routes/dash/[guildId]/redirects/+page.svelte b/Catalogger.Frontend/src/routes/dash/[guildId]/redirects/+page.svelte index cfc072c..bfff7cd 100644 --- a/Catalogger.Frontend/src/routes/dash/[guildId]/redirects/+page.svelte +++ b/Catalogger.Frontend/src/routes/dash/[guildId]/redirects/+page.svelte @@ -1,5 +1,5 @@