run dotnet format
This commit is contained in:
		@@ -5,23 +5,23 @@ using Dapper;
 | 
			
		||||
namespace PluralKit.Core
 | 
			
		||||
{
 | 
			
		||||
    public partial class ModelRepository
 | 
			
		||||
	{
 | 
			
		||||
		public Task SaveCommandMessage(IPKConnection conn, ulong messageId, ulong authorId) =>
 | 
			
		||||
			conn.QueryAsync("insert into command_messages (message_id, author_id) values (@Message, @Author)",
 | 
			
		||||
				new {Message = messageId, Author = authorId });
 | 
			
		||||
    {
 | 
			
		||||
        public Task SaveCommandMessage(IPKConnection conn, ulong messageId, ulong authorId) =>
 | 
			
		||||
            conn.QueryAsync("insert into command_messages (message_id, author_id) values (@Message, @Author)",
 | 
			
		||||
                new { Message = messageId, Author = authorId });
 | 
			
		||||
 | 
			
		||||
		public Task<CommandMessage> GetCommandMessage(IPKConnection conn, ulong messageId) =>
 | 
			
		||||
			conn.QuerySingleOrDefaultAsync<CommandMessage>("select * from command_messages where message_id = @Message", 
 | 
			
		||||
				new {Message = messageId});
 | 
			
		||||
        public Task<CommandMessage> GetCommandMessage(IPKConnection conn, ulong messageId) =>
 | 
			
		||||
            conn.QuerySingleOrDefaultAsync<CommandMessage>("select * from command_messages where message_id = @Message",
 | 
			
		||||
                new { Message = messageId });
 | 
			
		||||
 | 
			
		||||
        public Task<int> DeleteCommandMessagesBefore(IPKConnection conn, ulong messageIdThreshold) =>
 | 
			
		||||
            conn.ExecuteAsync("delete from command_messages where message_id < @Threshold",
 | 
			
		||||
                new {Threshold = messageIdThreshold});
 | 
			
		||||
                new { Threshold = messageIdThreshold });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
	public class CommandMessage
 | 
			
		||||
	{
 | 
			
		||||
    public class CommandMessage
 | 
			
		||||
    {
 | 
			
		||||
        public ulong AuthorId { get; set; }
 | 
			
		||||
        public ulong MessageId { get; set; }
 | 
			
		||||
	}
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Data;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
@@ -10,16 +10,16 @@ namespace PluralKit.Core
 | 
			
		||||
    {
 | 
			
		||||
        public Task<MessageContext> GetMessageContext(IPKConnection conn, ulong account, ulong guild, ulong channel)
 | 
			
		||||
        {
 | 
			
		||||
            return conn.QueryFirstAsync<MessageContext>("message_context", 
 | 
			
		||||
                new { account_id = account, guild_id = guild, channel_id = channel }, 
 | 
			
		||||
            return conn.QueryFirstAsync<MessageContext>("message_context",
 | 
			
		||||
                new { account_id = account, guild_id = guild, channel_id = channel },
 | 
			
		||||
                commandType: CommandType.StoredProcedure);
 | 
			
		||||
        }  
 | 
			
		||||
        
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public Task<IEnumerable<ProxyMember>> GetProxyMembers(IPKConnection conn, ulong account, ulong guild)
 | 
			
		||||
        {
 | 
			
		||||
            return conn.QueryAsync<ProxyMember>("proxy_members", 
 | 
			
		||||
                new { account_id = account, guild_id = guild }, 
 | 
			
		||||
            return conn.QueryAsync<ProxyMember>("proxy_members",
 | 
			
		||||
                new { account_id = account, guild_id = guild },
 | 
			
		||||
                commandType: CommandType.StoredProcedure);
 | 
			
		||||
        }  
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
#nullable enable
 | 
			
		||||
#nullable enable
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Data;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
@@ -12,14 +12,14 @@ namespace PluralKit.Core
 | 
			
		||||
    public partial class ModelRepository
 | 
			
		||||
    {
 | 
			
		||||
        public Task<PKGroup?> GetGroupByName(IPKConnection conn, SystemId system, string name) =>
 | 
			
		||||
            conn.QueryFirstOrDefaultAsync<PKGroup?>("select * from groups where system = @System and lower(Name) = lower(@Name)", new {System = system, Name = name});
 | 
			
		||||
        
 | 
			
		||||
            conn.QueryFirstOrDefaultAsync<PKGroup?>("select * from groups where system = @System and lower(Name) = lower(@Name)", new { System = system, Name = name });
 | 
			
		||||
 | 
			
		||||
        public Task<PKGroup?> GetGroupByDisplayName(IPKConnection conn, SystemId system, string display_name) =>
 | 
			
		||||
            conn.QueryFirstOrDefaultAsync<PKGroup?>("select * from groups where system = @System and lower(display_name) = lower(@Name)", new {System = system, Name = display_name});
 | 
			
		||||
        
 | 
			
		||||
            conn.QueryFirstOrDefaultAsync<PKGroup?>("select * from groups where system = @System and lower(display_name) = lower(@Name)", new { System = system, Name = display_name });
 | 
			
		||||
 | 
			
		||||
        public Task<PKGroup?> GetGroupByHid(IPKConnection conn, string hid) =>
 | 
			
		||||
            conn.QueryFirstOrDefaultAsync<PKGroup?>("select * from groups where hid = @hid", new {hid = hid.ToLowerInvariant()});
 | 
			
		||||
        
 | 
			
		||||
            conn.QueryFirstOrDefaultAsync<PKGroup?>("select * from groups where hid = @hid", new { hid = hid.ToLowerInvariant() });
 | 
			
		||||
 | 
			
		||||
        public Task<int> GetGroupMemberCount(IPKConnection conn, GroupId id, PrivacyLevel? privacyFilter = null)
 | 
			
		||||
        {
 | 
			
		||||
            var query = new StringBuilder("select count(*) from group_members");
 | 
			
		||||
@@ -28,14 +28,14 @@ namespace PluralKit.Core
 | 
			
		||||
            query.Append(" where group_members.group_id = @Id");
 | 
			
		||||
            if (privacyFilter != null)
 | 
			
		||||
                query.Append(" and members.member_visibility = @PrivacyFilter");
 | 
			
		||||
            return conn.QuerySingleOrDefaultAsync<int>(query.ToString(), new {Id = id, PrivacyFilter = privacyFilter});
 | 
			
		||||
            return conn.QuerySingleOrDefaultAsync<int>(query.ToString(), new { Id = id, PrivacyFilter = privacyFilter });
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        public async Task<PKGroup> CreateGroup(IPKConnection conn, SystemId system, string name, IDbTransaction? transaction = null)
 | 
			
		||||
        {
 | 
			
		||||
            var group = await conn.QueryFirstAsync<PKGroup>(
 | 
			
		||||
                "insert into groups (hid, system, name) values (find_free_group_hid(), @System, @Name) returning *",
 | 
			
		||||
                new {System = system, Name = name}, transaction);
 | 
			
		||||
                new { System = system, Name = name }, transaction);
 | 
			
		||||
            _logger.Information("Created group {GroupId} in system {SystemId}: {GroupName}", group.Id, system, name);
 | 
			
		||||
            return group;
 | 
			
		||||
        }
 | 
			
		||||
@@ -52,7 +52,7 @@ namespace PluralKit.Core
 | 
			
		||||
        public Task DeleteGroup(IPKConnection conn, GroupId group)
 | 
			
		||||
        {
 | 
			
		||||
            _logger.Information("Deleted {GroupId}", group);
 | 
			
		||||
            return conn.ExecuteAsync("delete from groups where id = @Id", new {Id = @group});
 | 
			
		||||
            return conn.ExecuteAsync("delete from groups where id = @Id", new { Id = @group });
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task AddMembersToGroup(IPKConnection conn, GroupId group,
 | 
			
		||||
@@ -76,7 +76,7 @@ namespace PluralKit.Core
 | 
			
		||||
        {
 | 
			
		||||
            _logger.Information("Removed members from {GroupId}: {MemberIds}", group, members);
 | 
			
		||||
            return conn.ExecuteAsync("delete from group_members where group_id = @Group and member_id = any(@Members)",
 | 
			
		||||
                new {Group = @group, Members = members.ToArray()});
 | 
			
		||||
                new { Group = @group, Members = members.ToArray() });
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -7,12 +7,12 @@ using Dapper;
 | 
			
		||||
namespace PluralKit.Core
 | 
			
		||||
{
 | 
			
		||||
    public partial class ModelRepository
 | 
			
		||||
    {        
 | 
			
		||||
    {
 | 
			
		||||
        public IAsyncEnumerable<PKGroup> GetMemberGroups(IPKConnection conn, MemberId id) =>
 | 
			
		||||
            conn.QueryStreamAsync<PKGroup>(
 | 
			
		||||
                "select groups.* from group_members inner join groups on group_members.group_id = groups.id where group_members.member_id = @Id",
 | 
			
		||||
                new {Id = id});
 | 
			
		||||
        
 | 
			
		||||
                new { Id = id });
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        public async Task AddGroupsToMember(IPKConnection conn, MemberId member, IReadOnlyCollection<GroupId> groups)
 | 
			
		||||
        {
 | 
			
		||||
@@ -33,7 +33,7 @@ namespace PluralKit.Core
 | 
			
		||||
        {
 | 
			
		||||
            _logger.Information("Removed groups from {MemberId}: {GroupIds}", member, groups);
 | 
			
		||||
            return conn.ExecuteAsync("delete from group_members where member_id = @Member and group_id = any(@Groups)",
 | 
			
		||||
                new {Member = @member, Groups = groups.ToArray() });
 | 
			
		||||
                new { Member = @member, Groups = groups.ToArray() });
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
using Dapper;
 | 
			
		||||
 | 
			
		||||
@@ -14,7 +14,7 @@ namespace PluralKit.Core
 | 
			
		||||
                .Build();
 | 
			
		||||
            return conn.ExecuteAsync(query, pms);
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        public Task UpsertSystemGuild(IPKConnection conn, SystemId system, ulong guild,
 | 
			
		||||
                                      SystemGuildPatch patch)
 | 
			
		||||
        {
 | 
			
		||||
@@ -36,18 +36,18 @@ namespace PluralKit.Core
 | 
			
		||||
                .Build();
 | 
			
		||||
            return conn.ExecuteAsync(query, pms);
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        public Task<GuildConfig> GetGuild(IPKConnection conn, ulong guild) =>
 | 
			
		||||
            conn.QueryFirstAsync<GuildConfig>("insert into servers (id) values (@guild) on conflict (id) do update set id = @guild returning *", new {guild});
 | 
			
		||||
            conn.QueryFirstAsync<GuildConfig>("insert into servers (id) values (@guild) on conflict (id) do update set id = @guild returning *", new { guild });
 | 
			
		||||
 | 
			
		||||
        public Task<SystemGuildSettings> GetSystemGuild(IPKConnection conn, ulong guild, SystemId system) =>
 | 
			
		||||
            conn.QueryFirstAsync<SystemGuildSettings>(
 | 
			
		||||
                "insert into system_guild (guild, system) values (@guild, @system) on conflict (guild, system) do update set guild = @guild, system = @system returning *", 
 | 
			
		||||
                new {guild, system});
 | 
			
		||||
                "insert into system_guild (guild, system) values (@guild, @system) on conflict (guild, system) do update set guild = @guild, system = @system returning *",
 | 
			
		||||
                new { guild, system });
 | 
			
		||||
 | 
			
		||||
        public Task<MemberGuildSettings> GetMemberGuild(IPKConnection conn, ulong guild, MemberId 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});
 | 
			
		||||
                new { guild, member });
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
#nullable enable
 | 
			
		||||
#nullable enable
 | 
			
		||||
using System.Data;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
@@ -9,22 +9,22 @@ namespace PluralKit.Core
 | 
			
		||||
    public partial class ModelRepository
 | 
			
		||||
    {
 | 
			
		||||
        public Task<PKMember?> GetMember(IPKConnection conn, MemberId id) =>
 | 
			
		||||
            conn.QueryFirstOrDefaultAsync<PKMember?>("select * from members where id = @id", new {id});
 | 
			
		||||
        
 | 
			
		||||
        public Task<PKMember?> GetMemberByHid(IPKConnection conn, string hid) => 
 | 
			
		||||
            conn.QueryFirstOrDefaultAsync<PKMember?>("select * from members where id = @id", new { id });
 | 
			
		||||
 | 
			
		||||
        public Task<PKMember?> GetMemberByHid(IPKConnection conn, string hid) =>
 | 
			
		||||
            conn.QuerySingleOrDefaultAsync<PKMember?>("select * from members where hid = @Hid", new { Hid = hid.ToLower() });
 | 
			
		||||
 | 
			
		||||
        public Task<PKMember?> GetMemberByName(IPKConnection conn, SystemId system, string name) => 
 | 
			
		||||
        public Task<PKMember?> GetMemberByName(IPKConnection conn, SystemId system, string name) =>
 | 
			
		||||
            conn.QueryFirstOrDefaultAsync<PKMember?>("select * from members where lower(name) = lower(@Name) and system = @SystemID", new { Name = name, SystemID = system });
 | 
			
		||||
 | 
			
		||||
        public Task<PKMember?> GetMemberByDisplayName(IPKConnection conn, SystemId system, string name) => 
 | 
			
		||||
        public Task<PKMember?> GetMemberByDisplayName(IPKConnection conn, SystemId system, string name) =>
 | 
			
		||||
            conn.QueryFirstOrDefaultAsync<PKMember?>("select * from members where lower(display_name) = lower(@Name) and system = @SystemID", new { Name = name, SystemID = system });
 | 
			
		||||
 | 
			
		||||
        public async Task<PKMember> CreateMember(IPKConnection conn, SystemId id, string memberName, IDbTransaction? transaction = null)
 | 
			
		||||
        {
 | 
			
		||||
            var member = await conn.QueryFirstAsync<PKMember>(
 | 
			
		||||
                "insert into members (hid, system, name) values (find_free_member_hid(), @SystemId, @Name) returning *",
 | 
			
		||||
                new {SystemId = id, Name = memberName}, transaction);
 | 
			
		||||
                new { SystemId = id, Name = memberName }, transaction);
 | 
			
		||||
            _logger.Information("Created {MemberId} in {SystemId}: {MemberName}",
 | 
			
		||||
                member.Id, id, memberName);
 | 
			
		||||
            return member;
 | 
			
		||||
@@ -42,7 +42,7 @@ namespace PluralKit.Core
 | 
			
		||||
        public Task DeleteMember(IPKConnection conn, MemberId id)
 | 
			
		||||
        {
 | 
			
		||||
            _logger.Information("Deleted {MemberId}", id);
 | 
			
		||||
            return conn.ExecuteAsync("delete from members where id = @Id", new {Id = id});
 | 
			
		||||
            return conn.ExecuteAsync("delete from members where id = @Id", new { Id = id });
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
@@ -8,26 +8,27 @@ namespace PluralKit.Core
 | 
			
		||||
{
 | 
			
		||||
    public partial class ModelRepository
 | 
			
		||||
    {
 | 
			
		||||
        public async Task AddMessage(IPKConnection conn, PKMessage msg) {
 | 
			
		||||
        public async Task AddMessage(IPKConnection conn, PKMessage msg)
 | 
			
		||||
        {
 | 
			
		||||
            // "on conflict do nothing" in the (pretty rare) case of duplicate events coming in from Discord, which would lead to a DB error before
 | 
			
		||||
            await conn.ExecuteAsync("insert into messages(mid, guild, channel, member, sender, original_mid) values(@Mid, @Guild, @Channel, @Member, @Sender, @OriginalMid) on conflict do nothing", msg);
 | 
			
		||||
            _logger.Debug("Stored message {@StoredMessage} in channel {Channel}", msg, msg.Channel);
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        public async Task<FullMessage?> GetMessage(IPKConnection conn, ulong id)
 | 
			
		||||
        {
 | 
			
		||||
            FullMessage Mapper(PKMessage msg, PKMember member, PKSystem system) =>
 | 
			
		||||
                new FullMessage {Message = msg, System = system, Member = member};
 | 
			
		||||
                new FullMessage { Message = msg, System = system, Member = member };
 | 
			
		||||
 | 
			
		||||
            var result = await conn.QueryAsync<PKMessage, PKMember, PKSystem, FullMessage>(
 | 
			
		||||
                "select messages.*, members.*, systems.* from messages, members, systems where (mid = @Id or original_mid = @Id) and messages.member = members.id and systems.id = members.system",
 | 
			
		||||
                Mapper, new {Id = id});
 | 
			
		||||
                Mapper, new { Id = id });
 | 
			
		||||
            return result.FirstOrDefault();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task DeleteMessage(IPKConnection conn, ulong id)
 | 
			
		||||
        {
 | 
			
		||||
            var rowCount = await conn.ExecuteAsync("delete from messages where mid = @Id", new {Id = id});
 | 
			
		||||
            var rowCount = await conn.ExecuteAsync("delete from messages where mid = @Id", new { Id = id });
 | 
			
		||||
            if (rowCount > 0)
 | 
			
		||||
                _logger.Information("Deleted message {MessageId} from database", id);
 | 
			
		||||
        }
 | 
			
		||||
@@ -37,7 +38,7 @@ namespace PluralKit.Core
 | 
			
		||||
            // Npgsql doesn't support ulongs in general - we hacked around it for plain ulongs but tbh not worth it for collections of ulong
 | 
			
		||||
            // Hence we map them to single longs, which *are* supported (this is ok since they're Technically (tm) stored as signed longs in the db anyway)
 | 
			
		||||
            var rowCount = await conn.ExecuteAsync("delete from messages where mid = any(@Ids)",
 | 
			
		||||
                new {Ids = ids.Select(id => (long) id).ToArray()});
 | 
			
		||||
                new { Ids = ids.Select(id => (long)id).ToArray() });
 | 
			
		||||
            if (rowCount > 0)
 | 
			
		||||
                _logger.Information("Bulk deleted messages ({FoundCount} found) from database: {MessageIds}", rowCount,
 | 
			
		||||
                    ids);
 | 
			
		||||
@@ -65,7 +66,7 @@ namespace PluralKit.Core
 | 
			
		||||
        public ulong Sender { get; set; }
 | 
			
		||||
        public ulong? OriginalMid { get; set; }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    public class FullMessage
 | 
			
		||||
    {
 | 
			
		||||
        public PKMessage Message;
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
using Dapper;
 | 
			
		||||
@@ -15,16 +15,16 @@ namespace PluralKit.Core
 | 
			
		||||
        public Task SetShardStatus(IPKConnection conn, int shard, PKShardInfo.ShardStatus status) =>
 | 
			
		||||
            conn.ExecuteAsync(
 | 
			
		||||
                "insert into shards (id, status) values (@Id, @Status) on conflict (id) do update set status = @Status",
 | 
			
		||||
                new {Id = shard, Status = status});
 | 
			
		||||
                new { Id = shard, Status = status });
 | 
			
		||||
 | 
			
		||||
        public Task RegisterShardHeartbeat(IPKConnection conn, int shard, Duration ping) =>
 | 
			
		||||
            conn.ExecuteAsync(
 | 
			
		||||
                "insert into shards (id, last_heartbeat, ping) values (@Id, now(), @Ping) on conflict (id) do update set last_heartbeat = now(), ping = @Ping",
 | 
			
		||||
                new {Id = shard, Ping = ping.TotalSeconds});
 | 
			
		||||
                new { Id = shard, Ping = ping.TotalSeconds });
 | 
			
		||||
 | 
			
		||||
        public Task RegisterShardConnection(IPKConnection conn, int shard) =>
 | 
			
		||||
            conn.ExecuteAsync(
 | 
			
		||||
                "insert into shards (id, last_connection) values (@Id, now()) on conflict (id) do update set last_connection = now()",
 | 
			
		||||
                new {Id = shard});
 | 
			
		||||
                new { Id = shard });
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Linq;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
@@ -19,7 +19,7 @@ namespace PluralKit.Core
 | 
			
		||||
 | 
			
		||||
            // First, we insert the switch itself
 | 
			
		||||
            var sw = await conn.QuerySingleAsync<PKSwitch>("insert into switches(system) values (@System) returning *",
 | 
			
		||||
                new {System = system});
 | 
			
		||||
                new { System = system });
 | 
			
		||||
 | 
			
		||||
            // Then we insert each member in the switch in the switch_members table
 | 
			
		||||
            await using (var w = conn.BeginBinaryImport("copy switch_members (switch, member) from stdin (format binary)"))
 | 
			
		||||
@@ -43,20 +43,20 @@ namespace PluralKit.Core
 | 
			
		||||
        public async Task MoveSwitch(IPKConnection conn, SwitchId id, Instant time)
 | 
			
		||||
        {
 | 
			
		||||
            await conn.ExecuteAsync("update switches set timestamp = @Time where id = @Id",
 | 
			
		||||
                new {Time = time, Id = id});
 | 
			
		||||
                new { Time = time, Id = id });
 | 
			
		||||
 | 
			
		||||
            _logger.Information("Updated {SwitchId} timestamp: {SwitchTimestamp}", id, time);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task DeleteSwitch(IPKConnection conn, SwitchId id)
 | 
			
		||||
        {
 | 
			
		||||
            await conn.ExecuteAsync("delete from switches where id = @Id", new {Id = id});
 | 
			
		||||
            await conn.ExecuteAsync("delete from switches where id = @Id", new { Id = id });
 | 
			
		||||
            _logger.Information("Deleted {Switch}", id);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task DeleteAllSwitches(IPKConnection conn, SystemId system)
 | 
			
		||||
        {
 | 
			
		||||
            await conn.ExecuteAsync("delete from switches where system = @Id", new {Id = system});
 | 
			
		||||
            await conn.ExecuteAsync("delete from switches where system = @Id", new { Id = system });
 | 
			
		||||
            _logger.Information("Deleted all switches in {SystemId}", system);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -65,7 +65,7 @@ namespace PluralKit.Core
 | 
			
		||||
            // TODO: refactor the PKSwitch data structure to somehow include a hydrated member list
 | 
			
		||||
            return conn.QueryStreamAsync<PKSwitch>(
 | 
			
		||||
                "select * from switches where system = @System order by timestamp desc",
 | 
			
		||||
                new {System = system});
 | 
			
		||||
                new { System = system });
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task<int> GetSwitchCount(IPKConnection conn, SystemId system)
 | 
			
		||||
@@ -86,7 +86,7 @@ namespace PluralKit.Core
 | 
			
		||||
                        FROM switches
 | 
			
		||||
                        WHERE switches.system = @System
 | 
			
		||||
                        AND switches.timestamp < @Start",
 | 
			
		||||
                new {System = system, Start = start});
 | 
			
		||||
                new { System = system, Start = start });
 | 
			
		||||
 | 
			
		||||
            // Then collect the time and members of all switches that overlap the range
 | 
			
		||||
            var switchMembersEntries = conn.QueryStreamAsync<SwitchMembersListEntry>(
 | 
			
		||||
@@ -101,7 +101,7 @@ namespace PluralKit.Core
 | 
			
		||||
                        )
 | 
			
		||||
                        AND switches.timestamp < @End
 | 
			
		||||
                        ORDER BY switches.timestamp DESC",
 | 
			
		||||
                new {System = system, Start = start, End = end, LastSwitch = lastSwitch});
 | 
			
		||||
                new { System = system, Start = start, End = end, LastSwitch = lastSwitch });
 | 
			
		||||
 | 
			
		||||
            // Yield each value here
 | 
			
		||||
            await foreach (var entry in switchMembersEntries)
 | 
			
		||||
@@ -114,7 +114,7 @@ namespace PluralKit.Core
 | 
			
		||||
        {
 | 
			
		||||
            return conn.QueryStreamAsync<PKMember>(
 | 
			
		||||
                "select * from switch_members, members where switch_members.member = members.id and switch_members.switch = @Switch order by switch_members.id",
 | 
			
		||||
                new {Switch = sw});
 | 
			
		||||
                new { Switch = sw });
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task<PKSwitch> GetLatestSwitch(IPKConnection conn, SystemId system) =>
 | 
			
		||||
@@ -136,13 +136,13 @@ namespace PluralKit.Core
 | 
			
		||||
            // key used in GetPerMemberSwitchDuration below
 | 
			
		||||
            var membersList = await conn.QueryAsync<PKMember>(
 | 
			
		||||
                "select * from members where id = any(@Switches)", // lol postgres specific `= any()` syntax
 | 
			
		||||
                new {Switches = switchMembers.Select(m => m.Member.Value).Distinct().ToList()});
 | 
			
		||||
                new { Switches = switchMembers.Select(m => m.Member.Value).Distinct().ToList() });
 | 
			
		||||
            var memberObjects = membersList.ToDictionary(m => m.Id);
 | 
			
		||||
 | 
			
		||||
            // check if a group ID is provided. if so, query DB for all members of said group, otherwise use membersList
 | 
			
		||||
            var groupMembersList = group != null ? await conn.QueryAsync<PKMember>(
 | 
			
		||||
                "select * from members inner join group_members on members.id = group_members.member_id where group_id = @id",
 | 
			
		||||
                new {id = group}) : membersList;
 | 
			
		||||
                new { id = group }) : membersList;
 | 
			
		||||
            var groupMemberObjects = groupMembersList.ToDictionary(m => m.Id);
 | 
			
		||||
 | 
			
		||||
            // Initialize entries - still need to loop to determine the TimespanEnd below
 | 
			
		||||
@@ -154,7 +154,7 @@ namespace PluralKit.Core
 | 
			
		||||
                select new SwitchListEntry
 | 
			
		||||
                {
 | 
			
		||||
                    TimespanStart = g.Key,
 | 
			
		||||
                    Members = g.Where(x => x.Member != default(MemberId) && groupMemberObjects.Any(m => x.Member == m.Key) ).Select(x => memberObjects[x.Member])
 | 
			
		||||
                    Members = g.Where(x => x.Member != default(MemberId) && groupMemberObjects.Any(m => x.Member == m.Key)).Select(x => memberObjects[x.Member])
 | 
			
		||||
                        .ToList()
 | 
			
		||||
                };
 | 
			
		||||
 | 
			
		||||
@@ -171,7 +171,9 @@ namespace PluralKit.Core
 | 
			
		||||
 | 
			
		||||
                outList.Add(new SwitchListEntry
 | 
			
		||||
                {
 | 
			
		||||
                    Members = e.Members, TimespanStart = switchStartClamped, TimespanEnd = endTime
 | 
			
		||||
                    Members = e.Members,
 | 
			
		||||
                    TimespanStart = switchStartClamped,
 | 
			
		||||
                    TimespanEnd = endTime
 | 
			
		||||
                });
 | 
			
		||||
 | 
			
		||||
                // next switch's end is this switch's start (we're working backward in time)
 | 
			
		||||
@@ -219,7 +221,7 @@ namespace PluralKit.Core
 | 
			
		||||
            };
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    public struct SwitchListEntry
 | 
			
		||||
    {
 | 
			
		||||
        public ICollection<PKMember> Members;
 | 
			
		||||
@@ -234,7 +236,7 @@ namespace PluralKit.Core
 | 
			
		||||
        public Instant RangeStart;
 | 
			
		||||
        public Instant RangeEnd;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    public struct SwitchMembersListEntry
 | 
			
		||||
    {
 | 
			
		||||
        public MemberId Member;
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
#nullable enable
 | 
			
		||||
#nullable enable
 | 
			
		||||
using System.Collections.Generic;
 | 
			
		||||
using System.Text;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
@@ -10,36 +10,36 @@ namespace PluralKit.Core
 | 
			
		||||
    public partial class ModelRepository
 | 
			
		||||
    {
 | 
			
		||||
        public Task<PKSystem?> GetSystem(IPKConnection conn, SystemId id) =>
 | 
			
		||||
            conn.QueryFirstOrDefaultAsync<PKSystem?>("select * from systems where id = @id", new {id});
 | 
			
		||||
            conn.QueryFirstOrDefaultAsync<PKSystem?>("select * from systems where id = @id", new { id });
 | 
			
		||||
 | 
			
		||||
        public Task<PKSystem?> GetSystemByAccount(IPKConnection conn, ulong accountId) =>
 | 
			
		||||
            conn.QuerySingleOrDefaultAsync<PKSystem?>(
 | 
			
		||||
                "select systems.* from systems, accounts where accounts.system = systems.id and accounts.uid = @Id",
 | 
			
		||||
                new {Id = accountId});
 | 
			
		||||
                new { Id = accountId });
 | 
			
		||||
 | 
			
		||||
        public Task<PKSystem?> GetSystemByHid(IPKConnection conn, string hid) =>
 | 
			
		||||
            conn.QuerySingleOrDefaultAsync<PKSystem?>("select * from systems where systems.hid = @Hid",
 | 
			
		||||
                new {Hid = hid.ToLower()});
 | 
			
		||||
                new { Hid = hid.ToLower() });
 | 
			
		||||
 | 
			
		||||
        public Task<IEnumerable<ulong>> GetSystemAccounts(IPKConnection conn, SystemId system) =>
 | 
			
		||||
            conn.QueryAsync<ulong>("select uid from accounts where system = @Id", new {Id = system});
 | 
			
		||||
            conn.QueryAsync<ulong>("select uid from accounts where system = @Id", new { Id = system });
 | 
			
		||||
 | 
			
		||||
        public IAsyncEnumerable<PKMember> GetSystemMembers(IPKConnection conn, SystemId system) =>
 | 
			
		||||
            conn.QueryStreamAsync<PKMember>("select * from members where system = @SystemID", new {SystemID = system});
 | 
			
		||||
            conn.QueryStreamAsync<PKMember>("select * from members where system = @SystemID", new { SystemID = system });
 | 
			
		||||
 | 
			
		||||
        public Task<int> GetSystemMemberCount(IPKConnection conn, SystemId id, PrivacyLevel? privacyFilter = null)
 | 
			
		||||
        {
 | 
			
		||||
            var query = new StringBuilder("select count(*) from members where system = @Id");
 | 
			
		||||
            if (privacyFilter != null)
 | 
			
		||||
                query.Append($" and member_visibility = {(int) privacyFilter.Value}");
 | 
			
		||||
            return conn.QuerySingleAsync<int>(query.ToString(), new {Id = id});
 | 
			
		||||
                query.Append($" and member_visibility = {(int)privacyFilter.Value}");
 | 
			
		||||
            return conn.QuerySingleAsync<int>(query.ToString(), new { Id = id });
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task<PKSystem> CreateSystem(IPKConnection conn, string? systemName = null, IPKTransaction? tx = null)
 | 
			
		||||
        {
 | 
			
		||||
            var system = await conn.QuerySingleAsync<PKSystem>(
 | 
			
		||||
                "insert into systems (hid, name) values (find_free_system_hid(), @Name) returning *",
 | 
			
		||||
                new {Name = systemName},
 | 
			
		||||
                new { Name = systemName },
 | 
			
		||||
                transaction: tx);
 | 
			
		||||
            _logger.Information("Created {SystemId}", system.Id);
 | 
			
		||||
            return system;
 | 
			
		||||
@@ -59,21 +59,21 @@ namespace PluralKit.Core
 | 
			
		||||
            // We have "on conflict do nothing" since linking an account when it's already linked to the same system is idempotent
 | 
			
		||||
            // This is used in import/export, although the pk;link command checks for this case beforehand
 | 
			
		||||
            await conn.ExecuteAsync("insert into accounts (uid, system) values (@Id, @SystemId) on conflict do nothing",
 | 
			
		||||
                new {Id = accountId, SystemId = system});
 | 
			
		||||
                new { Id = accountId, SystemId = system });
 | 
			
		||||
            _logger.Information("Linked account {UserId} to {SystemId}", accountId, system);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public async Task RemoveAccount(IPKConnection conn, SystemId system, ulong accountId)
 | 
			
		||||
        {
 | 
			
		||||
            await conn.ExecuteAsync("delete from accounts where uid = @Id and system = @SystemId",
 | 
			
		||||
                new {Id = accountId, SystemId = system});
 | 
			
		||||
                new { Id = accountId, SystemId = system });
 | 
			
		||||
            _logger.Information("Unlinked account {UserId} from {SystemId}", accountId, system);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public Task DeleteSystem(IPKConnection conn, SystemId id)
 | 
			
		||||
        {
 | 
			
		||||
            _logger.Information("Deleted {SystemId}", id);
 | 
			
		||||
            return conn.ExecuteAsync("delete from systems where id = @Id", new {Id = id});
 | 
			
		||||
            return conn.ExecuteAsync("delete from systems where id = @Id", new { Id = id });
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,4 +1,4 @@
 | 
			
		||||
using Serilog;
 | 
			
		||||
using Serilog;
 | 
			
		||||
 | 
			
		||||
namespace PluralKit.Core
 | 
			
		||||
{
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user