From e9e63ebaa87f9202fdcb0244f13633a13639d9da Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Sat, 9 Sep 2023 18:31:04 +0200 Subject: [PATCH] Verify and update existing user on createPerson --- .../src/remote/activitypub/models/person.ts | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/packages/backend/src/remote/activitypub/models/person.ts b/packages/backend/src/remote/activitypub/models/person.ts index 73f49d20a..ec747017e 100644 --- a/packages/backend/src/remote/activitypub/models/person.ts +++ b/packages/backend/src/remote/activitypub/models/person.ts @@ -187,6 +187,43 @@ export async function createPerson( const host = subjectHost ?? await getSubjectHostFromUri(object.id) ?? toPuny(new URL(object.id).hostname); + let checkUser = (await Users.findOneBy({ + usernameLower: person.preferredUsername!.toLowerCase(), + host: toPuny(new URL(object.id).hostname), + })) as IRemoteUser | null; + + if (checkUser != null) { + logger.info('Person already exists'); + if (host != checkUser.host) { + logger.info('Updating existing person with canonical account domain'); + checkUser.host = host; + await Users.update( + { + usernameLower: checkUser.usernameLower, + host: checkUser.host, + }, + { + host: host, + }, + ); + } + logger.info('Returning existing person'); + return checkUser; + } + + if (host != toPuny(new URL(object.id).hostname)) { + checkUser = (await Users.findOneBy({ + usernameLower: person.preferredUsername!.toLowerCase(), + host: host, + })) as IRemoteUser | null; + + if (checkUser != null) { + logger.info('Person already exists'); + logger.info('Returning existing person'); + return checkUser; + } + } + const { fields } = analyzeAttachments(person.attachment || []); const tags = extractApHashtags(person.tag)