2019-04-19 18:48:37 +00:00
using System ;
using System.Collections.Generic ;
using System.Data ;
using System.Linq ;
using System.Threading.Tasks ;
using Dapper ;
using Dapper.Contrib.Extensions ;
namespace PluralKit {
public class SystemStore {
private IDbConnection conn ;
public SystemStore ( IDbConnection conn ) {
this . conn = conn ;
}
public async Task < PKSystem > Create ( string systemName = null ) {
// TODO: handle HID collision case
2019-04-21 13:33:22 +00:00
var hid = Utils . GenerateHid ( ) ;
2019-04-19 18:48:37 +00:00
return await conn . QuerySingleAsync < PKSystem > ( "insert into systems (hid, name) values (@Hid, @Name) returning *" , new { Hid = hid , Name = systemName } ) ;
}
2019-04-21 13:33:22 +00:00
public async Task Link ( PKSystem system , ulong accountId ) {
await conn . ExecuteAsync ( "insert into accounts (uid, system) values (@Id, @SystemId)" , new { Id = accountId , SystemId = system . Id } ) ;
}
2019-05-21 21:40:26 +00:00
public async Task Unlink ( PKSystem system , ulong accountId ) {
await conn . ExecuteAsync ( "delete from accounts where uid = @Id and system = @SystemId" , new { Id = accountId , SystemId = system . Id } ) ;
}
2019-04-21 13:33:22 +00:00
2019-04-19 18:48:37 +00:00
public async Task < PKSystem > GetByAccount ( ulong accountId ) {
2019-05-21 21:40:26 +00:00
return await conn . QuerySingleOrDefaultAsync < PKSystem > ( "select systems.* from systems, accounts where accounts.system = systems.id and accounts.uid = @Id" , new { Id = accountId } ) ;
2019-04-19 18:48:37 +00:00
}
public async Task < PKSystem > GetByHid ( string hid ) {
2019-05-08 18:08:56 +00:00
return await conn . QuerySingleOrDefaultAsync < PKSystem > ( "select * from systems where systems.hid = @Hid" , new { Hid = hid . ToLower ( ) } ) ;
2019-04-19 18:48:37 +00:00
}
public async Task < PKSystem > GetByToken ( string token ) {
2019-05-08 18:08:56 +00:00
return await conn . QuerySingleOrDefaultAsync < PKSystem > ( "select * from systems where token = @Token" , new { Token = token } ) ;
2019-04-19 18:48:37 +00:00
}
2019-05-11 22:44:02 +00:00
public async Task < PKSystem > GetById ( int id )
{
return await conn . QuerySingleOrDefaultAsync < PKSystem > ( "select * from systems where id = @Id" , new { Id = id } ) ;
}
2019-04-19 18:48:37 +00:00
public async Task Save ( PKSystem system ) {
2019-04-26 15:14:20 +00:00
await conn . ExecuteAsync ( "update systems set name = @Name, description = @Description, tag = @Tag, avatar_url = @AvatarUrl, token = @Token, ui_tz = @UiTz where id = @Id" , system ) ;
2019-04-19 18:48:37 +00:00
}
public async Task Delete ( PKSystem system ) {
2019-04-26 15:14:20 +00:00
await conn . ExecuteAsync ( "delete from systems where id = @Id" , system ) ;
}
2019-04-21 13:33:22 +00:00
public async Task < IEnumerable < ulong > > GetLinkedAccountIds ( PKSystem system )
{
return await conn . QueryAsync < ulong > ( "select uid from accounts where system = @Id" , new { Id = system . Id } ) ;
}
2019-04-19 18:48:37 +00:00
}
public class MemberStore {
private IDbConnection conn ;
public MemberStore ( IDbConnection conn ) {
this . conn = conn ;
}
public async Task < PKMember > Create ( PKSystem system , string name ) {
// TODO: handle collision
2019-04-21 13:33:22 +00:00
var hid = Utils . GenerateHid ( ) ;
2019-04-27 14:30:34 +00:00
return await conn . QuerySingleAsync < PKMember > ( "insert into members (hid, system, name) values (@Hid, @SystemId, @Name) returning *" , new {
2019-04-19 18:48:37 +00:00
Hid = hid ,
SystemID = system . Id ,
Name = name
} ) ;
}
public async Task < PKMember > GetByHid ( string hid ) {
2019-04-27 14:30:34 +00:00
return await conn . QuerySingleOrDefaultAsync < PKMember > ( "select * from members where hid = @Hid" , new { Hid = hid . ToLower ( ) } ) ;
2019-04-19 18:48:37 +00:00
}
2019-04-27 14:30:34 +00:00
public async Task < PKMember > GetByName ( PKSystem system , string name ) {
2019-04-29 17:43:09 +00:00
// QueryFirst, since members can (in rare cases) share names
return await conn . QueryFirstOrDefaultAsync < PKMember > ( "select * from members where lower(name) = @Name and system = @SystemID" , new { Name = name , SystemID = system . Id } ) ;
2019-04-19 18:48:37 +00:00
}
public async Task < ICollection < PKMember > > GetUnproxyableMembers ( PKSystem system ) {
return ( await GetBySystem ( system ) )
. Where ( ( m ) = > {
var proxiedName = $"{m.Name} {system.Tag}" ;
return proxiedName . Length > 32 | | proxiedName . Length < 2 ;
} ) . ToList ( ) ;
}
public async Task < IEnumerable < PKMember > > GetBySystem ( PKSystem system ) {
return await conn . QueryAsync < PKMember > ( "select * from members where system = @SystemID" , new { SystemID = system . Id } ) ;
}
public async Task Save ( PKMember member ) {
2019-04-29 17:43:09 +00:00
await conn . ExecuteAsync ( "update members set name = @Name, description = @Description, color = @Color, avatar_url = @AvatarUrl, birthday = @Birthday, pronouns = @Pronouns, prefix = @Prefix, suffix = @Suffix where id = @Id" , member ) ;
2019-04-19 18:48:37 +00:00
}
public async Task Delete ( PKMember member ) {
2019-04-29 17:43:09 +00:00
await conn . ExecuteAsync ( "delete from members where id = @Id" , member ) ;
2019-04-19 18:48:37 +00:00
}
2019-05-11 22:44:02 +00:00
public async Task < int > MessageCount ( PKMember member )
{
return await conn . QuerySingleAsync < int > ( "select count(*) from messages where member = @Id" , member ) ;
}
2019-04-19 18:48:37 +00:00
}
public class MessageStore {
public class StoredMessage {
public ulong Mid ;
public ulong ChannelId ;
public ulong SenderId ;
public PKMember Member ;
public PKSystem System ;
}
private IDbConnection _connection ;
public MessageStore ( IDbConnection connection ) {
this . _connection = connection ;
}
public async Task Store ( ulong senderId , ulong messageId , ulong channelId , PKMember member ) {
await _connection . ExecuteAsync ( "insert into messages(mid, channel, member, sender) values(@MessageId, @ChannelId, @MemberId, @SenderId)" , new {
MessageId = messageId ,
ChannelId = channelId ,
MemberId = member . Id ,
SenderId = senderId
} ) ;
}
public async Task < StoredMessage > Get ( ulong id ) {
return ( await _connection . QueryAsync < StoredMessage , PKMember , PKSystem , StoredMessage > ( "select * from messages, members, systems where mid = @Id and messages.member = members.id and systems.id = members.system" , ( msg , member , system ) = > {
msg . System = system ;
msg . Member = member ;
return msg ;
2019-04-29 15:42:09 +00:00
} , new { Id = id } ) ) . FirstOrDefault ( ) ;
2019-04-19 18:48:37 +00:00
}
public async Task Delete ( ulong id ) {
await _connection . ExecuteAsync ( "delete from messages where mid = @Id" , new { Id = id } ) ;
}
}
}