From 877e2ca61a814947bae107896f4da64aeb680b04 Mon Sep 17 00:00:00 2001 From: Nathan Byrd Date: Sat, 4 Feb 2023 19:33:56 -0600 Subject: [PATCH] Added followers and following --- core/activitypub/actor_search.js | 64 +++++++++++++++++++++++++++++++- core/http_util.js | 2 + 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/core/activitypub/actor_search.js b/core/activitypub/actor_search.js index 90631211..a338a212 100644 --- a/core/activitypub/actor_search.js +++ b/core/activitypub/actor_search.js @@ -3,6 +3,8 @@ const { Errors } = require('../enig_error'); const Actor = require('../activitypub/actor'); const moment = require('moment'); const { htmlToMessageBody } = require('./util'); +const { getJson } = require('../http_util'); +const { ActivityStreamMediaType } = require('./const'); // deps const async = require('async'); @@ -153,7 +155,37 @@ exports.getModule = class ActivityPubActorSearch extends MenuModule { const manualFollowersView = v(MciViewIds.view.manualFollowers); manualFollowersView.setText(remoteActor.manuallyApprovesFollowers); - // TODO: Number of followers, number following + const followerCountView = v(MciViewIds.view.numberFollowers); + this._retrieveCountFromUrl( + remoteActor.followers, + (err, followerCount) => { + if (err) { + this.client.log.warn( + { err: err }, + 'Unable to get follower count' + ); + followerCountView.setText('--'); + } else { + followerCountView.setText(followerCount); + } + } + ); + + const followingCountView = v(MciViewIds.view.numberFollowing); + this._retrieveCountFromUrl( + remoteActor.following, + (err, followingCount) => { + if (err) { + this.client.log.warn( + { err: err }, + 'Unable to get following count' + ); + followingCountView.setText('--'); + } else { + followingCountView.setText(followingCount); + } + } + ); const summaryView = v(MciViewIds.view.summary); summaryView.setText(htmlToMessageBody(remoteActor.summary)); @@ -196,4 +228,34 @@ exports.getModule = class ActivityPubActorSearch extends MenuModule { } ); } + + _retrieveCountFromUrl(countUrl, cb) { + async.waterfall( + [ + callback => { + countUrl = countUrl.trim(); + if (isEmpty(countUrl)) { + return callback( + Errors.UnexpectedState('Count URL can not be empty.') + ); + } + const headers = { + Accept: ActivityStreamMediaType, + }; + getJson(countUrl, { headers }, (err, jsonResponse) => { + if (err) { + return callback(err, jsonResponse); + } + return callback(null, jsonResponse); + }); + }, + (jsonResponse, callback) => { + return callback(null, jsonResponse.totalItems); + }, + ], + (err, result) => { + return cb(err, result); + } + ); + } }; diff --git a/core/http_util.js b/core/http_util.js index ceb4ee1b..0474fc1c 100644 --- a/core/http_util.js +++ b/core/http_util.js @@ -5,6 +5,7 @@ const { isString, isObject, truncate } = require('lodash'); const https = require('https'); const httpSignature = require('http-signature'); const crypto = require('crypto'); +const Log = require('./logger').log; exports.getJson = getJson; exports.postJson = postJson; @@ -17,6 +18,7 @@ function getJson(url, options, cb) { return cb(err); } + Log.debug({ url: url, body: body }, 'Response from getJson'); let parsed; try { parsed = JSON.parse(body);