🐛 Dozens of bug fixes in chat

This commit is contained in:
2025-11-23 01:17:15 +08:00
parent aeef16495f
commit d553ca2ca7
2 changed files with 14 additions and 17 deletions

View File

@@ -29,18 +29,17 @@ public partial class ChatService(
/// This method is designed to be called from a background task /// This method is designed to be called from a background task
/// </summary> /// </summary>
/// <param name="message">The message to process link previews for</param> /// <param name="message">The message to process link previews for</param>
private async Task ProcessMessageLinkPreviewAsync(SnChatMessage message) private async Task CreateLinkPreviewBackgroundAsync(SnChatMessage message)
{ {
try try
{ {
// Create a new scope for database operations // Create a new scope for database operations
using var scope = scopeFactory.CreateScope(); using var scope = scopeFactory.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<AppDatabase>(); var dbContext = scope.ServiceProvider.GetRequiredService<AppDatabase>();
var webReader = scope.ServiceProvider.GetRequiredService<WebReader.WebReaderService>(); var webReader = scope.ServiceProvider.GetRequiredService<WebReaderService>();
var newChat = scope.ServiceProvider.GetRequiredService<ChatService>();
// Preview the links in the message // Preview the links in the message
var updatedMessage = await PreviewMessageLinkAsync(message, webReader); var updatedMessage = await CreateLinkPreviewAsync(message, webReader);
// If embeds were added, update the message in the database // If embeds were added, update the message in the database
if (updatedMessage.Meta != null && if (updatedMessage.Meta != null &&
@@ -89,8 +88,6 @@ public partial class ChatService(
syncMessage.ChatRoom = dbMessage.ChatRoom; syncMessage.ChatRoom = dbMessage.ChatRoom;
using var syncScope = scopeFactory.CreateScope(); using var syncScope = scopeFactory.CreateScope();
var syncCrs = syncScope.ServiceProvider.GetRequiredService<ChatRoomService>();
var syncMembers = await syncCrs.ListRoomMembers(dbMessage.ChatRoomId);
await DeliverMessageAsync( await DeliverMessageAsync(
syncMessage, syncMessage,
@@ -114,7 +111,7 @@ public partial class ChatService(
/// <param name="message">The message to process</param> /// <param name="message">The message to process</param>
/// <param name="webReader">The web reader service</param> /// <param name="webReader">The web reader service</param>
/// <returns>The message with link previews added to its meta data</returns> /// <returns>The message with link previews added to its meta data</returns>
public async Task<SnChatMessage> PreviewMessageLinkAsync(SnChatMessage message, WebReaderService? webReader = null) public async Task<SnChatMessage> CreateLinkPreviewAsync(SnChatMessage message, WebReaderService? webReader = null)
{ {
if (string.IsNullOrEmpty(message.Content)) if (string.IsNullOrEmpty(message.Content))
return message; return message;
@@ -205,6 +202,10 @@ public partial class ChatService(
// Create file references if message has attachments // Create file references if message has attachments
await CreateFileReferencesForMessageAsync(message); await CreateFileReferencesForMessageAsync(message);
// Copy the value to ensure the delivery is correct
message.Sender = sender;
message.ChatRoom = room;
// Then start the delivery process // Then start the delivery process
var localMessage = message; var localMessage = message;
@@ -225,10 +226,8 @@ public partial class ChatService(
// Process link preview in the background to avoid delaying message sending // Process link preview in the background to avoid delaying message sending
var localMessageForPreview = message; var localMessageForPreview = message;
_ = Task.Run(async () => await ProcessMessageLinkPreviewAsync(localMessageForPreview)); _ = Task.Run(async () => await CreateLinkPreviewBackgroundAsync(localMessageForPreview));
message.Sender = sender;
message.ChatRoom = room;
return message; return message;
} }
@@ -251,9 +250,7 @@ public partial class ChatService(
await DeliverWebSocketMessage(message, type, members, scope); await DeliverWebSocketMessage(message, type, members, scope);
if (notify) if (notify)
{
await SendPushNotificationsAsync(message, sender, room, type, members, scope); await SendPushNotificationsAsync(message, sender, room, type, members, scope);
}
} }
private async Task SendPushNotificationsAsync( private async Task SendPushNotificationsAsync(
@@ -608,7 +605,7 @@ public partial class ChatService(
{ {
Type = "call.ended", Type = "call.ended",
ChatRoomId = call.RoomId, ChatRoomId = call.RoomId,
SenderId = call.SenderId, SenderId = sender.Id,
Meta = new Dictionary<string, object> Meta = new Dictionary<string, object>
{ {
{ "call_id", call.Id }, { "call_id", call.Id },
@@ -740,7 +737,7 @@ public partial class ChatService(
// Process link preview in the background if content was updated // Process link preview in the background if content was updated
if (isContentChanged) if (isContentChanged)
_ = Task.Run(async () => await ProcessMessageLinkPreviewAsync(message)); _ = Task.Run(async () => await CreateLinkPreviewBackgroundAsync(message));
if (message.Sender.Account is null) if (message.Sender.Account is null)
message.Sender = await crs.LoadMemberAccount(message.Sender); message.Sender = await crs.LoadMemberAccount(message.Sender);

View File

@@ -203,7 +203,7 @@ public partial class PostService(
} }
// Process link preview in the background to avoid delaying post creation // Process link preview in the background to avoid delaying post creation
_ = Task.Run(async () => await ProcessPostLinkPreviewAsync(post)); _ = Task.Run(async () => await CreateLinkPreviewAsync(post));
return post; return post;
} }
@@ -280,7 +280,7 @@ public partial class PostService(
await db.SaveChangesAsync(); await db.SaveChangesAsync();
// Process link preview in the background to avoid delaying post update // Process link preview in the background to avoid delaying post update
_ = Task.Run(async () => await ProcessPostLinkPreviewAsync(post)); _ = Task.Run(async () => await CreateLinkPreviewAsync(post));
return post; return post;
} }
@@ -348,7 +348,7 @@ public partial class PostService(
/// This method is designed to be called from a background task /// This method is designed to be called from a background task
/// </summary> /// </summary>
/// <param name="post">The post to process link previews for</param> /// <param name="post">The post to process link previews for</param>
private async Task ProcessPostLinkPreviewAsync(SnPost post) private async Task CreateLinkPreviewAsync(SnPost post)
{ {
try try
{ {