From c803b2124c56edd8937bb73a154d74568e893070 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 8 Dec 2023 10:55:16 -0500 Subject: [PATCH] refactor: minor restructure to move logic out of main controller file to src/api --- src/api/activitypub.js | 53 ++++++++++++++++++++++++++++ src/api/index.js | 1 + src/controllers/activitypub/index.js | 45 ++++------------------- 3 files changed, 60 insertions(+), 39 deletions(-) create mode 100644 src/api/activitypub.js diff --git a/src/api/activitypub.js b/src/api/activitypub.js new file mode 100644 index 0000000000..fd9f219e18 --- /dev/null +++ b/src/api/activitypub.js @@ -0,0 +1,53 @@ +'use strict'; + +/** + * DEVELOPMENT NOTE + * + * THIS FILE IS UNDER ACTIVE DEVELOPMENT AND IS EXPLICITLY EXCLUDED FROM IMMUTABILITY GUARANTEES + * + * If you use api methods in this file, be prepared that they may be removed or modified with no warning. + */ + +const db = require('../database'); +const activitypub = require('../activitypub'); + +const activitypubApi = module.exports; + +activitypubApi.follow = async (caller, { actorId } = {}) => { + if (!actorId) { + throw new Error('[[error:invalid-uid]]'); // should be activitypub-specific + } + + await activitypub.send(caller.uid, actorId, { + type: 'Follow', + object: { + type: 'Person', + name: actorId, + }, + }); + + const now = Date.now(); + await Promise.all([ + db.sortedSetAdd(`followingRemote:${caller.uid}`, now, actorId), + db.incrObjectField(`user:${caller.uid}`, 'followingRemoteCount'), + ]); +}; + +activitypubApi.unfollow = async (caller, { actorId }) => { + if (!actorId) { + throw new Error('[[error:invalid-uid]]'); // should be activitypub-specific + } + + await activitypub.send(caller.uid, actorId, { + type: 'Unfollow', + object: { + type: 'Person', + name: actorId, + }, + }); + + await Promise.all([ + db.sortedSetRemove(`followingRemote:${caller.uid}`, actorId), + db.decrObjectField(`user:${caller.uid}`, 'followingRemoteCount'), + ]); +}; diff --git a/src/api/index.js b/src/api/index.js index c454de93a5..18cd8678f1 100644 --- a/src/api/index.js +++ b/src/api/index.js @@ -11,6 +11,7 @@ module.exports = { categories: require('./categories'), search: require('./search'), flags: require('./flags'), + activitypub: require('./activitypub'), files: require('./files'), utils: require('./utils'), }; diff --git a/src/controllers/activitypub/index.js b/src/controllers/activitypub/index.js index d366337b49..bfa07d33e3 100644 --- a/src/controllers/activitypub/index.js +++ b/src/controllers/activitypub/index.js @@ -5,6 +5,7 @@ const nconf = require('nconf'); const db = require('../../database'); const user = require('../../user'); const activitypub = require('../../activitypub'); +const api = require('../../api'); const helpers = require('../helpers'); const Controller = module.exports; @@ -101,6 +102,7 @@ Controller.getInbox = async (req, res) => { }; Controller.postInbox = async (req, res) => { + // Note: internal-only, hence no exposure via src/api switch (req.body.type) { case 'Follow': { await activitypub.inbox.follow(req.body.actor.name, req.body.object.name); @@ -121,46 +123,11 @@ Controller.postInbox = async (req, res) => { */ Controller.follow = async (req, res) => { - try { - const { uid: actorId } = req.params; - await activitypub.send(req.uid, actorId, { - type: 'Follow', - object: { - type: 'Person', - name: actorId, - }, - }); - - const now = Date.now(); - await Promise.all([ - db.sortedSetAdd(`followingRemote:${req.uid}`, now, actorId), - db.incrObjectField(`user:${req.uid}`, 'followingRemoteCount'), - ]); - - helpers.formatApiResponse(200, res); - } catch (e) { - helpers.formatApiResponse(400, res, e); - } + const { uid: actorId } = req.params; + helpers.formatApiResponse(200, res, await api.activitypub.follow(req, { actorId })); }; Controller.unfollow = async (req, res) => { - try { - const { uid: actorId } = req.params; - await activitypub.send(req.uid, actorId, { - type: 'Unfollow', - object: { - type: 'Person', - name: actorId, - }, - }); - - await Promise.all([ - db.sortedSetRemove(`followingRemote:${req.uid}`, actorId), - db.decrObjectField(`user:${req.uid}`, 'followingRemoteCount'), - ]); - - helpers.formatApiResponse(200, res); - } catch (e) { - helpers.formatApiResponse(400, res, e); - } + const { uid: actorId } = req.params; + helpers.formatApiResponse(200, res, await api.activitypub.unfollow(req, { actorId })); };