using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.Json.Serialization; using System.Threading.Tasks; using Yi.Framework.ChatHub.Domain.Shared.Enums; using Yi.Framework.Rbac.Domain.Shared.Dtos; namespace Yi.Framework.ChatHub.Domain.Shared.Model { public static class MessageContextExtensions { public static List GetUserIds(this List context) { return context.Where(x => x.ReceiveId is not null).Select(x => x.ReceiveId!.Value).Union(context.Select(x => x.SendUserId)).ToList(); } public static List MapperUserInfo(this List messageContexts, List userRoleMenuDtos) { 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; } } public class MessageContext { /// /// 映射用户信息 /// public static MessageContext CreatePersonal(string content, Guid userId, Guid sendUserId) { return new MessageContext() { MessageType = MessageTypeEnum.Personal, Content = content, ReceiveId = userId, SendUserId = sendUserId }; } public static MessageContext CreateAi(string? content, Guid receiveId, Guid id ) { return new MessageContext() { MessageType = MessageTypeEnum.Ai, Content = content??string.Empty, ReceiveId = receiveId, Id = id }; } public static MessageContext CreateAll(string content, Guid sendUserId) { return new MessageContext() { MessageType = MessageTypeEnum.All, Content = content, SendUserId = sendUserId }; } public void SetUserInfo(UserRoleMenuDto sendUserInfo, UserRoleMenuDto? receiveInfo) { this.SendUserInfo = sendUserInfo; this.ReceiveInfo = receiveInfo; } /// /// 消息类型 /// [JsonConverter(typeof(JsonStringEnumConverter))] public MessageTypeEnum MessageType { get; set; } /// /// 接收者(用户id、群组id) /// public Guid? ReceiveId { get; set; } /// /// 接收者用户信息 /// public UserRoleMenuDto? ReceiveInfo { get; set; } /// /// 发送者的用户id /// public Guid SendUserId { get; set; } /// /// 发送者用户信息 /// public UserRoleMenuDto SendUserInfo { get; set; } /// /// 消息内容 /// public string Content { get; set; } /// /// 上下文Id,主要用于ai流式传输 /// public Guid Id { get; set; } public DateTime CreationTime { get; protected set; } = DateTime.Now; } }