feat(dashboard): add tos/privacy/about pages, add delete all data page + endpoint

This commit is contained in:
sam 2024-10-24 15:53:27 +02:00
parent ac54b78a13
commit 31b6ac2cac
Signed by: sam
GPG key ID: 5F3C3C1B3166639D
21 changed files with 527 additions and 28 deletions

View file

@ -0,0 +1,110 @@
// Copyright (C) 2021-present sam (starshines.gay)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
using System.Net;
using Catalogger.Backend.Api.Middleware;
using Catalogger.Backend.Bot;
using Catalogger.Backend.Database.Queries;
using Catalogger.Backend.Extensions;
using Catalogger.Backend.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Remora.Discord.Extensions.Embeds;
namespace Catalogger.Backend.Api;
public partial class GuildsController
{
[HttpPost("leave")]
public async Task<IActionResult> LeaveGuildAsync(string id, [FromBody] LeaveGuildRequest req)
{
var (guildId, guild) = await ParseGuildAsync(id);
if (guild.Name != req.Name)
{
throw new ApiError(
HttpStatusCode.BadRequest,
ErrorCode.BadRequest,
"You must spell the server name correctly."
);
}
var guildConfig = await db.GetGuildAsync(guildId.Value);
var logChannelId =
webhookExecutor.GetLogChannel(guildConfig, LogChannelType.GuildUpdate)
?? webhookExecutor.GetLogChannel(guildConfig, LogChannelType.GuildMemberRemove);
if (logChannelId != null)
{
var currentUser = await discordRequestService.GetMeAsync(CurrentToken);
var embed = new EmbedBuilder()
.WithTitle("Catalogger is leaving this server")
.WithDescription(
$"A moderator in this server ({currentUser.Tag}, <@{currentUser.Id}>) requested that Catalogger leave. "
+ "All data related to this server will be deleted."
)
.WithColour(DiscordUtils.Red)
.WithCurrentTimestamp()
.Build()
.GetOrThrow();
await webhookExecutor.SendLogAsync(logChannelId.Value, [embed], []);
}
else
{
_logger.Information(
"Can't send guild {GuildId} a heads up when leaving as neither the guild update nor member remove events are logged",
guildId
);
}
var leaveRes = await userApi.LeaveGuildAsync(guildId);
if (!leaveRes.IsSuccess)
{
_logger.Error("Couldn't leave guild {GuildId}: {Error}", guildId, leaveRes.Error);
throw new ApiError(
HttpStatusCode.InternalServerError,
ErrorCode.InternalServerError,
"There was an error making Catalogger leave the server."
);
}
await using var tx = await db.Database.BeginTransactionAsync();
var inviteCount = await db
.Invites.Where(i => i.GuildId == guildId.Value)
.ExecuteDeleteAsync();
var watchlistCount = await db
.Watchlists.Where(w => w.GuildId == guildId.Value)
.ExecuteDeleteAsync();
var messageCount = await db
.Messages.Where(m => m.GuildId == guildId.Value)
.ExecuteDeleteAsync();
await db.Guilds.Where(g => g.Id == guildId.Value).ExecuteDeleteAsync();
await tx.CommitAsync();
_logger.Information(
"Deleted {InviteCount} invites, {WatchlistCount} watchlist entries, and {MessageCount} messages for guild {GuildId}",
inviteCount,
watchlistCount,
messageCount,
guildId
);
_logger.Information("Left guild {GuildId} and removed all data for it", guildId);
return NoContent();
}
public record LeaveGuildRequest(string Name);
}

View file

@ -20,9 +20,11 @@ using Catalogger.Backend.Cache.InMemoryCache;
using Catalogger.Backend.Database;
using Catalogger.Backend.Database.Queries;
using Catalogger.Backend.Database.Redis;
using Catalogger.Backend.Services;
using Microsoft.AspNetCore.Mvc;
using Remora.Discord.API;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.API.Abstractions.Rest;
using Remora.Rest.Core;
namespace Catalogger.Backend.Api;
@ -30,13 +32,17 @@ namespace Catalogger.Backend.Api;
[Route("/api/guilds/{id}")]
public partial class GuildsController(
Config config,
ILogger logger,
DatabaseContext db,
ChannelCache channelCache,
RedisService redisService,
IMemberCache memberCache,
DiscordRequestService discordRequestService
DiscordRequestService discordRequestService,
IDiscordRestUserAPI userApi,
WebhookExecutorService webhookExecutor
) : ApiControllerBase
{
private readonly ILogger _logger = logger.ForContext<GuildsController>();
private async Task<(Snowflake GuildId, Guild Guild)> ParseGuildAsync(string id)
{
var guilds = await discordRequestService.GetGuildsAsync(CurrentToken);

View file

@ -20,13 +20,25 @@ using Microsoft.AspNetCore.Mvc;
namespace Catalogger.Backend.Api;
[Route("/api")]
public class MetaController(GuildCache guildCache, DiscordRequestService discordRequestService)
: ApiControllerBase
public class MetaController(
Config config,
GuildCache guildCache,
DiscordRequestService discordRequestService
) : ApiControllerBase
{
[HttpGet("meta")]
public IActionResult GetMeta()
{
return Ok(new MetaResponse(Guilds: (int)CataloggerMetrics.GuildsCached.Value));
var inviteUrl =
$"https://discord.com/oauth2/authorize?client_id={config.Discord.ApplicationId}"
+ "&permissions=537250993&scope=bot+applications.commands";
return Ok(
new MetaResponse(
Guilds: (int)CataloggerMetrics.GuildsCached.Value,
InviteUrl: inviteUrl
)
);
}
[HttpGet("current-user")]
@ -48,7 +60,7 @@ public class MetaController(GuildCache guildCache, DiscordRequestService discord
);
}
private record MetaResponse(int Guilds);
private record MetaResponse(int Guilds, string InviteUrl);
private record CurrentUserResponse(ApiUser User, IEnumerable<ApiGuild> Guilds);
}