fix: some wip code regarding handling a follow activity, remove unfollow activity as that does not exist

This commit is contained in:
Julian Lam
2023-12-22 13:35:09 -05:00
parent 0eadad84cd
commit f3b0794d17
2 changed files with 30 additions and 60 deletions

View File

@@ -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;

View File

@@ -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;
}