前端:

1、完善创建群聊逻辑
后端:
1、完善群聊相关接口
This commit is contained in:
2026-02-11 22:44:28 +08:00
parent dc6ecf224d
commit 77db20dc38
55 changed files with 1986 additions and 95 deletions
@@ -0,0 +1,24 @@
using IM_API.Domain.Events;
using IM_API.Interface.Services;
using MassTransit;
namespace IM_API.Application.EventHandlers.GroupInviteActionUpdateHandler
{
public class RequestDbHandler : IConsumer<GroupInviteActionUpdateEvent>
{
private readonly IGroupService _groupService;
public RequestDbHandler(IGroupService groupService)
{
_groupService = groupService;
}
public async Task Consume(ConsumeContext<GroupInviteActionUpdateEvent> context)
{
var @event = context.Message;
if(@event.Action == Models.GroupInviteState.Passed)
{
await _groupService.MakeGroupRequestAsync(@event.UserId, @event.InviteUserId,@event.GroupId);
}
}
}
}
@@ -0,0 +1,34 @@
using IM_API.Domain.Events;
using IM_API.Dtos;
using IM_API.Hubs;
using IM_API.VOs.Group;
using MassTransit;
using Microsoft.AspNetCore.SignalR;
namespace IM_API.Application.EventHandlers.GroupInviteActionUpdateHandler
{
public class SignalRHandler : IConsumer<GroupInviteActionUpdateEvent>
{
private IHubContext<ChatHub> _hub;
public SignalRHandler(IHubContext<ChatHub> hub)
{
_hub = hub;
}
public async Task Consume(ConsumeContext<GroupInviteActionUpdateEvent> context)
{
var @event = context.Message;
var msg = new HubResponse<GroupInviteActionUpdateVo>("Event", new GroupInviteActionUpdateVo
{
Action = @event.Action,
GroupId = @event.GroupId,
InvitedUserId = @event.UserId,
InviteUserId = @event.InviteUserId,
InviteId = @event.InviteId
});
await _hub.Clients.Users([@event.UserId.ToString(), @event.InviteUserId.ToString()])
.SendAsync("ReceiveMessage",msg);
}
}
}
@@ -0,0 +1,26 @@
using IM_API.Domain.Events;
using IM_API.Dtos;
using IM_API.Hubs;
using IM_API.VOs.Group;
using MassTransit;
using Microsoft.AspNetCore.SignalR;
namespace IM_API.Application.EventHandlers.GroupInviteHandler
{
public class GroupInviteSignalRHandler : IConsumer<GroupInviteEvent>
{
private readonly IHubContext<ChatHub> _hub;
public GroupInviteSignalRHandler(IHubContext<ChatHub> hub)
{
_hub = hub;
}
public async Task Consume(ConsumeContext<GroupInviteEvent> context)
{
var @event = context.Message;
var list = @event.Ids.Select(id => id.ToString()).ToArray();
var msg = new HubResponse<GroupInviteVo>("Event", new GroupInviteVo { GroupId = @event.GroupId, UserId = @event.UserId });
await _hub.Clients.Users(list).SendAsync("ReceiveMessage", msg);
}
}
}
@@ -0,0 +1,21 @@
using IM_API.Domain.Events;
using IM_API.Interface.Services;
using MassTransit;
namespace IM_API.Application.EventHandlers.GroupJoinHandler
{
public class GroupJoinConversationHandler : IConsumer<GroupJoinEvent>
{
private IConversationService _conversationService;
public GroupJoinConversationHandler(IConversationService conversationService)
{
_conversationService = conversationService;
}
public async Task Consume(ConsumeContext<GroupJoinEvent> context)
{
var @event = context.Message;
await _conversationService.MakeConversationAsync(@event.UserId, @event.GroupId, Models.ChatType.GROUP);
}
}
}
@@ -0,0 +1,22 @@
using IM_API.Domain.Events;
using IM_API.Interface.Services;
using MassTransit;
namespace IM_API.Application.EventHandlers.GroupJoinHandler
{
public class GroupJoinDbHandler : IConsumer<GroupJoinEvent>
{
private readonly IGroupService _groupService;
public GroupJoinDbHandler(IGroupService groupService)
{
_groupService = groupService;
}
public async Task Consume(ConsumeContext<GroupJoinEvent> context)
{
await _groupService.MakeGroupMemberAsync(context.Message.UserId,
context.Message.GroupId, context.Message.IsCreated ?
Models.GroupMemberRole.Master : Models.GroupMemberRole.Normal);
}
}
}
@@ -0,0 +1,45 @@
using IM_API.Domain.Events;
using IM_API.Dtos;
using IM_API.Hubs;
using IM_API.Tools;
using IM_API.VOs.Group;
using MassTransit;
using Microsoft.AspNetCore.SignalR;
using StackExchange.Redis;
namespace IM_API.Application.EventHandlers.GroupJoinHandler
{
public class GroupJoinSignalrHandler : IConsumer<GroupJoinEvent>
{
private readonly IHubContext<ChatHub> _hub;
private readonly IDatabase _redis;
public GroupJoinSignalrHandler(IHubContext<ChatHub> hub, IConnectionMultiplexer connectionMultiplexer)
{
_hub = hub;
_redis = connectionMultiplexer.GetDatabase();
}
public async Task Consume(ConsumeContext<GroupJoinEvent> context)
{
var @event = context.Message;
string stramKey = StreamKeyBuilder.Group(@event.GroupId);
//将用户加入群组通知
var list = await _redis.SetMembersAsync(RedisKeys.GetConnectionIdKey(@event.UserId.ToString()));
if(list != null && list.Length > 0)
{
var tasks = list.Select(connectionId =>
_hub.Groups.AddToGroupAsync(connectionId!, stramKey)
).ToList();
await Task.WhenAll(tasks);
}
//发送通知给群成员
var msg = new GroupJoinVo
{
GroupId = @event.GroupId,
UserId = @event.UserId
};
await _hub.Clients.Group(stramKey).SendAsync("ReceiveMessage",new HubResponse<GroupJoinVo>("Event",msg));
}
}
}
@@ -1,13 +1,17 @@
using IM_API.Domain.Events;
using IM_API.Hubs;
using MassTransit;
using Microsoft.AspNetCore.SignalR;
namespace IM_API.Application.EventHandlers.GroupRequestHandler
{
public class GroupRequestSignalRHandler : IConsumer<GroupRequestEvent>
public class GroupRequestSignalRHandler(IHubContext<ChatHub> hubContext) : IConsumer<GroupRequestEvent>
{
Task IConsumer<GroupRequestEvent>.Consume(ConsumeContext<GroupRequestEvent> context)
private readonly IHubContext<ChatHub> _hub = hubContext;
public async Task Consume(ConsumeContext<GroupRequestEvent> context)
{
throw new NotImplementedException();
}
}
}
@@ -0,0 +1,31 @@
using IM_API.Domain.Events;
using MassTransit;
namespace IM_API.Application.EventHandlers.GroupRequestHandler
{
public class NextEventHandler : IConsumer<GroupRequestEvent>
{
private readonly IPublishEndpoint _endpoint;
public NextEventHandler(IPublishEndpoint endpoint)
{
_endpoint = endpoint;
}
public async Task Consume(ConsumeContext<GroupRequestEvent> context)
{
var @event = context.Message;
if(@event.Action == Models.GroupRequestState.Passed)
{
await _endpoint.Publish(new GroupJoinEvent
{
AggregateId = @event.AggregateId,
OccurredAt = @event.OccurredAt,
EventId = Guid.NewGuid(),
GroupId = @event.GroupId,
OperatorId = @event.OperatorId,
UserId = @event.UserId
});
}
}
}
}
@@ -0,0 +1,31 @@
using IM_API.Domain.Events;
using MassTransit;
namespace IM_API.Application.EventHandlers.GroupRequestUpdateHandler
{
public class NextEventHandler : IConsumer<GroupRequestUpdateEvent>
{
private readonly IPublishEndpoint _endpoint;
public NextEventHandler(IPublishEndpoint endpoint)
{
_endpoint = endpoint;
}
public async Task Consume(ConsumeContext<GroupRequestUpdateEvent> context)
{
var @event = context.Message;
if(@event.Action == Models.GroupRequestState.Passed)
{
await _endpoint.Publish(new GroupJoinEvent
{
AggregateId = @event.AggregateId,
OccurredAt = @event.OccurredAt,
EventId = Guid.NewGuid(),
GroupId = @event.GroupId,
OperatorId = @event.OperatorId,
UserId = @event.UserId
});
}
}
}
}
@@ -0,0 +1,29 @@
using IM_API.Domain.Events;
using IM_API.Dtos;
using IM_API.Hubs;
using IM_API.VOs.Group;
using MassTransit;
using Microsoft.AspNetCore.SignalR;
namespace IM_API.Application.EventHandlers.GroupRequestUpdateHandler
{
public class RequestUpdateSignalrHandler : IConsumer<GroupRequestUpdateEvent>
{
private readonly IHubContext<ChatHub> _hub;
public RequestUpdateSignalrHandler(IHubContext<ChatHub> hub)
{
_hub = hub;
}
public async Task Consume(ConsumeContext<GroupRequestUpdateEvent> context)
{
var msg = new HubResponse<GroupRequestUpdateVo>("Event", new GroupRequestUpdateVo
{
GroupId = context.Message.GroupId,
RequestId = context.Message.RequestId,
UserId = context.Message.UserId
});
await _hub.Clients.User(context.Message.UserId.ToString()).SendAsync("ReceiveMessage", msg);
}
}
}