feat: expose following/follower counts in actor response

This commit is contained in:
Julian Lam
2024-01-05 11:39:17 -05:00
parent 27e256d26e
commit 7fd15d6668
2 changed files with 24 additions and 7 deletions

View File

@@ -33,13 +33,16 @@ ActivityPub.getActor = async (input) => {
return actorCache.get(uri); return actorCache.get(uri);
} }
const { body: actor } = await request.get(uri, { const actor = await ActivityPub.get(uri);
headers: {
Accept: 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"', const [followers, following] = await Promise.all([
}, ActivityPub.get(actor.followers),
}); ActivityPub.get(actor.following),
]);
actor.hostname = new URL(uri).hostname; actor.hostname = new URL(uri).hostname;
actor.followerCount = followers.totalItems;
actor.followingCount = following.totalItems;
actorCache.set(uri, actor); actorCache.set(uri, actor);
return actor; return actor;
@@ -60,7 +63,7 @@ ActivityPub.mockProfile = async (actors, callerUid = 0) => {
} }
const uid = actor.id; const uid = actor.id;
const { preferredUsername, published, icon, image, name, summary, hostname } = actor; const { preferredUsername, published, icon, image, name, summary, hostname, followerCount, followingCount } = actor;
const isFollowing = await db.isSortedSetMember(`followingRemote:${callerUid}`, uid); const isFollowing = await db.isSortedSetMember(`followingRemote:${callerUid}`, uid);
let picture; let picture;
@@ -87,6 +90,10 @@ ActivityPub.mockProfile = async (actors, callerUid = 0) => {
aboutmeParsed: summary, aboutmeParsed: summary,
isFollowing, isFollowing,
counts: {
following: followingCount,
followers: followerCount,
},
}; };
return payload; return payload;
@@ -206,6 +213,16 @@ ActivityPub.verify = async (req) => {
} }
}; };
ActivityPub.get = async (uri) => {
const { body } = await request.get(uri, {
headers: {
Accept: 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
},
});
return body;
};
ActivityPub.send = async (uid, targets, payload) => { ActivityPub.send = async (uid, targets, payload) => {
if (!Array.isArray(targets)) { if (!Array.isArray(targets)) {
targets = [targets]; targets = [targets];

View File

@@ -204,7 +204,7 @@ module.exports = function (User) {
return; return;
} }
if (activitypub.helpers.isUri(String(user.uid))) { if (activitypub.helpers.isUri(user.uid)) {
intFields.splice(intFields.indexOf('uid'), 1); intFields.splice(intFields.indexOf('uid'), 1);
} }
db.parseIntFields(user, intFields, requestedFields); db.parseIntFields(user, intFields, requestedFields);