From 037b901e8519a41d896b69d261ebbff1b9262a3f Mon Sep 17 00:00:00 2001 From: barisusakli Date: Wed, 10 Aug 2016 21:28:22 +0300 Subject: [PATCH] closes #4919 --- public/language/en_GB/error.json | 3 ++ src/privileges/topics.js | 48 ++++++++++++++++++++----------- src/views/admin/settings/post.tpl | 4 +++ 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/public/language/en_GB/error.json b/public/language/en_GB/error.json index fa73ccfa8a..8dc3df4b02 100644 --- a/public/language/en_GB/error.json +++ b/public/language/en_GB/error.json @@ -67,6 +67,9 @@ "post-delete-duration-expired-days": "You are only allowed to delete posts for %1 day(s) after posting", "post-delete-duration-expired-days-hours": "You are only allowed to delete posts for %1 day(s) %2 hour(s) after posting", + "cant-delete-topic-has-reply": "You can't delete your topic after it has %1 reply", + "cant-delete-topic-has-replies": "You can't delete your topic after it has %1 replies", + "content-too-short": "Please enter a longer post. Posts should contain at least %1 character(s).", "content-too-long": "Please enter a shorter post. Posts can't be longer than %1 character(s).", "title-too-short": "Please enter a longer title. Titles should contain at least %1 character(s).", diff --git a/src/privileges/topics.js b/src/privileges/topics.js index c3a61fd317..7caffc06c3 100644 --- a/src/privileges/topics.js +++ b/src/privileges/topics.js @@ -3,6 +3,7 @@ var async = require('async'); +var meta = require('../meta'); var topics = require('../topics'); var user = require('../user'); var helpers = require('./helpers'); @@ -182,25 +183,40 @@ module.exports = function(privileges) { }; privileges.topics.canDelete = function(tid, uid, callback) { - topics.getTopicField(tid, 'cid', function(err, cid) { + var topicData; + async.waterfall([ + function(next) { + topics.getTopicFields(tid, ['cid', 'postcount'], next); + }, + function(_topicData, next) { + topicData = _topicData; + async.parallel({ + isModerator: async.apply(user.isModerator, uid, topicData.cid), + isAdministrator: async.apply(user.isAdministrator, uid), + isOwner: async.apply(topics.isOwner, tid, uid), + 'topics:delete': async.apply(helpers.isUserAllowedTo, 'topics:delete', uid, [topicData.cid]) + }, next); + } + ], function(err, results) { if (err) { return callback(err); } - helpers.some([ - async.apply(user.isModerator, uid, cid), - async.apply(user.isAdministrator, uid), - function(next) { - async.parallel({ - owner: async.apply(topics.isOwner, tid, uid), - 'topics:delete': async.apply(helpers.isUserAllowedTo, 'topics:delete', uid, [cid]) - }, function(err, result) { - if (err) { - return next(err); - } - next(null, result.owner && result['topics:delete'][0]); - }); - } - ], callback); + + if (results.isModerator || results.isAdministrator) { + return callback(null, true); + } + + var preventTopicDeleteAfterReplies = parseInt(meta.config.preventTopicDeleteAfterReplies, 10) || 0; + if (preventTopicDeleteAfterReplies && (topicData.postcount - 1) >= preventTopicDeleteAfterReplies) { + var langKey = preventTopicDeleteAfterReplies > 1 ? 'cant-delete-topic-has-replies' : 'cant-delete-topic-has-reply'; + return callback(new Error('[[error:' + langKey + ', ' + meta.config.preventTopicDeleteAfterReplies + ']]')); + } + + if (!results['topics:delete'][0]) { + return callback(null, false); + } + + callback(null, results.isOwner); }); }; diff --git a/src/views/admin/settings/post.tpl b/src/views/admin/settings/post.tpl index 4ac9aa3d2a..f58f524aca 100644 --- a/src/views/admin/settings/post.tpl +++ b/src/views/admin/settings/post.tpl @@ -52,6 +52,10 @@ +
+ + +