Node.js based forum software built for the modern web
fix: hide deleted main post content on
Barış Soner Uşaklı2db21463fade
diff --git a/src/api/posts.js b/src/api/posts.jsindex 63d5e517c4..8fde990b81 100644--- a/src/api/posts.js+++ b/src/api/posts.js@@ -37,8 +37,7 @@ postsAPI.get = async function (caller, data) {const selfPost = caller.uid && caller.uid === parseInt(post.uid, 10);if (post.deleted && !(userPrivilege.isAdminOrMod || selfPost)) {- post.content = '[[topic:post-is-deleted]]';- post.txContent = true;+ posts.clearDeletedPostContent(post);}return post;diff --git a/src/controllers/activitypub/topics.js b/src/controllers/activitypub/topics.jsindex 7143b82954..28ab4ae336 100644--- a/src/controllers/activitypub/topics.js+++ b/src/controllers/activitypub/topics.js@@ -105,10 +105,16 @@ controller.list = async function (req, res) {data.topicCount = topicCount;const mainPids = await topics.getMainPids(tids);- const postData = await posts.getPostSummaryByPids(mainPids, req.uid, {- stripTags: false,- extraFields: ['bookmarks'],- });+ const [postData, postPrivileges] = await Promise.all([+ posts.getPostSummaryByPids(mainPids, req.uid, {+ stripTags: false,+ extraFields: ['bookmarks'],+ }),+ privileges.posts.get(mainPids, req.uid),+ ]);+ postData.forEach(+ (postData, index) => posts.modifyPostByPrivilege(postData, postPrivileges[index])+ );const [{ upvotes }, bookmarkStatus] = await Promise.all([posts.getVoteStatusByPostIDs(mainPids, req.uid),posts.hasBookmarked(mainPids, req.uid),diff --git a/src/posts/index.js b/src/posts/index.jsindex 4c8a3873ac..5fd958d03f 100644--- a/src/posts/index.js+++ b/src/posts/index.js@@ -98,8 +98,7 @@ Posts.getPostIndices = async function (posts, uid) {Posts.modifyPostByPrivilege = function (post, privileges) {if (post && privileges) {if (post.deleted && !(post.selfPost || privileges['posts:view_deleted'])) {- post.content = '[[topic:post-is-deleted]]';- post.txContent = true;+ Posts.clearDeletedPostContent(post);if (post.user) {post.user.signature = '';}@@ -108,4 +107,14 @@ Posts.modifyPostByPrivilege = function (post, privileges) {}};+Posts.clearDeletedPostContent = function (post) {+ if (post) {+ post.content = '[[topic:post-is-deleted]]';+ if (Object.hasOwn(post, 'sourceContent')) {+ post.sourceContent = '[[topic:post-is-deleted]]';+ }+ post.txContent = true;+ }+};+require('../promisify')(Posts);diff --git a/src/privileges/categories.js b/src/privileges/categories.jsindex 3577baac7e..eccb3cd31f 100644--- a/src/privileges/categories.js+++ b/src/privileges/categories.js@@ -137,7 +137,7 @@ privsCategories.get = async function (cid, uid) {cid: cid,uid: uid,editable: isAdminOrMod,- view_deleted: isAdminOrMod || privData['posts:view_deleted'],+ view_deleted: isAdministrator || privData['posts:view_deleted'],view_scheduled : isAdministrator || privData['topics:schedule'],isAdminOrMod: isAdminOrMod,disabled,diff --git a/src/routes/feeds.js b/src/routes/feeds.jsindex f6551cbd40..224ed26a9b 100644--- a/src/routes/feeds.js+++ b/src/routes/feeds.js@@ -291,7 +291,7 @@ async function generateTopicsFeed(feedOptions, feedTopics, timestampField) {return;}const { mainPost } = topicData;- if (!mainPost) {+ if (!mainPost || mainPost.deleted) {feed.item(feedItem);return;}diff --git a/src/topics/posts.js b/src/topics/posts.jsindex bbfedfa80c..861ab11021 100644--- a/src/topics/posts.js+++ b/src/topics/posts.js@@ -199,8 +199,7 @@ module.exports = function (Topics) {await Promise.all(parentPosts.map(async (parentPost) => {const postPrivs = pidToPrivs[parentPost.pid];if (parentPost.deleted && String(parentPost.uid) !== String(callerUid, 10) && !postPrivs['posts:view_deleted']) {- parentPost.content = `[[topic:post-is-deleted]]`;- parentPost.txContent = true;+ posts.clearDeletedPostContent(parentPost);return;}const foundPost = postData.find(p => String(p.pid) === String(parentPost.pid));diff --git a/src/topics/teaser.js b/src/topics/teaser.jsindex 5d3fda2ee3..6a80f6ee87 100644--- a/src/topics/teaser.js+++ b/src/topics/teaser.js@@ -46,10 +46,10 @@ module.exports = function (Topics) {});const [allPostData, callerSettings] = await Promise.all([- posts.getPostsFields(teaserPids, ['pid', 'uid', 'timestamp', 'tid', 'content', 'sourceContent']),+ posts.getPostsFields(teaserPids, ['pid', 'uid', 'timestamp', 'tid', 'content', 'sourceContent', 'deleted']),user.getSettings(uid),]);- let postData = allPostData.filter(post => post && post.pid);+ let postData = allPostData.filter(post => post && post.pid && !post.deleted);postData = await handleBlocks(uid, postData);postData = postData.filter(Boolean);const uids = _.uniq(postData.map(post => post.uid));@@ -80,7 +80,7 @@ module.exports = function (Topics) {if (tidToPost[topic.tid]) {tidToPost[topic.tid].index = calcTeaserIndex(teaserPost, counts[index], sortNewToOld);}- return tidToPost[topic.tid];+ return tidToPost[topic.tid] || null;});const result = await plugins.hooks.fire('filter:teasers.get', { teasers, uid });diff --git a/src/views/partials/feed/item.tpl b/src/views/partials/feed/item.tplindex bdcf0260ad..c16a7ba1de 100644--- a/src/views/partials/feed/item.tpl+++ b/src/views/partials/feed/item.tpl@@ -56,7 +56,7 @@<div component="post/content" class="content text-sm text-break position-relative line-clamp-6"><a href="{config.relative_path}/post/{encodeURIComponent(./pid)}" class="stretched-link"></a>- {{./content}}+ {{{ if ./txContent}}}{{tx(./content)}}{{{ else }}}{{./content}}{{{ end }}}</div><button component="show/more" class="btn btn-link btn-sm fw-semibold hidden ff-secondary text-secondary ms-auto me-auto">{{tx("world:see-more")}}</button><hr class="my-2"/>diff --git a/test/topics.js b/test/topics.jsindex 91d3ff088c..4e24016d21 100644--- a/test/topics.js+++ b/test/topics.js@@ -2148,6 +2148,24 @@ describe('Topic\'s', () => {assert.equal(teaser.content, 'content 2');await User.blocks.remove(blockedUid, adminUid);});++ it('should not show deleted posts as teasers', async () => {+ const { topicData } = await topics.post({+ uid: adminUid,+ title: 'topic with deleted posts',+ content: 'content 1',+ cid: categoryObj.cid,+ });+ const { pid } = await topics.reply({ uid: adminUid, content: 'reply 1 content', tid: topicData.tid });+ await posts.delete(pid, adminUid);+ await posts.delete(topicData.mainPid);+ const teasers = await topics.getTeasers([topicData], {+ uid: 0, // call as guest+ teaserPost: 'last-post',+ });+ console.log({ teasers });+ assert.deepStrictEqual(teasers[0], null);+ });});describe('tag privilege', () => {