예제 #1
0
        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));
        }