fix: category following and acceptance logic

This commit is contained in:
Julian Lam
2024-02-06 10:40:46 -05:00
parent 35819cc953
commit 25f0d48432
3 changed files with 41 additions and 15 deletions

View File

@@ -109,13 +109,10 @@ inbox.follow = async (req) => {
} }
const watchState = await categories.getWatchState([id], req.body.actor); const watchState = await categories.getWatchState([id], req.body.actor);
if (watchState === categories.watchStates.tracking) { if (watchState[0] !== categories.watchStates.tracking) {
// No additional parsing required await user.setCategoryWatchState(req.body.actor, id, categories.watchStates.tracking);
return;
} }
await user.setCategoryWatchState(req.body.actor, id, categories.watchStates.tracking);
await activitypub.send('cid', id, req.body.actor, { await activitypub.send('cid', id, req.body.actor, {
type: 'Accept', type: 'Accept',
object: { object: {
@@ -149,10 +146,9 @@ inbox.accept = async (req) => {
if (type === 'Follow') { if (type === 'Follow') {
const now = Date.now(); const now = Date.now();
await Promise.all([ await db.sortedSetAdd(`followingRemote:${uid}`, now, actor);
db.sortedSetAdd(`followingRemote:${uid}`, now, actor), const followingRemoteCount = await db.sortedSetCard(`followingRemote:${uid}`);
db.incrObjectField(`user:${uid}`, 'followingRemoteCount'), await user.setUserField(uid, 'followingRemoteCount', followingRemoteCount);
]);
} }
}; };
@@ -177,10 +173,9 @@ inbox.undo = async (req) => {
throw new Error('[[error:invalid-uid]]'); throw new Error('[[error:invalid-uid]]');
} }
await Promise.all([ await db.sortedSetRemove(`followersRemote:${id}`, actor);
db.sortedSetRemove(`followersRemote:${id}`, actor), const followerRemoteCount = await db.sortedSetCard(`followerRemote:${id}`);
db.decrObjectField(`user:${id}`, 'followerRemoteCount'), await user.setUserField(id, 'followerRemoteCount', followerRemoteCount);
]);
break; break;
} }

View File

@@ -41,7 +41,7 @@ ActivityPub.getPublicKey = async (type, id) => {
let publicKey; let publicKey;
try { try {
({ publicKey } = await db.getObject(`uid:${id}:keys`)); ({ publicKey } = await db.getObject(`${type}:${id}:keys`));
} catch (e) { } catch (e) {
({ publicKey } = await ActivityPub.helpers.generateKeys(type, id)); ({ publicKey } = await ActivityPub.helpers.generateKeys(type, id));
} }
@@ -145,6 +145,7 @@ ActivityPub.verify = async (req) => {
return memo; return memo;
}, []).join('\n'); }, []).join('\n');
// Verify the signature string via public key // Verify the signature string via public key
try { try {
// Retrieve public key from remote instance // Retrieve public key from remote instance
@@ -196,9 +197,25 @@ ActivityPub.send = async (type, id, targets, payload) => {
const inboxes = await ActivityPub.resolveInboxes(targets); 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 = { payload = {
'@context': 'https://www.w3.org/ns/activitystreams', '@context': 'https://www.w3.org/ns/activitystreams',
actor: `${nconf.get('url')}/uid/${id}`, actor,
...payload, ...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);

View File

@@ -164,6 +164,7 @@ Mocks.actors.user = async (uid) => {
Mocks.actors.category = async (cid) => { Mocks.actors.category = async (cid) => {
let { name, slug, description: summary, backgroundImage } = await categories.getCategoryData(cid); let { name, slug, description: summary, backgroundImage } = await categories.getCategoryData(cid);
const publicKey = await activitypub.getPublicKey('cid', cid);
if (backgroundImage) { if (backgroundImage) {
const filename = utils.decodeHTMLEntities(backgroundImage).split('/').pop(); const filename = utils.decodeHTMLEntities(backgroundImage).split('/').pop();
@@ -189,6 +190,12 @@ Mocks.actors.category = async (cid) => {
preferredUsername: `cid.${cid}`, preferredUsername: `cid.${cid}`,
summary, summary,
icon: backgroundImage, icon: backgroundImage,
publicKey: {
id: `${nconf.get('url')}/category/${cid}#key`,
owner: `${nconf.get('url')}/category/${cid}`,
publicKeyPem: publicKey,
},
}; };
}; };