Exemple #1
0
    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));
    }
Exemple #2
0
    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));
    }