From e441bb3e705103fde3fd6406d0dcb4fb0cce356a Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Wed, 28 Feb 2024 19:54:56 +0100 Subject: [PATCH] [backend/core] Resolve user profile mentions on local user update (ISH-101) --- .../Controllers/Mastodon/AccountController.cs | 11 +--------- .../Core/Services/UserService.cs | 21 ++++++++++++++++++- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Iceshrimp.Backend/Controllers/Mastodon/AccountController.cs b/Iceshrimp.Backend/Controllers/Mastodon/AccountController.cs index 8059aa1a..b7a09386 100644 --- a/Iceshrimp.Backend/Controllers/Mastodon/AccountController.cs +++ b/Iceshrimp.Backend/Controllers/Mastodon/AccountController.cs @@ -107,16 +107,7 @@ public class AccountController( user.BannerUrl = banner.Url; } - db.Update(user); - db.Update(user.UserProfile); - await db.SaveChangesAsync(); - await userSvc.UpdateLocalUserAsync(user); - - if (prevAvatarId != null && user.Avatar?.Id != prevAvatarId) - await driveSvc.RemoveFile(prevAvatarId); - - if (prevBannerId != null && user.Banner?.Id != prevBannerId) - await driveSvc.RemoveFile(prevBannerId); + user = await userSvc.UpdateLocalUserAsync(user, prevAvatarId, prevBannerId); var res = await userRenderer.RenderAsync(user, user.UserProfile, source: true); return Ok(res); diff --git a/Iceshrimp.Backend/Core/Services/UserService.cs b/Iceshrimp.Backend/Core/Services/UserService.cs index b4b77177..1a88a394 100644 --- a/Iceshrimp.Backend/Core/Services/UserService.cs +++ b/Iceshrimp.Backend/Core/Services/UserService.cs @@ -296,11 +296,30 @@ public class UserService( return user; } - public async Task UpdateLocalUserAsync(User user) + public async Task UpdateLocalUserAsync(User user, string? prevAvatarId, string? prevBannerId) { if (user.Host != null) throw new Exception("This method is only valid for local users"); + if (user.UserProfile == null) throw new Exception("user.UserProfile must not be null at this stage"); + + db.Update(user); + db.Update(user.UserProfile); + await db.SaveChangesAsync(); + + user = await UpdateProfileMentions(user, null); + var activity = activityRenderer.RenderUpdate(await userRenderer.RenderAsync(user)); await deliverSvc.DeliverToFollowersAsync(activity, user, []); + + _ = followupTaskSvc.ExecuteTask("UpdateLocalUserAsync", async provider => + { + var bgDriveSvc = provider.GetRequiredService(); + if (prevAvatarId != null && user.Avatar?.Id != prevAvatarId) + await bgDriveSvc.RemoveFile(prevAvatarId); + if (prevBannerId != null && user.Banner?.Id != prevBannerId) + await bgDriveSvc.RemoveFile(prevBannerId); + }); + + return user; } public async Task CreateLocalUserAsync(string username, string password, string? invite)