From 7e1dac39eaaa0e86910359cf434492dcb41912e4 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Sun, 28 May 2023 15:42:29 -0400 Subject: [PATCH] feat: followers and following endpoints --- src/controllers/activitypub.js | 36 ++++++++++++++++++++++++++++++++++ src/routes/activitypub.js | 3 +++ 2 files changed, 39 insertions(+) diff --git a/src/controllers/activitypub.js b/src/controllers/activitypub.js index 921e59cf2c..9e4527c7ac 100644 --- a/src/controllers/activitypub.js +++ b/src/controllers/activitypub.js @@ -40,6 +40,42 @@ Controller.getActor = async (req, res) => { }); }; +Controller.getFollowing = async (req, res) => { + const { followingCount: totalItems } = await user.getUserFields(res.locals.uid, ['followingCount']); + + const page = parseInt(req.query.page, 10) || 1; + const resultsPerPage = 50; + const start = Math.max(0, page - 1) * resultsPerPage; + const stop = start + resultsPerPage - 1; + + let orderedItems = await user.getFollowing(res.locals.uid, start, stop); + orderedItems = orderedItems.map(({ userslug }) => `${nconf.get('url')}/user/${userslug}`); + res.status(200).json({ + '@context': 'https://www.w3.org/ns/activitystreams', + type: 'OrderedCollection', + totalItems, + orderedItems, + }); +}; + +Controller.getFollowers = async (req, res) => { + const { followerCount: totalItems } = await user.getUserFields(res.locals.uid, ['followerCount']); + + const page = parseInt(req.query.page, 10) || 1; + const resultsPerPage = 50; + const start = Math.max(0, page - 1) * resultsPerPage; + const stop = start + resultsPerPage - 1; + + let orderedItems = await user.getFollowers(res.locals.uid, start, stop); + orderedItems = orderedItems.map(({ userslug }) => `${nconf.get('url')}/user/${userslug}`); + res.status(200).json({ + '@context': 'https://www.w3.org/ns/activitystreams', + type: 'OrderedCollection', + totalItems, + orderedItems, + }); +}; + Controller.getOutbox = async (req, res) => { // stub res.status(200).json({ diff --git a/src/routes/activitypub.js b/src/routes/activitypub.js index f96484a7b2..02fe9a2bf0 100644 --- a/src/routes/activitypub.js +++ b/src/routes/activitypub.js @@ -5,6 +5,9 @@ module.exports = function (app, middleware, controllers) { app.get('/user/:userslug', middlewares, controllers.activitypub.getActor); + app.get('/user/:userslug/following', middlewares, controllers.activitypub.getFollowing); + app.get('/user/:userslug/followers', middlewares, controllers.activitypub.getFollowers); + app.get('/user/:userslug/outbox', middlewares, controllers.activitypub.getOutbox); app.post('/user/:userslug/outbox', middlewares, controllers.activitypub.postOutbox);