feat: Make follower counts for remote users correct (#9705)

#9293

Not sure if this is the right approach for this

Co-authored-by: s1idewhist1e <trombonedude05@gmail.com>
Reviewed-on: https://codeberg.org/calckey/calckey/pulls/9705
Co-authored-by: s1idewhist1e <s1idewhist1e@noreply.codeberg.org>
Co-committed-by: s1idewhist1e <s1idewhist1e@noreply.codeberg.org>
This commit is contained in:
s1idewhist1e 2023-03-17 01:16:31 +00:00 committed by Kainoa Kanter
parent d4e77ccd9b
commit 5eec5ac2ef
2 changed files with 78 additions and 4 deletions

View File

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

View File

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