Skip to content

Node.js based forum software built for the modern web

feat: expose following/follower counts in actor response

Julian Lam7fd15d6668ec
diff --git a/src/activitypub/index.js b/src/activitypub/index.js
index d08d1e6015..09095c8374 100644
--- a/src/activitypub/index.js
+++ b/src/activitypub/index.js
@@ -33,13 +33,16 @@ ActivityPub.getActor = async (input) => {
return actorCache.get(uri);
}
- const { body: actor } = await request.get(uri, {
- headers: {
- Accept: 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
- },
- });
+ const actor = await ActivityPub.get(uri);
+
+ const [followers, following] = await Promise.all([
+ ActivityPub.get(actor.followers),
+ ActivityPub.get(actor.following),
+ ]);
actor.hostname = new URL(uri).hostname;
+ actor.followerCount = followers.totalItems;
+ actor.followingCount = following.totalItems;
actorCache.set(uri, actor);
return actor;
@@ -60,7 +63,7 @@ ActivityPub.mockProfile = async (actors, callerUid = 0) => {
}
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);
let picture;
@@ -87,6 +90,10 @@ ActivityPub.mockProfile = async (actors, callerUid = 0) => {
aboutmeParsed: summary,
isFollowing,
+ counts: {
+ following: followingCount,
+ followers: followerCount,
+ },
};
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) => {
if (!Array.isArray(targets)) {
targets = [targets];
diff --git a/src/user/data.js b/src/user/data.js
index fc2835e2d3..caf2e58a69 100644
--- a/src/user/data.js
+++ b/src/user/data.js
@@ -204,7 +204,7 @@ module.exports = function (User) {
return;
}
- if (activitypub.helpers.isUri(String(user.uid))) {
+ if (activitypub.helpers.isUri(user.uid)) {
intFields.splice(intFields.indexOf('uid'), 1);
}
db.parseIntFields(user, intFields, requestedFields);