2024-10-27 23:02:42 +01:00
|
|
|
// 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 Catalogger.Backend.Database.Models;
|
|
|
|
|
using Dapper;
|
2024-11-05 15:32:53 +01:00
|
|
|
using Remora.Discord.API;
|
2024-10-27 23:02:42 +01:00
|
|
|
using Remora.Rest.Core;
|
|
|
|
|
|
2024-10-28 14:04:55 +01:00
|
|
|
namespace Catalogger.Backend.Database.Repositories;
|
2024-10-27 23:02:42 +01:00
|
|
|
|
|
|
|
|
public class GuildRepository(ILogger logger, DatabaseConnection conn)
|
|
|
|
|
: IDisposable,
|
|
|
|
|
IAsyncDisposable
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger = logger.ForContext<GuildRepository>();
|
|
|
|
|
|
|
|
|
|
public async Task<Guild> GetAsync(Optional<Snowflake> id) => await GetAsync(id.Value.Value);
|
|
|
|
|
|
|
|
|
|
public async Task<Guild> GetAsync(Snowflake id) => await GetAsync(id.Value);
|
|
|
|
|
|
|
|
|
|
public async Task<Guild> GetAsync(ulong id)
|
|
|
|
|
{
|
2024-10-29 20:19:49 +01:00
|
|
|
_logger.Verbose("Getting guild config for {GuildId}", id);
|
2024-10-27 23:02:42 +01:00
|
|
|
|
|
|
|
|
var guild = await conn.QueryFirstOrDefaultAsync<Guild>(
|
|
|
|
|
"select * from guilds where id = @Id",
|
|
|
|
|
new { Id = id }
|
|
|
|
|
);
|
|
|
|
|
if (guild == null)
|
|
|
|
|
throw new CataloggerError("Guild not found, was not initialized during guild create");
|
|
|
|
|
return guild;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> IsGuildKnown(ulong id) =>
|
|
|
|
|
await conn.ExecuteScalarAsync<bool>(
|
|
|
|
|
"select exists(select id from guilds where id = @Id)",
|
|
|
|
|
new { Id = id }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
public async Task AddGuildAsync(ulong id) =>
|
|
|
|
|
await conn.ExecuteAsync(
|
|
|
|
|
"""
|
2024-10-28 14:57:05 +01:00
|
|
|
insert into guilds (id, key_roles, banned_systems, channels)
|
2024-10-29 20:19:49 +01:00
|
|
|
values (@Id, array[]::bigint[], array[]::text[], @Channels::jsonb)
|
2024-10-27 23:02:42 +01:00
|
|
|
on conflict do nothing
|
|
|
|
|
""",
|
|
|
|
|
new { Id = id, Channels = new Guild.ChannelConfig() }
|
|
|
|
|
);
|
|
|
|
|
|
2024-11-05 15:32:53 +01:00
|
|
|
public async Task BanSystemAsync(Snowflake guildId, Snowflake userId, string hid, Guid uuid)
|
|
|
|
|
{
|
2024-10-27 23:02:42 +01:00
|
|
|
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()] }
|
|
|
|
|
);
|
|
|
|
|
|
2024-11-05 15:32:53 +01:00
|
|
|
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)
|
|
|
|
|
{
|
2024-10-27 23:02:42 +01:00
|
|
|
await conn.ExecuteAsync(
|
|
|
|
|
"update guilds set banned_systems = array_remove(array_remove(banned_systems, @Hid), @Uuid) where id = @Id",
|
|
|
|
|
new
|
|
|
|
|
{
|
|
|
|
|
GuildId = guildId.Value,
|
|
|
|
|
Hid = hid,
|
|
|
|
|
Uuid = uuid.ToString(),
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2024-11-05 15:32:53 +01:00
|
|
|
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);
|
|
|
|
|
|
2024-11-18 20:47:58 +01:00
|
|
|
public async Task UpdateConfigAsync(Snowflake id, Guild config) =>
|
2024-10-28 02:14:41 +01:00
|
|
|
await conn.ExecuteAsync(
|
2024-11-18 20:47:58 +01:00
|
|
|
"""
|
|
|
|
|
update guilds set channels = @Channels::jsonb,
|
|
|
|
|
messages = @Messages::jsonb,
|
|
|
|
|
ignored_channels = @IgnoredChannels,
|
2024-11-18 21:01:52 +01:00
|
|
|
ignored_roles = @IgnoredRoles,
|
|
|
|
|
key_roles = @KeyRoles
|
2024-11-18 20:47:58 +01:00
|
|
|
where id = @Id
|
|
|
|
|
""",
|
2024-11-18 00:47:27 +01:00
|
|
|
new
|
|
|
|
|
{
|
|
|
|
|
Id = id.Value,
|
|
|
|
|
config.Channels,
|
|
|
|
|
config.Messages,
|
2024-11-18 20:47:58 +01:00
|
|
|
config.IgnoredChannels,
|
|
|
|
|
config.IgnoredRoles,
|
2024-11-18 21:01:52 +01:00
|
|
|
config.KeyRoles,
|
2024-11-18 00:47:27 +01:00
|
|
|
}
|
2024-10-28 02:14:41 +01:00
|
|
|
);
|
|
|
|
|
|
2024-11-08 17:12:00 +01:00
|
|
|
public async Task ImportConfigAsync(
|
|
|
|
|
ulong id,
|
|
|
|
|
Guild.ChannelConfig channels,
|
2024-11-18 00:47:27 +01:00
|
|
|
Guild.MessageConfig messages,
|
2024-11-08 17:12:00 +01:00
|
|
|
string[] bannedSystems,
|
2024-11-18 21:01:52 +01:00
|
|
|
List<ulong> keyRoles
|
2024-11-08 17:12:00 +01:00
|
|
|
) =>
|
|
|
|
|
await conn.ExecuteAsync(
|
2024-11-18 00:47:27 +01:00
|
|
|
"update guilds set channels = @channels::jsonb, messages = @messages::jsonb, banned_systems = @bannedSystems, key_roles = @keyRoles where id = @id",
|
2024-11-08 17:12:00 +01:00
|
|
|
new
|
|
|
|
|
{
|
|
|
|
|
id,
|
|
|
|
|
channels,
|
2024-11-18 00:47:27 +01:00
|
|
|
messages,
|
2024-11-08 17:12:00 +01:00
|
|
|
bannedSystems,
|
|
|
|
|
keyRoles,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2024-10-27 23:02:42 +01:00
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
conn.Dispose();
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async ValueTask DisposeAsync()
|
|
|
|
|
{
|
|
|
|
|
await conn.DisposeAsync();
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
}
|