Skip to content

Node.js based forum software built for the modern web

fix: #7853, dissociation on post purge

Julian Lam30a86ed5533d
diff --git a/src/posts/delete.js b/src/posts/delete.js
index 8bd17e02b8..a64c74552c 100644
--- a/src/posts/delete.js
+++ b/src/posts/delete.js
@@ -57,6 +57,7 @@ module.exports = function (Posts) {
deletePostFromReplies(postData),
deletePostFromGroups(postData),
db.sortedSetsRemove(['posts:pid', 'posts:votes', 'posts:flagged'], pid),
+ Posts.uploads.dissociateAll(pid),
]);
plugins.fireHook('action:post.purge', { post: postData, uid: uid });
await db.delete('post:' + pid);
diff --git a/src/posts/uploads.js b/src/posts/uploads.js
index 8142f1b992..3715619f65 100644
--- a/src/posts/uploads.js
+++ b/src/posts/uploads.js
@@ -108,6 +108,11 @@ module.exports = function (Posts) {
]);
};
+ Posts.uploads.dissociateAll = async (pid) => {
+ const current = await Posts.uploads.list(pid);
+ await Promise.all(current.map(async path => await Posts.uploads.dissociate(pid, path)));
+ };
+
Posts.uploads.saveSize = async (filePaths) => {
await Promise.all(filePaths.map(async function (fileName) {
try {
diff --git a/test/posts.js b/test/posts.js
index 0bfdf2b633..546c6b8741 100644
--- a/test/posts.js
+++ b/test/posts.js
@@ -1060,23 +1060,29 @@ describe('Post\'s', function () {
});
describe('upload methods', function () {
- var pid;
+ let pid;
+ let purgePid;
- before(function (done) {
+ before(async () => {
// Create stub files for testing
['abracadabra.png', 'shazam.jpg', 'whoa.gif', 'amazeballs.jpg', 'wut.txt', 'test.bmp']
.forEach(filename => fs.closeSync(fs.openSync(path.join(nconf.get('upload_path'), 'files', filename), 'w')));
- topics.post({
+ const topicPostData = await topics.post({
uid: 1,
cid: 1,
title: 'topic with some images',
content: 'here is an image [alt text](/assets/uploads/files/abracadabra.png) and another [alt text](/assets/uploads/files/shazam.jpg)',
- }, function (err, topicPostData) {
- assert.ifError(err);
- pid = topicPostData.postData.pid;
- done();
});
+ pid = topicPostData.postData.pid;
+
+ const purgePostData = await topics.post({
+ uid: 1,
+ cid: 1,
+ title: 'topic with some images, to be purged',
+ content: 'here is an image [alt text](/assets/uploads/files/whoa.gif) and another [alt text](/assets/uploads/files/amazeballs.jpg)',
+ });
+ purgePid = purgePostData.postData.pid;
});
describe('.sync()', function () {
@@ -1225,6 +1231,31 @@ describe('Post\'s', function () {
});
});
});
+
+ describe('.dissociateAll()', () => {
+ it('should remove all images from a post\'s maintained list of uploads', async () => {
+ await posts.uploads.dissociateAll(pid);
+ const uploads = await posts.uploads.list(pid);
+
+ assert.equal(uploads.length, 0);
+ });
+ });
+
+ describe('Dissociation on purge', () => {
+ it('should not dissociate images on post deletion', async () => {
+ await posts.delete(purgePid, 1);
+ const uploads = await posts.uploads.list(purgePid);
+
+ assert.equal(uploads.length, 2);
+ });
+
+ it('should dissociate images on post purge', async () => {
+ await posts.purge(purgePid, 1);
+ const uploads = await posts.uploads.list(purgePid);
+
+ assert.equal(uploads.length, 0);
+ });
+ });
});
describe('post uploads management', function () {