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.Threading.Tasks;
|
|
|
|
|
2021-11-19 16:37:50 +00:00
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
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-11-02 10:08:17 +00:00
|
|
|
public Task<PKGroup?> GetGroup(GroupId id)
|
|
|
|
{
|
|
|
|
var query = new Query("groups").Where("id", id);
|
|
|
|
return _db.QueryFirst<PKGroup?>(query);
|
|
|
|
}
|
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
public Task<PKGroup?> GetGroupByName(SystemId system, string name)
|
|
|
|
{
|
|
|
|
var query = new Query("groups").Where("system", system).WhereRaw("lower(name) = lower(?)", name.ToLower());
|
|
|
|
return _db.QueryFirst<PKGroup?>(query);
|
|
|
|
}
|
2021-09-27 03:18:17 +00:00
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
public Task<PKGroup?> GetGroupByDisplayName(SystemId system, string display_name)
|
2020-08-29 11:46:27 +00:00
|
|
|
{
|
2021-09-30 01:51:38 +00:00
|
|
|
var query = new Query("groups").Where("system", system).WhereRaw("lower(display_name) = lower(?)", display_name.ToLower());
|
|
|
|
return _db.QueryFirst<PKGroup?>(query);
|
2020-08-29 11:46:27 +00:00
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
public Task<PKGroup?> GetGroupByHid(string hid, SystemId? system = null)
|
2020-08-29 11:46:27 +00:00
|
|
|
{
|
2021-09-30 01:51:38 +00:00
|
|
|
var query = new Query("groups").Where("hid", hid.ToLower());
|
|
|
|
if (system != null)
|
|
|
|
query = query.Where("system", system);
|
|
|
|
return _db.QueryFirst<PKGroup?>(query);
|
2020-08-29 11:46:27 +00:00
|
|
|
}
|
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
public Task<PKGroup?> GetGroupByGuid(Guid uuid)
|
2020-08-29 11:46:27 +00:00
|
|
|
{
|
2021-09-30 01:51:38 +00:00
|
|
|
var query = new Query("groups").Where("uuid", uuid);
|
|
|
|
return _db.QueryFirst<PKGroup?>(query);
|
2020-08-29 11:46:27 +00:00
|
|
|
}
|
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
public Task<int> GetGroupMemberCount(GroupId id, PrivacyLevel? privacyFilter = null)
|
2020-08-29 11:46:27 +00:00
|
|
|
{
|
2021-09-30 01:51:38 +00:00
|
|
|
var query = new Query("group_members")
|
|
|
|
.SelectRaw("count(*)")
|
|
|
|
.Where("group_members.group_id", id);
|
|
|
|
|
|
|
|
if (privacyFilter != null) query = query
|
|
|
|
.Join("members", "group_members.member_id", "members.id")
|
|
|
|
.Where("members.member_visibility", privacyFilter);
|
|
|
|
|
|
|
|
return _db.QueryFirst<int>(query);
|
2020-08-29 11:46:27 +00:00
|
|
|
}
|
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
public async Task<PKGroup> CreateGroup(SystemId system, string name, IPKConnection? conn = null)
|
2020-08-29 11:46:27 +00:00
|
|
|
{
|
2021-09-30 01:51:38 +00:00
|
|
|
var query = new Query("groups").AsInsert(new
|
2020-08-29 11:46:27 +00:00
|
|
|
{
|
2021-09-30 01:51:38 +00:00
|
|
|
hid = new UnsafeLiteral("find_free_group_hid()"),
|
|
|
|
system = system,
|
|
|
|
name = name
|
|
|
|
});
|
|
|
|
var group = await _db.QueryFirst<PKGroup>(conn, query, extraSql: "returning *");
|
|
|
|
_logger.Information("Created group {GroupId} in system {SystemId}: {GroupName}", group.Id, system, name);
|
|
|
|
return group;
|
|
|
|
}
|
2020-08-29 11:46:27 +00:00
|
|
|
|
2021-11-19 16:37:50 +00:00
|
|
|
public async Task<PKGroup> UpdateGroup(GroupId id, GroupPatch patch, IPKConnection? conn = null)
|
2021-09-30 01:51:38 +00:00
|
|
|
{
|
|
|
|
_logger.Information("Updated {GroupId}: {@GroupPatch}", id, patch);
|
|
|
|
var query = patch.Apply(new Query("groups").Where("id", id));
|
2021-11-19 16:37:50 +00:00
|
|
|
var group = await _db.QueryFirst<PKGroup>(conn, query, extraSql: "returning *");
|
2021-11-25 20:33:02 +00:00
|
|
|
|
|
|
|
if (conn == null)
|
|
|
|
_ = _dispatch.Dispatch(id, new()
|
|
|
|
{
|
|
|
|
Event = DispatchEvent.UPDATE_GROUP,
|
|
|
|
EventData = patch.ToJson(),
|
|
|
|
});
|
2021-11-19 16:37:50 +00:00
|
|
|
return group;
|
2020-08-29 11:46:27 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 16:37:50 +00:00
|
|
|
public async Task DeleteGroup(GroupId group)
|
2020-08-29 11:46:27 +00:00
|
|
|
{
|
2021-11-19 16:37:50 +00:00
|
|
|
var oldGroup = await GetGroup(group);
|
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
_logger.Information("Deleted {GroupId}", group);
|
|
|
|
var query = new Query("groups").AsDelete().Where("id", group);
|
2021-11-19 16:37:50 +00:00
|
|
|
await _db.ExecuteQuery(query);
|
|
|
|
|
|
|
|
if (oldGroup != null)
|
|
|
|
_ = _dispatch.Dispatch(oldGroup.System, oldGroup.Uuid, DispatchEvent.DELETE_GROUP);
|
2020-08-29 11:46:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|