fix: align backend APIs and upload flow
This commit is contained in:
@@ -11,6 +11,7 @@ namespace MessageService.Infrastructure.Configs
|
||||
builder.ToTable("conversations");
|
||||
builder.HasKey(x => x.Id);
|
||||
builder.HasIndex(x => x.UserId);
|
||||
builder.HasIndex(x => new { x.UserId, x.ChatType, x.TargetId, x.IsDeleted });
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
namespace MessageService.Infrastructure.Migrations
|
||||
{
|
||||
[DbContext(typeof(MessageDbContext))]
|
||||
[Migration("20260909000100_ApiAlignmentFixes")]
|
||||
public partial class ApiAlignmentFixes : Migration
|
||||
{
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_conversations_UserId_ChatType_TargetId_IsDeleted",
|
||||
table: "conversations",
|
||||
columns: new[] { "UserId", "ChatType", "TargetId", "IsDeleted" });
|
||||
}
|
||||
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropIndex(
|
||||
name: "IX_conversations_UserId_ChatType_TargetId_IsDeleted",
|
||||
table: "conversations");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -76,6 +76,8 @@ namespace MessageService.Infrastructure.Migrations
|
||||
|
||||
b.HasIndex("UserId");
|
||||
|
||||
b.HasIndex("UserId", "ChatType", "TargetId", "IsDeleted");
|
||||
|
||||
b.ToTable("conversations", (string)null);
|
||||
});
|
||||
|
||||
|
||||
@@ -26,30 +26,32 @@ namespace MessageService.Infrastructure.Reposities
|
||||
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 = [];
|
||||
List<Message> fetched;
|
||||
if (direction == 0) // Before: 找比锚点小的,按倒序排
|
||||
{
|
||||
if (cusor.HasValue)
|
||||
query = query.Where(m => m.SequenceId < cusor.Value);
|
||||
|
||||
var list = await query
|
||||
fetched = await query
|
||||
.OrderByDescending(m => m.SequenceId) // 最新消息在最前
|
||||
.Take(limit + 1)
|
||||
.ToListAsync();
|
||||
|
||||
messages = [.. list.OrderBy(s => s.SequenceId)];
|
||||
}
|
||||
else
|
||||
{
|
||||
if (cusor is null)
|
||||
return (messages, false);
|
||||
return (Array.Empty<Message>(), false);
|
||||
|
||||
messages = await query.OrderBy(o => o.SequenceId)
|
||||
fetched = await query
|
||||
.Where(m => m.SequenceId > cusor.Value)
|
||||
.OrderBy(o => o.SequenceId)
|
||||
.Take(limit + 1)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
return (messages, messages.Count > limit);
|
||||
var hasMore = fetched.Count > limit;
|
||||
var messages = fetched.Take(limit).OrderBy(s => s.SequenceId).ToList();
|
||||
return (messages, hasMore);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user