2020-02-13 15:35:50 +00:00
|
|
|
using System.Collections.Generic;
|
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;
|
|
|
|
|
2020-06-04 11:21:47 +00:00
|
|
|
using Dapper;
|
2020-02-01 12:03:02 +00:00
|
|
|
|
2020-06-06 23:30:19 +00:00
|
|
|
using NodaTime;
|
|
|
|
|
2020-02-12 14:16:19 +00:00
|
|
|
using PluralKit.Core;
|
2020-02-01 12:03:02 +00:00
|
|
|
|
2020-06-06 23:30:19 +00:00
|
|
|
using Serilog;
|
|
|
|
|
2020-02-12 14:16:19 +00:00
|
|
|
namespace PluralKit.Bot
|
2020-02-01 12:03:02 +00:00
|
|
|
{
|
|
|
|
public class SystemList
|
|
|
|
{
|
2020-06-06 23:30:19 +00:00
|
|
|
private readonly IClock _clock;
|
2020-06-04 11:21:47 +00:00
|
|
|
private readonly DbConnectionFactory _db;
|
2020-06-06 23:30:19 +00:00
|
|
|
private readonly ILogger _logger;
|
2020-06-04 11:21:47 +00:00
|
|
|
|
2020-06-06 23:30:19 +00:00
|
|
|
public SystemList(DbConnectionFactory db, ILogger logger, IClock clock)
|
2020-02-01 12:03:02 +00:00
|
|
|
{
|
2020-06-04 11:21:47 +00:00
|
|
|
_db = db;
|
2020-06-06 23:30:19 +00:00
|
|
|
_logger = logger;
|
|
|
|
_clock = clock;
|
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
|
|
|
|
2020-06-04 11:21:47 +00:00
|
|
|
// GetRendererFor must be called before GetOptions as it consumes a potential positional full argument that'd otherwise land in the filter
|
|
|
|
var renderer = GetRendererFor(ctx);
|
|
|
|
var opts = GetOptions(ctx, target);
|
|
|
|
var members = await GetMemberList(target, opts);
|
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) =>
|
|
|
|
{
|
2020-06-06 23:30:19 +00:00
|
|
|
eb.WithFooter($"{opts.CreateFilterString()}. {members.Count} results.");
|
|
|
|
renderer.RenderPage(eb, ctx.System, ms);
|
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 async Task<IReadOnlyList<PKListMember>> GetMemberList(PKSystem target, SortFilterOptions opts)
|
2020-02-13 15:35:50 +00:00
|
|
|
{
|
2020-06-12 18:29:50 +00:00
|
|
|
await using var conn = await _db.Obtain();
|
2020-06-06 23:30:19 +00:00
|
|
|
var query = opts.BuildQuery();
|
2020-06-04 11:21:47 +00:00
|
|
|
var args = new {System = target.Id, opts.Filter};
|
2020-06-12 18:29:50 +00:00
|
|
|
_logger.Debug("Executing sort/filter query `{Query}` with arguments {Args}", query, args);
|
|
|
|
|
2020-06-06 23:30:19 +00:00
|
|
|
var timeBefore = _clock.GetCurrentInstant();
|
|
|
|
var results = (await conn.QueryAsync<PKListMember>(query, args)).ToList();
|
|
|
|
var timeAfter = _clock.GetCurrentInstant();
|
2020-06-12 18:29:50 +00:00
|
|
|
|
|
|
|
_logger.Debug("Executed sort/filter query `{Query}` returning {ResultCount} results in {QueryTime}", query, results.Count, timeAfter - timeBefore);
|
2020-06-06 23:30:19 +00:00
|
|
|
|
|
|
|
return results;
|
2020-02-13 15:35:50 +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.SanitizeMentions()} (`{target.Hid}`)");
|
|
|
|
else title.Append($"`{target.Hid}`");
|
|
|
|
|
2020-06-06 23:30:19 +00:00
|
|
|
if (opts.Filter != null) title.Append($" matching **{opts.Filter.SanitizeMentions()}**");
|
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
|
|
|
|
2020-06-04 11:21:47 +00:00
|
|
|
private IListRenderer GetRendererFor(Context ctx)
|
2020-02-13 15:49:45 +00:00
|
|
|
{
|
2020-06-04 11:21:47 +00:00
|
|
|
var longList = ctx.Match("f", "full", "big", "details", "long") || ctx.MatchFlag("f", "full");
|
|
|
|
if (longList)
|
|
|
|
return new LongRenderer(LongRenderer.MemberFields.FromFlags(ctx));
|
|
|
|
return new ShortRenderer();
|
2020-02-13 15:49:45 +00:00
|
|
|
}
|
2020-02-01 12:03:02 +00:00
|
|
|
}
|
|
|
|
}
|