[backend/core] Fix entity reloading from database

This commit is contained in:
Laura Hausmann 2024-03-01 18:24:01 +01:00
parent 1a86ea990a
commit 18bcba7e30
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
3 changed files with 22 additions and 2 deletions

View file

@ -1139,6 +1139,20 @@ public class DatabaseContext(DbContextOptions<DatabaseContext> 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<Note> NoteAncestors(string noteId, int depth) public IQueryable<Note> NoteAncestors(string noteId, int depth)
=> FromExpression(() => NoteAncestors(noteId, depth)); => FromExpression(() => NoteAncestors(noteId, depth));

View file

@ -236,6 +236,9 @@ public class UserResolver(
logger.LogError("UpdateUserAsync for user {user} failed with {error}", user.Uri, e.Message); 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;
} }
} }

View file

@ -781,6 +781,9 @@ public class UserService(
await task.SafeWaitAsync(TimeSpan.FromMilliseconds(500)); 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;
} }
} }