2021-11-27 03:02:58 +00:00
|
|
|
using System.Text;
|
|
|
|
|
2022-01-23 06:47:55 +00:00
|
|
|
using Humanizer;
|
|
|
|
|
2021-11-27 03:02:58 +00:00
|
|
|
using Myriad.Builders;
|
|
|
|
|
|
|
|
using PluralKit.Core;
|
|
|
|
|
|
|
|
namespace PluralKit.Bot;
|
|
|
|
|
|
|
|
public class GroupMember
|
|
|
|
{
|
|
|
|
public async Task AddRemoveGroups(Context ctx, PKMember target, Groups.AddRemoveOperation op)
|
|
|
|
{
|
|
|
|
ctx.CheckSystem().CheckOwnMember(target);
|
|
|
|
|
|
|
|
var groups = (await ctx.ParseGroupList(ctx.System.Id))
|
|
|
|
.Select(g => g.Id)
|
|
|
|
.Distinct()
|
|
|
|
.ToList();
|
|
|
|
|
2022-01-22 08:05:01 +00:00
|
|
|
var existingGroups = (await ctx.Repository.GetMemberGroups(target.Id).ToListAsync())
|
2021-11-27 03:02:58 +00:00
|
|
|
.Select(g => g.Id)
|
|
|
|
.Distinct()
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
List<GroupId> toAction;
|
|
|
|
|
|
|
|
if (op == Groups.AddRemoveOperation.Add)
|
|
|
|
{
|
|
|
|
toAction = groups
|
|
|
|
.Where(group => !existingGroups.Contains(group))
|
|
|
|
.ToList();
|
|
|
|
|
2022-01-22 08:05:01 +00:00
|
|
|
await ctx.Repository.AddGroupsToMember(target.Id, toAction);
|
2021-11-27 03:02:58 +00:00
|
|
|
}
|
|
|
|
else if (op == Groups.AddRemoveOperation.Remove)
|
|
|
|
{
|
|
|
|
toAction = groups
|
|
|
|
.Where(group => existingGroups.Contains(group))
|
|
|
|
.ToList();
|
|
|
|
|
2022-01-22 08:05:01 +00:00
|
|
|
await ctx.Repository.RemoveGroupsFromMember(target.Id, toAction);
|
2021-11-27 03:02:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return; // otherwise toAction "may be unassigned"
|
|
|
|
}
|
|
|
|
|
|
|
|
await ctx.Reply(GroupMemberUtils.GenerateResponse(op, 1, groups.Count, toAction.Count,
|
|
|
|
groups.Count - toAction.Count));
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task ListMemberGroups(Context ctx, PKMember target)
|
|
|
|
{
|
2021-12-24 04:11:55 +00:00
|
|
|
var pctx = ctx.DirectLookupContextFor(target.System);
|
2021-11-27 03:02:58 +00:00
|
|
|
|
2022-01-22 08:05:01 +00:00
|
|
|
var groups = await ctx.Repository.GetMemberGroups(target.Id)
|
2021-11-27 03:02:58 +00:00
|
|
|
.Where(g => g.Visibility.CanAccess(pctx))
|
2022-01-15 03:30:02 +00:00
|
|
|
.OrderBy(g => (g.DisplayName ?? g.Name), StringComparer.InvariantCultureIgnoreCase)
|
2021-11-27 03:02:58 +00:00
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
|
var description = "";
|
|
|
|
var msg = "";
|
|
|
|
|
|
|
|
if (groups.Count == 0)
|
|
|
|
description = "This member has no groups.";
|
|
|
|
else
|
|
|
|
description = string.Join("\n", groups.Select(g => $"[`{g.Hid}`] **{g.DisplayName ?? g.Name}**"));
|
|
|
|
|
|
|
|
if (pctx == LookupContext.ByOwner)
|
|
|
|
{
|
|
|
|
msg +=
|
2022-02-05 14:26:14 +00:00
|
|
|
$"\n\nTo add this member to one or more groups, use `pk;m {target.Reference(ctx)} group add <group> [group 2] [group 3...]`";
|
2021-11-27 03:02:58 +00:00
|
|
|
if (groups.Count > 0)
|
|
|
|
msg +=
|
2022-02-05 14:26:14 +00:00
|
|
|
$"\nTo remove this member from one or more groups, use `pk;m {target.Reference(ctx)} group remove <group> [group 2] [group 3...]`";
|
2021-11-27 03:02:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await ctx.Reply(msg, new EmbedBuilder().Title($"{target.Name}'s groups").Description(description).Build());
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task AddRemoveMembers(Context ctx, PKGroup target, Groups.AddRemoveOperation op)
|
|
|
|
{
|
|
|
|
ctx.CheckOwnGroup(target);
|
|
|
|
|
|
|
|
var members = (await ctx.ParseMemberList(ctx.System.Id))
|
|
|
|
.Select(m => m.Id)
|
|
|
|
.Distinct()
|
|
|
|
.ToList();
|
|
|
|
|
2022-01-22 08:05:01 +00:00
|
|
|
var existingMembersInGroup = (await ctx.Database.Execute(conn => conn.QueryMemberList(target.System,
|
2022-01-15 03:30:02 +00:00
|
|
|
new DatabaseViewsExt.ListQueryOptions { GroupFilter = target.Id })))
|
2021-11-27 03:02:58 +00:00
|
|
|
.Select(m => m.Id.Value)
|
|
|
|
.Distinct()
|
|
|
|
.ToHashSet();
|
|
|
|
|
|
|
|
List<MemberId> toAction;
|
|
|
|
|
|
|
|
if (op == Groups.AddRemoveOperation.Add)
|
|
|
|
{
|
|
|
|
toAction = members
|
|
|
|
.Where(m => !existingMembersInGroup.Contains(m.Value))
|
|
|
|
.ToList();
|
2022-01-22 08:05:01 +00:00
|
|
|
await ctx.Repository.AddMembersToGroup(target.Id, toAction);
|
2021-11-27 03:02:58 +00:00
|
|
|
}
|
|
|
|
else if (op == Groups.AddRemoveOperation.Remove)
|
|
|
|
{
|
|
|
|
toAction = members
|
|
|
|
.Where(m => existingMembersInGroup.Contains(m.Value))
|
|
|
|
.ToList();
|
2022-01-22 08:05:01 +00:00
|
|
|
await ctx.Repository.RemoveMembersFromGroup(target.Id, toAction);
|
2021-11-27 03:02:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return; // otherwise toAction "may be undefined"
|
|
|
|
}
|
|
|
|
|
|
|
|
await ctx.Reply(GroupMemberUtils.GenerateResponse(op, members.Count, 1, toAction.Count,
|
|
|
|
members.Count - toAction.Count));
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task ListGroupMembers(Context ctx, PKGroup target)
|
|
|
|
{
|
2021-12-07 06:32:29 +00:00
|
|
|
// see global system list for explanation of how privacy settings are used here
|
|
|
|
|
2021-11-27 03:02:58 +00:00
|
|
|
var targetSystem = await GetGroupSystem(ctx, target);
|
2021-12-07 06:32:29 +00:00
|
|
|
ctx.CheckSystemPrivacy(targetSystem.Id, target.ListPrivacy);
|
2021-11-27 03:02:58 +00:00
|
|
|
|
2022-01-15 03:30:02 +00:00
|
|
|
var opts = ctx.ParseListOptions(ctx.DirectLookupContextFor(target.System));
|
2021-11-27 03:02:58 +00:00
|
|
|
opts.GroupFilter = target.Id;
|
|
|
|
|
|
|
|
var title = new StringBuilder($"Members of {target.DisplayName ?? target.Name} (`{target.Hid}`) in ");
|
|
|
|
if (targetSystem.Name != null)
|
|
|
|
title.Append($"{targetSystem.Name} (`{targetSystem.Hid}`)");
|
|
|
|
else
|
|
|
|
title.Append($"`{targetSystem.Hid}`");
|
|
|
|
if (opts.Search != null)
|
2022-01-23 06:47:55 +00:00
|
|
|
title.Append($" matching **{opts.Search.Truncate(100)}**");
|
2021-11-27 03:02:58 +00:00
|
|
|
|
|
|
|
await ctx.RenderMemberList(ctx.LookupContextFor(target.System), target.System, title.ToString(),
|
|
|
|
target.Color, opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<PKSystem> GetGroupSystem(Context ctx, PKGroup target)
|
|
|
|
{
|
|
|
|
var system = ctx.System;
|
|
|
|
if (system?.Id == target.System)
|
|
|
|
return system;
|
2022-01-22 08:05:01 +00:00
|
|
|
return await ctx.Repository.GetSystem(target.System)!;
|
2021-11-27 03:02:58 +00:00
|
|
|
}
|
|
|
|
}
|