2020-07-07 18:57:22 +00:00
|
|
|
using System.Text;
|
|
|
|
|
2022-01-23 06:47:55 +00:00
|
|
|
using Humanizer;
|
|
|
|
|
2020-07-07 18:57:22 +00:00
|
|
|
using NodaTime;
|
|
|
|
|
|
|
|
using PluralKit.Core;
|
|
|
|
|
|
|
|
#nullable enable
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.Bot;
|
|
|
|
|
2022-01-15 03:30:02 +00:00
|
|
|
public class ListOptions
|
2020-07-07 18:57:22 +00:00
|
|
|
{
|
2022-09-22 17:17:53 +00:00
|
|
|
private SortProperty? _sortProperty { get; set; }
|
|
|
|
public SortProperty SortProperty
|
|
|
|
{
|
|
|
|
get => _sortProperty ?? SortProperty.Name;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (_sortProperty != null)
|
|
|
|
throw new PKError("Cannot sort in multiple ways at the same time. Please choose only one sorting method.");
|
|
|
|
|
|
|
|
_sortProperty = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public bool Reverse { get; set; }
|
|
|
|
|
|
|
|
public PrivacyLevel? PrivacyFilter { get; set; } = PrivacyLevel.Public;
|
|
|
|
public GroupId? GroupFilter { get; set; }
|
|
|
|
public string? Search { get; set; }
|
|
|
|
public bool SearchDescription { get; set; }
|
|
|
|
|
|
|
|
public ListType Type { get; set; }
|
|
|
|
public bool IncludeMessageCount { get; set; }
|
|
|
|
public bool IncludeLastSwitch { get; set; }
|
|
|
|
public bool IncludeLastMessage { get; set; }
|
|
|
|
public bool IncludeCreated { get; set; }
|
|
|
|
public bool IncludeAvatar { get; set; }
|
|
|
|
public bool IncludePronouns { get; set; }
|
2022-01-19 22:21:37 +00:00
|
|
|
public bool IncludeDisplayName { get; set; }
|
2022-09-22 17:29:34 +00:00
|
|
|
public bool IncludeBirthday { get; set; }
|
2021-11-27 02:10:56 +00:00
|
|
|
|
2022-09-22 17:59:56 +00:00
|
|
|
// hacky but works, remember to update this when more include flags are added
|
|
|
|
public int includedCount => new[] {
|
|
|
|
IncludeMessageCount,
|
|
|
|
IncludeLastSwitch,
|
|
|
|
IncludeLastMessage,
|
|
|
|
IncludeCreated,
|
|
|
|
IncludeAvatar,
|
|
|
|
IncludePronouns,
|
|
|
|
IncludeDisplayName,
|
|
|
|
IncludeBirthday,
|
|
|
|
}.Sum(x => Convert.ToInt32(x));
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public string CreateFilterString()
|
2020-07-07 18:57:22 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
var str = new StringBuilder();
|
|
|
|
str.Append("Sorting ");
|
|
|
|
if (SortProperty != SortProperty.Random) str.Append("by ");
|
|
|
|
str.Append(SortProperty switch
|
|
|
|
{
|
2022-01-15 03:30:02 +00:00
|
|
|
SortProperty.Name => "name",
|
|
|
|
SortProperty.Hid => "ID",
|
2021-11-27 02:10:56 +00:00
|
|
|
SortProperty.DisplayName => "display name",
|
|
|
|
SortProperty.CreationDate => "creation date",
|
|
|
|
SortProperty.LastMessage => "last message",
|
|
|
|
SortProperty.LastSwitch => "last switch",
|
|
|
|
SortProperty.MessageCount => "message count",
|
|
|
|
SortProperty.Birthdate => "birthday",
|
|
|
|
SortProperty.Random => "randomly",
|
|
|
|
_ => new ArgumentOutOfRangeException($"Couldn't find readable string for sort property {SortProperty}")
|
|
|
|
});
|
|
|
|
|
|
|
|
if (Search != null)
|
2020-07-07 18:57:22 +00:00
|
|
|
{
|
2022-01-23 06:47:55 +00:00
|
|
|
str.Append($", searching for \"{Search.Truncate(100)}\"");
|
2021-11-27 02:10:56 +00:00
|
|
|
if (SearchDescription) str.Append(" (including description)");
|
2020-07-07 18:57:22 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
str.Append(PrivacyFilter switch
|
|
|
|
{
|
2022-01-15 03:30:02 +00:00
|
|
|
null => ", showing all items",
|
|
|
|
PrivacyLevel.Private => ", showing only private items",
|
2021-11-27 02:10:56 +00:00
|
|
|
PrivacyLevel.Public => "", // (default, no extra line needed)
|
|
|
|
_ => new ArgumentOutOfRangeException(
|
|
|
|
$"Couldn't find readable string for privacy filter {PrivacyFilter}")
|
|
|
|
});
|
|
|
|
|
|
|
|
return str.ToString();
|
2020-07-07 18:57:22 +00:00
|
|
|
}
|
|
|
|
|
2022-01-15 03:30:02 +00:00
|
|
|
public DatabaseViewsExt.ListQueryOptions ToQueryOptions() =>
|
2021-11-27 02:10:56 +00:00
|
|
|
new()
|
2020-07-07 18:57:22 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
PrivacyFilter = PrivacyFilter,
|
|
|
|
GroupFilter = GroupFilter,
|
|
|
|
Search = Search,
|
|
|
|
SearchDescription = SearchDescription
|
|
|
|
};
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2022-01-15 03:30:02 +00:00
|
|
|
public static class ListOptionsExt
|
2021-11-27 02:10:56 +00:00
|
|
|
{
|
|
|
|
public static IEnumerable<ListedMember> SortByMemberListOptions(this IEnumerable<ListedMember> input,
|
2022-01-15 03:30:02 +00:00
|
|
|
ListOptions opts, LookupContext ctx)
|
2020-07-07 18:57:22 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
IComparer<T> ReverseMaybe<T>(IComparer<T> c) =>
|
|
|
|
opts.Reverse ? Comparer<T>.Create((a, b) => c.Compare(b, a)) : c;
|
2020-07-07 18:57:22 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
var randGen = new global::System.Random();
|
|
|
|
|
|
|
|
var culture = StringComparer.InvariantCultureIgnoreCase;
|
|
|
|
return (opts.SortProperty switch
|
|
|
|
{
|
|
|
|
// As for the OrderByDescending HasValue calls: https://www.jerriepelser.com/blog/orderby-with-null-values/
|
|
|
|
// We want nulls last no matter what, even if orders are reversed
|
|
|
|
SortProperty.Hid => input.OrderBy(m => m.Hid, ReverseMaybe(culture)),
|
|
|
|
SortProperty.Name => input.OrderBy(m => m.NameFor(ctx), ReverseMaybe(culture)),
|
2022-01-15 04:16:10 +00:00
|
|
|
SortProperty.CreationDate => input
|
|
|
|
.OrderByDescending(m => m.MetadataPrivacy.CanAccess(ctx))
|
|
|
|
.ThenBy(m => m.MetadataPrivacy.Get(ctx, m.Created, default), ReverseMaybe(Comparer<Instant>.Default)),
|
|
|
|
SortProperty.MessageCount => input
|
|
|
|
.OrderByDescending(m => m.MessageCount != 0 && m.MetadataPrivacy.CanAccess(ctx))
|
|
|
|
.ThenByDescending(m => m.MetadataPrivacy.Get(ctx, m.MessageCount, 0), ReverseMaybe(Comparer<int>.Default)),
|
2021-11-27 02:10:56 +00:00
|
|
|
SortProperty.DisplayName => input
|
2022-01-15 04:16:10 +00:00
|
|
|
.OrderByDescending(m => m.DisplayName != null && m.NamePrivacy.CanAccess(ctx))
|
|
|
|
.ThenBy(m => m.NamePrivacy.Get(ctx, m.DisplayName), ReverseMaybe(culture)),
|
2021-11-27 02:10:56 +00:00
|
|
|
SortProperty.Birthdate => input
|
2022-01-15 04:16:10 +00:00
|
|
|
.OrderByDescending(m => m.AnnualBirthday.HasValue && m.BirthdayPrivacy.CanAccess(ctx))
|
|
|
|
.ThenBy(m => m.BirthdayPrivacy.Get(ctx, m.AnnualBirthday), ReverseMaybe(Comparer<AnnualDate?>.Default)),
|
2021-11-27 02:10:56 +00:00
|
|
|
SortProperty.LastMessage => throw new PKError(
|
|
|
|
"Sorting by last message is temporarily disabled due to database issues, sorry."),
|
|
|
|
// SortProperty.LastMessage => input
|
|
|
|
// .OrderByDescending(m => m.LastMessage.HasValue)
|
|
|
|
// .ThenByDescending(m => m.LastMessage, ReverseMaybe(Comparer<ulong?>.Default)),
|
|
|
|
SortProperty.LastSwitch => input
|
2022-01-15 04:16:10 +00:00
|
|
|
.OrderByDescending(m => m.LastSwitchTime.HasValue && m.MetadataPrivacy.CanAccess(ctx))
|
|
|
|
.ThenByDescending(m => m.MetadataPrivacy.Get(ctx, m.LastSwitchTime), ReverseMaybe(Comparer<Instant?>.Default)),
|
2021-11-27 02:10:56 +00:00
|
|
|
SortProperty.Random => input
|
|
|
|
.OrderBy(m => randGen.Next()),
|
|
|
|
_ => throw new ArgumentOutOfRangeException($"Unknown sort property {opts.SortProperty}")
|
|
|
|
})
|
|
|
|
// Lastly, add a by-name fallback order for collisions (generally hits w/ lots of null values)
|
|
|
|
.ThenBy(m => m.NameFor(ctx), culture);
|
2020-07-07 18:57:22 +00:00
|
|
|
}
|
2022-01-15 03:30:02 +00:00
|
|
|
|
|
|
|
public static IEnumerable<ListedGroup> SortByGroupListOptions(this IEnumerable<ListedGroup> input,
|
|
|
|
ListOptions opts, LookupContext ctx)
|
|
|
|
{
|
|
|
|
IComparer<T> ReverseMaybe<T>(IComparer<T> c) =>
|
|
|
|
opts.Reverse ? Comparer<T>.Create((a, b) => c.Compare(b, a)) : c;
|
|
|
|
|
|
|
|
var randGen = new global::System.Random();
|
|
|
|
|
|
|
|
var culture = StringComparer.InvariantCultureIgnoreCase;
|
|
|
|
return (opts.SortProperty switch
|
|
|
|
{
|
|
|
|
// As for the OrderByDescending HasValue calls: https://www.jerriepelser.com/blog/orderby-with-null-values/
|
|
|
|
// We want nulls last no matter what, even if orders are reversed
|
|
|
|
SortProperty.Hid => input.OrderBy(g => g.Hid, ReverseMaybe(culture)),
|
|
|
|
SortProperty.Name => input.OrderBy(g => g.NameFor(ctx), ReverseMaybe(culture)),
|
2022-01-15 04:16:10 +00:00
|
|
|
SortProperty.CreationDate => input
|
|
|
|
.OrderByDescending(g => g.MetadataPrivacy.CanAccess(ctx))
|
|
|
|
.ThenBy(g => g.MetadataPrivacy.Get(ctx, g.Created, default), ReverseMaybe(Comparer<Instant>.Default)),
|
2022-01-15 03:30:02 +00:00
|
|
|
SortProperty.DisplayName => input
|
2022-01-15 04:16:10 +00:00
|
|
|
.OrderByDescending(g => g.DisplayName != null && g.NamePrivacy.CanAccess(ctx))
|
|
|
|
.ThenBy(g => g.NamePrivacy.Get(ctx, g.DisplayName), ReverseMaybe(culture)),
|
2022-01-15 03:30:02 +00:00
|
|
|
SortProperty.Random => input
|
|
|
|
.OrderBy(g => randGen.Next()),
|
|
|
|
_ => throw new ArgumentOutOfRangeException($"Unknown sort property {opts.SortProperty}")
|
|
|
|
})
|
|
|
|
// Lastly, add a by-name fallback order for collisions (generally hits w/ lots of null values)
|
2022-01-15 04:16:10 +00:00
|
|
|
.ThenBy(g => g.NameFor(ctx), culture);
|
2022-01-15 03:30:02 +00:00
|
|
|
}
|
2022-09-22 17:17:53 +00:00
|
|
|
|
|
|
|
public static void AssertIsValid(this ListOptions opts)
|
|
|
|
{
|
2022-09-22 17:59:56 +00:00
|
|
|
if (opts.Type == ListType.Short && opts.includedCount > 1)
|
2022-09-22 19:17:43 +00:00
|
|
|
throw new PKError("The short list does not support showing information from multiple flags. Try using the full list instead.");
|
2022-09-22 17:17:53 +00:00
|
|
|
|
|
|
|
// the check for multiple *sorting* property flags is done in SortProperty setter
|
|
|
|
}
|
2021-11-27 02:10:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public enum SortProperty
|
|
|
|
{
|
|
|
|
Name,
|
|
|
|
DisplayName,
|
|
|
|
Hid,
|
|
|
|
MessageCount,
|
|
|
|
CreationDate,
|
|
|
|
LastSwitch,
|
|
|
|
LastMessage,
|
|
|
|
Birthdate,
|
|
|
|
Random
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum ListType { Short, Long }
|