Files
Yi.Admin/Yi.Abp.Net8/module/chat-hub/Yi.Framework.ChatHub.Domain.Shared/Model/MessageContext.cs

99 lines
3.3 KiB
C#
Raw Normal View History

2024-04-06 18:28:32 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
2024-04-07 16:35:21 +08:00
using Yi.Framework.ChatHub.Domain.Shared.Enums;
using Yi.Framework.Rbac.Domain.Shared.Dtos;
2024-04-06 18:28:32 +08:00
namespace Yi.Framework.ChatHub.Domain.Shared.Model
{
public static class MessageContextExtensions
{
public static List<Guid> GetUserIds(this List<MessageContext> context)
{
return context.Where(x => x.ReceiveId is not null).Select(x => x.ReceiveId!.Value).Union(context.Select(x => x.SendUserId)).ToList();
}
2024-07-21 13:37:56 +08:00
public static List<MessageContext> MapperUserInfo(this List<MessageContext> messageContexts, List<UserRoleMenuDto> userRoleMenuDtos)
{
2024-07-21 13:37:56 +08:00
var userInfoDic = userRoleMenuDtos.Select(x => new UserRoleMenuDto { User = x.User }).ToDictionary(x => x.User.Id);
foreach (var context in messageContexts)
{
if (context.ReceiveId is not null)
{
context.ReceiveInfo = userInfoDic[context.ReceiveId.Value];
}
context.SendUserInfo = userInfoDic[context.SendUserId];
}
return messageContexts;
}
}
2024-04-06 18:28:32 +08:00
public class MessageContext
{
/// <summary>
/// 映射用户信息
/// </summary>
public static MessageContext CreatePersonal(string content, Guid userId, Guid sendUserId)
2024-04-06 18:28:32 +08:00
{
return new MessageContext() { MessageType = MessageTypeEnum.Personal, Content = content, ReceiveId = userId, SendUserId = sendUserId };
2024-04-06 18:28:32 +08:00
}
2024-07-19 18:17:36 +08:00
2024-07-21 13:37:56 +08:00
public static MessageContext CreateAi(string? content, Guid receiveId, Guid id )
2024-07-19 18:17:36 +08:00
{
2024-07-21 13:37:56 +08:00
return new MessageContext() { MessageType = MessageTypeEnum.Ai, Content = content??string.Empty, ReceiveId = receiveId, Id = id };
2024-07-19 18:17:36 +08:00
}
2024-04-06 18:28:32 +08:00
public static MessageContext CreateAll(string content, Guid sendUserId)
{
2024-04-07 16:35:21 +08:00
return new MessageContext() { MessageType = MessageTypeEnum.All, Content = content, SendUserId = sendUserId };
2024-04-06 18:28:32 +08:00
}
public void SetUserInfo(UserRoleMenuDto sendUserInfo, UserRoleMenuDto? receiveInfo)
{
this.SendUserInfo = sendUserInfo;
this.ReceiveInfo = receiveInfo;
2024-04-06 18:28:32 +08:00
}
2024-04-06 18:28:32 +08:00
/// <summary>
/// 消息类型
/// </summary>
[JsonConverter(typeof(JsonStringEnumConverter))]
2024-04-07 16:35:21 +08:00
public MessageTypeEnum MessageType { get; set; }
2024-04-06 18:28:32 +08:00
/// <summary>
/// 接收者(用户id、群组id)
/// </summary>
public Guid? ReceiveId { get; set; }
/// <summary>
/// 接收者用户信息
/// </summary>
public UserRoleMenuDto? ReceiveInfo { get; set; }
2024-04-06 18:28:32 +08:00
/// <summary>
/// 发送者的用户id
/// </summary>
public Guid SendUserId { get; set; }
/// <summary>
/// 发送者用户信息
/// </summary>
public UserRoleMenuDto SendUserInfo { get; set; }
2024-04-06 18:28:32 +08:00
/// <summary>
/// 消息内容
/// </summary>
public string Content { get; set; }
2024-07-21 13:37:56 +08:00
/// <summary>
/// 上下文Id主要用于ai流式传输
/// </summary>
public Guid Id { get; set; }
public DateTime CreationTime { get; protected set; } = DateTime.Now;
2024-04-06 18:28:32 +08:00
}
}