From 18bcba7e30b62c3da4d1841633595e172aad28e3 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Fri, 1 Mar 2024 18:24:01 +0100 Subject: [PATCH] [backend/core] Fix entity reloading from database --- Iceshrimp.Backend/Core/Database/DatabaseContext.cs | 14 ++++++++++++++ .../Core/Federation/ActivityPub/UserResolver.cs | 5 ++++- Iceshrimp.Backend/Core/Services/UserService.cs | 5 ++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Iceshrimp.Backend/Core/Database/DatabaseContext.cs b/Iceshrimp.Backend/Core/Database/DatabaseContext.cs index 42fc53ae..9d849c8b 100644 --- a/Iceshrimp.Backend/Core/Database/DatabaseContext.cs +++ b/Iceshrimp.Backend/Core/Database/DatabaseContext.cs @@ -1139,6 +1139,20 @@ public class DatabaseContext(DbContextOptions options) }); } + public async Task ReloadEntityAsync(object entity) + { + await Entry(entity).ReloadAsync(); + } + + public async Task ReloadEntityRecursiveAsync(object entity) + { + await ReloadEntityAsync(entity); + await Entry(entity) + .References.Where(p => p is { IsLoaded: true, TargetEntry: not null }) + .Select(p => p.TargetEntry!.ReloadAsync()) + .AwaitAllAsync(); + } + public IQueryable NoteAncestors(string noteId, int depth) => FromExpression(() => NoteAncestors(noteId, depth)); diff --git a/Iceshrimp.Backend/Core/Federation/ActivityPub/UserResolver.cs b/Iceshrimp.Backend/Core/Federation/ActivityPub/UserResolver.cs index 84ba55dd..61d4ea8a 100644 --- a/Iceshrimp.Backend/Core/Federation/ActivityPub/UserResolver.cs +++ b/Iceshrimp.Backend/Core/Federation/ActivityPub/UserResolver.cs @@ -236,6 +236,9 @@ public class UserResolver( logger.LogError("UpdateUserAsync for user {user} failed with {error}", user.Uri, e.Message); } - return success ? await db.Users.IncludeCommonProperties().FirstAsync(p => p.Id == user.Id) : user; + if (success) + await db.ReloadEntityRecursiveAsync(user); + + return user; } } \ No newline at end of file diff --git a/Iceshrimp.Backend/Core/Services/UserService.cs b/Iceshrimp.Backend/Core/Services/UserService.cs index fc03a8dd..2b672f30 100644 --- a/Iceshrimp.Backend/Core/Services/UserService.cs +++ b/Iceshrimp.Backend/Core/Services/UserService.cs @@ -781,6 +781,9 @@ public class UserService( await task.SafeWaitAsync(TimeSpan.FromMilliseconds(500)); - return success ? await db.Users.IncludeCommonProperties().FirstAsync(p => p.Id == user.Id) : user; + if (success) + await db.ReloadEntityRecursiveAsync(user); + + return user; } } \ No newline at end of file