upd: fetch counts for notes, followers and following

This commit is contained in:
Mar0xy 2023-09-22 02:23:17 +02:00
parent 57c37a8938
commit 2bfe51e39d
No known key found for this signature in database
GPG Key ID: 56569BBE47D2C828
1 changed files with 106 additions and 0 deletions

View File

@ -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,