[backend/core] Fail early when user with acct already exists

This commit is contained in:
Laura Hausmann 2024-09-14 02:54:32 +02:00
parent 238b441560
commit b51d3826ed
No known key found for this signature in database
GPG key ID: D044E84C5BE01605

View file

@ -119,6 +119,12 @@ public class UserService(
actor.Normalize(uri); 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) if (actor.Id != uri)
throw GracefulException.UnprocessableEntity("Uri doesn't match id of fetched actor"); throw GracefulException.UnprocessableEntity("Uri doesn't match id of fetched actor");
if (actor.PublicKey?.Id == null || actor.PublicKey?.PublicKey == null) if (actor.PublicKey?.Id == null || actor.PublicKey?.PublicKey == null)
@ -153,7 +159,7 @@ public class UserService(
IsBot = actor.IsBot, IsBot = actor.IsBot,
Username = actor.Username!, Username = actor.Username!,
UsernameLower = actor.Username!.ToLowerInvariant(), UsernameLower = actor.Username!.ToLowerInvariant(),
Host = AcctToTuple(acct).Host, Host = host,
MovedToUri = actor.MovedTo?.Link, MovedToUri = actor.MovedTo?.Link,
AlsoKnownAs = actor.AlsoKnownAs?.Where(p => p.Link != null).Select(p => p.Link!).ToList(), AlsoKnownAs = actor.AlsoKnownAs?.Where(p => p.Link != null).Select(p => p.Link!).ToList(),
IsExplorable = actor.IsDiscoverable ?? false, IsExplorable = actor.IsDiscoverable ?? false,