fix: posts.uploads.sync dissociates uploaded thumbs of the main pid

v1.18.x
Julian Lam 4 years ago
parent 28b3013424
commit f79aeef889

@ -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 sets
const add = uploads.filter(path => !currentUploads.includes(path));
const remove = currentUploads.filter(path => !uploads.includes(path));

@ -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])));
});

Loading…
Cancel
Save