Files
NodeBB/src/activitypub/actors.js

75 lines
2.1 KiB
JavaScript
Raw Normal View History

'use strict';
const winston = require('winston');
const db = require('../database');
const utils = require('../utils');
const activitypub = module.parent.exports;
const Actors = module.exports;
2024-01-26 16:48:16 -05:00
Actors.assert = async (ids, options = {}) => {
// Handle single values
if (!Array.isArray(ids)) {
ids = [ids];
}
// Filter out uids if passed in
ids = ids.filter(id => !utils.isNumber(id));
// Filter out existing
2024-01-26 16:48:16 -05:00
if (!options.update) {
const exists = await db.isSortedSetMembers('usersRemote:lastCrawled', ids.map(id => ((typeof id === 'object' && id.hasOwnProperty('id')) ? id.id : id)));
ids = ids.filter((id, idx) => !exists[idx]);
}
if (!ids.length) {
return true;
}
const actors = await Promise.all(ids.map(async (id) => {
try {
const actor = (typeof id === 'object' && id.hasOwnProperty('id')) ? id : await activitypub.get('uid', 0, id);
// Follow counts
2024-01-26 16:24:14 -05:00
try {
const [followers, following] = await Promise.all([
actor.followers ? activitypub.get('uid', 0, actor.followers) : { totalItems: 0 },
actor.following ? activitypub.get('uid', 0, actor.following) : { totalItems: 0 },
2024-01-26 16:24:14 -05:00
]);
actor.followerCount = followers.totalItems;
actor.followingCount = following.totalItems;
} catch (e) {
// no action required
2024-01-26 16:48:16 -05:00
winston.verbose(`[activitypub/actor.assert] Unable to retrieve follower counts for ${actor.id}`);
2024-01-26 16:24:14 -05:00
}
// Post count
const outbox = actor.outbox ? await activitypub.get('uid', 0, actor.outbox) : { totalItems: 0 };
actor.postcount = outbox.totalItems;
return actor;
} catch (e) {
return null;
}
}));
// Build userData object for storage
const profiles = await activitypub.mocks.profile(actors);
const now = Date.now();
await Promise.all([
db.setObjectBulk(profiles.map((profile, idx) => {
if (!profile) {
return null;
}
const key = `userRemote:${ids[idx]}`;
return [key, profile];
}).filter(Boolean)),
db.sortedSetAdd('usersRemote:lastCrawled', ids.map((id, idx) => (profiles[idx] ? now : null)).filter(Boolean), ids.filter((id, idx) => profiles[idx])),
]);
return actors.every(Boolean);
};