Extract system/member guild settings and refactor DB access
(also refactor MemberAvatar now that I'm here)
This commit is contained in:
19
PluralKit.Core/Models/MemberGuildSettings.cs
Normal file
19
PluralKit.Core/Models/MemberGuildSettings.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
#nullable enable
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
public class MemberGuildSettings
|
||||
{
|
||||
public int Member { get; }
|
||||
public ulong Guild { get; }
|
||||
public string? DisplayName { get; }
|
||||
public string? AvatarUrl { get; }
|
||||
|
||||
public MemberGuildSettings() { }
|
||||
|
||||
public MemberGuildSettings(int member, ulong guild)
|
||||
{
|
||||
Member = member;
|
||||
Guild = guild;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -8,7 +9,21 @@ namespace PluralKit.Core
|
||||
{
|
||||
public static class ModelQueryExt
|
||||
{
|
||||
public static Task<PKMember?> QueryMember(this IDbConnection conn, int id) =>
|
||||
conn.QueryFirstOrDefaultAsync<PKMember?>("select * from members where id = @id", new {id});
|
||||
|
||||
public static Task<GuildConfig> QueryOrInsertGuildConfig(this IDbConnection conn, ulong guild) =>
|
||||
conn.QueryFirstOrDefaultAsync<GuildConfig>("insert into servers (id) values (@Guild) on conflict do nothing returning *", new {Guild = guild});
|
||||
conn.QueryFirstAsync<GuildConfig>("insert into servers (id) values (@Guild) on conflict do nothing returning *", new {Guild = guild});
|
||||
|
||||
public static Task<SystemGuildSettings> QueryOrInsertSystemGuildConfig(this IDbConnection conn, ulong guild, int system) =>
|
||||
conn.QueryFirstAsync<SystemGuildSettings>(
|
||||
"insert into member_guild (guild, member) values (@guild, @member) on conflict (guild, member) do update set guild = @guild, member = @member returning *",
|
||||
new {guild, system});
|
||||
|
||||
public static Task<MemberGuildSettings> QueryOrInsertMemberGuildConfig(
|
||||
this IDbConnection conn, ulong guild, int member) =>
|
||||
conn.QueryFirstAsync<MemberGuildSettings>(
|
||||
"insert into member_guild (guild, member) values (@guild, @member) on conflict (guild, member) do update set guild = @guild, member = @member returning *",
|
||||
new {guild, member});
|
||||
}
|
||||
}
|
||||
11
PluralKit.Core/Models/SystemGuildSettings.cs
Normal file
11
PluralKit.Core/Models/SystemGuildSettings.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace PluralKit.Core
|
||||
{
|
||||
public class SystemGuildSettings
|
||||
{
|
||||
public ulong Guild { get; }
|
||||
public bool ProxyEnabled { get; } = true;
|
||||
|
||||
public AutoproxyMode AutoproxyMode { get; } = AutoproxyMode.Off;
|
||||
public int? AutoproxyMember { get; }
|
||||
}
|
||||
}
|
||||
@@ -48,23 +48,7 @@ namespace PluralKit.Core {
|
||||
public int Member;
|
||||
public Instant Timestamp;
|
||||
}
|
||||
|
||||
public class SystemGuildSettings
|
||||
{
|
||||
public ulong Guild { get; set; }
|
||||
public bool ProxyEnabled { get; set; } = true;
|
||||
|
||||
public AutoproxyMode AutoproxyMode { get; set; } = AutoproxyMode.Off;
|
||||
public int? AutoproxyMember { get; set; }
|
||||
}
|
||||
|
||||
public class MemberGuildSettings
|
||||
{
|
||||
public int Member { get; set; }
|
||||
public ulong Guild { get; set; }
|
||||
public string DisplayName { get; set; }
|
||||
public string AvatarUrl { get; set; }
|
||||
}
|
||||
public interface IDataStore
|
||||
{
|
||||
/// <summary>
|
||||
@@ -112,17 +96,7 @@ namespace PluralKit.Core {
|
||||
/// </summary>
|
||||
/// <param name="system">The system to check in.</param>
|
||||
Task<IEnumerable<PKMember>> GetConflictingProxies(PKSystem system, ProxyTag tag);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a specific system's guild-specific settings for a given guild.
|
||||
/// </summary>
|
||||
Task<SystemGuildSettings> GetSystemGuildSettings(PKSystem system, ulong guild);
|
||||
|
||||
/// <summary>
|
||||
/// Saves a specific system's guild-specific settings.
|
||||
/// </summary>
|
||||
Task SetSystemGuildSettings(PKSystem system, ulong guild, SystemGuildSettings settings);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a system, auto-generating its corresponding IDs.
|
||||
/// </summary>
|
||||
@@ -210,16 +184,6 @@ namespace PluralKit.Core {
|
||||
/// </para>
|
||||
Task DeleteMember(PKMember member);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a specific member's guild-specific settings for a given guild.
|
||||
/// </summary>
|
||||
Task<MemberGuildSettings> GetMemberGuildSettings(PKMember member, ulong guild);
|
||||
|
||||
/// <summary>
|
||||
/// Saves a specific member's guild-specific settings.
|
||||
/// </summary>
|
||||
Task SetMemberGuildSettings(PKMember member, ulong guild, MemberGuildSettings settings);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a message and its information by its ID.
|
||||
/// </summary>
|
||||
|
||||
@@ -35,28 +35,7 @@ namespace PluralKit.Core {
|
||||
Suffix = tag.Suffix
|
||||
});
|
||||
}
|
||||
|
||||
public async Task<SystemGuildSettings> GetSystemGuildSettings(PKSystem system, ulong guild)
|
||||
{
|
||||
using (var conn = await _conn.Obtain())
|
||||
return await conn.QuerySingleOrDefaultAsync<SystemGuildSettings>(
|
||||
"select * from system_guild where system = @System and guild = @Guild",
|
||||
new {System = system.Id, Guild = guild}) ?? new SystemGuildSettings();
|
||||
}
|
||||
public async Task SetSystemGuildSettings(PKSystem system, ulong guild, SystemGuildSettings settings)
|
||||
{
|
||||
using (var conn = await _conn.Obtain())
|
||||
await conn.ExecuteAsync("insert into system_guild (system, guild, proxy_enabled, autoproxy_mode, autoproxy_member) values (@System, @Guild, @ProxyEnabled, @AutoproxyMode, @AutoproxyMember) on conflict (system, guild) do update set proxy_enabled = @ProxyEnabled, autoproxy_mode = @AutoproxyMode, autoproxy_member = @AutoproxyMember", new
|
||||
{
|
||||
System = system.Id,
|
||||
Guild = guild,
|
||||
settings.ProxyEnabled,
|
||||
settings.AutoproxyMode,
|
||||
settings.AutoproxyMember
|
||||
});
|
||||
_logger.Information("Updated system guild settings {@SystemGuildSettings}", settings);
|
||||
}
|
||||
|
||||
|
||||
public async Task<PKSystem> CreateSystem(string systemName = null) {
|
||||
PKSystem system;
|
||||
using (var conn = await _conn.Obtain())
|
||||
@@ -189,23 +168,6 @@ namespace PluralKit.Core {
|
||||
_logger.Information("Deleted member {@Member}", member);
|
||||
}
|
||||
|
||||
public async Task<MemberGuildSettings> GetMemberGuildSettings(PKMember member, ulong guild)
|
||||
{
|
||||
using var conn = await _conn.Obtain();
|
||||
return await conn.QuerySingleOrDefaultAsync<MemberGuildSettings>(
|
||||
"select * from member_guild where member = @Member and guild = @Guild", new { Member = member.Id, Guild = guild})
|
||||
?? new MemberGuildSettings { Guild = guild, Member = member.Id };
|
||||
}
|
||||
|
||||
public async Task SetMemberGuildSettings(PKMember member, ulong guild, MemberGuildSettings settings)
|
||||
{
|
||||
using var conn = await _conn.Obtain();
|
||||
await conn.ExecuteAsync(
|
||||
"insert into member_guild (member, guild, display_name, avatar_url) values (@Member, @Guild, @DisplayName, @AvatarUrl) on conflict (member, guild) do update set display_name = @DisplayName, avatar_url = @AvatarUrl",
|
||||
settings);
|
||||
_logger.Information("Updated member guild settings {@MemberGuildSettings}", settings);
|
||||
}
|
||||
|
||||
public async Task<int> GetSystemMemberCount(PKSystem system, bool includePrivate)
|
||||
{
|
||||
var query = "select count(*) from members where system = @Id";
|
||||
|
||||
@@ -311,6 +311,11 @@ namespace PluralKit.Core
|
||||
|
||||
public static class DatabaseExt
|
||||
{
|
||||
public static async Task Execute(this DbConnectionFactory db, Func<IDbConnection, Task> func)
|
||||
{
|
||||
await using var conn = await db.Obtain();
|
||||
await func(conn);
|
||||
}
|
||||
public static async Task<T> Execute<T>(this DbConnectionFactory db, Func<IDbConnection, Task<T>> func)
|
||||
{
|
||||
await using var conn = await db.Obtain();
|
||||
|
||||
Reference in New Issue
Block a user