PluralKit/PluralKit.Bot/Commands/SystemList.cs

73 lines
2.7 KiB
C#
Raw Normal View History

2020-02-01 12:03:02 +00:00
using System.Linq;
2020-06-04 11:21:47 +00:00
using System.Text;
2020-02-01 12:03:02 +00:00
using System.Threading.Tasks;
using NodaTime;
using PluralKit.Core;
2020-02-01 12:03:02 +00:00
namespace PluralKit.Bot
2020-02-01 12:03:02 +00:00
{
public class SystemList
{
2020-06-13 17:36:43 +00:00
private readonly IDatabase _db;
2020-06-04 11:21:47 +00:00
public SystemList(IDatabase db)
2020-02-01 12:03:02 +00:00
{
2020-06-04 11:21:47 +00:00
_db = db;
2020-02-01 12:03:02 +00:00
}
2020-06-04 11:21:47 +00:00
public async Task MemberList(Context ctx, PKSystem target)
2020-02-13 15:35:50 +00:00
{
2020-06-04 11:21:47 +00:00
if (target == null) throw Errors.NoSystemError;
ctx.CheckSystemPrivacy(target, target.MemberListPrivacy);
2020-02-13 15:35:50 +00:00
// Must match full before calling the other flag parsers to make sure we consume the token before trying to match search terms, etc
var isFull = ctx.Match("f", "full", "big", "details", "long") || ctx.MatchFlag("f", "full");
2020-06-04 11:21:47 +00:00
var opts = GetOptions(ctx, target);
var renderer = GetRendererFor(ctx, isFull, opts);
var members = (await _db.Execute(c => opts.Execute(c, target, ctx.LookupContextFor(target)))).ToList();
2020-02-01 12:03:02 +00:00
await ctx.Paginate(
2020-06-04 11:21:47 +00:00
members.ToAsyncEnumerable(),
members.Count,
renderer.MembersPerPage,
GetEmbedTitle(target, opts),
2020-02-01 12:03:02 +00:00
(eb, ms) =>
{
eb.WithFooter($"{opts.CreateFilterString()}. {members.Count} results.");
renderer.RenderPage(eb, ctx.System?.Zone ?? DateTimeZone.Utc, ms, ctx.LookupContextFor(target));
2020-06-04 11:21:47 +00:00
return Task.CompletedTask;
2020-02-01 12:03:02 +00:00
});
}
2020-06-04 11:21:47 +00:00
private string GetEmbedTitle(PKSystem target, SortFilterOptions opts)
2020-02-13 15:35:50 +00:00
{
2020-06-04 11:21:47 +00:00
var title = new StringBuilder("Members of ");
if (target.Name != null) title.Append($"{target.Name} (`{target.Hid}`)");
2020-06-04 11:21:47 +00:00
else title.Append($"`{target.Hid}`");
if (opts.Filter != null) title.Append($" matching **{opts.Filter}**");
2020-06-04 11:21:47 +00:00
return title.ToString();
2020-02-13 15:35:50 +00:00
}
2020-02-01 12:03:02 +00:00
2020-06-04 11:21:47 +00:00
private SortFilterOptions GetOptions(Context ctx, PKSystem target)
2020-02-13 15:35:50 +00:00
{
2020-06-04 11:21:47 +00:00
var opts = SortFilterOptions.FromFlags(ctx);
opts.Filter = ctx.RemainderOrNull();
// If we're *explicitly* trying to access non-public members of another system, error
if (opts.PrivacyFilter != PrivacyFilter.PublicOnly && ctx.LookupContextFor(target) != LookupContext.ByOwner)
throw new PKError("You cannot look up private members of another system.");
return opts;
2020-02-01 12:03:02 +00:00
}
2020-02-13 15:49:45 +00:00
private IListRenderer GetRendererFor(Context ctx, bool isLongList, SortFilterOptions opts)
2020-02-13 15:49:45 +00:00
{
if (isLongList)
return new LongRenderer(LongRenderer.MemberFields.FromFlags(ctx, opts));
2020-06-04 11:21:47 +00:00
return new ShortRenderer();
2020-02-13 15:49:45 +00:00
}
2020-02-01 12:03:02 +00:00
}
}