Skip to content

Commit

Permalink
refactor: move library methods in src/activitypub/(in|out)box.js to s…
Browse files Browse the repository at this point in the history
…rc/api/activitypub.js
  • Loading branch information
julianlam committed Dec 6, 2023
1 parent 99cc60c commit 523f4bb
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
13 changes: 0 additions & 13 deletions src/activitypub/outbox.js

This file was deleted.

25 changes: 20 additions & 5 deletions src/activitypub/inbox.js → src/api/activitypub.js
@@ -1,27 +1,42 @@
'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 user = require('../user');

const helpers = require('./helpers');

const inbox = module.exports;
const activitypubApi = module.exports;

inbox.follow = async (actorId, objectId) => {
activitypubApi.follow = async (actorId, objectId) => {
await handleFollow('follow', actorId, objectId);
};

inbox.unfollow = async (actorId, objectId) => {
activitypubApi.unfollow = async (actorId, objectId) => {
await handleFollow('unfollow', actorId, objectId);
};

inbox.isFollowed = async (actorId, uid) => {
activitypubApi.isFollowed = async (actorId, uid) => {
if (actorId.indexOf('@') === -1 || parseInt(uid, 10) <= 0) {
return false;
}
return await db.isSortedSetMember(`followersRemote:${uid}`, actorId);
};

activitypubApi.isFollowing = async (uid, actorId) => {
if (parseInt(uid, 10) <= 0 || actorId.indexOf('@') === -1) {
return false;
}
return await db.isSortedSetMember(`followingRemote:${uid}`, actorId);
};

async function handleFollow(type, actorId, objectId) {
// Sanity checks
const actorExists = await helpers.query(actorId);
Expand All @@ -39,7 +54,7 @@ async function handleFollow(type, actorId, objectId) {
}

// matches toggleFollow() in src/user/follow.js
const isFollowed = await inbox.isFollowed(actorId, localUid);
const isFollowed = await activitypubApi.isFollowed(actorId, localUid);
if (type === 'follow') {
if (isFollowed) {
throw new Error('[[error:already-following]]');
Expand Down
1 change: 1 addition & 0 deletions src/api/index.js
Expand Up @@ -12,5 +12,6 @@ module.exports = {
search: require('./search'),
flags: require('./flags'),
files: require('./files'),
activitypub: require('./activitypub'),
utils: require('./utils'),
};
5 changes: 3 additions & 2 deletions src/controllers/activitypub/index.js
Expand Up @@ -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;
Expand Down Expand Up @@ -103,12 +104,12 @@ Controller.getInbox = async (req, res) => {
Controller.postInbox = async (req, res) => {
switch (req.body.type) {
case 'Follow': {
await activitypub.inbox.follow(req.body.actor.name, req.body.object.name);
await api.activitypub.follow(req.body.actor.name, req.body.object.name);
break;
}

case 'Unfollow': {
await activitypub.inbox.unfollow(req.body.actor.name, req.body.object.name);
await api.activitypub.unfollow(req.body.actor.name, req.body.object.name);
break;
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/controllers/activitypub/profiles.js
@@ -1,6 +1,7 @@
'use strict';

const { getActor, outbox } = require('../../activitypub');
const { getActor } = require('../../activitypub');
const api = require('../../api');

const controller = module.exports;

Expand All @@ -11,7 +12,7 @@ controller.get = async function (req, res, next) {
return next();
}
const { preferredUsername, published, icon, image, name, summary, hostname } = actor;
const isFollowing = await outbox.isFollowing(req.uid, uid);
const isFollowing = await api.activitypub.isFollowing(req.uid, uid);

const payload = {
uid,
Expand Down

0 comments on commit 523f4bb

Please sign in to comment.