2021-08-27 15:03:47 +00:00
|
|
|
#nullable enable
|
2021-09-30 01:51:38 +00:00
|
|
|
using SqlKata;
|
2020-08-29 11:46:27 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.Core;
|
|
|
|
|
|
|
|
public partial class ModelRepository
|
2020-08-29 11:46:27 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task<PKMember?> GetMember(MemberId id)
|
2020-08-29 11:46:27 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
var query = new Query("members").Where("id", id);
|
|
|
|
return _db.QueryFirst<PKMember?>(query);
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task<PKMember?> GetMemberByHid(string hid, SystemId? system = null)
|
|
|
|
{
|
|
|
|
var query = new Query("members").Where("hid", hid.ToLower());
|
|
|
|
if (system != null)
|
|
|
|
query = query.Where("system", system);
|
|
|
|
return _db.QueryFirst<PKMember?>(query);
|
|
|
|
}
|
2020-08-29 11:46:27 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task<PKMember?> GetMemberByGuid(Guid uuid)
|
|
|
|
{
|
|
|
|
var query = new Query("members").Where("uuid", uuid);
|
|
|
|
return _db.QueryFirst<PKMember?>(query);
|
|
|
|
}
|
2021-09-27 03:18:17 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task<PKMember?> GetMemberByName(SystemId system, string name)
|
|
|
|
{
|
|
|
|
var query = new Query("members").WhereRaw(
|
|
|
|
"lower(name) = lower(?)",
|
|
|
|
name.ToLower()
|
|
|
|
).Where("system", system);
|
|
|
|
return _db.QueryFirst<PKMember?>(query);
|
|
|
|
}
|
2020-08-29 11:46:27 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task<PKMember?> GetMemberByDisplayName(SystemId system, string name)
|
|
|
|
{
|
|
|
|
var query = new Query("members").WhereRaw(
|
|
|
|
"lower(display_name) = lower(?)",
|
|
|
|
name.ToLower()
|
|
|
|
).Where("system", system);
|
|
|
|
return _db.QueryFirst<PKMember?>(query);
|
|
|
|
}
|
2020-08-29 11:46:27 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task<IEnumerable<Guid>> GetMemberGuids(IEnumerable<MemberId> ids)
|
|
|
|
{
|
|
|
|
var query = new Query("members")
|
|
|
|
.Select("uuid")
|
|
|
|
.WhereIn("id", ids);
|
2021-11-19 16:37:50 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
return _db.Query<Guid>(query);
|
|
|
|
}
|
2021-11-19 16:37:50 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public async Task<PKMember> CreateMember(SystemId systemId, string memberName, IPKConnection? conn = null)
|
|
|
|
{
|
|
|
|
var query = new Query("members").AsInsert(new
|
2020-08-29 11:46:27 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
hid = new UnsafeLiteral("find_free_member_hid()"),
|
|
|
|
system = systemId,
|
|
|
|
name = memberName
|
|
|
|
});
|
|
|
|
var member = await _db.QueryFirst<PKMember>(conn, query, "returning *");
|
|
|
|
_logger.Information("Created {MemberId} in {SystemId}: {MemberName}",
|
|
|
|
member.Id, systemId, memberName);
|
|
|
|
return member;
|
|
|
|
}
|
2020-08-29 11:46:27 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Task<PKMember> UpdateMember(MemberId id, MemberPatch patch, IPKConnection? conn = null)
|
|
|
|
{
|
|
|
|
_logger.Information("Updated {MemberId}: {@MemberPatch}", id, patch);
|
|
|
|
var query = patch.Apply(new Query("members").Where("id", id));
|
2021-11-25 20:33:02 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (conn == null)
|
|
|
|
_ = _dispatch.Dispatch(id,
|
|
|
|
new UpdateDispatchData { Event = DispatchEvent.UPDATE_MEMBER, EventData = patch.ToJson() });
|
|
|
|
return _db.QueryFirst<PKMember>(conn, query, "returning *");
|
|
|
|
}
|
2020-08-29 11:46:27 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public async Task DeleteMember(MemberId id)
|
|
|
|
{
|
|
|
|
var oldMember = await GetMember(id);
|
2021-11-19 16:37:50 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
_logger.Information("Deleted {MemberId}", id);
|
|
|
|
var query = new Query("members").AsDelete().Where("id", id);
|
|
|
|
await _db.ExecuteQuery(query);
|
2021-11-19 16:37:50 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
// shh, compiler
|
|
|
|
if (oldMember != null)
|
|
|
|
_ = _dispatch.Dispatch(oldMember.System, oldMember.Uuid, DispatchEvent.DELETE_MEMBER);
|
2020-08-29 11:46:27 +00:00
|
|
|
}
|
2022-11-23 09:17:19 +00:00
|
|
|
|
|
|
|
public async Task<bool> IsMemberOwnedByAccount(MemberId id, ulong userId)
|
|
|
|
{
|
|
|
|
return await _db.QueryFirst<bool>(
|
|
|
|
"select true from accounts, members where members.id = @member and accounts.uid = @account and members.system = accounts.system",
|
|
|
|
new { member = id, account = userId }
|
|
|
|
);
|
|
|
|
}
|
2022-12-10 16:50:22 +00:00
|
|
|
|
|
|
|
public async Task UpdateMemberForSentMessage(MemberId id)
|
|
|
|
{
|
|
|
|
var query = new Query("members")
|
2022-12-14 14:47:48 +00:00
|
|
|
.AsUpdate(new
|
|
|
|
{
|
2022-12-10 16:50:22 +00:00
|
|
|
last_message_timestamp = new UnsafeLiteral("now()"),
|
|
|
|
message_count = new UnsafeLiteral("message_count + 1")
|
|
|
|
})
|
|
|
|
.Where("id", id);
|
|
|
|
|
|
|
|
await _db.ExecuteQuery(query);
|
|
|
|
}
|
2020-08-29 11:46:27 +00:00
|
|
|
}
|