From b51d3826ed397ac93a1405a16a79d340ad528804 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Sat, 14 Sep 2024 02:54:32 +0200 Subject: [PATCH] [backend/core] Fail early when user with acct already exists --- Iceshrimp.Backend/Core/Services/UserService.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Iceshrimp.Backend/Core/Services/UserService.cs b/Iceshrimp.Backend/Core/Services/UserService.cs index 876c119b..ccabdfd3 100644 --- a/Iceshrimp.Backend/Core/Services/UserService.cs +++ b/Iceshrimp.Backend/Core/Services/UserService.cs @@ -119,6 +119,12 @@ public class UserService( actor.Normalize(uri); + user = await db.Users.FirstOrDefaultAsync(p => p.UsernameLower == actor.Username!.ToLowerInvariant() && + p.Host == host); + if (user is not null) + throw GracefulException + .UnprocessableEntity($"A user with acct @{user.UsernameLower}@{user.Host} already exists: {user.Uri}"); + if (actor.Id != uri) throw GracefulException.UnprocessableEntity("Uri doesn't match id of fetched actor"); if (actor.PublicKey?.Id == null || actor.PublicKey?.PublicKey == null) @@ -153,7 +159,7 @@ public class UserService( IsBot = actor.IsBot, Username = actor.Username!, UsernameLower = actor.Username!.ToLowerInvariant(), - Host = AcctToTuple(acct).Host, + Host = host, MovedToUri = actor.MovedTo?.Link, AlsoKnownAs = actor.AlsoKnownAs?.Where(p => p.Link != null).Select(p => p.Link!).ToList(), IsExplorable = actor.IsDiscoverable ?? false,