From 2bfe51e39da318b442616881c70fa9fe3cba715d Mon Sep 17 00:00:00 2001 From: Mar0xy Date: Fri, 22 Sep 2023 02:23:17 +0200 Subject: [PATCH] upd: fetch counts for notes, followers and following --- .../activitypub/models/ApPersonService.ts | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/packages/backend/src/core/activitypub/models/ApPersonService.ts b/packages/backend/src/core/activitypub/models/ApPersonService.ts index ea64883395..53105c6f2f 100644 --- a/packages/backend/src/core/activitypub/models/ApPersonService.ts +++ b/packages/backend/src/core/activitypub/models/ApPersonService.ts @@ -291,6 +291,56 @@ export class ApPersonService implements OnModuleInit { }); //#endregion + //#region Resolve counts + let followersCount: number | undefined; + + if (typeof person.followers === "string") { + try { + this.logger.info(person.followers); + const data = await fetch(person.followers, { + headers: { Accept: "application/json" }, + }); + const json_data = JSON.parse(await data.text()); + + followersCount = json_data.totalItems; + } catch { + followersCount = undefined; + } + } + + let followingCount: number | undefined; + + if (typeof person.following === "string") { + try { + this.logger.info(person.following); + const data = await fetch(person.following, { + headers: { Accept: "application/json" }, + }); + const json_data = JSON.parse(await data.text()); + + followingCount = json_data.totalItems; + } catch (e) { + followingCount = undefined; + } + } + + let notesCount: number | undefined; + + if (typeof person.outbox === "string") { + try { + this.logger.info(person.outbox); + const data = await fetch(person.outbox, { + headers: { Accept: "application/json" }, + }); + const json_data = JSON.parse(await data.text()); + + notesCount = json_data.totalItems; + } catch (e) { + notesCount = undefined; + } + } + //#endregion + try { // Start transaction await this.db.transaction(async transactionalEntityManager => { @@ -311,6 +361,9 @@ export class ApPersonService implements OnModuleInit { host, inbox: person.inbox, sharedInbox: person.sharedInbox ?? person.endpoints?.sharedInbox, + notesCount: notesCount !== undefined ? notesCount : person.outbox && typeof person.outbox !== "string" && isCollectionOrOrderedCollection(person.outbox) ? person.outbox.totalItems : 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, followersUri: person.followers ? getApId(person.followers) : undefined, featured: person.featured ? getApId(person.featured) : undefined, uri: person.id, @@ -440,10 +493,63 @@ export class ApPersonService implements OnModuleInit { throw new Error('unexpected schema of person url: ' + url); } + //#region Resolve counts + let followersCount: number | undefined; + + if (typeof person.followers === "string") { + try { + this.logger.info(person.followers); + const data = await fetch(person.followers, { + headers: { Accept: "application/json" }, + }); + const json_data = JSON.parse(await data.text()); + + followersCount = json_data.totalItems; + } catch { + followersCount = undefined; + } + } + + let followingCount: number | undefined; + + if (typeof person.following === "string") { + try { + this.logger.info(person.following); + const data = await fetch(person.following, { + headers: { Accept: "application/json" }, + }); + const json_data = JSON.parse(await data.text()); + + followingCount = json_data.totalItems; + } catch (e) { + followingCount = undefined; + } + } + + let notesCount: number | undefined; + + if (typeof person.outbox === "string") { + try { + this.logger.info(person.outbox); + const data = await fetch(person.outbox, { + headers: { Accept: "application/json" }, + }); + const json_data = JSON.parse(await data.text()); + + notesCount = json_data.totalItems; + } catch (e) { + notesCount = undefined; + } + } + //#endregion + const updates = { lastFetchedAt: new Date(), inbox: person.inbox, sharedInbox: person.sharedInbox ?? person.endpoints?.sharedInbox, + notesCount: notesCount !== undefined ? notesCount : person.outbox && typeof person.outbox !== "string" && isCollectionOrOrderedCollection(person.outbox) ? person.outbox.totalItems : 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, followersUri: person.followers ? getApId(person.followers) : undefined, featured: person.featured, emojis: emojiNames,