Beispiel #1
0
        private async Task HandleChannelMessagesAsync(IEnumerable <MessageVm> messages)
        {
            foreach (var message in messages)
            {
                bool       hasException = true;
                MessageDto sentMessage  = null;
                while (hasException)
                {
                    try
                    {
                        if (NodeData.Instance.RoutedMessagesId.Contains(message.GlobalId.GetValueOrDefault()))
                        {
                            hasException = false;
                            continue;
                        }
                        if (message.Attachments != null)
                        {
                            foreach (var attachment in message.Attachments)
                            {
                                attachment.MessageId = 0;
                            }
                            await attachmentsService.DownloadAttachmentsPayloadAsync(message.Attachments, current).ConfigureAwait(false);

                            await attachmentsService.ThrowIfAttachmentsInvalidAsync(message, true).ConfigureAwait(false);
                        }
                        sentMessage = await createMessagesService.CreateChannelMessageAsync(MessageConverter.GetMessageDto(message)).ConfigureAwait(false);

                        hasException = false;
                    }
                    catch (ConversationNotFoundException ex)
                    {
                        var channel = await nodeRequestSender.GetChannelInformationAsync(ex.ConversationId, current).ConfigureAwait(false);

                        await createChannelsService.CreateOrUpdateUserChannelsAsync(new List <ChannelDto> {
                            channel
                        }).ConfigureAwait(false);

                        hasException = true;
                    }
                    catch (UserNotFoundException ex)
                    {
                        List <UserVm> users = new List <UserVm>(await nodeRequestSender.GetUsersInfoAsync(ex.UsersId.ToList(), null, current).ConfigureAwait(false));
                        await crossNodeService.CreateNewUsersAsync(users).ConfigureAwait(false);

                        hasException = true;
                    }
                    catch (Exception ex)
                    {
                        Logger.WriteLog(ex);
                        hasException = false;
                    }
                }
                SendChannelNotificationsAsync(sentMessage);
            }
        }
        public async Task HandleAsync()
        {
            bool hasException = true;
            List <ChannelUserVm> channelUsers = null;

            while (hasException)
            {
                try
                {
                    channelUsers = await createChannelsService.CreateOrEditChannelUsersAsync(notice.ChannelUsers, notice.RequestorId).ConfigureAwait(false);

                    hasException = false;
                }
                catch (UserNotFoundException)
                {
                    List <long>   usersId = notice.ChannelUsers.Select(opt => opt.UserId).Append(notice.RequestorId).ToList();
                    List <UserVm> users   = await nodeRequestSender.GetUsersInfoAsync(usersId, null, current).ConfigureAwait(false);

                    await crossNodeService.CreateNewUsersAsync(users).ConfigureAwait(false);

                    hasException = true;
                }
                catch (ConversationNotFoundException)
                {
                    ChannelDto channel = await nodeRequestSender.GetChannelInformationAsync(notice.ChannelId, current).ConfigureAwait(false);

                    await createChannelsService.CreateOrUpdateUserChannelsAsync(new List <ChannelDto> {
                        channel
                    }).ConfigureAwait(false);

                    hasException = true;
                }
                catch (Exception ex)
                {
                    Logger.WriteLog(ex);
                    hasException = false;
                }
            }
            BlockSegmentVm segment = await BlockSegmentsService.Instance.CreateChannelUsersSegmentAsync(
                channelUsers,
                current.Node.Id,
                notice.ChannelId,
                NodeData.Instance.NodeKeys.SignPrivateKey,
                NodeData.Instance.NodeKeys.SymmetricKey,
                NodeData.Instance.NodeKeys.Password,
                NodeData.Instance.NodeKeys.KeyId).ConfigureAwait(false);

            BlockGenerationHelper.Instance.AddSegment(segment);
        }
        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));
        }