添加项目文件。

This commit is contained in:
2026-05-09 17:06:30 +08:00
parent c60f5fe117
commit 720ef957d4
378 changed files with 14843 additions and 0 deletions
@@ -0,0 +1,17 @@
using MessageService.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace MessageService.Infrastructure.Configs
{
public class ConversationConfig : IEntityTypeConfiguration<Conversation>
{
public void Configure(EntityTypeBuilder<Conversation> builder)
{
builder.ToTable("conversations");
builder.HasKey(x => x.Id);
builder.HasIndex(x => x.UserId);
}
}
}
@@ -0,0 +1,46 @@
using MessageService.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace MessageService.Infrastructure.Configs
{
public class MessageConfig : IEntityTypeConfiguration<Message>
{
public void Configure(EntityTypeBuilder<Message> builder)
{
builder.ToTable("messages");
builder.HasKey(x => x.Id);
builder.HasIndex(x => new { x.StreamKey, x.SequenceId });
builder.ComplexProperty(x => x.Content, c =>
{
// 1. Fallback 是简单字符串,直接映射
c.Property(p => p.Fallback)
.HasMaxLength(255)
.IsRequired();
// 2. Body 是 object (JSON 载荷)
// ComplexProperty 无法直接处理 object 类型,通常将其序列化为 JSON 字符串存储
c.Property(p => p.RawBody)
.HasColumnName("Content_Body");
// 3. Ext 是 Dictionary<string, string>
// 同样推荐使用 HasConversion 映射为 JSON 字符串,或者在某些库中使用 JSONB 映射
// 映射 RawExt 字符串
c.Property(p => p.RawExt)
.HasColumnName("Content_Ext");
// 4. Quote 是嵌套的值对象 (QuoteInfo)
// ComplexProperty 支持嵌套定义
c.ComplexProperty(p => p.Quote, q =>
{
q.IsRequired(true); // 引用信息是可选的
q.Property(qi => qi.MessageId).HasColumnName("Quote_MsgId");
q.Property(qi => qi.SenderId).HasColumnName("Quote_SenderId");
q.Property(qi => qi.SenderName).HasMaxLength(50).HasColumnName("Quote_SenderName");
q.Property(qi => qi.MessageType).HasColumnName("Quote_MsgType");
q.Property(qi => qi.Preview).HasMaxLength(100).HasColumnName("Quote_Preview");
});
});
}
}
}
@@ -0,0 +1,25 @@
using IM.Infrastructure.Efcore;
using MediatR;
using MessageService.Domain.Entities;
using Microsoft.EntityFrameworkCore;
namespace MessageService.Infrastructure
{
public class MessageDbContext : BaseDbContext
{
public DbSet<Message> Messages { get; private set; }
public DbSet<Conversation> Conversations { get; private set; }
public MessageDbContext(DbContextOptions options, IMediator mediator) : base(options, mediator)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
modelBuilder.EnableSoftDeletionGlobalFilter();
}
}
}
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="9.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\IM.Commons\IM.Commons.csproj" />
<ProjectReference Include="..\Infrastructure\IM.Infrastructure.csproj" />
<ProjectReference Include="..\MessageService.Domain\MessageService.Domain.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,187 @@
// <auto-generated />
using System;
using System.Collections.Generic;
using MessageService.Infrastructure;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace MessageService.Infrastructure.Migrations
{
[DbContext(typeof(MessageDbContext))]
[Migration("20260423115234_InitMessageDb")]
partial class InitMessageDb
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "9.0.0")
.HasAnnotation("Relational:MaxIdentifierLength", 64);
MySqlModelBuilderExtensions.AutoIncrementColumns(modelBuilder);
modelBuilder.Entity("MessageService.Domain.Entities.Conversation", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<int>("ChatType")
.HasColumnType("int");
b.Property<DateTimeOffset>("CreationTime")
.HasColumnType("datetime(6)");
b.Property<DateTimeOffset?>("Deletion")
.HasColumnType("datetime(6)");
b.Property<bool>("IsDeleted")
.HasColumnType("tinyint(1)");
b.Property<string>("LastMessage")
.IsRequired()
.HasColumnType("longtext");
b.Property<long?>("LastReadSequenceId")
.HasColumnType("bigint");
b.Property<DateTimeOffset?>("ModificationTime")
.HasColumnType("datetime(6)");
b.Property<string>("StreamKey")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("TargetAvatar")
.IsRequired()
.HasColumnType("longtext");
b.Property<Guid>("TargetId")
.HasColumnType("char(36)");
b.Property<string>("TargetName")
.IsRequired()
.HasColumnType("longtext");
b.Property<int>("UnreadCount")
.HasColumnType("int");
b.Property<Guid>("UserId")
.HasColumnType("char(36)");
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("conversations", (string)null);
});
modelBuilder.Entity("MessageService.Domain.Entities.Message", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<int>("ChatType")
.HasColumnType("int");
b.Property<Guid>("ClientMsgId")
.HasColumnType("char(36)");
b.Property<DateTimeOffset>("CreationTime")
.HasColumnType("datetime(6)");
b.Property<DateTimeOffset?>("Deletion")
.HasColumnType("datetime(6)");
b.Property<bool>("IsDeleted")
.HasColumnType("tinyint(1)");
b.Property<DateTimeOffset?>("ModificationTime")
.HasColumnType("datetime(6)");
b.Property<int>("MsgType")
.HasColumnType("int");
b.Property<Guid>("SenderId")
.HasColumnType("char(36)");
b.Property<long>("SequenceId")
.HasColumnType("bigint");
b.Property<int>("State")
.HasColumnType("int");
b.Property<string>("StreamKey")
.IsRequired()
.HasColumnType("varchar(255)");
b.Property<Guid>("TargetId")
.HasColumnType("char(36)");
b.ComplexProperty<Dictionary<string, object>>("Content", "MessageService.Domain.Entities.Message.Content#MessageContent", b1 =>
{
b1.IsRequired();
b1.Property<string>("Fallback")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("varchar(255)")
.HasAnnotation("Relational:JsonPropertyName", "fallback");
b1.Property<string>("RawBody")
.IsRequired()
.HasColumnType("longtext")
.HasColumnName("Content_Body");
b1.Property<string>("RawExt")
.IsRequired()
.HasColumnType("longtext")
.HasColumnName("Content_Ext");
b1.ComplexProperty<Dictionary<string, object>>("Quote", "MessageService.Domain.Entities.Message.Content#MessageContent.Quote#QuoteInfo", b2 =>
{
b2.IsRequired();
b2.Property<Guid>("MessageId")
.HasColumnType("char(36)")
.HasColumnName("Quote_MsgId");
b2.Property<int>("MessageType")
.HasColumnType("int")
.HasColumnName("Quote_MsgType");
b2.Property<string>("Preview")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("varchar(100)")
.HasColumnName("Quote_Preview");
b2.Property<Guid>("SenderId")
.HasColumnType("char(36)")
.HasColumnName("Quote_SenderId");
b2.Property<string>("SenderName")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("varchar(50)")
.HasColumnName("Quote_SenderName");
});
});
b.HasKey("Id");
b.HasIndex("StreamKey", "SequenceId");
b.ToTable("messages", (string)null);
});
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,99 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace MessageService.Infrastructure.Migrations
{
/// <inheritdoc />
public partial class InitMessageDb : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterDatabase()
.Annotation("MySql:Charset", "utf8mb4");
migrationBuilder.CreateTable(
name: "conversations",
columns: table => new
{
Id = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
UserId = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
TargetId = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
TargetAvatar = table.Column<string>(type: "longtext", nullable: false)
.Annotation("MySql:Charset", "utf8mb4"),
TargetName = table.Column<string>(type: "longtext", nullable: false)
.Annotation("MySql:Charset", "utf8mb4"),
LastReadSequenceId = table.Column<long>(type: "bigint", nullable: true),
UnreadCount = table.Column<int>(type: "int", nullable: false),
ChatType = table.Column<int>(type: "int", nullable: false),
StreamKey = table.Column<string>(type: "longtext", nullable: false)
.Annotation("MySql:Charset", "utf8mb4"),
LastMessage = table.Column<string>(type: "longtext", nullable: false)
.Annotation("MySql:Charset", "utf8mb4"),
IsDeleted = table.Column<bool>(type: "tinyint(1)", nullable: false),
CreationTime = table.Column<DateTimeOffset>(type: "datetime(6)", nullable: false),
Deletion = table.Column<DateTimeOffset>(type: "datetime(6)", nullable: true),
ModificationTime = table.Column<DateTimeOffset>(type: "datetime(6)", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_conversations", x => x.Id);
})
.Annotation("MySql:Charset", "utf8mb4");
migrationBuilder.CreateTable(
name: "messages",
columns: table => new
{
Id = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
ChatType = table.Column<int>(type: "int", nullable: false),
MsgType = table.Column<int>(type: "int", nullable: false),
ClientMsgId = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
SenderId = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
TargetId = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
State = table.Column<int>(type: "int", nullable: false),
StreamKey = table.Column<string>(type: "varchar(255)", nullable: false)
.Annotation("MySql:Charset", "utf8mb4"),
SequenceId = table.Column<long>(type: "bigint", nullable: false),
Content_Fallback = table.Column<string>(type: "varchar(255)", maxLength: 255, nullable: false),
Content_Body = table.Column<string>(type: "longtext", nullable: false),
Content_Ext = table.Column<string>(type: "longtext", nullable: false),
Quote_MsgId = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
Quote_MsgType = table.Column<int>(type: "int", nullable: false),
Quote_Preview = table.Column<string>(type: "varchar(100)", maxLength: 100, nullable: false),
Quote_SenderId = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
Quote_SenderName = table.Column<string>(type: "varchar(50)", maxLength: 50, nullable: false),
IsDeleted = table.Column<bool>(type: "tinyint(1)", nullable: false),
CreationTime = table.Column<DateTimeOffset>(type: "datetime(6)", nullable: false),
Deletion = table.Column<DateTimeOffset>(type: "datetime(6)", nullable: true),
ModificationTime = table.Column<DateTimeOffset>(type: "datetime(6)", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_messages", x => x.Id);
})
.Annotation("MySql:Charset", "utf8mb4");
migrationBuilder.CreateIndex(
name: "IX_conversations_UserId",
table: "conversations",
column: "UserId");
migrationBuilder.CreateIndex(
name: "IX_messages_StreamKey_SequenceId",
table: "messages",
columns: new[] { "StreamKey", "SequenceId" });
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "conversations");
migrationBuilder.DropTable(
name: "messages");
}
}
}
@@ -0,0 +1,184 @@
// <auto-generated />
using System;
using System.Collections.Generic;
using MessageService.Infrastructure;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace MessageService.Infrastructure.Migrations
{
[DbContext(typeof(MessageDbContext))]
partial class MessageDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "9.0.0")
.HasAnnotation("Relational:MaxIdentifierLength", 64);
MySqlModelBuilderExtensions.AutoIncrementColumns(modelBuilder);
modelBuilder.Entity("MessageService.Domain.Entities.Conversation", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<int>("ChatType")
.HasColumnType("int");
b.Property<DateTimeOffset>("CreationTime")
.HasColumnType("datetime(6)");
b.Property<DateTimeOffset?>("Deletion")
.HasColumnType("datetime(6)");
b.Property<bool>("IsDeleted")
.HasColumnType("tinyint(1)");
b.Property<string>("LastMessage")
.IsRequired()
.HasColumnType("longtext");
b.Property<long?>("LastReadSequenceId")
.HasColumnType("bigint");
b.Property<DateTimeOffset?>("ModificationTime")
.HasColumnType("datetime(6)");
b.Property<string>("StreamKey")
.IsRequired()
.HasColumnType("longtext");
b.Property<string>("TargetAvatar")
.IsRequired()
.HasColumnType("longtext");
b.Property<Guid>("TargetId")
.HasColumnType("char(36)");
b.Property<string>("TargetName")
.IsRequired()
.HasColumnType("longtext");
b.Property<int>("UnreadCount")
.HasColumnType("int");
b.Property<Guid>("UserId")
.HasColumnType("char(36)");
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("conversations", (string)null);
});
modelBuilder.Entity("MessageService.Domain.Entities.Message", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<int>("ChatType")
.HasColumnType("int");
b.Property<Guid>("ClientMsgId")
.HasColumnType("char(36)");
b.Property<DateTimeOffset>("CreationTime")
.HasColumnType("datetime(6)");
b.Property<DateTimeOffset?>("Deletion")
.HasColumnType("datetime(6)");
b.Property<bool>("IsDeleted")
.HasColumnType("tinyint(1)");
b.Property<DateTimeOffset?>("ModificationTime")
.HasColumnType("datetime(6)");
b.Property<int>("MsgType")
.HasColumnType("int");
b.Property<Guid>("SenderId")
.HasColumnType("char(36)");
b.Property<long>("SequenceId")
.HasColumnType("bigint");
b.Property<int>("State")
.HasColumnType("int");
b.Property<string>("StreamKey")
.IsRequired()
.HasColumnType("varchar(255)");
b.Property<Guid>("TargetId")
.HasColumnType("char(36)");
b.ComplexProperty<Dictionary<string, object>>("Content", "MessageService.Domain.Entities.Message.Content#MessageContent", b1 =>
{
b1.IsRequired();
b1.Property<string>("Fallback")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("varchar(255)")
.HasAnnotation("Relational:JsonPropertyName", "fallback");
b1.Property<string>("RawBody")
.IsRequired()
.HasColumnType("longtext")
.HasColumnName("Content_Body");
b1.Property<string>("RawExt")
.IsRequired()
.HasColumnType("longtext")
.HasColumnName("Content_Ext");
b1.ComplexProperty<Dictionary<string, object>>("Quote", "MessageService.Domain.Entities.Message.Content#MessageContent.Quote#QuoteInfo", b2 =>
{
b2.IsRequired();
b2.Property<Guid>("MessageId")
.HasColumnType("char(36)")
.HasColumnName("Quote_MsgId");
b2.Property<int>("MessageType")
.HasColumnType("int")
.HasColumnName("Quote_MsgType");
b2.Property<string>("Preview")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("varchar(100)")
.HasColumnName("Quote_Preview");
b2.Property<Guid>("SenderId")
.HasColumnType("char(36)")
.HasColumnName("Quote_SenderId");
b2.Property<string>("SenderName")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("varchar(50)")
.HasColumnName("Quote_SenderName");
});
});
b.HasKey("Id");
b.HasIndex("StreamKey", "SequenceId");
b.ToTable("messages", (string)null);
});
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,17 @@
using IM.Commons;
using MessageService.Domain.IReposities;
using MessageService.Infrastructure.Reposities;
using Microsoft.Extensions.DependencyInjection;
namespace MessageService.Infrastructure
{
public class ModuleInit : IModuleInitializer
{
public void Initialize(IServiceCollection services)
{
services.AddScoped<IMessageReposity, MessageReposity>();
services.AddScoped<IConversationReposity, ConversationReposity>();
}
}
}
@@ -0,0 +1,48 @@
using MessageService.Domain.Entities;
using MessageService.Domain.IReposities;
using Microsoft.EntityFrameworkCore;
namespace MessageService.Infrastructure.Reposities
{
public class ConversationReposity : IConversationReposity
{
private readonly MessageDbContext db;
public ConversationReposity(MessageDbContext db)
{
this.db = db;
}
public void Create(Conversation conversation)
{
db.Conversations.Add(conversation);
}
public async Task<IEnumerable<string>> FindAllStreamKeyAsync(Guid userId)
{
return await db.Conversations.Where(x => x.UserId == userId)
.Select(s => s.StreamKey)
.ToListAsync();
}
public async Task<Conversation?> FindByIdAsync(Guid id)
{
return await db.Conversations.FirstOrDefaultAsync(x => x.Id == id);
}
public async Task<IEnumerable<Conversation>> FindByStreamKeyAsync(string streamKey)
{
return await db.Conversations.Where(x => x.StreamKey == streamKey).ToListAsync();
}
public async Task<IEnumerable<Conversation>> FindByTargetIdAsync(Guid targetId)
{
return await db.Conversations.Where(x => x.TargetId == targetId).ToListAsync();
}
public async Task<IEnumerable<Conversation>> FindByUserIdAsync(Guid userId)
{
return await db.Conversations.Where(x => x.UserId == userId).ToListAsync();
}
}
}
@@ -0,0 +1,55 @@
using MessageService.Domain.Entities;
using MessageService.Domain.IReposities;
using Microsoft.EntityFrameworkCore;
namespace MessageService.Infrastructure.Reposities
{
public class MessageReposity : IMessageReposity
{
private readonly MessageDbContext db;
public MessageReposity(MessageDbContext db)
{
this.db = db;
}
public void Create(Message message)
{
db.Messages.Add(message);
}
public async Task<Message?> FindByIdAsync(Guid id)
{
return await db.Messages.FirstOrDefaultAsync(x => x.Id == id);
}
public async Task<(IEnumerable<Message> messages, bool hasMore)> GetAsync(string streamKey, long? cusor, int direction, int limit)
{
var query = db.Messages.Where(x => x.StreamKey == streamKey);
List<Message> messages = [];
if (direction == 0) // Before: 找比锚点小的,按倒序排
{
if (cusor.HasValue)
query = query.Where(m => m.SequenceId < cusor.Value);
var list = await query
.OrderByDescending(m => m.SequenceId) // 最新消息在最前
.Take(limit + 1)
.ToListAsync();
messages = [.. list.OrderBy(s => s.SequenceId)];
}
else
{
if (cusor is null)
return (messages, false);
messages = await query.OrderBy(o => o.SequenceId)
.Take(limit + 1)
.ToListAsync();
}
return (messages, messages.Count > limit);
}
}
}