public async Task GetConversationNodeId() { var chat = fillTestDbHelper.Chats.FirstOrDefault(); var chatCreatorId = chat.ChatUsers.FirstOrDefault(opt => opt.UserRole == UserRole.Creator).UserId; var chatCreator = fillTestDbHelper.Users.FirstOrDefault(opt => opt.Id == chatCreatorId); var actualChatNodeId = await conversationsService.GetConversationNodeIdAsync(ConversationType.Chat, chat.Id); Assert.Equal(chatCreator.NodeId, actualChatNodeId); var channel = fillTestDbHelper.Channels.FirstOrDefault(); var channelCreatorId = channel.ChannelUsers.FirstOrDefault(opt => opt.ChannelUserRole == ChannelUserRole.Creator).UserId; var channelCreator = fillTestDbHelper.Users.FirstOrDefault(opt => opt.Id == channelCreatorId); var actualChannelNodeId = await conversationsService.GetConversationNodeIdAsync(ConversationType.Channel, channel.ChannelId); Assert.Equal(channelCreator.NodeId, actualChannelNodeId); }
public async Task <PollDto> GetPollAsync(Guid pollId, long conversationId, ConversationType conversationType) { using (MessengerDbContext context = contextFactory.Create()) { var poll = await context.Polls .Include(opt => opt.Options) .ThenInclude(opt => opt.PollOptionVotes) .FirstOrDefaultAsync(opt => opt.PollId == pollId && opt.ConvertsationId == conversationId && opt.ConversationType == conversationType) .ConfigureAwait(false); if (poll == null) { var nodeId = await conversationsService.GetConversationNodeIdAsync(conversationType, conversationId).ConfigureAwait(false); var connection = connectionsService.GetNodeConnection(nodeId); if (connection != null) { var loadedPoll = await nodeRequestSender.GetPollInformationAsync(conversationId, conversationType, pollId, connection).ConfigureAwait(false); loadedPoll = await SavePollAsync(loadedPoll).ConfigureAwait(false); return(loadedPoll); } } return(PollConverter.GetPollDto(poll)); } }
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)); } }