Exemplo n.º 1
0
        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);
            }
        }