Exemple #1
0
        public async Task <ActionResult <MainResponse> > GetConversations(GetChatsRequest request)
        {
            User user = HttpContext.GetUser();

            if (user == null || user.KeySession == null)
            {
                return(Unauthorized());
            }

            Chat[] chats = await _context.Chats.Where(p => p.EncryptionChatDatas.Where(c => c.KeySession != null && c.KeySession.Id == user.KeySession.Id).Count() > 0).OrderByDescending(p => p.LastMessageDate).Skip(_serverConfig.Chats.PartsSize.ChatsInPart * (request.Part - 1)).Take(_serverConfig.Chats.PartsSize.ChatsInPart).ToArrayAsync().ConfigureAwait(false);

            string[]  messageIds = chats.Select(p => p.LastMessageId).ToArray();
            Message[] messages   = await _context.Messages.Where(p => messageIds.Contains(p.Id)).ToArrayAsync();

            GetChatsResponse chatsResponse = new GetChatsResponse()
            {
                Chats = new List <ExtChat>()
            };

            EncryptionChatData encryptionChatData;

            for (int i = 0; i < chats.Length; i++)
            {
                encryptionChatData = chats[i].EncryptionChatDatas.FirstOrDefault(p => p?.KeySession?.Id == user.KeySession.Id);
                ExtChat extChat = (ExtChat)chats[i];
                extChat.LastMessage      = (ExtMessage)messages.FirstOrDefault(p => p.Id == chats[i].LastMessageId);
                extChat.EncryptedChatKey = encryptionChatData.AESEncryptedKey;
                extChat.Title            = encryptionChatData.EncryptedTitle;
                chatsResponse.Chats.Add(extChat);
            }

            return(MainResponse.GetSuccess(chatsResponse));
        }
 public GetChatsRequestHandler(Request request, ILoadChatsService loadChatsService, ClientConnection clientConnection)
 {
     this.request          = (GetChatsRequest)request;
     this.loadChatsService = loadChatsService;
     this.clientConnection = clientConnection;
 }