public async Task HandleAsync() { foreach (var chat in notice.Chats) { var editableChat = await loadChatsService.GetChatByIdAsync(chat.Id.Value).ConfigureAwait(false); var editedChat = await crossNodeService.NewOrEditChatAsync(chat).ConfigureAwait(false); if (editableChat.Name != editedChat.Name) { var systemMessageInfo = SystemMessageInfoFactory.CreateNameChangedMessageInfo(editableChat.Name, editedChat.Name); var message = await systemMessagesService.CreateMessageAsync(ConversationType.Chat, editedChat.Id.Value, systemMessageInfo); conversationsNoticeService.SendSystemMessageNoticeAsync(message); } List <BlockSegmentVm> segments = await BlockSegmentsService.Instance.CreateEditPrivateChatSegmentsAsync( editedChat, nodeConnection.Node.Id, NodeData.Instance.NodeKeys.SignPrivateKey, NodeData.Instance.NodeKeys.SymmetricKey, NodeData.Instance.NodeKeys.Password, NodeData.Instance.NodeKeys.KeyId).ConfigureAwait(false); if (segments.Any()) { nodeNoticeService.SendBlockSegmentsNodeNoticeAsync(segments.ToList()); BlockGenerationHelper.Instance.AddSegments(segments); } editedChat.Users = null; conversationsNoticeService.SendEditChatNoticeAsync(editedChat, null); UsersConversationsCacheService.Instance.UpdateUsersChatsAsync(await loadChatsService.GetChatUsersIdAsync(editedChat.Id.GetValueOrDefault()).ConfigureAwait(false)); } }
public async Task <Response> CreateResponseAsync() { try { List <long> usersId = await updateUsersService.DeleteUsersFromBlackListAsync(request.UsersId, clientConnection.UserId.GetValueOrDefault()).ConfigureAwait(false); if (usersId.Any()) { nodeNoticeService.SendUsersRemovedFromBlacklistNodeNoticeAsync(usersId.ToList(), clientConnection.UserId.GetValueOrDefault()); BlockSegmentVm segment = await BlockSegmentsService.Instance.CreateUsersRemovedFromUserBlacklistSegmentAsync( usersId, clientConnection.UserId.GetValueOrDefault(), NodeSettings.Configs.Node.Id, NodeData.Instance.NodeKeys.PrivateKey, NodeData.Instance.NodeKeys.SymmetricKey, NodeData.Instance.NodeKeys.Password, NodeData.Instance.NodeKeys.KeyId).ConfigureAwait(false); BlockGenerationHelper.Instance.AddSegment(segment); UsersConversationsCacheService.Instance.UpdateUsersDialogsAsync(usersId.Append(clientConnection.UserId.GetValueOrDefault())); nodeNoticeService.SendBlockSegmentsNodeNoticeAsync(new List <BlockSegmentVm> { segment }); } return(new ResultResponse(request.RequestId)); } catch (ObjectDoesNotExistsException ex) { Logger.WriteLog(ex, request); return(new ResultResponse(request.RequestId, "Users not found.", ErrorCode.ObjectDoesNotExists)); } }
public async Task HandleAsync() { foreach (var chat in notice.Chats) { await crossNodeService.NewOrEditChatAsync(chat).ConfigureAwait(false); conversationsNoticeService.SendNewChatNoticeAsync(chat, null); UsersConversationsCacheService.Instance.UpdateUsersChatsAsync(chat.Users.Select(opt => opt.UserId)); List <BlockSegmentVm> segments = await BlockSegmentsService.Instance.CreateNewPrivateChatSegmentsAsync( chat, NodeSettings.Configs.Node.Id, NodeData.Instance.NodeKeys.SignPrivateKey, NodeData.Instance.NodeKeys.SymmetricKey, NodeData.Instance.NodeKeys.Password, NodeData.Instance.NodeKeys.KeyId).ConfigureAwait(false); if (segments.Any()) { nodeNoticeService.SendBlockSegmentsNodeNoticeAsync(segments.ToList()); foreach (var segment in segments) { BlockGenerationHelper.Instance.AddSegment(segment); } } } }
public async Task <Response> CreateResponseAsync() { List <ChatVm> result = new List <ChatVm>(); try { foreach (var chat in request.Chats) { ChatVm editableChat = await loadChatsService.GetChatByIdAsync(chat.Id); ChatVm editedChat = await updateChatsService.EditChatAsync(chat, clientConnection.UserId.GetValueOrDefault()).ConfigureAwait(false); result.Add(editedChat); if (editableChat.Name != editedChat.Name) { var systemMessageInfo = SystemMessageInfoFactory.CreateNameChangedMessageInfo(editableChat.Name, editedChat.Name); var message = await systemMessagesService.CreateMessageAsync(ConversationType.Chat, editedChat.Id.Value, systemMessageInfo); conversationsNoticeService.SendSystemMessageNoticeAsync(message); } List <BlockSegmentVm> segments = await BlockSegmentsService.Instance.CreateEditPrivateChatSegmentsAsync( editedChat, NodeSettings.Configs.Node.Id, NodeData.Instance.NodeKeys.SignPrivateKey, NodeData.Instance.NodeKeys.SymmetricKey, NodeData.Instance.NodeKeys.Password, NodeData.Instance.NodeKeys.KeyId).ConfigureAwait(false); if (segments.Any()) { nodeNoticeService.SendBlockSegmentsNodeNoticeAsync(segments.ToList()); foreach (var segment in segments) { BlockGenerationHelper.Instance.AddSegment(segment); } } editedChat.Users = null; conversationsNoticeService.SendEditChatNoticeAsync(editedChat, clientConnection); IEnumerable <long> chatUsersId = await loadChatsService.GetChatUsersIdAsync(chat.Id).ConfigureAwait(false); UsersConversationsCacheService.Instance.UpdateUsersChatsAsync(chatUsersId); } nodeNoticeService.SendEditChatsNodeNoticeAsync(result); return(new ChatsResponse(request.RequestId, result)); } catch (PermissionDeniedException ex) { Logger.WriteLog(ex, request); return(new ResultResponse(request.RequestId, "Chat not found or user does not have access to chat.", ErrorCode.PermissionDenied)); } }
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)); } }
public async Task <Response> CreateResponseAsync() { List <ChatVm> chats = new List <ChatVm>(); try { foreach (var chat in request.Chats) { ChatVm newChat = await createChatsService.CreateChatAsync(chat, clientConnection.UserId.GetValueOrDefault()).ConfigureAwait(false); conversationsNoticeService.SendNewChatNoticeAsync(newChat, clientConnection); UsersConversationsCacheService.Instance.UpdateUsersChatsAsync(newChat.Users.Select(opt => opt.UserId)); chats.Add(newChat); List <BlockSegmentVm> segments = await BlockSegmentsService.Instance.CreateNewPrivateChatSegmentsAsync( newChat, NodeSettings.Configs.Node.Id, NodeData.Instance.NodeKeys.SignPrivateKey, NodeData.Instance.NodeKeys.SymmetricKey, NodeData.Instance.NodeKeys.Password, NodeData.Instance.NodeKeys.KeyId).ConfigureAwait(false); if (segments.Any()) { BlockGenerationHelper.Instance.AddSegments(segments); nodeNoticeService.SendBlockSegmentsNodeNoticeAsync(segments.ToList()); } } nodeNoticeService.SendNewChatsNodeNoticeAsync(chats); return(new ChatsResponse(request.RequestId, chats)); } catch (UserBlockedException ex) { Logger.WriteLog(ex, request); return(new ResultResponse(request.RequestId, "The user is blacklisted by another user.", ErrorCode.UserBlocked)); } }
public async Task <Response> CreateResponseAsync() { try { List <ChatUserVm> chatUsers = await updateChatsService.EditChatUsersAsync( request.ChatUsers, request.ChatId, clientConnection.UserId.GetValueOrDefault()).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); } } ChatVm chat = await loadChatsService.GetChatByIdAsync(request.ChatId).ConfigureAwait(false); BlockSegmentVm segment = await BlockSegmentsService.Instance.CreateChangeUsersChatSegmetAsync( chatUsers, request.ChatId, 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.SendBlockSegmentsNodeNoticeAsync(new List <BlockSegmentVm> { segment }); conversationsNoticeService.SendChangeChatUsersNoticeAsync(chatUsers, request.ChatId, clientConnection); nodeNoticeService.SendChangeChatUsersNodeNoticeAsync(chatUsers.ToList(), request.ChatId, clientConnection.UserId.GetValueOrDefault(), chat); UsersConversationsCacheService.Instance.UpdateUsersChatsAsync(chatUsers.Select(opt => opt.UserId)); return(new ChatUsersResponse(request.RequestId, chatUsers)); } catch (ChatUserBlockedException ex) { Logger.WriteLog(ex, request); return(new ResultResponse(request.RequestId, "User blocked in chat.", ErrorCode.UserBlocked)); } catch (UserIsNotInConversationException ex) { Logger.WriteLog(ex, request); return(new ResultResponse(request.RequestId, "User is not in chat.", ErrorCode.UserIsNotInChat)); } catch (EditConversationUsersException ex) { Logger.WriteLog(ex, request); return(new ResultResponse(request.RequestId, "User does not access to the chat.", ErrorCode.PermissionDenied)); } catch (ConversationNotFoundException ex) { Logger.WriteLog(ex); return(new ResultResponse(request.RequestId, "Chat not found.", ErrorCode.ObjectDoesNotExists)); } }
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)); } }