refactor: simplify remote (un)follow controller

This commit is contained in:
Julian Lam
2024-01-23 12:11:35 -05:00
parent 3bdaa8a836
commit c9feb92539
3 changed files with 8 additions and 26 deletions

View File

@@ -16,7 +16,7 @@ const user = require('../user');
const activitypubApi = module.exports; const activitypubApi = module.exports;
activitypubApi.follow = async (caller, { actorId } = {}) => { activitypubApi.follow = async (caller, { uid: actorId } = {}) => {
const object = await activitypub.getActor(caller.uid, actorId); const object = await activitypub.getActor(caller.uid, actorId);
if (!object) { if (!object) {
throw new Error('[[error:activitypub.invalid-id]]'); 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 object = await activitypub.getActor(caller.uid, actorId);
const userslug = await user.getUserField(caller.uid, 'userslug'); const userslug = await user.getUserField(caller.uid, 'userslug');
if (!object) { if (!object) {

View File

@@ -105,17 +105,3 @@ Controller.postInbox = async (req, res) => {
res.sendStatus(201); 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 }));
};

View File

@@ -94,20 +94,16 @@ Users.changePassword = async (req, res) => {
}; };
Users.follow = async (req, res) => { Users.follow = async (req, res) => {
if (req.params.uid.indexOf('@') !== -1) { const remote = String(req.params.uid).includes('@');
return await activitypubController.follow(req, res); const controller = remote ? api.activitypub.follow : api.users.follow;
} await controller(req, req.params);
await api.users.follow(req, req.params);
helpers.formatApiResponse(200, res); helpers.formatApiResponse(200, res);
}; };
Users.unfollow = async (req, res) => { Users.unfollow = async (req, res) => {
if (req.params.uid.indexOf('@') !== -1) { const remote = String(req.params.uid).includes('@');
return await activitypubController.unfollow(req, res); const controller = remote ? api.activitypub.unfollow : api.users.unfollow;
} await controller(req, req.params);
await api.users.unfollow(req, req.params);
helpers.formatApiResponse(200, res); helpers.formatApiResponse(200, res);
}; };