Node.js based forum software built for the modern web
feat(activitypub): support content warnings on incoming remote posts
Julian Lamc968c281997c
diff --git a/src/activitypub/mocks.js b/src/activitypub/mocks.jsindex 2e6fe6a62f..7f30f78742 100644--- a/src/activitypub/mocks.js+++ b/src/activitypub/mocks.js@@ -442,7 +442,7 @@ Mocks.post = async (objects) => {attributedTo: uid,inReplyTo: toPid,published, updated, name, content, sourceContent,- to, cc, audience, attachment, tag, image,+ to, cc, audience, attachment, tag, image, summary, sensitive,} = object;await activitypub.actors.assert(uid);@@ -469,6 +469,8 @@ Mocks.post = async (objects) => {edited,editor: edited ? uid : undefined,+ // Store contentWarning only for Note + sensitive + summary (Fediverse CW convention)+ ...(object.type === 'Note' && sensitive && summary && { contentWarning: summary }),_activitypub: { to, cc, audience, attachment, tag, url, image },};diff --git a/src/activitypub/notes.js b/src/activitypub/notes.jsindex eb6a457839..b7e95613fa 100644--- a/src/activitypub/notes.js+++ b/src/activitypub/notes.js@@ -119,7 +119,17 @@ Notes.assert = async (uid, input, options = { skipChecks: false, queue: false })chain = chain.sort((a, b) => a.timestamp - b.timestamp);const mainPost = chain[0];- let { pid: mainPid, tid, uid: authorId, timestamp, title, content, sourceContent, _activitypub } = mainPost;+ let {+ pid: mainPid,+ tid,+ uid: authorId,+ timestamp,+ title,+ content,+ sourceContent,+ contentWarning,+ _activitypub,+ } = mainPost;const hasTid = !!tid;const authorBanned = await user.bans.isBanned(authorId);@@ -206,6 +216,11 @@ Notes.assert = async (uid, input, options = { skipChecks: false, queue: false })generatedTitle = 1;}+ if (contentWarning) {+ title = contentWarning;+ generatedTitle = 1;+ }+// Extract emoji metadata for title renderingconst titleEmoji = [];const emojiTags = (_activitypub.tag || []).filter(tag => tag.type === 'Emoji');@@ -297,6 +312,7 @@ Notes.assert = async (uid, input, options = { skipChecks: false, queue: false })timestamp,content: mainPost.content,sourceContent: mainPost.sourceContent,+ contentWarning,generatedTitle,_activitypub: mainPost._activitypub,};@@ -322,6 +338,7 @@ Notes.assert = async (uid, input, options = { skipChecks: false, queue: false })tags,content: mainPost.content,sourceContent: mainPost.sourceContent,+ contentWarning,generatedTitle,_activitypub: mainPost._activitypub,});@@ -381,6 +398,7 @@ Notes.assert = async (uid, input, options = { skipChecks: false, queue: false })pid: post.pid,content: post.content,sourceContent: post.sourceContent,+ contentWarning: post.contentWarning,timestamp: post.timestamp,_activitypub: post._activitypub,});diff --git a/src/posts/create.js b/src/posts/create.jsindex b0cd5bd566..618f87a6ad 100644--- a/src/posts/create.js+++ b/src/posts/create.js@@ -39,6 +39,9 @@ module.exports = function (Posts) {if (data.handle && !parseInt(uid, 10)) {postData.handle = data.handle;}+ if (data.contentWarning) {+ postData.contentWarning = data.contentWarning;+ }if (_activitypub) {if (_activitypub.url) {postData.url = _activitypub.url;diff --git a/test/activitypub/notes.js b/test/activitypub/notes.jsindex d5cdfba695..29f3b46fc9 100644--- a/test/activitypub/notes.js+++ b/test/activitypub/notes.js@@ -74,6 +74,101 @@ describe('Notes', () => {assert(exists);});+ describe('Content warnings', () => {+ it('should store contentWarning for Note with summary and sensitive: true', async () => {+ const cwText = 'Spoiler: major plot twist';+ const { id } = helpers.mocks.note({+ type: 'Note',+ summary: cwText,+ sensitive: true,+ });+ const assertion = await activitypub.notes.assert(0, id, { skipChecks: true });+ assert(assertion);+ assert.strictEqual(assertion.count, 1);++ const mainPid = await topics.getTopicField(assertion.tid, 'mainPid');+ const storedCw = await posts.getPostField(mainPid, 'contentWarning');+ assert.strictEqual(storedCw, cwText);+ });++ it('should use contentWarning as topic title to prevent content leaks', async () => {+ const cwText = 'Trigger warning: graphic content';+ const { id } = helpers.mocks.note({+ type: 'Note',+ summary: cwText,+ sensitive: true,+ });+ const assertion = await activitypub.notes.assert(0, id, { skipChecks: true });+ assert(assertion);+ assert.strictEqual(assertion.count, 1);++ const topicTitle = await topics.getTopicField(assertion.tid, 'title');+ assert.strictEqual(topicTitle, cwText);++ const generatedTitle = await topics.getTopicField(assertion.tid, 'generatedTitle');+ assert.strictEqual(generatedTitle, 1);+ });++ it('should NOT store contentWarning for Note with summary but no sensitive', async () => {+ const { id } = helpers.mocks.note({+ type: 'Note',+ summary: 'Some summary text',+ sensitive: false,+ });+ const assertion = await activitypub.notes.assert(0, id, { skipChecks: true });+ assert(assertion);+ assert.strictEqual(assertion.count, 1);++ const mainPid = await topics.getTopicField(assertion.tid, 'mainPid');+ const storedCw = await posts.getPostField(mainPid, 'contentWarning');+ assert.strictEqual(storedCw, null);+ });++ it('should NOT store contentWarning for Note with sensitive but no summary', async () => {+ const { id } = helpers.mocks.note({+ type: 'Note',+ summary: 'remove',+ sensitive: true,+ });+ const assertion = await activitypub.notes.assert(0, id, { skipChecks: true });+ assert(assertion);+ assert.strictEqual(assertion.count, 1);++ const mainPid = await topics.getTopicField(assertion.tid, 'mainPid');+ const storedCw = await posts.getPostField(mainPid, 'contentWarning');+ assert.strictEqual(storedCw, null);+ });++ it('should NOT store contentWarning for Article with summary and sensitive: true', async () => {+ const { id } = helpers.mocks.note({+ type: 'Article',+ summary: 'Article preview text',+ sensitive: true,+ });+ const assertion = await activitypub.notes.assert(0, id, { skipChecks: true });+ assert(assertion);+ assert.strictEqual(assertion.count, 1);++ const mainPid = await topics.getTopicField(assertion.tid, 'mainPid');+ const storedCw = await posts.getPostField(mainPid, 'contentWarning');+ assert.strictEqual(storedCw, null);+ });++ it('should NOT store contentWarning for Note without summary or sensitive', async () => {+ const { id } = helpers.mocks.note({+ type: 'Note',+ summary: 'remove',+ });+ const assertion = await activitypub.notes.assert(0, id, { skipChecks: true });+ assert(assertion);+ assert.strictEqual(assertion.count, 1);++ const mainPid = await topics.getTopicField(assertion.tid, 'mainPid');+ const storedCw = await posts.getPostField(mainPid, 'contentWarning');+ assert.strictEqual(storedCw, null);+ });+ });+describe('Category-specific behaviours', () => {it('should slot newly created topic in local category if addressed', async () => {const { cid } = await categories.create({ name: utils.generateUUID() });