Add getRemoteCollectionStats() and usage

This commit is contained in:
Bryan Ashby 2023-02-04 23:16:44 -07:00
parent 24de0fa0bf
commit bd2dc27477
No known key found for this signature in database
GPG Key ID: C2C1B501E4EFD994
2 changed files with 59 additions and 54 deletions

View File

@ -3,8 +3,7 @@ const { Errors } = require('../enig_error');
const Actor = require('../activitypub/actor'); const Actor = require('../activitypub/actor');
const moment = require('moment'); const moment = require('moment');
const { htmlToMessageBody } = require('./util'); const { htmlToMessageBody } = require('./util');
const { getJson } = require('../http_util'); const Collection = require('./collection');
const { ActivityStreamMediaType } = require('./const');
// deps // deps
const async = require('async'); const async = require('async');
@ -156,35 +155,15 @@ exports.getModule = class ActivityPubActorSearch extends MenuModule {
manualFollowersView.setText(remoteActor.manuallyApprovesFollowers); manualFollowersView.setText(remoteActor.manuallyApprovesFollowers);
const followerCountView = v(MciViewIds.view.numberFollowers); const followerCountView = v(MciViewIds.view.numberFollowers);
this._retrieveCountFromUrl( this._updateViewWithCollectionItemCount(
remoteActor.followers, remoteActor.followers,
(err, followerCount) => { followerCountView
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); const followingCountView = v(MciViewIds.view.numberFollowing);
this._retrieveCountFromUrl( this._updateViewWithCollectionItemCount(
remoteActor.following, remoteActor.following,
(err, followingCount) => { followingCountView
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); const summaryView = v(MciViewIds.view.summary);
@ -229,33 +208,28 @@ exports.getModule = class ActivityPubActorSearch extends MenuModule {
); );
} }
_retrieveCountFromUrl(countUrl, cb) { _updateViewWithCollectionItemCount(collectionUrl, view) {
async.waterfall( this._retrieveCountFromCollectionUrl(collectionUrl, (err, count) => {
[
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) { if (err) {
return callback(err, jsonResponse); this.client.log.warn(
} { err: err },
return callback(null, jsonResponse); `Unable to get Collection count for ${collectionUrl}`
});
},
(jsonResponse, callback) => {
return callback(null, jsonResponse.totalItems);
},
],
(err, result) => {
return cb(err, result);
}
); );
view.setText('--');
} else {
view.setText(count);
}
});
}
_retrieveCountFromCollectionUrl(collectionUrl, cb) {
collectionUrl = collectionUrl.trim();
if (isEmpty(collectionUrl)) {
return cb(Errors.UnexpectedState('Count URL can not be empty.'));
}
Collection.getRemoteCollectionStats(collectionUrl, (err, stats) => {
return cb(err, stats.totalItems);
});
} }
}; };

View File

@ -3,8 +3,12 @@ const ActivityPubObject = require('./object');
const apDb = require('../database').dbs.activitypub; const apDb = require('../database').dbs.activitypub;
const { getISOTimestampString } = require('../database'); const { getISOTimestampString } = require('../database');
const { Errors } = require('../enig_error.js'); const { Errors } = require('../enig_error.js');
const { PublicCollectionId: APPublicCollectionId } = require('./const'); const {
PublicCollectionId: APPublicCollectionId,
ActivityStreamMediaType,
} = require('./const');
const UserProps = require('../user_property'); const UserProps = require('../user_property');
const { getJson } = require('../http_util');
// deps // deps
const { isString } = require('lodash'); const { isString } = require('lodash');
@ -18,6 +22,33 @@ module.exports = class Collection extends ActivityPubObject {
return APPublicCollectionId; return APPublicCollectionId;
} }
static getRemoteCollectionStats(collectionUrl, cb) {
const headers = {
Accept: ActivityStreamMediaType,
};
getJson(collectionUrl, { headers }, (err, collection) => {
if (err) {
return cb(err);
}
// :TODO: validate headers?
collection = new Collection(collection);
if (!collection.isValid()) {
return cb(Errors.Invalid('Invalid Collection'));
}
const { totalItems, type, id, summary } = collection;
return cb(null, {
totalItems,
type,
id,
summary,
});
});
}
static followers(collectionId, page, cb) { static followers(collectionId, page, cb) {
return Collection.publicOrderedById( return Collection.publicOrderedById(
'followers', 'followers',