mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
fix: #7853, dissociation on post purge
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 () {
|
||||
|
||||
Reference in New Issue
Block a user