Skip to content

Node.js based forum software built for the modern web

refactor: simplify remote (un)follow controller

Julian Lamc9feb9253910
diff --git a/src/api/activitypub.js b/src/api/activitypub.js
index f6544f0962..b8f9813f21 100644
--- a/src/api/activitypub.js
+++ b/src/api/activitypub.js
@@ -16,7 +16,7 @@ const user = require('../user');
const activitypubApi = module.exports;
-activitypubApi.follow = async (caller, { actorId } = {}) => {
+activitypubApi.follow = async (caller, { uid: actorId } = {}) => {
const object = await activitypub.getActor(caller.uid, actorId);
if (!object) {
throw new Error('[[error:activitypub.invalid-id]]');
@@ -28,7 +28,7 @@ activitypubApi.follow = async (caller, { actorId } = {}) => {
});
};
-activitypubApi.unfollow = async (caller, { actorId }) => {
+activitypubApi.unfollow = async (caller, { uid: actorId }) => {
const object = await activitypub.getActor(caller.uid, actorId);
const userslug = await user.getUserField(caller.uid, 'userslug');
if (!object) {
diff --git a/src/controllers/activitypub/index.js b/src/controllers/activitypub/index.js
index ae13e3e25e..8a2154acfd 100644
--- a/src/controllers/activitypub/index.js
+++ b/src/controllers/activitypub/index.js
@@ -105,17 +105,3 @@ Controller.postInbox = async (req, res) => {
res.sendStatus(201);
};
-
-/**
- * Main ActivityPub verbs
- */
-
-Controller.follow = async (req, res) => {
- const { uid: actorId } = req.params;
- helpers.formatApiResponse(200, res, await api.activitypub.follow(req, { actorId }));
-};
-
-Controller.unfollow = async (req, res) => {
- const { uid: actorId } = req.params;
- helpers.formatApiResponse(200, res, await api.activitypub.unfollow(req, { actorId }));
-};
diff --git a/src/controllers/write/users.js b/src/controllers/write/users.js
index fe63ab9019..e8411054f6 100644
--- a/src/controllers/write/users.js
+++ b/src/controllers/write/users.js
@@ -94,20 +94,16 @@ Users.changePassword = async (req, res) => {
};
Users.follow = async (req, res) => {
- if (req.params.uid.indexOf('@') !== -1) {
- return await activitypubController.follow(req, res);
- }
-
- await api.users.follow(req, req.params);
+ const remote = String(req.params.uid).includes('@');
+ const controller = remote ? api.activitypub.follow : api.users.follow;
+ await controller(req, req.params);
helpers.formatApiResponse(200, res);
};
Users.unfollow = async (req, res) => {
- if (req.params.uid.indexOf('@') !== -1) {
- return await activitypubController.unfollow(req, res);
- }
-
- await api.users.unfollow(req, req.params);
+ const remote = String(req.params.uid).includes('@');
+ const controller = remote ? api.activitypub.unfollow : api.users.unfollow;
+ await controller(req, req.params);
helpers.formatApiResponse(200, res);
};