public async Task MarkAllUnreadMessagesOfUserAsRead(MarkAllUnreadMessagesOfUserAsReadInput input) { var userId = AbpSession.GetUserId(); var messages = await _chatMessageRepository .GetAll() .Where(m => m.UserId == userId && m.TargetTenantId == input.TenantId && m.TargetUserId == input.UserId && m.ReadState == ChatMessageReadState.Unread) .ToListAsync(); if (!messages.Any()) { return; } foreach (var message in messages) { message.ChangeReadState(ChatMessageReadState.Read); } var userIdentifier = AbpSession.ToUserIdentifier(); var friendIdentifier = input.ToUserIdentifier(); _userFriendsCache.ResetUnreadMessageCount(userIdentifier, friendIdentifier); var onlineClients = _onlineClientManager.GetAllByUserId(userIdentifier); if (onlineClients.Any()) { _chatCommunicator.SendAllUnreadMessagesOfUserReadToClients(onlineClients, friendIdentifier); } }
public async Task MarkAllUnreadMessagesOfUserAsRead(MarkAllUnreadMessagesOfUserAsReadInput input) { var userId = AbpSession.GetUserId(); var tenantId = AbpSession.TenantId; // receiver messages var messages = await _chatMessageRepository .GetAll() .Where(m => m.UserId == userId && m.TargetTenantId == input.TenantId && m.TargetUserId == input.UserId && m.ReadState == EChatMessageReadState.Unread) .ToListAsync(); if (!messages.Any()) { return; } foreach (var message in messages) { message.ChangeReadState(EChatMessageReadState.Read); } // sender messages using (CurrentUnitOfWork.SetTenantId(input.TenantId)) { var reverseMessages = await _chatMessageRepository.GetAll() .Where(m => m.UserId == input.UserId && m.TargetTenantId == tenantId && m.TargetUserId == userId) .ToListAsync(); if (!reverseMessages.Any()) { return; } foreach (var message in reverseMessages) { message.ChangeReceiverReadState(EChatMessageReadState.Read); } } var userIdentifier = AbpSession.ToUserIdentifier(); var friendIdentifier = input.ToUserIdentifier(); _userFriendsCache.ResetUnreadMessageCount(userIdentifier, friendIdentifier); var onlineUserClients = _onlineClientManager.GetAllByUserId(userIdentifier); if (onlineUserClients.Any()) { _chatCommunicator.SendAllUnreadMessagesOfUserReadToClients(onlineUserClients, friendIdentifier); } var onlineFriendClients = _onlineClientManager.GetAllByUserId(friendIdentifier); if (onlineFriendClients.Any()) { _chatCommunicator.SendReadStateChangeToClients(onlineFriendClients, userIdentifier); } }