From dd9cc3ef0c74e90cb75bc8bd5b2d6c2d66d7a8eb Mon Sep 17 00:00:00 2001 From: Ske Date: Thu, 18 Jun 2020 17:33:37 +0200 Subject: [PATCH] Respect description privacy when searching members --- PluralKit.Bot/Lists/SortFilterOptions.cs | 6 +++--- PluralKit.Core/Database/Views/DatabaseViewsExt.cs | 13 ++++++++++--- PluralKit.Core/Database/Views/views.sql | 9 ++++++++- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/PluralKit.Bot/Lists/SortFilterOptions.cs b/PluralKit.Bot/Lists/SortFilterOptions.cs index e3644a74..08560d1c 100644 --- a/PluralKit.Bot/Lists/SortFilterOptions.cs +++ b/PluralKit.Bot/Lists/SortFilterOptions.cs @@ -54,12 +54,12 @@ namespace PluralKit.Bot public async Task> Execute(IPKConnection conn, PKSystem system, LookupContext ctx) { - var filtered = await QueryWithFilter(conn, system); + var filtered = await QueryWithFilter(conn, system, ctx); return Sort(filtered, ctx); } - private Task> QueryWithFilter(IPKConnection conn, PKSystem system) => - conn.QueryMemberList(system.Id, PrivacyFilter switch + private Task> QueryWithFilter(IPKConnection conn, PKSystem system, LookupContext ctx) => + conn.QueryMemberList(system.Id, ctx, PrivacyFilter switch { PrivacyFilter.PrivateOnly => PrivacyLevel.Private, PrivacyFilter.PublicOnly => PrivacyLevel.Public, diff --git a/PluralKit.Core/Database/Views/DatabaseViewsExt.cs b/PluralKit.Core/Database/Views/DatabaseViewsExt.cs index df03e43a..97e59efa 100644 --- a/PluralKit.Core/Database/Views/DatabaseViewsExt.cs +++ b/PluralKit.Core/Database/Views/DatabaseViewsExt.cs @@ -12,7 +12,7 @@ namespace PluralKit.Core public static Task> QueryCurrentFronters(this IPKConnection conn, SystemId system) => conn.QueryAsync("select * from system_fronters where system = @system", new {system}); - public static Task> QueryMemberList(this IPKConnection conn, SystemId system, PrivacyLevel? privacyFilter = null, string? filter = null, bool includeDescriptionInNameFilter = false) + public static Task> QueryMemberList(this IPKConnection conn, SystemId system, LookupContext ctx, PrivacyLevel? privacyFilter = null, string? filter = null, bool includeDescriptionInNameFilter = false) { StringBuilder query = new StringBuilder("select * from member_list where system = @system"); @@ -21,10 +21,17 @@ namespace PluralKit.Core if (filter != null) { - static string Filter(string column) => $"position(lower(@filter) in lower(coalesce({column}, ''))) > 0"; + static string Filter(string column) => $"(position(lower(@filter) in lower(coalesce({column}, ''))) > 0)"; query.Append($" and ({Filter("name")} or {Filter("display_name")}"); - if (includeDescriptionInNameFilter) query.Append($" or {Filter("description")}"); + if (includeDescriptionInNameFilter) + { + // 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 = ctx == LookupContext.ByOwner ? "description" : "public_description"; + query.Append($"or {Filter(descriptionColumn)}"); + } query.Append(")"); } diff --git a/PluralKit.Core/Database/Views/views.sql b/PluralKit.Core/Database/Views/views.sql index 87ffbe86..344c05b1 100644 --- a/PluralKit.Core/Database/Views/views.sql +++ b/PluralKit.Core/Database/Views/views.sql @@ -46,5 +46,12 @@ select members.*, 4, extract(month from members.birthday)::integer, extract(day from members.birthday)::integer - ) end as birthday_md + ) end as birthday_md, + + -- Extract member description as seen by "the public" + case + -- Privacy '1' = public; just return description as normal + when members.description_privacy = 1 then members.description + -- Any other privacy (rn just '2'), return null description (missing case = null in SQL) + end as public_description from members; \ No newline at end of file