From ee42edfccde8643e7b855b681f56f723e039e96f Mon Sep 17 00:00:00 2001 From: Marie Piontek Date: Sat, 12 Aug 2023 01:00:29 +0000 Subject: [PATCH] feat: Fetch total posts of users on create/update --- .../src/remote/activitypub/models/person.ts | 48 ++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/packages/backend/src/remote/activitypub/models/person.ts b/packages/backend/src/remote/activitypub/models/person.ts index 8f17cfae8b..56520a9485 100644 --- a/packages/backend/src/remote/activitypub/models/person.ts +++ b/packages/backend/src/remote/activitypub/models/person.ts @@ -231,6 +231,21 @@ export async function createPerson( } } + let notesCount: number | undefined; + + if (typeof person.outbox === "string") { + try { + let data = await fetch(person.outbox, { + headers: { Accept: "application/json" }, + }); + let json_data = JSON.parse(await data.text()); + + notesCount = json_data.totalItems; + } catch (e) { + notesCount = undefined; + } + } + // Create user let user: IRemoteUser; try { @@ -274,6 +289,14 @@ export async function createPerson( isCollectionOrOrderedCollection(person.following) ? person.following.totalItems : undefined, + notesCount: + notesCount !== undefined + ? notesCount + : person.outbox && + typeof person.outbox !== "string" && + isCollectionOrOrderedCollection(person.outbox) + ? person.outbox.totalItems + : undefined, featured: person.featured ? getApId(person.featured) : undefined, uri: person.id, tags, @@ -472,6 +495,21 @@ export async function updatePerson( } } + let notesCount: number | undefined; + + if (typeof person.outbox === "string") { + try { + let data = await fetch(person.outbox, { + headers: { Accept: "application/json" }, + }); + let json_data = JSON.parse(await data.text()); + + notesCount = json_data.totalItems; + } catch (e) { + notesCount = undefined; + } + } + const updates = { lastFetchedAt: new Date(), inbox: person.inbox, @@ -495,6 +533,14 @@ export async function updatePerson( isCollectionOrOrderedCollection(person.following) ? person.following.totalItems : undefined, + notesCount: + notesCount !== undefined + ? notesCount + : person.outbox && + typeof person.outbox !== "string" && + isCollectionOrOrderedCollection(person.outbox) + ? person.outbox.totalItems + : undefined, featured: person.featured, emojis: emojiNames, name: truncate(person.name, nameLength), @@ -663,7 +709,7 @@ export async function updateFeatured(userId: User["id"], resolver?: Resolver) { ? collection.items : collection.orderedItems; const items = await Promise.all( - toArray(unresolvedItems).map((x) => resolver.resolve(x)), + toArray(unresolvedItems).map((x) => resolver?.resolve(x)), ); // Resolve and regist Notes