Exemplo n.º 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);
        }