feat: add assertion lock on activitypub.notes.assert

This commit is contained in:
Julian Lam
2024-03-25 14:55:25 -04:00
parent ac765f1e01
commit 2688b6bbdc

View File

@@ -16,6 +16,15 @@ const utils = require('../utils');
const activitypub = module.parent.exports; const activitypub = module.parent.exports;
const Notes = module.exports; const Notes = module.exports;
async function lock(value) {
const count = await db.incrObjectField('locks', value);
return count <= 1;
}
async function unlock(value) {
await db.deleteObjectField('locks', value);
}
Notes.assert = async (uid, input) => { Notes.assert = async (uid, input) => {
/** /**
* Given the id or object of any as:Note, traverses up to cache the entire threaded context * Given the id or object of any as:Note, traverses up to cache the entire threaded context
@@ -25,8 +34,16 @@ Notes.assert = async (uid, input) => {
*/ */
const object = !activitypub.helpers.isUri(input) && input; const object = !activitypub.helpers.isUri(input) && input;
const id = object ? object.id : input;
const lockStatus = await lock(id, '[[error:activitypub.already-asserting]]');
if (!lockStatus) { // unable to achieve lock, stop processing.
return null;
}
const chain = Array.from(await Notes.getParentChain(uid, input)); const chain = Array.from(await Notes.getParentChain(uid, input));
if (!chain.length) { if (!chain.length) {
unlock(id);
return null; return null;
} }
@@ -39,6 +56,7 @@ Notes.assert = async (uid, input) => {
if (tid && members.every(Boolean)) { if (tid && members.every(Boolean)) {
// All cached, return early. // All cached, return early.
winston.verbose('[notes/assert] No new notes to process.'); winston.verbose('[notes/assert] No new notes to process.');
unlock(id);
return tid; return tid;
} }
@@ -60,6 +78,7 @@ Notes.assert = async (uid, input) => {
const privilege = `topics:${tid ? 'reply' : 'create'}`; const privilege = `topics:${tid ? 'reply' : 'create'}`;
const allowed = await privileges.categories.can(privilege, cid, activitypub._constants.uid); const allowed = await privileges.categories.can(privilege, cid, activitypub._constants.uid);
if (!allowed) { if (!allowed) {
unlock(id);
return null; return null;
} }
@@ -141,7 +160,10 @@ Notes.assert = async (uid, input) => {
} }
} }
await Notes.syncUserInboxes(tid); await Promise.all([
Notes.syncUserInboxes(tid),
unlock(id),
]);
return { tid, count }; return { tid, count };
}; };