mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
fix: some wip code regarding handling a follow activity, remove unfollow activity as that does not exist
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user