From 36281ec413d4250221595d2a173c19dc15d0480a Mon Sep 17 00:00:00 2001 From: s1idewhist1e Date: Fri, 17 Mar 2023 01:16:31 +0000 Subject: [PATCH] feat: Make follower counts for remote users correct (#9705) #9293 Not sure if this is the right approach for this Co-authored-by: s1idewhist1e Reviewed-on: https://codeberg.org/calckey/calckey/pulls/9705 Co-authored-by: s1idewhist1e Co-committed-by: s1idewhist1e --- .../backend/src/models/repositories/user.ts | 4 +- .../src/remote/activitypub/models/person.ts | 78 ++++++++++++++++++- 2 files changed, 78 insertions(+), 4 deletions(-) diff --git a/packages/backend/src/models/repositories/user.ts b/packages/backend/src/models/repositories/user.ts index aa224b6674..0455b9a5f7 100644 --- a/packages/backend/src/models/repositories/user.ts +++ b/packages/backend/src/models/repositories/user.ts @@ -488,8 +488,8 @@ export const UserRepository = db.getRepository(User).extend({ birthday: profile!.birthday, lang: profile!.lang, fields: profile!.fields, - followersCount: followersCount || 0, - followingCount: followingCount || 0, + followersCount: followersCount || '?', + followingCount: followingCount || '?', notesCount: user.notesCount, pinnedNoteIds: pins.map((pin) => pin.noteId), pinnedNotes: Notes.packMany( diff --git a/packages/backend/src/remote/activitypub/models/person.ts b/packages/backend/src/remote/activitypub/models/person.ts index 16b265b0b3..f4f792df15 100644 --- a/packages/backend/src/remote/activitypub/models/person.ts +++ b/packages/backend/src/remote/activitypub/models/person.ts @@ -198,9 +198,36 @@ export async function createPerson( const url = getOneApHrefNullable(person.url); if (url && !url.startsWith("https://")) { - throw new Error(`unexpected shcema of person url: ${url}`); + throw new Error(`unexpected schema of person url: ${url}`); } + let followersCount: number | undefined; + + if (typeof person.followers === "string") { + try { + let data = await fetch(person.followers, { headers: { "Accept": "application/json" } }); + let json_data = JSON.parse(await data.text()); + + followersCount = json_data.totalItems; + } catch { + followersCount = undefined; + } + } + + let followingCount: number | undefined; + + if (typeof person.following === "string") { + try { + let data = await fetch(person.following, { headers: { "Accept": "application/json" } }); + let json_data = JSON.parse(await data.text()); + + followingCount = json_data.totalItems; + } catch (e) { + followingCount = undefined; + } + } + + // Create user let user: IRemoteUser; try { @@ -228,6 +255,16 @@ export async function createPerson( followersUri: person.followers ? getApId(person.followers) : undefined, + followersCount: followersCount !== undefined + ? followersCount + : person.followers && typeof person.followers !== "string" && isCollectionOrOrderedCollection(person.followers) + ? person.followers.totalItems + : undefined, + followingCount: followingCount !== undefined + ? followingCount + : person.following && typeof person.following !== "string" && isCollectionOrOrderedCollection(person.following) + ? person.following.totalItems + : undefined, featured: person.featured ? getApId(person.featured) : undefined, uri: person.id, tags, @@ -396,7 +433,34 @@ export async function updatePerson( const url = getOneApHrefNullable(person.url); if (url && !url.startsWith("https://")) { - throw new Error(`unexpected shcema of person url: ${url}`); + throw new Error(`unexpected schema of person url: ${url}`); + } + + let followersCount: number | undefined; + + if (typeof person.followers === "string") { + try { + let data = await fetch(person.followers, { headers: { "Accept": "application/json" } } ); + let json_data = JSON.parse(await data.text()); + + followersCount = json_data.totalItems; + } catch { + followersCount = undefined; + } + } + + + let followingCount: number | undefined; + + if (typeof person.following === "string") { + try { + let data = await fetch(person.following, { headers: { "Accept": "application/json" } } ); + let json_data = JSON.parse(await data.text()); + + followingCount = json_data.totalItems; + } catch { + followingCount = undefined; + } } const updates = { @@ -406,6 +470,16 @@ export async function updatePerson( person.sharedInbox || (person.endpoints ? person.endpoints.sharedInbox : undefined), followersUri: person.followers ? getApId(person.followers) : undefined, + followersCount: followersCount !== undefined + ? followersCount + : person.followers && typeof person.followers !== "string" && isCollectionOrOrderedCollection(person.followers) + ? person.followers.totalItems + : undefined, + followingCount: followingCount !== undefined + ? followingCount + : person.following && typeof person.following !== "string" && isCollectionOrOrderedCollection(person.following) + ? person.following.totalItems + : undefined, featured: person.featured, emojis: emojiNames, name: truncate(person.name, nameLength),