feat: handle Update(note)

This commit is contained in:
Julian Lam
2024-01-16 13:55:58 -05:00
parent 641a94d64a
commit ac672f08a7
3 changed files with 23 additions and 2 deletions

View File

@@ -23,6 +23,18 @@ inbox.create = async (req) => {
} }
}; };
inbox.update = async (req) => {
const { object } = req.body;
const postData = await activitypub.mocks.post(object);
if (postData) {
await activitypub.notes.assert(1, [postData], { update: true });
winston.info(`[activitypub/inbox] Updating note ${postData.pid}`);
} else {
winston.warn('[activitypub/inbox] Received object was not a note');
}
};
inbox.follow = async (req) => { inbox.follow = async (req) => {
// Sanity checks // Sanity checks
const localUid = await helpers.resolveLocalUid(req.body.object); const localUid = await helpers.resolveLocalUid(req.body.object);

View File

@@ -15,14 +15,18 @@ Notes.resolveId = async (uid, id) => {
// todo: when asserted, notes aren't added to a global sorted set // todo: when asserted, notes aren't added to a global sorted set
// also, db.exists call is probably expensive // also, db.exists call is probably expensive
Notes.assert = async (uid, input) => { Notes.assert = async (uid, input, options = {}) => {
// Ensures that each note has been saved to the database // Ensures that each note has been saved to the database
await Promise.all(input.map(async (item) => { await Promise.all(input.map(async (item) => {
const id = activitypub.helpers.isUri(item) ? item : item.pid; const id = activitypub.helpers.isUri(item) ? item : item.pid;
const key = `post:${id}`; const key = `post:${id}`;
const exists = await db.exists(key); let exists = await db.exists(key);
winston.verbose(`[activitypub/notes.assert] Asserting note id ${id}`); winston.verbose(`[activitypub/notes.assert] Asserting note id ${id}`);
if (options.update === true) {
exists = false;
}
if (!exists) { if (!exists) {
let postData; let postData;
winston.verbose(`[activitypub/notes.assert] Not found, saving note to database`); winston.verbose(`[activitypub/notes.assert] Not found, saving note to database`);

View File

@@ -110,6 +110,11 @@ Controller.postInbox = async (req, res) => {
break; break;
} }
case 'Update': {
await activitypub.inbox.update(req);
break;
}
case 'Follow': { case 'Follow': {
await activitypub.inbox.follow(req); await activitypub.inbox.follow(req);
break; break;