feat(dashboard): add tos/privacy/about pages, add delete all data page + endpoint
This commit is contained in:
parent
ac54b78a13
commit
31b6ac2cac
21 changed files with 527 additions and 28 deletions
110
Catalogger.Backend/Api/GuildsController.Remove.cs
Normal file
110
Catalogger.Backend/Api/GuildsController.Remove.cs
Normal 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);
|
||||
}
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,35 @@
|
|||
[Logging]
|
||||
# The minimum level that will be logged. Possible options are: Debug, Information, Warning, Error, Fatal
|
||||
LogEventLevel = Debug
|
||||
# Whether to log SQL queries. This is extremely verbose.
|
||||
LogQueries = false
|
||||
# URL for a Seq instance to log to. Generally not needed if you're self-hosting.
|
||||
SeqLogUrl = http://localhost:5341
|
||||
# Whether to enable Prometheus metrics. If disabled, Catalogger will update metrics manually every so often.
|
||||
EnableMetrics = false
|
||||
|
||||
[Database]
|
||||
Url = Host=localhost;Database=postgres;Username=postgres;Password=postgres
|
||||
# URL for a Redis-compatible database. Not required; Catalogger will fall back to in-memory caching if it's not set.
|
||||
Redis = localhost:6379
|
||||
EncryptionKey = changeMe!FNmZbotJnAAJ7grWHDluCoKIwj6NcUagKE= # base64 key
|
||||
# The key used to encrypt message data. This should be base64;
|
||||
# you can use `openssl rand -base64 32` on the command line to generate a key.
|
||||
EncryptionKey = changeMe!FNmZbotJnAAJ7grWHDluCoKIwj6NcUagKE=
|
||||
|
||||
[Discord]
|
||||
ApplicationId = <applicationIdHere>
|
||||
Token = <discordTokenHere>
|
||||
CommandsGuildId = <testGuildIdHere>
|
||||
SyncCommands = true
|
||||
# If you only want to sync commands with a single server, uncomment the line below and put its ID.
|
||||
# CommandsGuildId = <testGuildIdHere>
|
||||
# If disabled, commands will not be synced with Discord. This generally shouldn't be turned off.
|
||||
SyncCommands = true
|
||||
# The channel where guild join/leave logs are sent. The bot needs to have permission to manage webhooks in this channel.
|
||||
GuildLogId = 803961980758261800
|
||||
# This is not necessary when not using the dashboard.
|
||||
# ClientSecret = <secret>
|
||||
|
||||
# Dashboard related settings. You shouldn't need to change these.
|
||||
[Web]
|
||||
Host = localhost
|
||||
Port = 5005
|
||||
BaseUrl = https://catalogger.localhost
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue