feat: store system UUIDs of banned users per guild
This commit is contained in:
parent
5ac607fd0a
commit
5f24a6aa88
9 changed files with 121 additions and 18 deletions
|
|
@ -0,0 +1 @@
|
|||
drop table pluralkit_systems;
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
create table pluralkit_systems (
|
||||
system_id uuid not null,
|
||||
user_id bigint not null,
|
||||
guild_id bigint not null,
|
||||
|
||||
primary key (system_id, user_id, guild_id)
|
||||
);
|
||||
|
||||
create index ix_pluralkit_systems_user_guild on pluralkit_systems (user_id, guild_id);
|
||||
|
|
@ -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",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue