Node.js based forum software built for the modern web
fix: during post creation don't use posts.uploads.sync
Barış Soner Uşaklı1e4904266cd3
diff --git a/src/posts/create.js b/src/posts/create.jsindex 2586d1656b..b0cd5bd566 100644--- a/src/posts/create.js+++ b/src/posts/create.js@@ -55,6 +55,9 @@ module.exports = function (Posts) {}({ post: postData } = await plugins.hooks.fire('filter:post.create', { post: postData, data: data }));++ const uploads = await Posts.uploads.getUploadsForPost(postData, isMain);+ postData.uploads = JSON.stringify(uploads);await db.setObject(`post:${postData.pid}`, postData);const topicData = await topics.getTopicFields(tid, ['cid', 'pinned']);@@ -68,7 +71,8 @@ module.exports = function (Posts) {categories.onNewPostMade(topicData.cid, topicData.pinned, postData),groups.onNewPostMade(postData),addReplyTo(postData, timestamp),- Posts.uploads.sync(pid),+ Posts.uploads.saveUploadsToPid(uploads, postData.pid),+ Posts.uploads.saveSize(uploads),hasAttachment ? Posts.attachments.update(pid, _activitypub.attachment) : null,]);diff --git a/src/posts/uploads.js b/src/posts/uploads.jsindex c0daa32bd3..6a1b3b4417 100644--- a/src/posts/uploads.js+++ b/src/posts/uploads.js@@ -55,42 +55,47 @@ module.exports = function (Posts) {};Posts.uploads.sync = async function (pid) {- // Scans a post's content and updates sorted set of uploads-const [postData, isMainPost] = await Promise.all([Posts.getPostFields(pid, ['tid', 'content', 'uploads']),Posts.isMain(pid),]);- const content = postData.content || '';const currentUploads = postData.uploads || [];+ const uploads = await Posts.uploads.getUploadsForPost(postData, isMainPost);- // Extract upload file paths from post content- let match = searchRegex.exec(content);- let uploads = new Set();- while (match) {- uploads.add(match[1].replace('-resized', ''));- match = searchRegex.exec(content);- }+ const add = uploads.filter(path => !currentUploads.includes(path));+ const remove = currentUploads.filter(path => !uploads.includes(path));+ await Posts.uploads.associate(pid, add);+ await Posts.uploads.dissociate(pid, remove);+ };- // Main posts can contain topic thumbs, which are also tracked by pid+ Posts.uploads.saveUploadsToPid = async function (uploads, pid) {+ const now = Date.now();+ await db.sortedSetAddBulk(uploads.map(path => [`upload:${md5(path)}:pids`, now, pid]));+ };++ Posts.uploads.getUploadsForPost = async function ({ tid, content }, isMainPost) {+ const uploadsSet = getUploadsFromPostContent(content || '');if (isMainPost) {- let thumbs = await topics.thumbs.get(postData.tid, { thumbsOnly: true });+ let thumbs = await topics.thumbs.get(tid, { thumbsOnly: true });thumbs = thumbs.map(thumb => thumb.path).filter(path => !validator.isURL(path, {require_protocol: true,}));- thumbs.forEach(t => uploads.add(t));+ thumbs.forEach(t => uploadsSet.add(t));}-- uploads = Array.from(uploads);-- // Create add/remove sets- const add = uploads.filter(path => !currentUploads.includes(path));- const remove = currentUploads.filter(path => !uploads.includes(path));- await Posts.uploads.associate(pid, add);- await Posts.uploads.dissociate(pid, remove);+ return await _filterValidPaths(Array.from(uploadsSet));};+ function getUploadsFromPostContent(content) {+ let match = searchRegex.exec(content);+ const uploads = new Set();+ while (match) {+ uploads.add(match[1].replace('-resized', ''));+ match = searchRegex.exec(content);+ }+ return uploads;+ }+Posts.uploads.list = async function (pids) {const isArray = Array.isArray(pids);if (isArray) {@@ -186,12 +191,9 @@ module.exports = function (Posts) {}});- const now = Date.now();- const bulkAdd = filePaths.map(path => [`upload:${md5(path)}:pids`, now, pid]);-await Promise.all([db.setObjectField(`post:${pid}`, 'uploads', JSON.stringify(currentUploads)),- db.sortedSetAddBulk(bulkAdd),+ Posts.uploads.saveUploadsToPid(filePaths, pid),Posts.uploads.saveSize(filePaths),]);};