Skip to content

Commit

Permalink
VM-1501: conversation unread count improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimir-buravlev committed Nov 22, 2024
1 parent 785bc86 commit 5046d04
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,16 @@ public class ConversationCrudService : CrudService<Conversation, ConversationEnt
{
private readonly Func<ICommunicationRepository> _repositoryFactory;
private readonly IMessageCrudService _messageCrudService;
private readonly IMessageService _messageService;

public ConversationCrudService(
Func<ICommunicationRepository> repositoryFactory,
IPlatformMemoryCache platformMemoryCache,
IEventPublisher eventPublisher,
IMessageCrudService messageCrudService,
IMessageService messageService
IMessageCrudService messageCrudService
) : base(repositoryFactory, platformMemoryCache, eventPublisher)
{
_repositoryFactory = repositoryFactory;
_messageCrudService = messageCrudService;
_messageService = messageService;
}

protected override Task<IList<ConversationEntity>> LoadEntities(IRepository repository, IList<string> ids, string responseGroup)
Expand Down Expand Up @@ -58,17 +55,6 @@ public override async Task<IList<Conversation>> GetAsync(IList<string> ids, stri
}
}

if (respGroupEnum.HasFlag(ConversationResponseGroup.WithUnreadCount))
{
if (!conversations.IsNullOrEmpty())
{
foreach (var conversation in conversations)
{
conversation.UnreadMessagesCount = await _messageService.GetUnreadMessagesCount(conversation.Users.FirstOrDefault()?.UserId, conversation.Id);
}
}
}

return conversations;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using VirtoCommerce.CommunicationModule.Core.Models;
Expand All @@ -18,13 +19,18 @@ public class ConversationSearchService : SearchService<SearchConversationCriteri
SearchConversationResult, Conversation, ConversationEntity>,
IConversationSearchService
{
private readonly IMessageService _messageService;

public ConversationSearchService(
Func<ICommunicationRepository> repositoryFactory,
IPlatformMemoryCache platformMemoryCache,
IConversationCrudService crudService,
IOptions<CrudOptions> crudOptions)
IOptions<CrudOptions> crudOptions,
IMessageService messageService
)
: base(repositoryFactory, platformMemoryCache, crudService, crudOptions)
{
_messageService = messageService;
}

protected override IQueryable<ConversationEntity> BuildQuery(IRepository repository, SearchConversationCriteria criteria)
Expand Down Expand Up @@ -56,4 +62,21 @@ protected override IList<SortInfo> BuildSortExpression(SearchConversationCriteri

return sortInfos;
}

protected override async Task<SearchConversationResult> ProcessSearchResultAsync(SearchConversationResult result, SearchConversationCriteria criteria)
{
var respGroupEnum = EnumUtility.SafeParseFlags(criteria.ResponseGroup, ConversationResponseGroup.None);
if (respGroupEnum.HasFlag(ConversationResponseGroup.WithUnreadCount))
{
if (!result.Results.IsNullOrEmpty())
{
foreach (var conversation in result.Results)
{
conversation.UnreadMessagesCount = await _messageService.GetUnreadMessagesCount(criteria.UserIds.FirstOrDefault(), conversation.Id);
}
}
}

return result;
}
}

0 comments on commit 5046d04

Please sign in to comment.