public async Task SaveChatMessages() { var chat = fillTestDbHelper.Chats.FirstOrDefault(); var expectedMessages = new List <MessageDto> { new MessageDto { ConversationId = chat.Id, ConversationType = ConversationType.Chat, SenderId = chat.ChatUsers.FirstOrDefault().UserId, Text = "Save messages test", GlobalId = Guid.NewGuid(), SendingTime = DateTime.UtcNow.ToUnixTime() }, new MessageDto { ConversationId = chat.Id, ConversationType = ConversationType.Chat, SenderId = chat.ChatUsers.LastOrDefault().UserId, Text = "Save messages test", GlobalId = Guid.NewGuid(), SendingTime = DateTime.UtcNow.ToUnixTime() } }; var actualMessages = await createMessagesService.SaveMessagesAsync(expectedMessages, expectedMessages[0].SenderId.Value); Assert.Equal( expectedMessages.OrderBy(message => message.SendingTime).Select(message => message.GlobalId), actualMessages.OrderBy(message => message.SendingTime).Select(message => message.GlobalId)); }
public async Task <bool> SaveUserDataAsync(UserDto userData) { try { UserDto user = await updateUsersService.CreateOrUpdateUserAsync(userData).ConfigureAwait(false); IEnumerable <ChatDto> chats = await createChatsService.CreateOrUpdateUserChatsAsync(userData.Chats).ConfigureAwait(false); IEnumerable <ChannelDto> channels = await createChannelsService.CreateOrUpdateUserChannelsAsync(userData.Channels).ConfigureAwait(false); foreach (var chat in userData.Chats) { await createMessagesService.SaveMessagesAsync(chat.Messages, user.Id).ConfigureAwait(false); } foreach (var channel in userData.Channels) { await createMessagesService.SaveMessagesAsync(channel.Messages, user.Id).ConfigureAwait(false); } foreach (var dialog in userData.Dialogs) { await createMessagesService.SaveMessagesAsync(dialog.Messages, user.Id).ConfigureAwait(false); } foreach (var contact in userData.Contacts) { await contactsService.CreateOrEditContactAsync(contact).ConfigureAwait(false); } foreach (var group in userData.ContactGroups) { await groupsService.CreateOrEditGroupAsync(group).ConfigureAwait(false); } await favoritesService.ChangeUserFavoritesAsync(userData.Favorites, userData.Id).ConfigureAwait(false); return(true); } catch (Exception ex) { Logger.WriteLog(ex); return(false); } }
public async Task <Response> CreateResponseAsync() { try { IEnumerable <MessageDto> result = default; switch (request.ConversationType) { case ConversationType.Dialog: { result = await GetDialogMessagesAsync().ConfigureAwait(false); } break; case ConversationType.Chat: { result = await GetChatMessagesAsync().ConfigureAwait(false); } break; case ConversationType.Channel: { result = await GetChannelMessagesAsync().ConfigureAwait(false); } break; } if (result.Count() < LIMIT && request.MessagesId.IsNullOrEmpty() && request.AttachmentsTypes.IsNullOrEmpty()) { long nodeId = await conversationsService.GetConversationNodeIdAsync(request.ConversationType, request.ConversationId).ConfigureAwait(false); NodeConnection connection = connectionsService.GetNodeConnection(nodeId); var otherNodeMessages = await nodeRequestSender.GetMessagesAsync( connection, request.ConversationId, request.ConversationType, request.NavigationMessageId, request.AttachmentsTypes, request.Direction.GetValueOrDefault(), LIMIT).ConfigureAwait(false); if (otherNodeMessages.Where(message => !message.Deleted).Count() > result.Count()) { await MetricsHelper.Instance.SetCrossNodeApiInvolvedAsync(request.RequestId).ConfigureAwait(false); var messagesVm = MessageConverter.GetMessagesVm(otherNodeMessages, clientConnection.UserId); foreach (var message in messagesVm) { if (message.Attachments != null) { await attachmentsService.DownloadAttachmentsPayloadAsync(message.Attachments, connection).ConfigureAwait(false); bool isValid = await attachmentsService.ValidateAttachmentsAsync(message.Attachments, message, clientConnection.UserId, true).ConfigureAwait(false); } } var savedMessages = await createMessagesService.SaveMessagesAsync(otherNodeMessages, clientConnection.UserId.Value).ConfigureAwait(false); result = savedMessages; if (savedMessages.Any()) { var localMessage = await loadMessagesService.GetLastValidConversationMessage(request.ConversationType, request.ConversationId).ConfigureAwait(false); var lastMessage = savedMessages.OrderByDescending(opt => opt.SendingTime).FirstOrDefault(); if (localMessage == null || localMessage.SendingTime <= lastMessage.SendingTime) { await UsersConversationsCacheService.Instance .NewMessageUpdateUsersConversationsAsync(MessageConverter.GetMessageVm(lastMessage, clientConnection.UserId)).ConfigureAwait(false); } } } } return(new MessagesResponse(request.RequestId, MessageConverter.GetMessagesVm(result, clientConnection.UserId.GetValueOrDefault()))); } catch (Exception ex) { Logger.WriteLog(ex, request); return(new ResultResponse(request.RequestId, "An error ocurred while getting messages.", ErrorCode.GetMessagesProblem)); } }