mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-27 09:06:15 +01:00
fix: posts.uploads.sync dissociates uploaded thumbs of the main pid
This commit is contained in:
@@ -6,9 +6,11 @@ const crypto = require('crypto');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const winston = require('winston');
|
const winston = require('winston');
|
||||||
const mime = require('mime');
|
const mime = require('mime');
|
||||||
|
const validator = require('validator');
|
||||||
|
|
||||||
const db = require('../database');
|
const db = require('../database');
|
||||||
const image = require('../image');
|
const image = require('../image');
|
||||||
|
const topics = require('../topics');
|
||||||
const file = require('../file');
|
const file = require('../file');
|
||||||
|
|
||||||
module.exports = function (Posts) {
|
module.exports = function (Posts) {
|
||||||
@@ -21,9 +23,10 @@ module.exports = function (Posts) {
|
|||||||
Posts.uploads.sync = async function (pid) {
|
Posts.uploads.sync = async function (pid) {
|
||||||
// Scans a post's content and updates sorted set of uploads
|
// 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.getPostField(pid, 'content'),
|
||||||
Posts.uploads.list(pid),
|
Posts.uploads.list(pid),
|
||||||
|
Posts.isMain(pid),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Extract upload file paths from post content
|
// Extract upload file paths from post content
|
||||||
@@ -34,6 +37,16 @@ module.exports = function (Posts) {
|
|||||||
match = searchRegex.exec(content);
|
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
|
// Create add/remove sets
|
||||||
const add = uploads.filter(path => !currentUploads.includes(path));
|
const add = uploads.filter(path => !currentUploads.includes(path));
|
||||||
const remove = currentUploads.filter(path => !uploads.includes(path));
|
const remove = currentUploads.filter(path => !uploads.includes(path));
|
||||||
|
|||||||
@@ -114,6 +114,7 @@ describe('Topic thumbs', () => {
|
|||||||
|
|
||||||
describe('.associate()', () => {
|
describe('.associate()', () => {
|
||||||
let tid;
|
let tid;
|
||||||
|
let mainPid;
|
||||||
|
|
||||||
before(async () => {
|
before(async () => {
|
||||||
topicObj = await topics.post({
|
topicObj = await topics.post({
|
||||||
@@ -123,6 +124,7 @@ describe('Topic thumbs', () => {
|
|||||||
content: 'The content of test topic',
|
content: 'The content of test topic',
|
||||||
});
|
});
|
||||||
tid = topicObj.topicData.tid;
|
tid = topicObj.topicData.tid;
|
||||||
|
mainPid = topicObj.postData.pid;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should add an uploaded file to a zset', async () => {
|
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 () => {
|
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);
|
const uploads = await posts.uploads.list(mainPid);
|
||||||
assert(uploads.includes(path.basename(relativeThumbPaths[0])));
|
assert(uploads.includes(path.basename(relativeThumbPaths[0])));
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user