public async Task <UserChatVM> GetChatHistoryAndDoAppropriateActions(string appId, string chatId, string userId) { var chat = await _chatRepository.GetAsync(appId, chatId); if (chat != null) { if (chat.ChatMembers.Any(member => member.UserId == userId)) { var lastChatConversations = await _chatRepository.GetChatConversationsAsync(appId, chatId, 0, 100); var unReadChatconversations = await _chatRepository.GetUnReadChatConversationsAsync(appId, chatId, userId); foreach (var conversation in unReadChatconversations) { await _chatRepository.AddReaderToConversationAsync(chatId, conversation.Id, new ChatConversationReader() { UserId = userId, Date = DateTime.Now }); } if (chat.ChatType == ChatType.Private) { var otherUser = chat.ChatMembers.Find(chatMember => chatMember.UserId != userId); var otherUserInfo = await _userRepository.GetAsync(appId, otherUser.UserId); chat.Name = otherUserInfo.FullName; } return(new UserChatVM() { UserId = userId, Chat = new Chat { Id = chatId, Name = chat.Name, ChatConversations = lastChatConversations, ChatType = chat.ChatType }, NewMessagesCount = 0 }); } } return(null); }