diff --git a/src/activitypub/inbox.js b/src/activitypub/inbox.js index c2d1d60247..2f526c6477 100644 --- a/src/activitypub/inbox.js +++ b/src/activitypub/inbox.js @@ -109,13 +109,10 @@ inbox.follow = async (req) => { } const watchState = await categories.getWatchState([id], req.body.actor); - if (watchState === categories.watchStates.tracking) { - // No additional parsing required - return; + if (watchState[0] !== categories.watchStates.tracking) { + await user.setCategoryWatchState(req.body.actor, id, categories.watchStates.tracking); } - await user.setCategoryWatchState(req.body.actor, id, categories.watchStates.tracking); - await activitypub.send('cid', id, req.body.actor, { type: 'Accept', object: { @@ -149,10 +146,9 @@ inbox.accept = async (req) => { if (type === 'Follow') { const now = Date.now(); - await Promise.all([ - db.sortedSetAdd(`followingRemote:${uid}`, now, actor), - db.incrObjectField(`user:${uid}`, 'followingRemoteCount'), - ]); + await db.sortedSetAdd(`followingRemote:${uid}`, now, actor); + const followingRemoteCount = await db.sortedSetCard(`followingRemote:${uid}`); + await user.setUserField(uid, 'followingRemoteCount', followingRemoteCount); } }; @@ -177,10 +173,9 @@ inbox.undo = async (req) => { throw new Error('[[error:invalid-uid]]'); } - await Promise.all([ - db.sortedSetRemove(`followersRemote:${id}`, actor), - db.decrObjectField(`user:${id}`, 'followerRemoteCount'), - ]); + await db.sortedSetRemove(`followersRemote:${id}`, actor); + const followerRemoteCount = await db.sortedSetCard(`followerRemote:${id}`); + await user.setUserField(id, 'followerRemoteCount', followerRemoteCount); break; } diff --git a/src/activitypub/index.js b/src/activitypub/index.js index 63ec8eca49..b3b68c5057 100644 --- a/src/activitypub/index.js +++ b/src/activitypub/index.js @@ -41,7 +41,7 @@ ActivityPub.getPublicKey = async (type, id) => { let publicKey; try { - ({ publicKey } = await db.getObject(`uid:${id}:keys`)); + ({ publicKey } = await db.getObject(`${type}:${id}:keys`)); } catch (e) { ({ publicKey } = await ActivityPub.helpers.generateKeys(type, id)); } @@ -145,6 +145,7 @@ ActivityPub.verify = async (req) => { return memo; }, []).join('\n'); + // Verify the signature string via public key try { // Retrieve public key from remote instance @@ -196,9 +197,25 @@ ActivityPub.send = async (type, id, targets, payload) => { const inboxes = await ActivityPub.resolveInboxes(targets); + let actor; + switch (type) { + case 'uid': { + actor = `${nconf.get('url')}/uid/${id}`; + break; + } + + case 'cid': { + actor = `${nconf.get('url')}/category/${id}`; + break; + } + + default: + throw new Error('[[error:activitypub.invalid-id]]'); + } + payload = { '@context': 'https://www.w3.org/ns/activitystreams', - actor: `${nconf.get('url')}/uid/${id}`, + actor, ...payload, }; @@ -220,3 +237,10 @@ ActivityPub.send = async (type, id, targets, payload) => { } })); }; + +setTimeout(async () => { + await ActivityPub.send('uid', 1, 'https://localhost/category/1', { + type: 'Follow', + object: 'https://localhost/category/1', + }); +}, 2000); diff --git a/src/activitypub/mocks.js b/src/activitypub/mocks.js index 1e4e017f10..4074bc57f7 100644 --- a/src/activitypub/mocks.js +++ b/src/activitypub/mocks.js @@ -164,6 +164,7 @@ Mocks.actors.user = async (uid) => { Mocks.actors.category = async (cid) => { let { name, slug, description: summary, backgroundImage } = await categories.getCategoryData(cid); + const publicKey = await activitypub.getPublicKey('cid', cid); if (backgroundImage) { const filename = utils.decodeHTMLEntities(backgroundImage).split('/').pop(); @@ -189,6 +190,12 @@ Mocks.actors.category = async (cid) => { preferredUsername: `cid.${cid}`, summary, icon: backgroundImage, + + publicKey: { + id: `${nconf.get('url')}/category/${cid}#key`, + owner: `${nconf.get('url')}/category/${cid}`, + publicKeyPem: publicKey, + }, }; };