Node.js based forum software built for the modern web
feat: #14277, handle remote chat message deletion
Julian Lamd69b6e8e8c22
diff --git a/src/activitypub/inbox.js b/src/activitypub/inbox.jsindex 289ff6a0bc..49b29fe007 100644--- a/src/activitypub/inbox.js+++ b/src/activitypub/inbox.js@@ -277,15 +277,15 @@ inbox.delete = async (req) => {// Deletions must be made by an actor of the same originconst actorHostname = new URL(actor).hostname;-const objectHostname = new URL(id).hostname;if (actorHostname !== objectHostname) {throw new Error('[[error:activitypub.origin-mismatch]]');}- const [isNote, isContext/* , isActor */] = await Promise.all([+ const [isNote, isContext, isMessage/* , isActor */] = await Promise.all([posts.exists(id),activitypub.contexts.getItems(0, id, { returnRootId: true }), // ⚠️ unreliable, needs better logic (Contexts.is?)+ messaging.messageExists(id),// db.isSortedSetMember('usersRemote:lastCrawled', object.id),]);@@ -323,6 +323,11 @@ inbox.delete = async (req) => {break;}+ case isMessage: {+ await api.chats.deleteMessage({ uid: actor }, { mid: id });+ break;+ }+// case isActor: {// console.log('actor');// break;diff --git a/test/activitypub/inbox.js b/test/activitypub/inbox.jsindex d501e989aa..17cdc7e83c 100644--- a/test/activitypub/inbox.js+++ b/test/activitypub/inbox.js@@ -11,6 +11,7 @@ const categories = require('../../src/categories');const topics = require('../../src/topics');const posts = require('../../src/posts');const privileges = require('../../src/privileges');+const messaging = require('../../src/messaging');const activitypub = require('../../src/activitypub');const utils = require('../../src/utils');@@ -584,6 +585,64 @@ describe('Inbox', () => {assert.strictEqual(topicExistsAfter, true);});});++ describe('(Delete) with non-public notes (aka chat messages)', () => {+ before(async function () {+ this.uid = await user.create({ username: utils.generateUUID().slice(0, 10) });+ const remote = helpers.mocks.person();+ this.remoteId = remote.id;+ const result = await activitypub.actors.assert([remote.id]);++ // Create a private chat room between the users+ const { note } = helpers.mocks.note({+ attributedTo: remote.id,+ to: [`${nconf.get('url')}/uid/${this.uid}`],+ cc: [],+ });+ this.mid = note.id;+ const { roomId } = await activitypub.notes.assertPrivate(note);+ this.roomId = roomId;+ });++ it('should soft-delete a chat message via inbox.delete', async function () {+ // Verify message exists before deletion+ const existsBefore = await messaging.messageExists(this.mid);+ assert.strictEqual(existsBefore, true);+ const deletedBefore = await messaging.getMessageField(this.mid, 'deleted');+ assert.strictEqual(deletedBefore, 0);++ // Create a Delete activity for the chat message+ const { activity: deleteActivity } = helpers.mocks.delete({+ actor: this.remoteId,+ object: this.mid,+ });++ // Process the delete directly+ await activitypub.inbox.delete({ body: deleteActivity });++ // Verify message is soft-deleted+ const existsAfter = await messaging.messageExists(this.mid);+ assert.strictEqual(existsAfter, true);+ const deletedAfter = await messaging.getMessageField(this.mid, 'deleted');+ assert.strictEqual(deletedAfter, 1);+ });++ it('should do nothing when deleting a non-existent message', async function () {+ const { hostname } = new URL(this.remoteId);+ const fakeMid = `https://${hostname}/note/9999999999`;+ const { activity: deleteActivity } = helpers.mocks.delete({+ actor: this.remoteId,+ object: fakeMid,+ });++ // This should not throw – it should hit the default case+ await activitypub.inbox.delete({ body: deleteActivity });++ // Verify message still doesn't exist+ const exists = await messaging.messageExists(fakeMid);+ assert.strictEqual(exists, false);+ });+ });});describe('Like', () => {