34 lines
1.3 KiB
C#
34 lines
1.3 KiB
C#
using GroupService.Domain.Events;
|
|
using GroupService.Infrastructure;
|
|
using IM.Commons.IntegrationEvents;
|
|
using MassTransit;
|
|
using MediatR;
|
|
|
|
namespace GroupService.WebApi.Application.EventHandler
|
|
{
|
|
public class GroupMemberJoinedHandler : INotificationHandler<GroupMemberJoinedDomainEvent>
|
|
{
|
|
private readonly IPublishEndpoint endpoint;
|
|
private readonly GroupDbContext db;
|
|
|
|
public GroupMemberJoinedHandler(IPublishEndpoint endpoint, GroupDbContext db)
|
|
{
|
|
this.endpoint = endpoint;
|
|
this.db = db;
|
|
}
|
|
|
|
public async Task Handle(GroupMemberJoinedDomainEvent notification, CancellationToken cancellationToken)
|
|
{
|
|
var member = notification.Member;
|
|
// 用 FindAsync 而非 FirstOrDefaultAsync:群可能刚创建尚未提交到数据库,FindAsync 先查内存跟踪器
|
|
var group = await db.Set<Domain.Entities.Group>().FindAsync(member.GroupId);
|
|
var groupName = group?.Name ?? "群聊";
|
|
var groupAvatar = group?.Avatar;
|
|
|
|
await endpoint.Publish(new GroupMemberJoinedEvent(member.Id,
|
|
member.UserId, member.GroupId, member.GroupNickName, member.Avatar,
|
|
member.Role.ToString(), groupName, groupAvatar), cancellationToken);
|
|
}
|
|
}
|
|
}
|