コード例 #1
0
        public async Task <Response> CreateResponseAsync()
        {
            try
            {
                List <ChannelUserVm> editedChannelUsers = await updateChannelsService.EditChannelUsersAsync(
                    request.Users, clientConnection.UserId.GetValueOrDefault(), request.ChannelId).ConfigureAwait(false);

                ChannelVm channel = await loadChannelsService.GetChannelByIdAsync(request.ChannelId).ConfigureAwait(false);

                nodeNoticeService.SendChannelUsersNodeNoticeAsync(editedChannelUsers, clientConnection.UserId.GetValueOrDefault(), channel);
                UsersConversationsCacheService.Instance.UpdateUsersChannelsAsync(editedChannelUsers.Select(opt => opt.UserId));
                BlockSegmentVm segment = await BlockSegmentsService.Instance.CreateChannelUsersSegmentAsync(
                    editedChannelUsers,
                    NodeSettings.Configs.Node.Id,
                    request.ChannelId,
                    NodeData.Instance.NodeKeys.SignPrivateKey,
                    NodeData.Instance.NodeKeys.SymmetricKey,
                    NodeData.Instance.NodeKeys.Password,
                    NodeData.Instance.NodeKeys.KeyId).ConfigureAwait(false);

                nodeNoticeService.SendBlockSegmentsNodeNoticeAsync(new List <BlockSegmentVm> {
                    segment
                });
                BlockGenerationHelper.Instance.AddSegment(segment);
                return(new ChannelUsersResponse(
                           request.RequestId,
                           editedChannelUsers.Where(opt => opt.ChannelUserRole.GetValueOrDefault() >= ChannelUserRole.Administrator && opt.Banned == false),
                           editedChannelUsers.Where(opt => opt.ChannelUserRole.GetValueOrDefault() == ChannelUserRole.Subscriber && opt.Banned == false),
                           editedChannelUsers.Where(opt => opt.Banned == true)));
            }
            catch (PermissionDeniedException ex)
            {
                Logger.WriteLog(ex);
                return(new ResultResponse(request.RequestId, "Channel not found or user does not have access to the channel.", ErrorCode.PermissionDenied));
            }
        }
コード例 #2
0
        public async Task <List <ChannelUserVm> > CreateOrEditChannelUsersAsync(List <ChannelUserVm> channelUsers, long requestorId)
        {
            try
            {
                using (MessengerDbContext context = contextFactory.Create())
                {
                    var channelUsersCondition = PredicateBuilder.New <ChannelUser>();
                    channelUsersCondition = channelUsers.Aggregate(channelUsersCondition,
                                                                   (current, value) => current.Or(opt => opt.ChannelId == value.ChannelId && opt.UserId == value.UserId).Expand());
                    List <ChannelUserVm> result = new List <ChannelUserVm>();
                    var existingChannelsUsers   = ChannelConverter.GetChannelUsers(await context.ChannelUsers.Where(channelUsersCondition).ToListAsync().ConfigureAwait(false));
                    List <ChannelUserVm> nonExistingChannelUsers = new List <ChannelUserVm>();
                    if (existingChannelsUsers?.Any() ?? false)
                    {
                        var groups = existingChannelsUsers.GroupBy(opt => opt.ChannelId);
                        foreach (var group in groups)
                        {
                            var edited = channelUsers.Where(opt => group.Any(p => p.ChannelId == opt.ChannelId && p.UserId == opt.UserId)).ToList();
                            result.AddRange(await updateChannelsService.EditChannelUsersAsync(edited, requestorId, group.Key.GetValueOrDefault()).ConfigureAwait(false));
                        }
                        nonExistingChannelUsers = channelUsers.Where(opt => !existingChannelsUsers.Any(p => p.ChannelId == opt.ChannelId && p.UserId == opt.UserId)).ToList();
                    }
                    else
                    {
                        nonExistingChannelUsers = channelUsers;
                    }
                    if (nonExistingChannelUsers?.Any() ?? false)
                    {
                        var newChannelUsers = ChannelConverter.GetChannelUsers(nonExistingChannelUsers).ToList();
                        await context.AddRangeAsync(newChannelUsers).ConfigureAwait(false);

                        result.AddRange(ChannelConverter.GetChannelUsers(newChannelUsers));
                    }
                    var channelGroups = channelUsers.GroupBy(opt => opt.ChannelId);
                    foreach (var group in channelGroups)
                    {
                        var channelsNodesIds = await context.ChannelUsers
                                               .Where(opt => opt.ChannelId == group.Key)
                                               .Include(opt => opt.User)
                                               .Select(opt => opt.User.NodeId)
                                               .ToArrayAsync()
                                               .ConfigureAwait(false);

                        var channel = await context.Channels.FindAsync(group.Key).ConfigureAwait(false);

                        channel.NodesId = channelsNodesIds.Select(id => id.GetValueOrDefault()).Distinct().ToArray();
                        context.Channels.Update(channel);
                    }
                    await context.SaveChangesAsync().ConfigureAwait(false);

                    return(result);
                }
            }
            catch (DbUpdateException ex)
            {
                if (ex.InnerException is PostgresException postgresException)
                {
                    if (postgresException.ConstraintName == "FK_ChannelUsers_Channels_ChannelId")
                    {
                        throw new ConversationNotFoundException();
                    }

                    if (postgresException.ConstraintName == "FK_ChannelUsers_Users_UserId")
                    {
                        throw new UserNotFoundException();
                    }
                }
                throw new AddOrRemoveChannelUsersException();
            }
        }