Node.js based forum software built for the modern web
fix: posts.uploads.sync dissociates uploaded thumbs of the main pid
Julian Lamf79aeef889a9
diff --git a/src/posts/uploads.js b/src/posts/uploads.jsindex 38ed486f61..f27f02fbe6 100644--- a/src/posts/uploads.js+++ b/src/posts/uploads.js@@ -6,9 +6,11 @@ const crypto = require('crypto');const path = require('path');const winston = require('winston');const mime = require('mime');+const validator = require('validator');const db = require('../database');const image = require('../image');+const topics = require('../topics');const file = require('../file');module.exports = function (Posts) {@@ -21,9 +23,10 @@ module.exports = function (Posts) {Posts.uploads.sync = async function (pid) {// Scans a post's content and updates sorted set of uploads- const [content, currentUploads] = await Promise.all([+ const [content, currentUploads, isMainPost] = await Promise.all([Posts.getPostField(pid, 'content'),Posts.uploads.list(pid),+ Posts.isMain(pid),]);// Extract upload file paths from post content@@ -34,6 +37,16 @@ module.exports = function (Posts) {match = searchRegex.exec(content);}+ // Main posts can contain topic thumbs, which are also tracked by pid+ if (isMainPost) {+ const tid = await Posts.getPostField(pid, 'tid');+ let thumbs = await topics.thumbs.get(tid);+ thumbs = thumbs.map(thumb => thumb.url.replace(path.join(nconf.get('upload_url'), 'files/'), '')).filter(path => !validator.isURL(path, {+ require_protocol: true,+ }));+ uploads.push(...thumbs);+ }+// Create add/remove setsconst add = uploads.filter(path => !currentUploads.includes(path));const remove = currentUploads.filter(path => !uploads.includes(path));diff --git a/test/topicThumbs.js b/test/topicThumbs.jsindex e85e1de16d..bbd898b269 100644--- a/test/topicThumbs.js+++ b/test/topicThumbs.js@@ -114,6 +114,7 @@ describe('Topic thumbs', () => {describe('.associate()', () => {let tid;+ let mainPid;before(async () => {topicObj = await topics.post({@@ -123,6 +124,7 @@ describe('Topic thumbs', () => {content: 'The content of test topic',});tid = topicObj.topicData.tid;+ mainPid = topicObj.postData.pid;});it('should add an uploaded file to a zset', async () => {@@ -156,7 +158,12 @@ describe('Topic thumbs', () => {});it('should associate the thumbnail with that topic\'s main pid\'s uploads', async () => {- const mainPid = (await topics.getMainPids([tid]))[0];+ const uploads = await posts.uploads.list(mainPid);+ assert(uploads.includes(path.basename(relativeThumbPaths[0])));+ });++ it('should maintain state in the topic\'s main pid\'s uploads if posts.uploads.sync() is called', async () => {+ await posts.uploads.sync(mainPid);const uploads = await posts.uploads.list(mainPid);assert(uploads.includes(path.basename(relativeThumbPaths[0])));});