From f3b0794d179f2c729eb194e78b00a1e39dea8b8e Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 22 Dec 2023 13:35:09 -0500 Subject: [PATCH] fix: some wip code regarding handling a follow activity, remove unfollow activity as that does not exist --- src/activitypub/inbox.js | 83 ++++++++++------------------ src/controllers/activitypub/index.js | 7 +-- 2 files changed, 30 insertions(+), 60 deletions(-) diff --git a/src/activitypub/inbox.js b/src/activitypub/inbox.js index de64dcde6c..78de0bfb69 100644 --- a/src/activitypub/inbox.js +++ b/src/activitypub/inbox.js @@ -8,12 +8,36 @@ const helpers = require('./helpers'); const inbox = module.exports; -inbox.follow = async (actorId, objectId) => { - await handleFollow('follow', actorId, objectId); -}; +inbox.follow = async (req) => { + // Sanity checks + const from = await activitypub.getActor(req.body.actor); + if (!from) { + throw new Error('[[error:invalid-uid]]'); // should probably be AP specific + } -inbox.unfollow = async (actorId, objectId) => { - await handleFollow('unfollow', actorId, objectId); + const localUid = await helpers.resolveLocalUid(req.body.object); + if (!localUid) { + throw new Error('[[error:invalid-uid]]'); + } + + const isFollowed = await inbox.isFollowed(from.id, localUid); + if (isFollowed) { + // No additional parsing required + return; + } + + const now = Date.now(); + await db.sortedSetAdd(`followersRemote:${localUid}`, now, from.id); + await activitypub.send(localUid, from.id, { + type: 'Accept', + object: { + type: 'Follow', + actor: from.actorUri, + }, + }); + + const followerRemoteCount = await db.sortedSetCard(`followersRemote:${localUid}`); + await user.setUserField(localUid, 'followerRemoteCount', followerRemoteCount); }; inbox.isFollowed = async (actorId, uid) => { @@ -23,55 +47,6 @@ inbox.isFollowed = async (actorId, uid) => { return await db.isSortedSetMember(`followersRemote:${uid}`, actorId); }; -async function handleFollow(type, actorId, objectId) { - // Sanity checks - const from = await helpers.query(actorId); - if (!actorId || !from) { - throw new Error('[[error:invalid-uid]]'); // should probably be AP specific - } - - if (!objectId) { - throw new Error('[[error:invalid-uid]]'); // should probably be AP specific - } - - const localUid = await helpers.resolveLocalUid(objectId); - if (!localUid) { - throw new Error('[[error:invalid-uid]]'); - } - - // matches toggleFollow() in src/user/follow.js - const isFollowed = await inbox.isFollowed(actorId, localUid); - if (type === 'follow') { - if (isFollowed) { - throw new Error('[[error:already-following]]'); - } - const now = Date.now(); - await db.sortedSetAdd(`followersRemote:${localUid}`, now, actorId); - await activitypub.send(localUid, actorId, { - type: 'Accept', - object: { - type: 'Follow', - actor: from.actorUri, - }, - }); - } else { - if (!isFollowed) { - throw new Error('[[error:not-following]]'); - } - await db.sortedSetRemove(`followersRemote:${localUid}`, actorId); - await activitypub.send(localUid, actorId, { - type: 'Undo', - object: { - type: 'Follow', - actor: from.actorUri, - }, - }); - } - - const followerRemoteCount = await db.sortedSetCard(`followersRemote:${localUid}`); - await user.setUserField(localUid, 'followerRemoteCount', followerRemoteCount); -} - inbox.accept = async (req) => { const { actor, object } = req.body; const { type } = object; diff --git a/src/controllers/activitypub/index.js b/src/controllers/activitypub/index.js index cc23b1c8b0..4477efeb38 100644 --- a/src/controllers/activitypub/index.js +++ b/src/controllers/activitypub/index.js @@ -104,12 +104,7 @@ Controller.postInbox = async (req, res) => { // Note: underlying methods are internal use 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); - break; - } - - case 'Unfollow': { - await activitypub.inbox.unfollow(req.body.actor.name, req.body.object.name); + await activitypub.inbox.follow(req); break; }