public async Task HandleAsync() { try { var chatUsers = await updateChatsService.EditChatUsersAsync(notice.ChatUsers, notice.ChatId, notice.RequestedUserId).ConfigureAwait(false); var bannedUsers = chatUsers.Where(opt => opt.Banned == true); if (!bannedUsers.IsNullOrEmpty()) { foreach (var user in bannedUsers) { var systemMessageInfo = SystemMessageInfoFactory.CreateUserBannedMessageInfo(user.UserId); var message = await systemMessagesService.CreateMessageAsync(ConversationType.Chat, user.ChatId.Value, systemMessageInfo); conversationsNoticeService.SendSystemMessageNoticeAsync(message); } } var deletedUsers = chatUsers.Where(opt => opt.Deleted == true); if (!deletedUsers.IsNullOrEmpty()) { foreach (var user in deletedUsers) { var systemMessageInfo = SystemMessageInfoFactory.CreateUserRemovedMessageInfo(user.UserId); var message = await systemMessagesService.CreateMessageAsync(ConversationType.Chat, user.ChatId.Value, systemMessageInfo); conversationsNoticeService.SendSystemMessageNoticeAsync(message); } } } catch (UserIsNotInConversationException) { ChatVm newChat = await nodeRequestSender.GetFullChatInformationAsync(notice.ChatId, node).ConfigureAwait(false); await crossNodeService.NewOrEditChatAsync(newChat).ConfigureAwait(false); } catch (ConversationNotFoundException ex) { ChatVm newChat = await nodeRequestSender.GetFullChatInformationAsync(ex.ConversationId, node).ConfigureAwait(false); await crossNodeService.NewOrEditChatAsync(newChat).ConfigureAwait(false); } conversationsNoticeService.SendChangeChatUsersNoticeAsync(notice.ChatUsers, notice.ChatId); ChatVm chat = await loadChatsService.GetChatByIdAsync(notice.ChatId).ConfigureAwait(false); BlockSegmentVm segment = await BlockSegmentsService.Instance.CreateChangeUsersChatSegmetAsync( notice.ChatUsers, notice.ChatId, notice.RequestedUserId, 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 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); }
private async Task HandleChatMessagesAsync(IEnumerable <MessageVm> messages) { foreach (var message in messages) { if (NodeData.Instance.RoutedMessagesId.Contains(message.GlobalId.GetValueOrDefault())) { 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); } MessageDto sentMessage; try { sentMessage = await createMessagesService.CreateChatMessageAsync(MessageConverter.GetMessageDto(message), message.SenderId.GetValueOrDefault()).ConfigureAwait(false); } catch (ConversationNotFoundException ex) { ChatVm chat = await nodeRequestSender.GetFullChatInformationAsync(ex.ConversationId, current).ConfigureAwait(false); await crossNodeService.NewOrEditChatAsync(chat).ConfigureAwait(false); sentMessage = await createMessagesService.CreateChatMessageAsync(MessageConverter.GetMessageDto(message), message.SenderId.GetValueOrDefault()).ConfigureAwait(false); } catch (UserIsNotInConversationException ex) { List <ChatUserVm> chatUsers = await nodeRequestSender.GetChatUsersInformationAsync( new List <long> { ex.UserId.GetValueOrDefault() }, ex.ChatId.GetValueOrDefault(), current).ConfigureAwait(false); await crossNodeService.AddChatUsersAsync(chatUsers).ConfigureAwait(false); sentMessage = await createMessagesService.CreateChatMessageAsync(MessageConverter.GetMessageDto(message), message.SenderId.GetValueOrDefault()).ConfigureAwait(false); } SendNotificationsAsync(sentMessage); NodeData.Instance.RoutedMessagesId.Add(message.GlobalId.GetValueOrDefault()); } }
public async Task <Response> CreateResponseAsync() { try { List <ChatUserVm> chatUsers = await loadChatsService.GetChatUsersAsync( request.ChatId, clientConnection.UserId.GetValueOrDefault(), 100, request.NavigationUserId.GetValueOrDefault()).ConfigureAwait(false); if (!chatUsers.Any(user => user.UserRole == UserRole.Creator)) { var nodesIds = await loadChatsService.GetChatNodeListAsync(request.ChatId).ConfigureAwait(false); var nodeConnection = connectionsService.GetNodeConnection(nodesIds.FirstOrDefault(id => id != NodeSettings.Configs.Node.Id)); if (nodeConnection != null) { var chat = await nodeRequestSender.GetFullChatInformationAsync(request.ChatId, nodeConnection).ConfigureAwait(false); if (chat != null) { await crossNodeService.NewOrEditChatAsync(chat).ConfigureAwait(false); chatUsers = chat.Users?.Take(100).ToList(); } } } chatUsers.ForEach(item => { if (item.UserInfo != null) { item.UserInfo = privacyService.ApplyPrivacySettings( item.UserInfo, item.UserInfo.Privacy); } }); return(new ChatUsersResponse(request.RequestId, chatUsers)); } catch (GetUsersException ex) { Logger.WriteLog(ex, request); return(new ResultResponse(request.RequestId, "User does not have access to the chat.", ErrorCode.PermissionDenied)); } }
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)); } }