Refactor and simplify member list code
This commit is contained in:
@@ -11,31 +11,39 @@ namespace PluralKit.Core
|
||||
{
|
||||
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<ListedMember>> QueryMemberList(this IPKConnection conn, SystemId system, LookupContext ctx, PrivacyLevel? privacyFilter = null, string? filter = null, bool includeDescriptionInNameFilter = false)
|
||||
|
||||
public static Task<IEnumerable<ListedMember>> QueryMemberList(this IPKConnection conn, SystemId system, MemberListQueryOptions opts)
|
||||
{
|
||||
StringBuilder query = new StringBuilder("select * from member_list where system = @system");
|
||||
|
||||
if (privacyFilter != null)
|
||||
query.Append($" and member_visibility = {(int) privacyFilter}");
|
||||
if (opts.PrivacyFilter != null)
|
||||
query.Append($" and member_visibility = {(int) opts.PrivacyFilter}");
|
||||
|
||||
if (filter != null)
|
||||
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 (includeDescriptionInNameFilter)
|
||||
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 = ctx == LookupContext.ByOwner ? "description" : "public_description";
|
||||
var descriptionColumn = opts.Context == LookupContext.ByOwner ? "description" : "public_description";
|
||||
query.Append($"or {Filter(descriptionColumn)}");
|
||||
}
|
||||
query.Append(")");
|
||||
}
|
||||
|
||||
return conn.QueryAsync<ListedMember>(query.ToString(), new {system, filter});
|
||||
return conn.QueryAsync<ListedMember>(query.ToString(), new {system, filter = opts.Search});
|
||||
}
|
||||
|
||||
public struct MemberListQueryOptions
|
||||
{
|
||||
public PrivacyLevel? PrivacyFilter;
|
||||
public string? Search;
|
||||
public bool SearchDescription;
|
||||
public LookupContext Context;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace PluralKit.Core
|
||||
@@ -56,5 +58,36 @@ namespace PluralKit.Core
|
||||
// so we remove 'em all :)
|
||||
return Regex.Replace(input, " *\n", "\n");
|
||||
}
|
||||
|
||||
public static IReadOnlyList<string> JoinPages(IEnumerable<string> input, int characterLimit) =>
|
||||
JoinPages(input, _ => characterLimit);
|
||||
|
||||
public static IReadOnlyList<string> JoinPages(IEnumerable<string> input, Func<int, int> characterLimitByPage)
|
||||
{
|
||||
var output = new List<string>();
|
||||
|
||||
var buf = new StringBuilder();
|
||||
foreach (var s in input)
|
||||
{
|
||||
var limit = characterLimitByPage.Invoke(output.Count);
|
||||
|
||||
// Would adding this string put us over the limit?
|
||||
// (note: don't roll over if the buffer's already empty; this means an individual section is above the character limit. todo: truncate, then?)
|
||||
if (buf.Length > 0 && buf.Length + s.Length > limit)
|
||||
{
|
||||
// If so, "roll over" (before adding the string to the buffer)
|
||||
output.Add(buf.ToString());
|
||||
buf.Clear();
|
||||
}
|
||||
|
||||
buf.Append(s);
|
||||
}
|
||||
|
||||
// We most likely have something left over, so add that in too
|
||||
if (buf.Length > 0)
|
||||
output.Add(buf.ToString());
|
||||
|
||||
return output;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user