예제 #1
0
        public async Task AddUsersToChat()
        {
            var chat       = fillTestDbHelper.Chats.FirstOrDefault();
            var users      = fillTestDbHelper.Users.TakeLast(2);
            var resultChat = await updateChatsService.AddUsersToChatAsync(
                users.Select(opt => opt.Id),
                chat.Id,
                chat.ChatUsers.FirstOrDefault(opt => opt.UserRole >= UserRole.Admin).UserId);

            Assert.Equal(users.Select(opt => opt.Id), resultChat.Users.Select(opt => opt.UserId));
        }
        public async Task HandleAsync()
        {
            ChatVm chat;

            try
            {
                chat = await updateChatsService.AddUsersToChatAsync(notice.UsersId, notice.ChatId, notice.RequestorId).ConfigureAwait(false);
            }
            catch (AddUserChatException)
            {
                chat = await nodeRequestSender.GetFullChatInformationAsync(notice.ChatId, node).ConfigureAwait(false);

                await crossNodeService.NewOrEditChatAsync(chat).ConfigureAwait(false);
            }
            catch (ConversationNotFoundException ex)
            {
                chat = await nodeRequestSender.GetFullChatInformationAsync(ex.ConversationId, node).ConfigureAwait(false);

                await crossNodeService.NewOrEditChatAsync(chat).ConfigureAwait(false);

                UsersConversationsCacheService.Instance.UpdateUsersChatsAsync(notice.UsersId);
            }
            foreach (var userId in notice.UsersId)
            {
                var message = await systemMessagesService.CreateMessageAsync(ConversationType.Chat, chat.Id.Value, SystemMessageInfoFactory.CreateUserAddedMessageInfo(userId));

                conversationsNoticeService.SendSystemMessageNoticeAsync(message);
            }
            conversationsNoticeService.SendNewUsersAddedToChatNoticeAsync(chat, null);
            BlockSegmentVm segment = await BlockSegmentsService.Instance.CreateAddUsersChatSegmentAsync(
                notice.UsersId,
                notice.ChatId,
                notice.RequestorId,
                node.Node.Id,
                chat.Type,
                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()
        {
            try
            {
                List <ChatUserVm> chatUsers    = new List <ChatUserVm>();
                List <ChatVm>     updatedChats = new List <ChatVm>();
                foreach (long chatId in request.ChatsId)
                {
                    var chat = await loadChatsService.GetChatByIdAsync(chatId).ConfigureAwait(false);

                    if (chat != null && !(chat.Users?.Any(opt => opt.UserRole == UserRole.Creator) ?? false))
                    {
                        var nodeId         = chat.NodesId.FirstOrDefault(id => id != NodeSettings.Configs.Node.Id);
                        var nodeConnection = connectionsService.GetNodeConnection(nodeId);
                        if (nodeConnection != null)
                        {
                            await nodeRequestSender.GetFullChatInformationAsync(chatId, nodeConnection).ConfigureAwait(false);
                        }
                    }
                    updatedChats.Add(await updateChatsService.AddUsersToChatAsync(request.UsersId, chatId, clientConnection.UserId.GetValueOrDefault()).ConfigureAwait(false));
                    foreach (var userId in request.UsersId)
                    {
                        var message = await systemMessagesService.CreateMessageAsync(ConversationType.Chat, chatId, SystemMessageInfoFactory.CreateUserAddedMessageInfo(userId));

                        conversationsNoticeService.SendSystemMessageNoticeAsync(message);
                    }
                }
                foreach (ChatVm chat in updatedChats)
                {
                    conversationsNoticeService.SendNewUsersAddedToChatNoticeAsync(chat, clientConnection);
                    if (chat.Users != null)
                    {
                        chatUsers.AddRange(chat.Users);
                        BlockSegmentVm segment = await BlockSegmentsService.Instance.CreateAddUsersChatSegmentAsync(
                            chat.Users.Select(opt => opt.UserId).ToList(),
                            chat.Id.GetValueOrDefault(),
                            clientConnection.UserId.GetValueOrDefault(),
                            NodeSettings.Configs.Node.Id,
                            chat.Type,
                            NodeData.Instance.NodeKeys.SignPrivateKey,
                            NodeData.Instance.NodeKeys.SymmetricKey,
                            NodeData.Instance.NodeKeys.Password,
                            NodeData.Instance.NodeKeys.KeyId).ConfigureAwait(false);

                        BlockGenerationHelper.Instance.AddSegment(segment);
                        nodeNoticeService.SendAddUsersToChatNodeNoticeAsync(chat, clientConnection.UserId.GetValueOrDefault());
                        if (chat.Type == ChatType.Private)
                        {
                            nodeNoticeService.SendBlockSegmentsNodeNoticeAsync(new List <BlockSegmentVm> {
                                segment
                            });
                        }
                    }
                }
                return(new ChatUsersResponse(request.RequestId, chatUsers));
            }
            catch (AddUserChatException ex)
            {
                Logger.WriteLog(ex, request);
                return(new ResultResponse(request.RequestId, "Failed to add user to chat.", ErrorCode.AddUserProblem));
            }
            catch (ConversationNotFoundException ex)
            {
                Logger.WriteLog(ex, request);
                return(new ResultResponse(request.RequestId, "Chat not found.", ErrorCode.ChatIsNotValid));
            }
            catch (ConversationIsNotValidException ex)
            {
                Logger.WriteLog(ex, request);
                return(new ResultResponse(request.RequestId, "Chat is deleted.", ErrorCode.ChatIsNotValid));
            }
            catch (UserBlockedException ex)
            {
                Logger.WriteLog(ex, request);
                return(new ResultResponse(request.RequestId, "User is blocked in chat.", ErrorCode.UserBlocked));
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex, request);
                return(new ResultResponse(request.RequestId, "Internal server error.", ErrorCode.UnknownError));
            }
        }