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(); var existingMembersInGroup = (await ctx.Database.Execute(conn => conn.QueryMemberList(target.System, new DatabaseViewsExt.ListQueryOptions { GroupFilter = target.Id }))) .Select(m => m.Id.Value) .Distinct() .ToHashSet(); List <MemberId> toAction; if (op == Groups.AddRemoveOperation.Add) { toAction = members .Where(m => !existingMembersInGroup.Contains(m.Value)) .ToList(); await ctx.Repository.AddMembersToGroup(target.Id, toAction); } else if (op == Groups.AddRemoveOperation.Remove) { toAction = members .Where(m => existingMembersInGroup.Contains(m.Value)) .ToList(); await ctx.Repository.RemoveMembersFromGroup(target.Id, toAction); } 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 AddRemove(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(); await using var conn = await _db.Obtain(); var existingGroups = (await _repo.GetMemberGroups(conn, target.Id).ToListAsync()) .Select(g => g.Id) .Distinct() .ToList(); List <GroupId> toAction; if (op == Groups.AddRemoveOperation.Add) { toAction = groups .Where(group => !existingGroups.Contains(group)) .ToList(); await _repo.AddGroupsToMember(conn, target.Id, toAction); } else if (op == Groups.AddRemoveOperation.Remove) { toAction = groups .Where(group => existingGroups.Contains(group)) .ToList(); await _repo.RemoveGroupsFromMember(conn, target.Id, toAction); } else { return; // otherwise toAction "may be unassigned" } await ctx.Reply(MiscUtils.GroupAddRemoveResponse <GroupId>(groups, toAction, op)); }
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(); var existingGroups = (await ctx.Repository.GetMemberGroups(target.Id).ToListAsync()) .Select(g => g.Id) .Distinct() .ToList(); List <GroupId> toAction; if (op == Groups.AddRemoveOperation.Add) { toAction = groups .Where(group => !existingGroups.Contains(group)) .ToList(); await ctx.Repository.AddGroupsToMember(target.Id, toAction); } else if (op == Groups.AddRemoveOperation.Remove) { toAction = groups .Where(group => existingGroups.Contains(group)) .ToList(); await ctx.Repository.RemoveGroupsFromMember(target.Id, toAction); } else { return; // otherwise toAction "may be unassigned" } await ctx.Reply(GroupMemberUtils.GenerateResponse(op, 1, groups.Count, toAction.Count, groups.Count - toAction.Count)); }
public static string GenerateResponse(Groups.AddRemoveOperation action, int memberCount, int groupCount, int actionedOn, int notActionedOn) { var op = action; var actionStr = action == Groups.AddRemoveOperation.Add ? "added to" : "removed from"; var containStr = action == Groups.AddRemoveOperation.Add ? "in" : "not in"; var emojiStr = actionedOn > 0 ? Emojis.Success : Emojis.Error; var memberPlural = memberCount > 1; var groupPlural = groupCount > 1; // sanity checking: we can't add multiple groups to multiple members (at least for now) if (memberPlural && groupPlural) { throw new ArgumentOutOfRangeException(); } // sanity checking: we can't act/not act on a different number of entities than we have if (memberPlural && actionedOn + notActionedOn != memberCount) { throw new ArgumentOutOfRangeException(); } if (groupPlural && actionedOn + notActionedOn != groupCount) { throw new ArgumentOutOfRangeException(); } // name generators string MemberString(int count, bool capitalize = false) => capitalize ? count == 1 ? "Member" : "Members" : count == 1 ? "member" : "members"; string GroupString(int count) => count == 1 ? "group" : "groups"; // string generators string ResponseString() { if (actionedOn > 0 && notActionedOn > 0 && memberPlural) { return($"{actionedOn} {MemberString(actionedOn)} {actionStr} {GroupString(groupCount)}"); } if (actionedOn > 0 && notActionedOn > 0 && groupPlural) { return($"{MemberString(memberCount, true)} {actionStr} {actionedOn} {GroupString(actionedOn)}"); } if (notActionedOn == 0) { return($"{MemberString(memberCount, true)} {actionStr} {GroupString(groupCount)}"); } if (actionedOn == 0) { return($"{MemberString(memberCount, true)} not {actionStr} {GroupString(groupCount)}"); } throw new ArgumentOutOfRangeException(); } string InfoMessage() { if (notActionedOn == 0) { return(""); } var msg = ""; if (actionedOn > 0 && memberPlural) { msg += $"{notActionedOn} {MemberString(notActionedOn)}"; } else { msg += $"{MemberString(memberCount)}"; } msg += $" already {containStr}"; if (actionedOn > 0 && groupPlural) { msg += $" {notActionedOn} {GroupString(notActionedOn)}"; } else { msg += $" {GroupString(groupCount)}"; } return($" ({msg})"); } return($"{emojiStr} {ResponseString()}{InfoMessage()}."); }
public static String GroupAddRemoveResponse <T>(List <T> entityList, List <T> actionedOn, Groups.AddRemoveOperation op) { var opStr = op == Groups.AddRemoveOperation.Add ? "added to" : "removed from"; var inStr = op == Groups.AddRemoveOperation.Add ? "in" : "not in"; var notActionedOn = entityList.Count - actionedOn.Count; var groupNotActionedPosStr = typeof(T) == typeof(GroupId) ? notActionedOn.ToString() + " " : ""; var memberNotActionedPosStr = typeof(T) == typeof(MemberId) ? notActionedOn.ToString() + " " : ""; if (actionedOn.Count == 0) { return($"{Emojis.Error} {entityTerm<T>(notActionedOn, true)} not {opStr} {entityTerm<T>(entityList.Count, false).ToLower()} ({entityTerm<T>(notActionedOn, true).ToLower()} already {inStr} {entityTerm<T>(entityList.Count, false).ToLower()})."); } else if (notActionedOn == 0) { return($"{Emojis.Success} {entityTerm<T>(actionedOn.Count, true)} {opStr} {entityTerm<T>(actionedOn.Count, false).ToLower()}."); } else { return($"{Emojis.Success} {entityTerm<T>(actionedOn.Count, true)} {opStr} {actionedOn.Count} {entityTerm<T>(actionedOn.Count, false).ToLower()} ({memberNotActionedPosStr}{entityTerm<T>(actionedOn.Count, true).ToLower()} already {inStr} {groupNotActionedPosStr}{entityTerm<T>(notActionedOn, false).ToLower()})."); } }