2021-08-27 15:03:47 +00:00
|
|
|
#nullable enable
|
2021-09-27 03:18:17 +00:00
|
|
|
using System;
|
2020-08-29 11:46:27 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
using SqlKata;
|
2020-08-29 11:46:27 +00:00
|
|
|
|
|
|
|
namespace PluralKit.Core
|
|
|
|
{
|
|
|
|
public partial class ModelRepository
|
|
|
|
{
|
2021-09-30 01:51:38 +00:00
|
|
|
public Task<PKSystem?> GetSystem(SystemId id)
|
|
|
|
{
|
|
|
|
var query = new Query("systems").Where("id", id);
|
|
|
|
return _db.QueryFirst<PKSystem?>(query);
|
|
|
|
}
|
2020-08-29 11:46:27 +00:00
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
public Task<PKSystem?> GetSystemByGuid(Guid id)
|
|
|
|
{
|
|
|
|
var query = new Query("systems").Where("uuid", id);
|
|
|
|
return _db.QueryFirst<PKSystem?>(query);
|
|
|
|
}
|
2021-09-27 03:18:17 +00:00
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
public Task<PKSystem?> GetSystemByAccount(ulong accountId)
|
|
|
|
{
|
|
|
|
var query = new Query("accounts").Select("systems.*").LeftJoin("systems", "systems.id", "accounts.system", "=").Where("uid", accountId);
|
|
|
|
return _db.QueryFirst<PKSystem?>(query);
|
|
|
|
}
|
2020-08-29 11:46:27 +00:00
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
public Task<PKSystem?> GetSystemByHid(string hid)
|
|
|
|
{
|
|
|
|
var query = new Query("systems").Where("hid", hid.ToLower());
|
|
|
|
return _db.QueryFirst<PKSystem?>(query);
|
|
|
|
}
|
2020-08-29 11:46:27 +00:00
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
public Task<IEnumerable<ulong>> GetSystemAccounts(SystemId system)
|
|
|
|
{
|
|
|
|
var query = new Query("accounts").Select("uid").Where("system", system);
|
|
|
|
return _db.Query<ulong>(query);
|
|
|
|
}
|
2020-08-29 11:46:27 +00:00
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
public IAsyncEnumerable<PKMember> GetSystemMembers(SystemId system)
|
|
|
|
{
|
|
|
|
var query = new Query("members").Where("system", system);
|
|
|
|
return _db.QueryStream<PKMember>(query);
|
|
|
|
}
|
2020-08-29 11:46:27 +00:00
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
public IAsyncEnumerable<PKGroup> GetSystemGroups(SystemId system)
|
|
|
|
{
|
|
|
|
var query = new Query("groups").Where("system", system);
|
|
|
|
return _db.QueryStream<PKGroup>(query);
|
|
|
|
}
|
2021-09-22 01:42:41 +00:00
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
public Task<int> GetSystemMemberCount(SystemId system, PrivacyLevel? privacyFilter = null)
|
2020-08-29 11:46:27 +00:00
|
|
|
{
|
2021-09-30 01:51:38 +00:00
|
|
|
var query = new Query("members").SelectRaw("count(*)").Where("system", system);
|
2020-08-29 11:46:27 +00:00
|
|
|
if (privacyFilter != null)
|
2021-09-30 01:51:38 +00:00
|
|
|
query.Where("member_visibility", (int)privacyFilter.Value);
|
|
|
|
|
|
|
|
return _db.QueryFirst<int>(query);
|
2020-08-29 11:46:27 +00:00
|
|
|
}
|
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
public Task<int> GetSystemGroupCount(SystemId system, PrivacyLevel? privacyFilter = null)
|
2021-09-27 03:18:17 +00:00
|
|
|
{
|
2021-09-30 01:51:38 +00:00
|
|
|
var query = new Query("groups").SelectRaw("count(*)").Where("system", system);
|
2021-09-27 03:18:17 +00:00
|
|
|
if (privacyFilter != null)
|
2021-09-30 01:51:38 +00:00
|
|
|
query.Where("visibility", (int)privacyFilter.Value);
|
|
|
|
|
|
|
|
return _db.QueryFirst<int>(query);
|
2021-09-27 03:18:17 +00:00
|
|
|
}
|
2021-09-30 01:51:38 +00:00
|
|
|
|
|
|
|
public async Task<PKSystem> CreateSystem(string? systemName = null, IPKConnection? conn = null)
|
2020-08-29 11:46:27 +00:00
|
|
|
{
|
2021-09-30 01:51:38 +00:00
|
|
|
var query = new Query("systems").AsInsert(new
|
|
|
|
{
|
|
|
|
hid = new UnsafeLiteral("find_free_system_hid()"),
|
|
|
|
name = systemName
|
|
|
|
});
|
|
|
|
var system = await _db.QueryFirst<PKSystem>(conn, query, extraSql: "returning *");
|
2020-08-29 11:46:27 +00:00
|
|
|
_logger.Information("Created {SystemId}", system.Id);
|
|
|
|
return system;
|
|
|
|
}
|
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
public Task<PKSystem> UpdateSystem(SystemId id, SystemPatch patch, IPKConnection? conn = null)
|
2020-08-29 11:46:27 +00:00
|
|
|
{
|
|
|
|
_logger.Information("Updated {SystemId}: {@SystemPatch}", id, patch);
|
2021-09-30 01:51:38 +00:00
|
|
|
var query = patch.Apply(new Query("systems").Where("id", id));
|
|
|
|
return _db.QueryFirst<PKSystem>(conn, query, extraSql: "returning *");
|
2020-08-29 11:46:27 +00:00
|
|
|
}
|
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
public Task AddAccount(SystemId system, ulong accountId)
|
2020-08-29 11:46:27 +00:00
|
|
|
{
|
|
|
|
// 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
|
2021-09-30 01:51:38 +00:00
|
|
|
|
|
|
|
var query = new Query("accounts").AsInsert(new
|
|
|
|
{
|
|
|
|
system = system,
|
|
|
|
uid = accountId,
|
|
|
|
});
|
|
|
|
|
2020-08-29 11:46:27 +00:00
|
|
|
_logger.Information("Linked account {UserId} to {SystemId}", accountId, system);
|
2021-09-30 01:51:38 +00:00
|
|
|
return _db.ExecuteQuery(query, extraSql: "on conflict do nothing");
|
2020-08-29 11:46:27 +00:00
|
|
|
}
|
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
public async Task RemoveAccount(SystemId system, ulong accountId)
|
2020-08-29 11:46:27 +00:00
|
|
|
{
|
2021-09-30 01:51:38 +00:00
|
|
|
var query = new Query("accounts").AsDelete().Where("uid", accountId).Where("system", system);
|
|
|
|
await _db.ExecuteQuery(query);
|
2020-08-29 11:46:27 +00:00
|
|
|
_logger.Information("Unlinked account {UserId} from {SystemId}", accountId, system);
|
|
|
|
}
|
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
public Task DeleteSystem(SystemId id)
|
2020-08-29 11:46:27 +00:00
|
|
|
{
|
2021-09-30 01:51:38 +00:00
|
|
|
var query = new Query("systems").AsDelete().Where("id", id);
|
2020-08-29 11:46:27 +00:00
|
|
|
_logger.Information("Deleted {SystemId}", id);
|
2021-09-30 01:51:38 +00:00
|
|
|
return _db.ExecuteQuery(query);
|
2020-08-29 11:46:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|