Files
Yi.Admin/Yi.Abp.Net8/module/chat-hub/Yi.Framework.ChatHub.Domain/Managers/UserMessageManager.cs

93 lines
3.7 KiB
C#
Raw Normal View History

2024-04-07 16:35:21 +08:00
using System.Security.AccessControl;
using FreeRedis;
2024-04-06 18:28:32 +08:00
using Mapster;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Options;
using Volo.Abp.Caching;
2024-04-07 16:35:21 +08:00
using Volo.Abp.Domain.Repositories;
2024-04-06 18:28:32 +08:00
using Volo.Abp.Domain.Services;
2024-04-07 16:35:21 +08:00
using Yi.Framework.ChatHub.Domain.Entities;
2024-04-06 18:28:32 +08:00
using Yi.Framework.ChatHub.Domain.Shared.Caches;
using Yi.Framework.ChatHub.Domain.Shared.Consts;
2024-04-07 16:35:21 +08:00
using Yi.Framework.ChatHub.Domain.Shared.Enums;
2024-04-06 18:28:32 +08:00
using Yi.Framework.ChatHub.Domain.Shared.Model;
using Yi.Framework.ChatHub.Domain.SignalRHubs;
2024-04-07 16:35:21 +08:00
using Yi.Framework.SqlSugarCore.Abstractions;
2024-04-06 18:28:32 +08:00
namespace Yi.Framework.ChatHub.Domain.Managers
{
public class UserMessageManager : DomainService
{
private IHubContext<ChatCenterHub> _hubContext;
2024-05-22 14:35:08 +08:00
public ISqlSugarRepository<MessageAggregateRoot> _repository;
public UserMessageManager(IHubContext<ChatCenterHub> hubContext, ISqlSugarRepository<MessageAggregateRoot> repository)
2024-04-06 18:28:32 +08:00
{
_hubContext = hubContext;
2024-04-07 16:35:21 +08:00
_repository = repository;
2024-04-06 18:28:32 +08:00
}
/// <summary>
/// 使用懒加载防止报错
/// </summary>
private IRedisClient RedisClient => LazyServiceProvider.LazyGetRequiredService<IRedisClient>();
private string CacheKeyPrefix => LazyServiceProvider.LazyGetRequiredService<IOptions<AbpDistributedCacheOptions>>().Value.KeyPrefix;
public async Task SendMessageAsync(MessageContext context)
{
switch (context.MessageType)
{
2024-04-07 16:35:21 +08:00
case MessageTypeEnum.Personal:
var userModel = await GetUserAsync(context.ReceiveId.Value);
2024-04-06 18:28:32 +08:00
if (userModel is not null)
{
await _hubContext.Clients.Client(userModel.ClientId).SendAsync(ChatConst.ClientActionReceiveMsg, context.MessageType, context);
}
break;
2024-04-07 16:35:21 +08:00
case MessageTypeEnum.Group:
2024-04-06 18:28:32 +08:00
throw new NotImplementedException();
break;
2024-04-07 16:35:21 +08:00
case MessageTypeEnum.All:
2024-04-06 18:28:32 +08:00
await _hubContext.Clients.All.SendAsync(ChatConst.ClientActionReceiveMsg, context.MessageType, context);
break;
2024-07-19 18:17:36 +08:00
case MessageTypeEnum.Ai:
var userModel2 = await GetUserAsync(context.ReceiveId.Value);
if (userModel2 is not null)
{
await _hubContext.Clients.Client(userModel2.ClientId).SendAsync(ChatConst.ClientActionReceiveMsg, context.MessageType, context);
}
break;
2024-04-06 18:28:32 +08:00
default:
break;
}
}
2024-04-07 16:35:21 +08:00
public async Task CreateMessageStoreAsync(MessageContext context)
{
2024-05-22 14:35:08 +08:00
await _repository.InsertAsync(context.Adapt<MessageAggregateRoot>());
2024-04-07 16:35:21 +08:00
}
2024-04-06 18:28:32 +08:00
public async Task<List<ChatOnlineUserCacheItem>> GetAllUserAsync()
{
var key = new ChatOnlineUserCacheKey(CacheKeyPrefix);
var cacheUsers = (await RedisClient.HGetAllAsync(key.GetKey())).Select(x => System.Text.Json.JsonSerializer.Deserialize<ChatOnlineUserCacheItem>(x.Value)).ToList();
return cacheUsers;
}
public async Task<ChatOnlineUserCacheItem?> GetUserAsync(Guid userId)
{
var key = new ChatOnlineUserCacheKey(CacheKeyPrefix);
2024-11-10 17:23:00 +08:00
var cacheUserOrNull= await RedisClient.HGetAsync(key.GetKey(), key.GetField(userId));
if (cacheUserOrNull is null)
{
return null;
}
var cacheUser = System.Text.Json.JsonSerializer.Deserialize<ChatOnlineUserCacheItem>(cacheUserOrNull);
2024-04-06 18:28:32 +08:00
return cacheUser;
}
}
}