Skip to content

Commit df3362b

Browse files
committed
Merge pull request #562 from Martii/emptyCommentingCheck
Prevent empty spaced commenting and topics Auto-merge
2 parents 4e2166c + cf4fa8c commit df3362b

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

controllers/discussion.js

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,9 @@ function postTopic(aUser, aCategory, aTopic, aContent, aIssue, aCallback) {
362362
var params = { sort: {} };
363363
params.sort.duplicateId = -1;
364364

365-
if (!urlTopic) { aCallback(null); }
365+
if (!urlTopic) {
366+
aCallback(null);
367+
}
366368

367369
Discussion.findOne({ path: path }, null, params, function (aErr, aDiscussion) {
368370
var newDiscussion = null;
@@ -414,8 +416,9 @@ exports.createTopic = function (aReq, aRes, aNext) {
414416
var content = aReq.body['comment-content'];
415417

416418
var category = _.findWhere(categories, { slug: categorySlug });
417-
if (!category)
419+
if (!category) {
418420
return aNext();
421+
}
419422

420423
//
421424
var options = {};
@@ -428,7 +431,14 @@ exports.createTopic = function (aReq, aRes, aNext) {
428431
if (!category.canUserPostTopic(authedUser)) {
429432
return statusCodePage(aReq, aRes, aNext, {
430433
statusCode: 403,
431-
statusMessage: 'You cannot post a topic to this category',
434+
statusMessage: 'You cannot post a topic to this category'
435+
});
436+
}
437+
438+
if ((!topic || !topic.trim()) || (!content || !content.trim())) {
439+
return statusCodePage(aReq, aRes, aNext, {
440+
statusCode: 403,
441+
statusMessage: 'You cannot post an empty discussion topic to this category'
432442
});
433443
}
434444

@@ -447,14 +457,25 @@ exports.createComment = function (aReq, aRes, aNext) {
447457
var authedUser = aReq.session.user;
448458
var content = aReq.body['comment-content'];
449459

450-
if (!authedUser) { return aNext(); }
460+
if (!authedUser) {
461+
return aNext();
462+
}
451463

452-
findDiscussion(category, topic, function (discussion) {
453-
if (!discussion) { return aNext(); }
464+
findDiscussion(category, topic, function (aDiscussion) {
465+
if (!aDiscussion) {
466+
return aNext();
467+
}
468+
469+
if (!content || !content.trim()) {
470+
return statusCodePage(aReq, aRes, aNext, {
471+
statusCode: 403,
472+
statusMessage: 'You cannot post an empty comment to this discussion'
473+
});
474+
}
454475

455-
postComment(authedUser, discussion, content, false, function (err, discussion) {
456-
aRes.redirect(encodeURI(discussion.path
457-
+ (discussion.duplicateId ? '_' + discussion.duplicateId : '')));
476+
postComment(authedUser, aDiscussion, content, false, function (aErr, aDiscussion) {
477+
aRes.redirect(encodeURI(aDiscussion.path
478+
+ (aDiscussion.duplicateId ? '_' + aDiscussion.duplicateId : '')));
458479
});
459480
});
460481
};

controllers/issue.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var scriptStorage = require('./scriptStorage');
1919
var discussionLib = require('./discussion');
2020
var execQueryTask = require('../libs/tasks').execQueryTask;
2121
var countTask = require('../libs/tasks').countTask;
22+
var statusCodePage = require('../libs/templateHelpers').statusCodePage;
2223
var pageMetadata = require('../libs/templateHelpers').pageMetadata;
2324
var orderDir = require('../libs/templateHelpers').orderDir;
2425

@@ -284,6 +285,14 @@ exports.open = function (aReq, aRes, aNext) {
284285
options.category = category;
285286

286287
if (topic && content) {
288+
if (!topic.trim() || !content.trim()) {
289+
return statusCodePage(aReq, aRes, aNext, {
290+
statusCode: 403,
291+
statusMessage: 'You cannot post an empty issue topic to this ' +
292+
(type === 'libs' ? 'library' : 'script')
293+
});
294+
}
295+
287296
// Issue Submission
288297
discussionLib.postTopic(authedUser, category.slug, topic, content, true,
289298
function (aDiscussion) {
@@ -315,12 +324,23 @@ exports.comment = function (aReq, aRes, aNext) {
315324

316325
Script.findOne({ installName: scriptStorage.caseInsensitive(installName
317326
+ (type === 'libs' ? '.js' : '.user.js')) }, function (aErr, aScript) {
318-
var content = aReq.body['comment-content'];
327+
var content = aReq.body['comment-content'];
319328

320-
if (aErr || !aScript) { return aNext(); }
329+
if (aErr || !aScript) {
330+
return aNext();
331+
}
332+
333+
if (!content || !content.trim()) {
334+
return statusCodePage(aReq, aRes, aNext, {
335+
statusCode: 403,
336+
statusMessage: 'You cannot post an empty comment to this issue'
337+
});
338+
}
321339

322340
discussionLib.findDiscussion(category, topic, function (aIssue) {
323-
if (!aIssue) { return aNext(); }
341+
if (!aIssue) {
342+
return aNext();
343+
}
324344

325345
discussionLib.postComment(authedUser, aIssue, content, false,
326346
function (aErr, aDiscussion) {

views/includes/commentForm.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<form action="{{{discussion.discussionPageUrl}}}" method="post">
33
<div class="container-fluid row user-content">
44
<div class="submit-panel pull-right btn-toolbar">
5-
<button class="btn btn-sm btn-success" type="submit"><i class="fa fa-reply"></i> Reply</button>
65
<button class="btn btn-sm btn-danger" type="button" data-toggle="collapse" data-target="#reply-control"><i class="fa fa-close"></i> Cancel</button>
6+
<button class="btn btn-sm btn-success" type="submit"><i class="fa fa-reply"></i> Reply</button>
77
</div>
88
<textarea name="comment-content" data-provide="markdown" data-iconlibrary="fa" class="col-xs-12" placeholder="Type here using Markdown."></textarea>
99
</div>

0 commit comments

Comments
 (0)