feat: store system UUIDs of banned users per guild

This commit is contained in:
sam 2024-11-05 15:32:53 +01:00
parent 5ac607fd0a
commit 5f24a6aa88
Signed by: sam
GPG key ID: 5F3C3C1B3166639D
9 changed files with 121 additions and 18 deletions

View file

@ -15,6 +15,8 @@
using Catalogger.Backend.Database.Models;
using Dapper;
using Npgsql.Replication;
using Remora.Discord.API;
using Remora.Rest.Core;
namespace Catalogger.Backend.Database.Repositories;
@ -58,13 +60,30 @@ public class GuildRepository(ILogger logger, DatabaseConnection conn)
new { Id = id, Channels = new Guild.ChannelConfig() }
);
public async Task BanSystemAsync(Snowflake guildId, string hid, Guid uuid) =>
public async Task BanSystemAsync(Snowflake guildId, Snowflake userId, string hid, Guid uuid)
{
await conn.ExecuteAsync(
"update guilds set banned_systems = array_cat(banned_systems, @SystemIds) where id = @GuildId",
new { GuildId = guildId.Value, SystemIds = (string[])[hid, uuid.ToString()] }
);
public async Task UnbanSystemAsync(Snowflake guildId, string hid, Guid uuid) =>
await conn.ExecuteAsync(
"""
insert into pluralkit_systems (system_id, user_id, guild_id)
values (@SystemId, @UserId, @GuildId)
on conflict (system_id, user_id, guild_id) do nothing
""",
new
{
SystemId = uuid,
UserId = userId.Value,
GuildId = guildId.Value,
}
);
}
public async Task UnbanSystemAsync(Snowflake guildId, Snowflake userId, string hid, Guid uuid)
{
await conn.ExecuteAsync(
"update guilds set banned_systems = array_remove(array_remove(banned_systems, @Hid), @Uuid) where id = @Id",
new
@ -75,6 +94,32 @@ public class GuildRepository(ILogger logger, DatabaseConnection conn)
}
);
await conn.ExecuteAsync(
"""
delete from pluralkit_systems where system_id = @SystemId
and user_id = @UserId
and guild_id = @GuildId
""",
new
{
SystemId = uuid,
UserId = userId.Value,
GuildId = guildId.Value,
}
);
}
public async Task<Snowflake[]> GetSystemAccountsAsync(Snowflake guildId, Guid systemId)
{
var bannedAccounts = await conn.QueryAsync<BannedSystem>(
"select * from pluralkit_systems where system_id = @SystemId and guild_id = @GuildId",
new { SystemId = systemId, GuildId = guildId.Value }
);
return bannedAccounts.Select(s => DiscordSnowflake.New(s.UserId)).ToArray();
}
private record BannedSystem(Guid SystemId, ulong UserId, ulong GuildId);
public async Task AddKeyRoleAsync(Snowflake guildId, Snowflake roleId) =>
await conn.ExecuteAsync(
"update guilds set key_roles = array_append(key_roles, @RoleId) where id = @GuildId",