feat: ability to browse follower/following lists of remote accounts

This commit is contained in:
Julian Lam
2024-01-05 15:31:09 -05:00
parent c66b18670d
commit 2a77ae550b
3 changed files with 46 additions and 5 deletions

View File

@@ -35,9 +35,14 @@ ActivityPub.getActor = async (input) => {
const actor = await ActivityPub.get(uri);
// todo: remove this after ActivityPub.get is updated to handle errors more effectively
if (typeof actor === 'string' || actor.hasOwnProperty('error')) {
return null;
}
const [followers, following] = await Promise.all([
ActivityPub.get(actor.followers),
ActivityPub.get(actor.following),
actor.followers ? ActivityPub.get(actor.followers) : { totalItems: 0 },
actor.following ? ActivityPub.get(actor.following) : { totalItems: 0 },
]);
actor.hostname = new URL(uri).hostname;
@@ -56,12 +61,16 @@ ActivityPub.mockProfile = async (actors, callerUid = 0) => {
actors = [actors];
}
const profiles = await Promise.all(actors.map(async (actor) => {
const profiles = (await Promise.all(actors.map(async (actor) => {
// convert uri to actor object
if (typeof actor === 'string' && ActivityPub.helpers.isUri(actor)) {
actor = await ActivityPub.getActor(actor);
}
if (!actor) {
return null;
}
const uid = actor.id;
const { preferredUsername, published, icon, image, name, summary, hostname, followerCount, followingCount } = actor;
const isFollowing = await db.isSortedSetMember(`followingRemote:${callerUid}`, uid);
@@ -97,7 +106,7 @@ ActivityPub.mockProfile = async (actors, callerUid = 0) => {
};
return payload;
}));
}))).filter(Boolean);
return single ? profiles.pop() : profiles;
};

View File

@@ -4,6 +4,8 @@ const user = require('../../user');
const helpers = require('../helpers');
const pagination = require('../../pagination');
const activitypubController = require('../activitypub');
const followController = module.exports;
followController.getFollowing = async function (req, res, next) {
@@ -15,6 +17,10 @@ followController.getFollowers = async function (req, res, next) {
};
async function getFollow(tpl, name, req, res) {
if (res.locals.uid === -2) {
return activitypubController.profiles.getFollow(tpl, name, req, res);
}
const {
username, userslug, followerCount, followingCount,
} = await user.getUserFields(res.locals.uid, [

View File

@@ -1,6 +1,8 @@
'use strict';
const { getActor, mockProfile } = require('../../activitypub');
const { getActor, mockProfile, get } = require('../../activitypub');
const helpers = require('../helpers');
const pagination = require('../../pagination');
const controller = module.exports;
@@ -14,3 +16,27 @@ controller.get = async function (req, res, next) {
const payload = await mockProfile(actor, req.uid);
res.render('account/profile', payload);
};
controller.getFollow = async function (tpl, name, req, res) {
const actor = await getActor(req.params.userslug);
const { userslug } = req.params;
const { preferredUsername: username, followerCount, followingCount } = actor;
const page = parseInt(req.query.page, 10) || 1;
const payload = {};
payload.title = `[[pages:${tpl}, ${username}]]`;
const collection = await get(`${actor[name]}?page=${page}`);
const resultsPerPage = collection.orderedItems.length;
payload.users = await mockProfile(collection.orderedItems, req.uid);
const count = name === 'following' ? followingCount : followerCount;
const pageCount = Math.ceil(count / resultsPerPage);
payload.pagination = pagination.create(page, pageCount);
payload.breadcrumbs = helpers.buildBreadcrumbs([{ text: username, url: `/user/${userslug}` }, { text: `[[user:${name}]]` }]);
res.render(tpl, payload);
};