Node.js based forum software built for the modern web
fix(activitypub): regenerate topic title when post with generatedTitle is edited
Julian Lam359077a21c0a
diff --git a/src/activitypub/helpers.js b/src/activitypub/helpers.jsindex 78083896e9..eeada2d718 100644--- a/src/activitypub/helpers.js+++ b/src/activitypub/helpers.js@@ -6,6 +6,8 @@ const nconf = require('nconf');const winston = require('winston');const validator = require('validator');const crypto = require('crypto');+const tokenizer = require('sbd');+const pretty = require('pretty');const meta = require('../meta');const posts = require('../posts');@@ -15,7 +17,6 @@ const request = require('../request');const db = require('../database');const ttl = require('../cache/ttl');const user = require('../user');-const utils = require('../utils');const activitypub = require('.');const webfingerRegex = /^(@|acct:)?[\w-.]+@.+$/;@@ -353,43 +354,14 @@ Helpers.resolveObjects = async (ids) => {return objects.length === 1 ? objects[0] : objects;};-const titleishTags = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'title', 'p', 'span'];-const titleRegex = new RegExp(`<(${titleishTags.join('|')})>(.+?)</\\1>`, 'm');Helpers.generateTitle = (html) => {// Given an html string, generates a more appropriate title if possible- let title;+ const prettified = pretty(html);- // Try the first paragraph-like element- const match = html.match(titleRegex);- if (match && match.index === 0) {- title = match[2];- }-- // Fall back to newline splitting (i.e. if no paragraph elements)- title = title || html.split('\n').filter(Boolean).shift();-- // Discard everything after a line break element- title = title.replace(/<br(\s\/)?>.*/g, '');-- // Strip html- title = utils.stripHTMLTags(title);-- // Split sentences and use only first one- const sentences = title- .split(/(\.|\?|!)\s/)- .reduce((memo, cur, idx, sentences) => {- if (idx % 2) {- memo.push(`${sentences[idx - 1]}${cur}`);- } else if (idx === sentences.length - 1) {- memo.push(cur);- }-- return memo;- }, []);-- if (sentences.length > 1) {- title = sentences.shift();- }+ // Remove any lines that contain quote-post fallbacks+ const cleaned = prettified.split('\n').filter(line => !line.startsWith('<p class="quote-inline"')).join('\n');+ const sentences = tokenizer.sentences(cleaned, { sanitize: true, newline_boundaries: true });+ let title = sentences.shift();// Truncate down if too longif (title.length > meta.config.maximumTitleLength) {@@ -399,6 +371,7 @@ Helpers.generateTitle = (html) => {return title;};+Helpers.remoteAnchorToLocalProfile = async (content, isMarkdown = false) => {let anchorRegex;if (isMarkdown) {diff --git a/src/activitypub/inbox.js b/src/activitypub/inbox.jsindex 563e618dcc..99cbe5eba7 100644--- a/src/activitypub/inbox.js+++ b/src/activitypub/inbox.js@@ -15,6 +15,7 @@ const flags = require('../flags');const api = require('../api');const apiHelpers = require('../api/helpers');const utils = require('../utils');+const slugify = require('../slugify');const activitypub = require('.');const socketHelpers = require('../socket.io/helpers');@@ -181,6 +182,13 @@ inbox.update = async (req) => {const postData = await activitypub.mocks.post(object);postData.tags = await activitypub.notes._normalizeTags(postData._activitypub.tag, postData.cid);await posts.edit(postData);+ const tid = await posts.getPostField(object.id, 'tid');+ const { generatedTitle } = await topics.getTopicFields(tid, ['generatedTitle']);+ if (generatedTitle && (!postData.title || !postData.title.trim())) {+ const newTitle = activitypub.helpers.generateTitle(postData.content);+ await topics.setTopicField(tid, 'title', newTitle);+ await topics.setTopicField(tid, 'slug', `${tid}/${slugify(newTitle) || 'topic'}`);+ }const isDeleted = await posts.getPostField(object.id, 'deleted');if (isDeleted) {await api.posts.restore({ uid: actor }, { pid: object.id });