feat: import/export settings, send backup of settings when leaving guild

This commit is contained in:
sam 2024-11-08 17:12:00 +01:00
parent e6d68338db
commit db5d7bb4f8
Signed by: sam
GPG key ID: 5F3C3C1B3166639D
18 changed files with 392 additions and 39 deletions

View file

@ -0,0 +1,117 @@
// 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.Database.Models;
using Microsoft.AspNetCore.Mvc;
using NodaTime;
using Remora.Discord.API;
namespace Catalogger.Backend.Api;
public partial class GuildsController
{
[Authorize]
[HttpGet("config")]
public async Task<IActionResult> ExportConfigAsync(string id)
{
var (guildId, _) = await ParseGuildAsync(id);
var guildConfig = await guildRepository.GetAsync(guildId);
return Ok(await ToExport(guildConfig));
}
[Authorize]
[HttpPost("config")]
public async Task<IActionResult> ImportConfigAsync(
string id,
[FromBody] GuildConfigExport export
)
{
var (guildId, _) = await ParseGuildAsync(id);
if (export.Id != guildId.Value)
throw new ApiError(
HttpStatusCode.BadRequest,
ErrorCode.BadRequest,
"This backup is not from this server."
);
// Filter invites to *only* those that exist for this guild.
// Blame past me for not making (code, guild_id) a unique index >:|
var cachedInvites = (await inviteCache.TryGetAsync(guildId)).ToList();
var invites = export.Invites.Where(i => cachedInvites.Any(ci => i.Code == ci.Code));
await guildRepository.ImportConfigAsync(
guildId.Value,
export.Channels,
export.BannedSystems,
export.KeyRoles
);
await inviteRepository.ImportInvitesAsync(
guildId,
invites.Select(i => new Invite
{
Code = i.Code,
Name = i.Name,
GuildId = guildId.Value,
})
);
await watchlistRepository.ImportWatchlistAsync(
guildId,
export.Watchlist.Select(w => new Watchlist
{
GuildId = guildId.Value,
UserId = w.UserId,
ModeratorId = w.ModeratorId,
AddedAt = w.AddedAt,
Reason = w.Reason,
})
);
return NoContent();
}
private async Task<GuildConfigExport> ToExport(Database.Models.Guild config)
{
var id = DiscordSnowflake.New(config.Id);
var invites = await inviteRepository.GetGuildInvitesAsync(id);
var watchlist = await watchlistRepository.GetGuildWatchlistAsync(id);
return new GuildConfigExport(
config.Id,
config.Channels,
config.BannedSystems,
config.KeyRoles,
invites.Select(i => new InviteExport(i.Code, i.Name)),
watchlist.Select(w => new WatchlistExport(w.UserId, w.AddedAt, w.ModeratorId, w.Reason))
);
}
public record GuildConfigExport(
ulong Id,
Database.Models.Guild.ChannelConfig Channels,
string[] BannedSystems,
ulong[] KeyRoles,
IEnumerable<InviteExport> Invites,
IEnumerable<WatchlistExport> Watchlist
);
public record InviteExport(string Code, string Name);
public record WatchlistExport(ulong UserId, Instant AddedAt, ulong ModeratorId, string Reason);
}

View file

@ -14,12 +14,15 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
using System.Net;
using System.Text;
using System.Text.Json;
using Catalogger.Backend.Api.Middleware;
using Catalogger.Backend.Bot;
using Catalogger.Backend.Extensions;
using Catalogger.Backend.Services;
using Dapper;
using Microsoft.AspNetCore.Mvc;
using Remora.Discord.API.Abstractions.Rest;
using Remora.Discord.Extensions.Embeds;
namespace Catalogger.Backend.Api;
@ -40,6 +43,8 @@ public partial class GuildsController
}
var guildConfig = await guildRepository.GetAsync(guildId);
var export = await ToExport(guildConfig);
var logChannelId =
webhookExecutor.GetLogChannel(guildConfig, LogChannelType.GuildUpdate)
?? webhookExecutor.GetLogChannel(guildConfig, LogChannelType.GuildMemberRemove);
@ -50,15 +55,25 @@ public partial class GuildsController
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."
$"""
A moderator in this server ({currentUser.Tag}, <@{currentUser.Id}>) requested that Catalogger leave.
All data related to this server will be deleted.
A backup of this server's configuration is attached to this message,
in case you want to use the bot again later.
"""
)
.WithColour(DiscordUtils.Red)
.WithCurrentTimestamp()
.Build()
.GetOrThrow();
await webhookExecutor.SendLogAsync(logChannelId.Value, [embed], []);
var exportData = JsonSerializer.Serialize(export, JsonUtils.ApiJsonOptions);
var file = new FileData(
"config-backup.json",
new MemoryStream(Encoding.UTF8.GetBytes(exportData))
);
await webhookExecutor.SendLogAsync(logChannelId.Value, [embed], [file]);
}
else
{
@ -125,7 +140,7 @@ public partial class GuildsController
_logger.Information("Left guild {GuildId} and removed all data for it", guildId);
return NoContent();
return Ok(export);
}
public record LeaveGuildRequest(string Name);

View file

@ -34,6 +34,8 @@ public partial class GuildsController(
ILogger logger,
DatabaseConnection dbConn,
GuildRepository guildRepository,
InviteRepository inviteRepository,
WatchlistRepository watchlistRepository,
GuildCache guildCache,
EmojiCache emojiCache,
ChannelCache channelCache,