Node.js based forum software built for the modern web
fix: don't display crosspost button if user doesn't have privilege
Barış Soner Uşaklı0cd65312f288
diff --git a/install/package.json b/install/package.jsonindex bdf1eaa44c..384eaa0483 100644--- a/install/package.json+++ b/install/package.json@@ -113,10 +113,10 @@"nodebb-plugin-spam-be-gone": "2.3.5","nodebb-plugin-web-push": "0.9.3","nodebb-rewards-essentials": "1.0.3",- "nodebb-theme-harmony": "3.2.10",+ "nodebb-theme-harmony": "3.2.11","nodebb-theme-lavender": "8.1.0","nodebb-theme-peace": "3.1.3",- "nodebb-theme-persona": "15.2.6",+ "nodebb-theme-persona": "15.2.7","nodebb-widget-essentials": "8.0.9","nodemailer": "9.0.5","nprogress": "0.2.0",diff --git a/public/openapi/read/topic/topic_id.yaml b/public/openapi/read/topic/topic_id.yamlindex a18eb62dfe..ef56aa26e4 100644--- a/public/openapi/read/topic/topic_id.yaml+++ b/public/openapi/read/topic/topic_id.yaml@@ -222,6 +222,9 @@ get:type: arrayitems:$ref: ../../components/schemas/CrosspostObject.yaml#/CrosspostObject+ canCrosspost:+ type: boolean+ description: Whether the user can cross post to any category other than the topic's current category.- type: objectdescription: Optional properties that may or may not be present (except for `tid`, which is always present, and is only here as a hack to pass validation)properties:diff --git a/src/controllers/topics.js b/src/controllers/topics.jsindex 9485dfdc5a..a38f322d54 100644--- a/src/controllers/topics.js+++ b/src/controllers/topics.js@@ -17,6 +17,12 @@ const utils = require('../utils');const analytics = require('../analytics');const activitypub = require('../activitypub');const translator = require('../translator');+const cacheCreate = require('../cache/lru');+const crosspostCache = cacheCreate({+ name: 'crosspost',+ size: 500,+ ttl: 3600000,+});const topicsController = module.exports;@@ -134,9 +140,10 @@ topicsController.get = async function getTopic(req, res, next) {p => parseInt(p.index, 10) === parseInt(Math.max(0, postIndex - 1), 10));- const [author, crossposts] = await Promise.all([+ const [author, crossposts, canCrosspost] = await Promise.all([user.getUserFields(topicData.uid, ['username', 'userslug']),topics.crossposts.get(topicData.tid, req.uid),+ loadCrosspostPrivilege(req, topicData.cid),buildBreadcrumbs(topicData),addOldCategory(topicData, userPrivileges, settings.userLang),addTags(topicData, req, res, currentPage, postAtIndex),@@ -147,6 +154,7 @@ topicsController.get = async function getTopic(req, res, next) {topicData.author = author;topicData.crossposts = crossposts;+ topicData.canCrosspost = canCrosspost;topicData.pagination = pagination.create(currentPage, pageCount, req.query);topicData.pagination.rel.forEach((rel) => {rel.href = `${url}/topic/${topicData.slug}${rel.href}`;@@ -211,6 +219,17 @@ async function markAsRead(req, tid) {}}+async function loadCrosspostPrivilege(req, excludeCid) {+ excludeCid = String(excludeCid || '');+ let cidsUserCanCrosspost = crosspostCache.get(`uid:${req.uid}`);+ if (cidsUserCanCrosspost === undefined) {+ const cids = await categories.getAllCidsFromSet('categories:cid');+ cidsUserCanCrosspost = await privileges.categories.filterCids('topics:crosspost', cids, req.uid);+ crosspostCache.set(`uid:${req.uid}`, cidsUserCanCrosspost, 3600000);+ }+ return cidsUserCanCrosspost.filter(cid => cid !== excludeCid).length > 0;+}+async function buildBreadcrumbs(topicData) {const breadcrumbs = [{