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 { switch (request.ConversationType) { case ConversationType.Dialog: { var users = await loadDialogsService.GetDialogUsersAsync(request.ConversationId).ConfigureAwait(false); await deleteDialogsService.DeleteDialogAsync(request.ConversationId, clientConnection.UserId.GetValueOrDefault()).ConfigureAwait(false); UsersConversationsCacheService.Instance.UpdateUsersDialogsAsync(users.Select(opt => opt.Id.Value)); } break; case ConversationType.Chat: { List <long> usersId = await loadChatsService.GetChatUsersIdAsync(request.ConversationId).ConfigureAwait(false); await deleteChatsService.DeleteChatAsync(request.ConversationId, clientConnection.UserId.GetValueOrDefault()).ConfigureAwait(false); UsersConversationsCacheService.Instance.UpdateUsersChatsAsync(usersId); BlockSegmentVm segment = await BlockSegmentsService.Instance.CreateDeleteChatSegmentAsync( request.ConversationId, NodeSettings.Configs.Node.Id).ConfigureAwait(false); BlockGenerationHelper.Instance.AddSegment(segment); } break; case ConversationType.Channel: { List <long> usersId = await loadChannelsService.GetChannelUsersIdAsync(request.ConversationId).ConfigureAwait(false); await deleteChannelsService.DeleteChannelAsync(request.ConversationId, clientConnection.UserId.GetValueOrDefault()).ConfigureAwait(false); UsersConversationsCacheService.Instance.UpdateUsersChannelsAsync(usersId); BlockSegmentVm segment = await BlockSegmentsService.Instance.CreateDeleteChannelSegmentAsync( request.ConversationId, NodeSettings.Configs.Node.Id).ConfigureAwait(false); BlockGenerationHelper.Instance.AddSegment(segment); } break; } nodeNoticeService.SendDeleteConversationsNodeNoticeAsync(request.ConversationId, request.ConversationType, clientConnection.UserId.GetValueOrDefault()); return(new ResultResponse(request.RequestId)); } catch (PermissionDeniedException ex) { Logger.WriteLog(ex, request); return(new ResultResponse(request.RequestId, "Conversation not found or user does not have access to conversation.", ErrorCode.DeleteConversationProblem)); } }
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 void NewMessageUpdateUserChatsAsync(MessageVm message) { try { UserVm senderInfo = await loadUsersService.GetUserAsync(message.SenderId.GetValueOrDefault()).ConfigureAwait(false); IEnumerable <long> usersId = await loadChatsService.GetChatUsersIdAsync(message.ConversationId.GetValueOrDefault()).ConfigureAwait(false); foreach (long userId in usersId) { List <ConversationPreviewVm> cachedChats = (await GetUserChatsAsync(userId).ConfigureAwait(false))?.ToList(); if (cachedChats == null || cachedChats.All(opt => opt.ConversationId != message.ConversationId)) { cachedChats = (await loadChatsService.GetUserChatPreviewAsync(userId, DateTime.UtcNow.ToUnixTime()).ConfigureAwait(false))?.ToList(); UpdateUserChats(userId, cachedChats); continue; } ConversationPreviewVm updatedChat = cachedChats.FirstOrDefault(opt => opt.ConversationId == message.ConversationId); if (updatedChat != null) { updatedChat.LastMessageSenderId = message.SenderId; updatedChat.LastMessageTime = message.SendingTime; updatedChat.LastMessageSenderName = senderInfo.NameFirst; updatedChat.PreviewText = message.Text; updatedChat.LastMessageId = message.GlobalId; updatedChat.Read = false; updatedChat.AttachmentType = (AttachmentType?)message.Attachments?.FirstOrDefault()?.Type ?? null; UpdateUserChats(userId, cachedChats); } } } catch (Exception ex) { Logger.WriteLog(ex); } }
private async Task <IEnumerable <MessageDto> > DeleteChatMessagesAsync() { List <MessageDto> deletedMessages = await deleteMessagesService.DeleteMessagesInfoAsync( request.ConversationId, ConversationType.Chat, request.MessagesIds, clientConnection.UserId.GetValueOrDefault()).ConfigureAwait(false); conversationsNoticeService.SendMessagesUpdatedNoticeAsync( request.ConversationId, ConversationType.Chat, deletedMessages, clientConnection.UserId.GetValueOrDefault(), true, clientConnection); UsersConversationsCacheService.Instance.UpdateUsersChatsAsync( await loadChatsService.GetChatUsersIdAsync(request.ConversationId).ConfigureAwait(false)); return(deletedMessages); }
public async void SendConversationActionNoticeAsync(long userId, ConversationType conversationType, long conversationId, ConversationAction action) { try { List <long> usersIds = null; ConversationActionNotice notice = null; switch (conversationType) { case ConversationType.Dialog: { var users = await loadDialogsService.GetDialogUsersAsync(conversationId).ConfigureAwait(false); var secondUser = users.FirstOrDefault(opt => opt.Id != userId); usersIds = new List <long> { secondUser.Id.Value }; var dialogId = await loadDialogsService.GetMirrorDialogIdAsync(conversationId); notice = new ConversationActionNotice(dialogId, ConversationType.Dialog, userId, action); } break; case ConversationType.Chat: { usersIds = await loadChatsService.GetChatUsersIdAsync(conversationId); notice = new ConversationActionNotice(conversationId, ConversationType.Chat, userId, action); } break; case ConversationType.Channel: { usersIds = await loadChannelsService.GetChannelUsersIdAsync(conversationId); notice = new ConversationActionNotice(conversationId, ConversationType.Channel, null, action); } break; default: return; } usersIds.Remove(userId); var clientConnections = connectionsService.GetClientConnections(usersIds); await SendNoticeToClientsAsync(clientConnections, notice); } catch (Exception ex) { Logger.WriteLog(ex); } }
public async Task HandleAsync() { switch (notice.ConversationType) { case ConversationType.Dialog: { List <long> dialogsId = await loadDialogsService.GetDialogsIdByUsersIdPairAsync(notice.RequestingUserId, notice.ConversationId).ConfigureAwait(false); await deleteDialogsService.DeleteDialogAsync(dialogsId.FirstOrDefault(), notice.RequestingUserId).ConfigureAwait(false); UsersConversationsCacheService.Instance.UpdateUsersDialogsAsync(notice.RequestingUserId, notice.ConversationId); } break; case ConversationType.Chat: { var usersId = await loadChatsService.GetChatUsersIdAsync(notice.ConversationId).ConfigureAwait(false); await deleteChatsService.DeleteChatAsync(notice.ConversationId, notice.RequestingUserId).ConfigureAwait(false); UsersConversationsCacheService.Instance.UpdateUsersChatsAsync(usersId); BlockSegmentVm segment = await BlockSegmentsService.Instance.CreateDeleteChatSegmentAsync(notice.ConversationId, notice.NodeId).ConfigureAwait(false); BlockGenerationHelper.Instance.AddSegment(segment); } break; case ConversationType.Channel: { var usersId = await loadChannelsService.GetChannelUsersIdAsync(notice.ConversationId).ConfigureAwait(false); await deleteChannelsService.DeleteChannelAsync(notice.ConversationId, notice.RequestingUserId).ConfigureAwait(false); UsersConversationsCacheService.Instance.UpdateUsersChannelsAsync(usersId); BlockSegmentVm segment = await BlockSegmentsService.Instance.CreateDeleteChannelSegmentAsync(notice.ConversationId, notice.NodeId).ConfigureAwait(false); BlockGenerationHelper.Instance.AddSegment(segment); } break; } }
public async Task HandleAsync() { switch (notice.ConversationType) { case ConversationType.Dialog: { var dialogs = await loadDialogsService.GetUsersDialogsAsync( notice.Messages.FirstOrDefault().SenderId.GetValueOrDefault(), notice.Messages.FirstOrDefault().ReceiverId.GetValueOrDefault()).ConfigureAwait(false); if (!dialogs.Any()) { await deleteMessagesService.DeleteForwardedDialogMessagesAsync(notice.Messages).ConfigureAwait(false); return; } List <MessageDto> messages = await deleteMessagesService.DeleteMessagesInfoAsync( dialogs.FirstOrDefault(dialog => dialog.FirstUserId == notice.RequestorId).Id, ConversationType.Dialog, notice.Messages.Select(opt => opt.GlobalId.GetValueOrDefault()), notice.RequestorId).ConfigureAwait(false); conversationsNoticeService.SendMessagesUpdatedNoticeAsync( dialogs.FirstOrDefault(dialog => dialog.FirstUserId == notice.RequestorId).Id, ConversationType.Dialog, messages, notice.RequestorId, true, null); UsersConversationsCacheService.Instance.UpdateUsersDialogsAsync( notice.Messages.FirstOrDefault().SenderId.GetValueOrDefault(), notice.Messages.FirstOrDefault().ReceiverId.GetValueOrDefault()); } break; case ConversationType.Chat: { List <MessageDto> messages = await deleteMessagesService.DeleteMessagesInfoAsync( notice.ConversationId.GetValueOrDefault(), ConversationType.Chat, notice.Messages.Select(opt => opt.GlobalId.GetValueOrDefault()), notice.RequestorId).ConfigureAwait(false); conversationsNoticeService.SendMessagesUpdatedNoticeAsync( notice.ConversationId.GetValueOrDefault(), ConversationType.Chat, messages, notice.RequestorId, true, null); UsersConversationsCacheService.Instance.UpdateUsersChatsAsync( await loadChatsService.GetChatUsersIdAsync(notice.ConversationId.GetValueOrDefault()).ConfigureAwait(false)); } break; case ConversationType.Channel: { List <MessageDto> messages = await deleteMessagesService.DeleteMessagesInfoAsync( notice.ConversationId.GetValueOrDefault(), ConversationType.Channel, notice.Messages.Select(opt => opt.GlobalId.GetValueOrDefault()), notice.RequestorId).ConfigureAwait(false); conversationsNoticeService.SendMessagesUpdatedNoticeAsync( notice.ConversationId.GetValueOrDefault(), ConversationType.Channel, messages, notice.RequestorId, true, null); UsersConversationsCacheService.Instance.UpdateUsersChannelsAsync( await loadChannelsService.GetChannelUsersIdAsync(notice.ConversationId.GetValueOrDefault()).ConfigureAwait(false)); } break; } }