feat: rework group list into member list
This commit is contained in:
7
PluralKit.Core/Database/Migrations/25.sql
Normal file
7
PluralKit.Core/Database/Migrations/25.sql
Normal file
@@ -0,0 +1,7 @@
|
||||
-- schema version 25
|
||||
-- group name privacy
|
||||
|
||||
alter table groups add column name_privacy integer check (name_privacy in (1, 2)) not null default 1;
|
||||
alter table groups add column metadata_privacy integer check (metadata_privacy in (1, 2)) not null default 1;
|
||||
|
||||
update info set schema_version = 25;
|
||||
@@ -10,11 +10,39 @@ public static class DatabaseViewsExt
|
||||
public static Task<IEnumerable<SystemFronter>> QueryCurrentFronters(this IPKConnection conn, SystemId system) =>
|
||||
conn.QueryAsync<SystemFronter>("select * from system_fronters where system = @system", new { system });
|
||||
|
||||
public static Task<IEnumerable<ListedGroup>> QueryGroupList(this IPKConnection conn, SystemId system) =>
|
||||
conn.QueryAsync<ListedGroup>("select * from group_list where system = @System", new { System = system });
|
||||
public static Task<IEnumerable<ListedGroup>> QueryGroupList(this IPKConnection conn, SystemId system,
|
||||
ListQueryOptions opts)
|
||||
{
|
||||
StringBuilder query = new StringBuilder("select * from group_list where system = @system");
|
||||
|
||||
if (opts.PrivacyFilter != null)
|
||||
query.Append($" and visibility = {(int)opts.PrivacyFilter}");
|
||||
|
||||
if (opts.Search != null)
|
||||
{
|
||||
static string Filter(string column) =>
|
||||
$"(position(lower(@filter) in lower(coalesce({column}, ''))) > 0)";
|
||||
|
||||
query.Append($" and ({Filter("name")} or {Filter("display_name")}");
|
||||
if (opts.SearchDescription)
|
||||
{
|
||||
// We need to account for the possibility of description privacy when searching
|
||||
// If we're looking up from the outside, only search "public_description" (defined in the view; null if desc is private)
|
||||
// If we're the owner, just search the full description
|
||||
var descriptionColumn =
|
||||
opts.Context == LookupContext.ByOwner ? "description" : "public_description";
|
||||
query.Append($"or {Filter(descriptionColumn)}");
|
||||
}
|
||||
|
||||
query.Append(")");
|
||||
}
|
||||
|
||||
return conn.QueryAsync<ListedGroup>(
|
||||
query.ToString(),
|
||||
new { system, filter = opts.Search });
|
||||
}
|
||||
public static Task<IEnumerable<ListedMember>> QueryMemberList(this IPKConnection conn, SystemId system,
|
||||
MemberListQueryOptions opts)
|
||||
ListQueryOptions opts)
|
||||
{
|
||||
StringBuilder query;
|
||||
if (opts.GroupFilter == null)
|
||||
@@ -49,7 +77,7 @@ public static class DatabaseViewsExt
|
||||
new { system, filter = opts.Search, groupFilter = opts.GroupFilter });
|
||||
}
|
||||
|
||||
public struct MemberListQueryOptions
|
||||
public struct ListQueryOptions
|
||||
{
|
||||
public PrivacyLevel? PrivacyFilter;
|
||||
public string? Search;
|
||||
|
||||
@@ -2,5 +2,6 @@ namespace PluralKit.Core;
|
||||
|
||||
public class ListedGroup: PKGroup
|
||||
{
|
||||
public int MemberCount { get; }
|
||||
public int PublicMemberCount { get; }
|
||||
public int TotalMemberCount { get; }
|
||||
}
|
||||
@@ -64,5 +64,12 @@ select groups.*,
|
||||
inner join members on group_members.member_id = members.id
|
||||
where
|
||||
group_members.group_id = groups.id and members.member_visibility = 1
|
||||
) as member_count
|
||||
) as public_member_count,
|
||||
-- Find private group member count
|
||||
(
|
||||
select count(*) from group_members
|
||||
inner join members on group_members.member_id = members.id
|
||||
where
|
||||
group_members.group_id = groups.id
|
||||
) as total_member_count
|
||||
from groups;
|
||||
@@ -43,9 +43,11 @@ public class PKGroup
|
||||
public string? BannerImage { get; private set; }
|
||||
public string? Color { get; private set; }
|
||||
|
||||
public PrivacyLevel NamePrivacy { get; private set; }
|
||||
public PrivacyLevel DescriptionPrivacy { get; private set; }
|
||||
public PrivacyLevel IconPrivacy { get; private set; }
|
||||
public PrivacyLevel ListPrivacy { get; private set; }
|
||||
public PrivacyLevel MetadataPrivacy { get; private set; }
|
||||
public PrivacyLevel Visibility { get; private set; }
|
||||
|
||||
public Instant Created { get; private set; }
|
||||
@@ -53,12 +55,18 @@ public class PKGroup
|
||||
|
||||
public static class PKGroupExt
|
||||
{
|
||||
public static string? NameFor(this PKGroup group, LookupContext ctx) =>
|
||||
group.NamePrivacy.Get(ctx, group.Name, group.DisplayName ?? group.Name);
|
||||
|
||||
public static string? DescriptionFor(this PKGroup group, LookupContext ctx) =>
|
||||
group.DescriptionPrivacy.Get(ctx, group.Description);
|
||||
|
||||
public static string? IconFor(this PKGroup group, LookupContext ctx) =>
|
||||
group.IconPrivacy.Get(ctx, group.Icon?.TryGetCleanCdnUrl());
|
||||
|
||||
public static Instant? CreatedFor(this PKGroup group, LookupContext ctx) =>
|
||||
group.MetadataPrivacy.Get(ctx, (Instant?)group.Created);
|
||||
|
||||
public static JObject ToJson(this PKGroup group, LookupContext ctx, string? systemStr = null,
|
||||
bool needsMembersArray = false)
|
||||
{
|
||||
@@ -66,18 +74,18 @@ public static class PKGroupExt
|
||||
|
||||
o.Add("id", group.Hid);
|
||||
o.Add("uuid", group.Uuid.ToString());
|
||||
o.Add("name", group.Name);
|
||||
o.Add("name", group.NameFor(ctx));
|
||||
|
||||
if (systemStr != null)
|
||||
o.Add("system", systemStr);
|
||||
|
||||
o.Add("display_name", group.DisplayName);
|
||||
o.Add("display_name", group.NamePrivacy.CanAccess(ctx) ? group.DisplayName : null);
|
||||
o.Add("description", group.DescriptionPrivacy.Get(ctx, group.Description));
|
||||
o.Add("icon", group.IconFor(ctx));
|
||||
o.Add("banner", group.DescriptionPrivacy.Get(ctx, group.BannerImage));
|
||||
o.Add("color", group.Color);
|
||||
|
||||
o.Add("created", group.Created.FormatExport());
|
||||
o.Add("created", group.CreatedFor(ctx)?.FormatExport());
|
||||
|
||||
if (needsMembersArray)
|
||||
o.Add("members", new JArray());
|
||||
@@ -86,9 +94,11 @@ public static class PKGroupExt
|
||||
{
|
||||
var p = new JObject();
|
||||
|
||||
p.Add("name_privacy", group.NamePrivacy.ToJsonString());
|
||||
p.Add("description_privacy", group.DescriptionPrivacy.ToJsonString());
|
||||
p.Add("icon_privacy", group.IconPrivacy.ToJsonString());
|
||||
p.Add("list_privacy", group.ListPrivacy.ToJsonString());
|
||||
p.Add("metadata_privacy", group.MetadataPrivacy.ToJsonString());
|
||||
p.Add("visibility", group.Visibility.ToJsonString());
|
||||
|
||||
o.Add("privacy", p);
|
||||
|
||||
@@ -15,9 +15,11 @@ public class GroupPatch: PatchObject
|
||||
public Partial<string?> BannerImage { get; set; }
|
||||
public Partial<string?> Color { get; set; }
|
||||
|
||||
public Partial<PrivacyLevel> NamePrivacy { get; set; }
|
||||
public Partial<PrivacyLevel> DescriptionPrivacy { get; set; }
|
||||
public Partial<PrivacyLevel> IconPrivacy { get; set; }
|
||||
public Partial<PrivacyLevel> ListPrivacy { get; set; }
|
||||
public Partial<PrivacyLevel> MetadataPrivacy { get; set; }
|
||||
public Partial<PrivacyLevel> Visibility { get; set; }
|
||||
|
||||
public override Query Apply(Query q) => q.ApplyPatch(wrapper => wrapper
|
||||
@@ -28,9 +30,11 @@ public class GroupPatch: PatchObject
|
||||
.With("icon", Icon)
|
||||
.With("banner_image", BannerImage)
|
||||
.With("color", Color)
|
||||
.With("name_privacy", NamePrivacy)
|
||||
.With("description_privacy", DescriptionPrivacy)
|
||||
.With("icon_privacy", IconPrivacy)
|
||||
.With("list_privacy", ListPrivacy)
|
||||
.With("metadata_privacy", MetadataPrivacy)
|
||||
.With("visibility", Visibility)
|
||||
);
|
||||
|
||||
@@ -74,6 +78,9 @@ public class GroupPatch: PatchObject
|
||||
{
|
||||
var privacy = o.Value<JObject>("privacy");
|
||||
|
||||
if (privacy.ContainsKey("name_privacy"))
|
||||
patch.NamePrivacy = patch.ParsePrivacy(privacy, "name_privacy");
|
||||
|
||||
if (privacy.ContainsKey("description_privacy"))
|
||||
patch.DescriptionPrivacy = patch.ParsePrivacy(privacy, "description_privacy");
|
||||
|
||||
@@ -83,6 +90,9 @@ public class GroupPatch: PatchObject
|
||||
if (privacy.ContainsKey("list_privacy"))
|
||||
patch.ListPrivacy = patch.ParsePrivacy(privacy, "list_privacy");
|
||||
|
||||
if (privacy.ContainsKey("metadata_privacy"))
|
||||
patch.MetadataPrivacy = patch.ParsePrivacy(privacy, "metadata_privacy");
|
||||
|
||||
if (privacy.ContainsKey("visibility"))
|
||||
patch.Visibility = patch.ParsePrivacy(privacy, "visibility");
|
||||
}
|
||||
@@ -110,14 +120,19 @@ public class GroupPatch: PatchObject
|
||||
o.Add("color", Color.Value);
|
||||
|
||||
if (
|
||||
DescriptionPrivacy.IsPresent
|
||||
NamePrivacy.IsPresent
|
||||
|| DescriptionPrivacy.IsPresent
|
||||
|| IconPrivacy.IsPresent
|
||||
|| ListPrivacy.IsPresent
|
||||
|| MetadataPrivacy.IsPresent
|
||||
|| Visibility.IsPresent
|
||||
)
|
||||
{
|
||||
var p = new JObject();
|
||||
|
||||
if (NamePrivacy.IsPresent)
|
||||
p.Add("name_privacy", NamePrivacy.Value.ToJsonString());
|
||||
|
||||
if (DescriptionPrivacy.IsPresent)
|
||||
p.Add("description_privacy", DescriptionPrivacy.Value.ToJsonString());
|
||||
|
||||
@@ -127,6 +142,9 @@ public class GroupPatch: PatchObject
|
||||
if (ListPrivacy.IsPresent)
|
||||
p.Add("list_privacy", ListPrivacy.Value.ToJsonString());
|
||||
|
||||
if (MetadataPrivacy.IsPresent)
|
||||
p.Add("metadata_privacy", MetadataPrivacy.Value.ToJsonString());
|
||||
|
||||
if (Visibility.IsPresent)
|
||||
p.Add("visibility", Visibility.Value.ToJsonString());
|
||||
|
||||
|
||||
@@ -2,9 +2,11 @@ namespace PluralKit.Core;
|
||||
|
||||
public enum GroupPrivacySubject
|
||||
{
|
||||
Name,
|
||||
Description,
|
||||
Icon,
|
||||
List,
|
||||
Metadata,
|
||||
Visibility
|
||||
}
|
||||
|
||||
@@ -15,9 +17,11 @@ public static class GroupPrivacyUtils
|
||||
// what do you mean switch expressions can't be statements >.>
|
||||
_ = subject switch
|
||||
{
|
||||
GroupPrivacySubject.Name => group.NamePrivacy = level,
|
||||
GroupPrivacySubject.Description => group.DescriptionPrivacy = level,
|
||||
GroupPrivacySubject.Icon => group.IconPrivacy = level,
|
||||
GroupPrivacySubject.List => group.ListPrivacy = level,
|
||||
GroupPrivacySubject.Metadata => group.MetadataPrivacy = level,
|
||||
GroupPrivacySubject.Visibility => group.Visibility = level,
|
||||
_ => throw new ArgumentOutOfRangeException($"Unknown privacy subject {subject}")
|
||||
};
|
||||
@@ -36,6 +40,9 @@ public static class GroupPrivacyUtils
|
||||
{
|
||||
switch (input.ToLowerInvariant())
|
||||
{
|
||||
case "name":
|
||||
subject = GroupPrivacySubject.Name;
|
||||
break;
|
||||
case "description":
|
||||
case "desc":
|
||||
case "text":
|
||||
@@ -54,6 +61,11 @@ public static class GroupPrivacyUtils
|
||||
case "visible":
|
||||
subject = GroupPrivacySubject.Visibility;
|
||||
break;
|
||||
case "meta":
|
||||
case "metadata":
|
||||
case "created":
|
||||
subject = GroupPrivacySubject.Metadata;
|
||||
break;
|
||||
case "list":
|
||||
case "listing":
|
||||
case "members":
|
||||
|
||||
Reference in New Issue
Block a user