From a968f219577c2f505a2807cc103cc6123fb952e5 Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Sun, 26 Feb 2023 21:29:07 -0700 Subject: [PATCH] A few bugs fixed with Note storage --- core/activitypub/collection.js | 12 ++--- core/activitypub/note.js | 5 +- .../content/web_handlers/activitypub.js | 52 ++++++++++++++++--- 3 files changed, 50 insertions(+), 19 deletions(-) diff --git a/core/activitypub/collection.js b/core/activitypub/collection.js index 69376501..e3863fa8 100644 --- a/core/activitypub/collection.js +++ b/core/activitypub/collection.js @@ -5,7 +5,7 @@ const apDb = require('../database').dbs.activitypub; const { getISOTimestampString } = require('../database'); const { Errors } = require('../enig_error.js'); const { - PublicCollectionId: APPublicCollectionId, + PublicCollectionId, ActivityStreamMediaType, Collections, ActorCollectionId, @@ -23,10 +23,6 @@ module.exports = class Collection extends ActivityPubObject { super(obj); } - static get PublicCollectionId() { - return APPublicCollectionId; - } - static getRemoteCollectionStats(collectionUrl, cb) { const headers = { Accept: ActivityStreamMediaType, @@ -162,7 +158,7 @@ module.exports = class Collection extends ActivityPubObject { return Collection.addToCollection( Collections.SharedInbox, null, // N/A - Collection.PublicCollectionId, + PublicCollectionId, inboxItem.id, inboxItem, false, @@ -232,7 +228,7 @@ module.exports = class Collection extends ActivityPubObject { ActorCollectionId, Collections.Actors, getISOTimestampString(), - APPublicCollectionId, + PublicCollectionId, actor.id, JSON.stringify(actor), false, @@ -616,7 +612,7 @@ module.exports = class Collection extends ActivityPubObject { ); } } else { - actorId = Collection.APPublicCollectionId; + actorId = PublicCollectionId; } isPrivate = isPrivate ? 1 : 0; diff --git a/core/activitypub/note.js b/core/activitypub/note.js index 185e750f..06b420bc 100644 --- a/core/activitypub/note.js +++ b/core/activitypub/note.js @@ -9,6 +9,7 @@ const { htmlToMessageBody, recipientIdsFromObject, } = require('./util'); +const { PublicCollectionId } = require('./const'); const { isAnsi } = require('../string_util'); // deps @@ -118,9 +119,7 @@ module.exports = class Note extends ActivityPubObject { }, (replyToNoteId, fromUser, fromActor, remoteActor, callback) => { const to = [ - message.isPrivate() - ? remoteActor.id - : Collection.PublicCollectionId, + message.isPrivate() ? remoteActor.id : PublicCollectionId, ]; const sourceMediaType = isAnsi(message.message) diff --git a/core/servers/content/web_handlers/activitypub.js b/core/servers/content/web_handlers/activitypub.js index ebfe6f5c..9403731d 100644 --- a/core/servers/content/web_handlers/activitypub.js +++ b/core/servers/content/web_handlers/activitypub.js @@ -10,6 +10,7 @@ const { ActivityStreamMediaType, WellKnownActivity, Collections, + PublicCollectionId, } = require('../../../activitypub/const'); const Config = require('../../../config').get; const Activity = require('../../../activitypub/activity'); @@ -381,7 +382,7 @@ exports.getModule = class ActivityPubWebHandler extends WebHandlerModule { recipientActorIds, (actorId, nextActorId) => { switch (actorId) { - case Collection.PublicCollectionId: + case PublicCollectionId: this._deliverNoteToSharedInbox(activity, note, err => { return nextActorId(err); }); @@ -436,6 +437,32 @@ exports.getModule = class ActivityPubWebHandler extends WebHandlerModule { }); } + _getMatchingObjectsForDeleteRequest(objectId, cb) { + async.waterfall( + [ + callback => { + return Collection.objectsById(objectId, callback); + }, + (objectsInfo, callback) => { + Collection.objectByEmbeddedId(objectId, (err, obj, objInfo) => { + if (err) { + return callback(err); + } + + const allObjsInfo = objectsInfo; + if (obj) { + allObjsInfo.push({ info: objInfo, object: obj }); + } + return callback(null, objectsInfo); + }); + }, + ], + (err, objectsInfo) => { + return cb(err, objectsInfo); + } + ); + } + _inboxDeleteActivity(inboxType, signature, resp, activity) { const objectId = _.get(activity, 'object.id', activity.object); @@ -443,7 +470,8 @@ exports.getModule = class ActivityPubWebHandler extends WebHandlerModule { // :TODO: we need to DELETE the existing stored Message object if this is a Note, or associated if this is an Actor // :TODO: delete / invalidate any actor cache if actor - Collection.objectsById(objectId, (err, objectsInfo) => { + + this._getMatchingObjectsForDeleteRequest(objectId, (err, objectsInfo) => { if (err) { this.log.warn({ objectId }); // We'll respond accepted so they don't keep trying @@ -750,7 +778,10 @@ exports.getModule = class ActivityPubWebHandler extends WebHandlerModule { } _deliverNoteToSharedInbox(activity, note, cb) { - this.log.info({ noteId: note.id }, 'Delivering Note to Public inbox'); + this.log.info( + { activityId: activity.id, noteId: note.id }, + 'Delivering Note to Public inbox' + ); Collection.addSharedInboxItem(activity, true, err => { if (err) { @@ -768,16 +799,21 @@ exports.getModule = class ActivityPubWebHandler extends WebHandlerModule { } _deliverNoteToLocalActor(actorId, activity, note, cb) { - this.log.info( - { noteId: note.id, actorId }, - 'Delivering Note to local Actor Private inbox' - ); - + // Skip over e.g. actorId = https://someethingsomething/users/Actor/followers userFromActorId(actorId, (err, localUser) => { if (err) { + this.log.trace( + { activityId: activity.id, noteId: note.id, actorId }, + `No Actor by ID ${actorId}` + ); return cb(null); // not found/etc., just bail } + this.log.info( + { activityId: activity.id, noteId: note.id, actorId }, + 'Delivering Note to local Actor Private inbox' + ); + Collection.addInboxItem(activity, localUser, this.webServer, false, err => { if (err) { return cb(err);