Merge branch '459-activitypub-integration' of github.com:NuSkooler/enigma-bbs into 459-activitypub-integration

This commit is contained in:
Bryan Ashby 2023-02-04 22:55:20 -07:00
commit 24de0fa0bf
No known key found for this signature in database
GPG Key ID: C2C1B501E4EFD994
2 changed files with 65 additions and 1 deletions

View File

@ -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);
}
);
}
};

View File

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