change to default functions for resolving collections

This commit is contained in:
Mar0xy 2023-09-22 02:46:41 +02:00
parent 2bfe51e39d
commit 2bcd2eefc1
No known key found for this signature in database
GPG key ID: 56569BBE47D2C828

View file

@ -291,55 +291,11 @@ export class ApPersonService implements OnModuleInit {
}); });
//#endregion //#endregion
//#region Resolve counts //#region resolve counts
let followersCount: number | undefined; const _resolver = resolver ?? this.apResolverService.createResolver();
const outboxcollection = await _resolver.resolveCollection(person.outbox);
if (typeof person.followers === "string") { const followerscollection = await _resolver.resolveCollection(person.followers!);
try { const followingcollection = await _resolver.resolveCollection(person.following!);
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 { try {
// Start transaction // Start transaction
@ -361,9 +317,9 @@ export class ApPersonService implements OnModuleInit {
host, host,
inbox: person.inbox, inbox: person.inbox,
sharedInbox: person.sharedInbox ?? person.endpoints?.sharedInbox, sharedInbox: person.sharedInbox ?? person.endpoints?.sharedInbox,
notesCount: notesCount !== undefined ? notesCount : person.outbox && typeof person.outbox !== "string" && isCollectionOrOrderedCollection(person.outbox) ? person.outbox.totalItems : undefined, notesCount: outboxcollection.totalItems ?? 0,
followersCount: followersCount !== undefined ? followersCount : person.followers && typeof person.followers !== "string" && isCollectionOrOrderedCollection(person.followers) ? person.followers.totalItems : undefined, followersCount: followerscollection.totalItems ?? 0,
followingCount: followingCount !== undefined ? followingCount : person.following && typeof person.following !== "string" && isCollectionOrOrderedCollection(person.following) ? person.following.totalItems : undefined, followingCount: followingcollection.totalItems ?? 0,
followersUri: person.followers ? getApId(person.followers) : undefined, followersUri: person.followers ? getApId(person.followers) : undefined,
featured: person.featured ? getApId(person.featured) : undefined, featured: person.featured ? getApId(person.featured) : undefined,
uri: person.id, uri: person.id,
@ -493,63 +449,10 @@ export class ApPersonService implements OnModuleInit {
throw new Error('unexpected schema of person url: ' + url); 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 = { const updates = {
lastFetchedAt: new Date(), lastFetchedAt: new Date(),
inbox: person.inbox, inbox: person.inbox,
sharedInbox: person.sharedInbox ?? person.endpoints?.sharedInbox, 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, followersUri: person.followers ? getApId(person.followers) : undefined,
featured: person.featured, featured: person.featured,
emojis: emojiNames, emojis: emojiNames,