public async Task <Response> CreateResponseAsync() { List <ChannelUserVm> resultChannelsUsers = new List <ChannelUserVm>(); List <BlockSegmentVm> segments = new List <BlockSegmentVm>(); foreach (long channelId in request.ChannelsId) { try { ChannelVm channel = await loadChannelsService.GetChannelByIdAsync(channelId).ConfigureAwait(false); var existingUsers = await loadChannelsService.GetChannelUsersAsync(channelId, null, null).ConfigureAwait(false); if (!existingUsers.Any(opt => opt.ChannelUserRole == ChannelUserRole.Creator)) { var nodeConnection = connectionsService.GetNodeConnection(channel.NodesId.FirstOrDefault(id => id != NodeSettings.Configs.Node.Id)); if (nodeConnection != null) { await nodeRequestSender.GetChannelInformationAsync(channelId, nodeConnection).ConfigureAwait(false); } } List <ChannelUserVm> channelUsers = await updateChannelsService.AddUsersToChannelAsync(request.UsersId.ToList(), channelId, clientConnection.UserId.GetValueOrDefault()).ConfigureAwait(false); resultChannelsUsers.AddRange(channelUsers); BlockSegmentVm segment = await BlockSegmentsService.Instance.CreateChannelUsersSegmentAsync( channelUsers, NodeSettings.Configs.Node.Id, channelId, NodeData.Instance.NodeKeys.SignPrivateKey, NodeData.Instance.NodeKeys.SymmetricKey, NodeData.Instance.NodeKeys.Password, NodeData.Instance.NodeKeys.KeyId).ConfigureAwait(false); segments.Add(segment); conversationsNoticeService.SendNewChannelNoticesAsync(channelUsers, channelId, clientConnection); nodeNoticeService.SendChannelUsersNodeNoticeAsync(channelUsers, clientConnection.UserId.Value, channel); } catch (Exception ex) { Logger.WriteLog(ex, request); } } BlockGenerationHelper.Instance.AddSegments(segments); return(new ChannelUsersResponse(request.RequestId, null, resultChannelsUsers, null)); }
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)); } }