2020-06-13 19:49:31 +00:00
|
|
|
|
#nullable enable
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
using Dapper;
|
|
|
|
|
|
|
|
|
|
namespace PluralKit.Core
|
|
|
|
|
{
|
|
|
|
|
public static class DatabaseViewsExt
|
|
|
|
|
{
|
2020-06-14 19:37:04 +00:00
|
|
|
|
public static Task<IEnumerable<SystemFronter>> QueryCurrentFronters(this IPKConnection conn, SystemId system) =>
|
2020-06-13 19:49:31 +00:00
|
|
|
|
conn.QueryAsync<SystemFronter>("select * from system_fronters where system = @system", new {system});
|
|
|
|
|
|
2020-06-18 15:33:37 +00:00
|
|
|
|
public static Task<IEnumerable<ListedMember>> QueryMemberList(this IPKConnection conn, SystemId system, LookupContext ctx, PrivacyLevel? privacyFilter = null, string? filter = null, bool includeDescriptionInNameFilter = false)
|
2020-06-13 19:49:31 +00:00
|
|
|
|
{
|
|
|
|
|
StringBuilder query = new StringBuilder("select * from member_list where system = @system");
|
|
|
|
|
|
|
|
|
|
if (privacyFilter != null)
|
2020-06-17 19:31:39 +00:00
|
|
|
|
query.Append($" and member_visibility = {(int) privacyFilter}");
|
2020-06-13 19:49:31 +00:00
|
|
|
|
|
|
|
|
|
if (filter != null)
|
|
|
|
|
{
|
2020-06-18 15:33:37 +00:00
|
|
|
|
static string Filter(string column) => $"(position(lower(@filter) in lower(coalesce({column}, ''))) > 0)";
|
2020-06-13 19:49:31 +00:00
|
|
|
|
|
|
|
|
|
query.Append($" and ({Filter("name")} or {Filter("display_name")}");
|
2020-06-18 15:33:37 +00:00
|
|
|
|
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)}");
|
|
|
|
|
}
|
2020-06-13 19:49:31 +00:00
|
|
|
|
query.Append(")");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return conn.QueryAsync<ListedMember>(query.ToString(), new {system, filter});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|