From 8cfb239aac788e23afcb7a362e4b754ad33aee2f Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Tue, 31 Dec 2013 14:25:26 -0500 Subject: [PATCH 1/3] closes #705 --- public/src/modules/composer.js | 46 ++++++++++++++-------------------- src/install.js | 3 +++ src/posts.js | 19 -------------- src/routes/api.js | 1 + src/upgrade.js | 27 ++++++++++++++++++++ src/websockets.js | 45 +++++++++++++++++++++++++++------ 6 files changed, 88 insertions(+), 53 deletions(-) diff --git a/public/src/modules/composer.js b/public/src/modules/composer.js index c95a8ed508..077a7c3a01 100644 --- a/public/src/modules/composer.js +++ b/public/src/modules/composer.js @@ -340,6 +340,7 @@ define(['taskbar'], function(taskbar) { } } + composer.post = function(post_uuid) { var postData = composer.posts[post_uuid], postContainer = $('#cmp-uuid-' + post_uuid), @@ -350,33 +351,13 @@ define(['taskbar'], function(taskbar) { bodyEl.val(bodyEl.val().trim()); if(postData.uploadsInProgress && postData.uploadsInProgress.length) { - return app.alert({ - type: 'warning', - timeout: 2000, - title: 'Still uploading', - message: "Please wait for uploads to complete.", - alert_id: 'post_error' - }); - } - - if (titleEl.val().length < config.minimumTitleLength) { - return app.alert({ - type: 'danger', - timeout: 2000, - title: 'Title too short', - message: "Please enter a longer title. At least " + config.minimumTitleLength+ " characters.", - alert_id: 'post_error' - }); - } - - if (bodyEl.val().length < config.minimumPostLength) { - return app.alert({ - type: 'danger', - timeout: 2000, - title: 'Content too short', - message: "Please enter a longer post. At least " + config.minimumPostLength + " characters.", - alert_id: 'post_error' - }); + return composerAlert('Still uploading', 'Please wait for uploads to complete.'); + } else if (titleEl.val().length < parseInt(config.minimumTitleLength, 10)) { + return composerAlert('Title too short', 'Please enter a longer title. At least ' + config.minimumTitleLength+ ' characters.'); + } else if (titleEl.val().length > parseInt(config.maximumTitleLength, 10)) { + return composerAlert('Title too long', 'Please enter a shorter title. Titles can\'t be longer than ' + config.maximumTitleLength + ' characters.'); + } else if (bodyEl.val().length < parseInt(config.minimumPostLength, 10)) { + return composerAlert('Content too short', 'Please enter a longer post. At least ' + config.minimumPostLength + ' characters.'); } // Still here? Let's post. @@ -402,6 +383,17 @@ define(['taskbar'], function(taskbar) { composer.discard(post_uuid); } + function composerAlert(title, message) { + app.alert({ + type: 'danger', + timeout: 2000, + title: title, + message: message, + alert_id: 'post_error' + }); + } + + composer.discard = function(post_uuid) { if (composer.posts[post_uuid]) { $('#cmp-uuid-' + post_uuid).remove(); diff --git a/src/install.js b/src/install.js index b5c4d4b089..dbbb748f01 100644 --- a/src/install.js +++ b/src/install.js @@ -215,6 +215,9 @@ var async = require('async'), }, { field: 'minimumTitleLength', value: 3 + }, { + field: 'maximumTitleLength', + value: 255 }, { field: 'minimumUsernameLength', value: 2 diff --git a/src/posts.js b/src/posts.js index 469d256927..200af7c452 100644 --- a/src/posts.js +++ b/src/posts.js @@ -356,25 +356,6 @@ var db = require('./database'), }); } - Posts.emitContentTooShortAlert = function(socket) { - socket.emit('event:alert', { - type: 'danger', - timeout: 2000, - title: 'Content too short', - message: "Please enter a longer post. At least " + meta.config.minimumPostLength + " characters.", - alert_id: 'post_error' - }); - } - - Posts.emitTooManyPostsAlert = function(socket) { - socket.emit('event:alert', { - title: 'Too many posts!', - message: 'You can only post every ' + meta.config.postDelay + ' seconds.', - type: 'danger', - timeout: 2000 - }); - } - Posts.uploadPostImage = function(image, callback) { if(!image) { diff --git a/src/routes/api.js b/src/routes/api.js index 9051916188..623d654fbd 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -30,6 +30,7 @@ var path = require('path'), config.postDelay = meta.config.postDelay; config.minimumTitleLength = meta.config.minimumTitleLength; + config.maximumTitleLength = meta.config.maximumTitleLength; config.minimumPostLength = meta.config.minimumPostLength; config.imgurClientIDSet = !! meta.config.imgurClientID; config.minimumUsernameLength = meta.config.minimumUsernameLength; diff --git a/src/upgrade.js b/src/upgrade.js index a506cff10b..f526491398 100644 --- a/src/upgrade.js +++ b/src/upgrade.js @@ -81,6 +81,33 @@ Upgrade.upgrade = function(callback) { winston.info('[2013/12/31] Re-slugify Topics and Users skipped'); next(); } + }, + function(next) { + thisSchemaDate = new Date(2014, 0, 1).getTime(); + if (schemaDate < thisSchemaDate) { + updatesMade = true; + + db.isObjectField('config', 'maximumTitleLength', function(err, isField) { + if(err) { + return next(err); + } + if(!isField) { + db.setObjectField('config', 'maximumTitleLength', 255, function(err) { + if(err) { + return next(err); + } + winston.info('[2013/12/31] Added maximumTitleLength'); + next(); + }); + } else { + winston.info('[2013/12/31] maximumTitleLength already set'); + next(); + } + }); + } else { + winston.info('[2013/12/31] maximumTitleLength skipped'); + next(); + } } // Add new schema updates here // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema IN LINE 17!!! diff --git a/src/websockets.js b/src/websockets.js index 310af2fbc9..1a16b76af7 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -362,6 +362,35 @@ websockets.init = function(io) { } }); + function emitAlert(socket, title, message) { + socket.emit('event:alert', { + type: 'danger', + timeout: 2000, + title: title, + message: message, + alert_id: 'post_error' + }); + } + + function emitContentTooShortAlert(socket) { + socket.emit('event:alert', { + type: 'danger', + timeout: 2000, + title: 'Content too short', + message: "Please enter a longer post. At least " + meta.config.minimumPostLength + " characters.", + alert_id: 'post_error' + }); + } + + function emitTooManyPostsAlert(socket) { + socket.emit('event:alert', { + title: 'Too many posts!', + message: 'You can only post every ' + meta.config.postDelay + ' seconds.', + type: 'danger', + timeout: 2000 + }); + } + socket.on('api:topics.post', function(data) { if (uid < 1 && parseInt(meta.config.allowGuestPosting, 10) === 0) { socket.emit('event:alert', { @@ -376,11 +405,13 @@ websockets.init = function(io) { topics.post(uid, data.title, data.content, data.category_id, function(err, result) { if(err) { if (err.message === 'title-too-short') { - topics.emitTitleTooShortAlert(socket); + emitAlert(socket, 'Title too short', 'Please enter a longer title. At least ' + meta.config.minimumTitleLength + ' characters.'); + } else if (err.message === 'title-too-long') { + emitAlert(socket, 'Title too long', 'Please enter a shorter title. Titles can\'t be longer than ' + meta.config.maximumTitleLength + ' characters.'); } else if (err.message === 'content-too-short') { - posts.emitContentTooShortAlert(socket); + emitContentTooShortAlert(socket); } else if (err.message === 'too-many-posts') { - posts.emitTooManyPostsAlert(socket); + emitTooManyPostsAlert(socket); } else if (err.message === 'no-privileges') { socket.emit('event:alert', { title: 'Unable to post', @@ -445,16 +476,16 @@ websockets.init = function(io) { } if (Date.now() - lastPostTime < meta.config.postDelay * 1000) { - posts.emitTooManyPostsAlert(socket); + emitTooManyPostsAlert(socket); return; } topics.reply(data.topic_id, uid, data.content, function(err, postData) { if(err) { if (err.message === 'content-too-short') { - posts.emitContentTooShortAlert(socket); + emitContentTooShortAlert(socket); } else if (err.message === 'too-many-posts') { - posts.emitTooManyPostsAlert(socket); + emitTooManyPostsAlert(socket); } else if (err.message === 'reply-error') { socket.emit('event:alert', { title: 'Reply Unsuccessful', @@ -628,7 +659,7 @@ websockets.init = function(io) { topics.emitTitleTooShortAlert(socket); return; } else if (!data.content || data.content.length < parseInt(meta.config.minimumPostLength, 10)) { - posts.emitContentTooShortAlert(socket); + emitContentTooShortAlert(socket); return; } From f933fc0167ff69d09a917e990cb5559739fabf74 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Tue, 31 Dec 2013 14:27:56 -0500 Subject: [PATCH 2/3] forgot topics.js oops --- src/topics.js | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/topics.js b/src/topics.js index a96effea17..6b77b31d14 100644 --- a/src/topics.js +++ b/src/topics.js @@ -33,8 +33,10 @@ var async = require('async'), return callback(new Error('no-privileges')); } else if (!cid) { return callback(new Error('invalid-cid')); - } else if (!title || title.length < meta.config.minimumTitleLength) { + } else if (!title || title.length < parseInt(meta.config.minimumTitleLength, 10) { return callback(new Error('title-too-short'), null); + } else if(title.length > parseInt(meta.config.maximumTitleLength, 10)) { + return callback(new Error('title-too-long'), null); } else if (!content || content.length < meta.config.miminumPostLength) { return callback(new Error('content-too-short'), null); } @@ -897,16 +899,6 @@ var async = require('async'), }); } - Topics.emitTitleTooShortAlert = function(socket) { - socket.emit('event:alert', { - type: 'danger', - timeout: 2000, - title: 'Title too short', - message: "Please enter a longer title. At least " + meta.config.minimumTitleLength + " characters.", - alert_id: 'post_error' - }); - } - Topics.getTopicField = function(tid, field, callback) { db.getObjectField('topic:' + tid, field, callback); } From 1b41a8f4673c91e3481c2e2a75cca35accae6faf Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Tue, 31 Dec 2013 14:32:16 -0500 Subject: [PATCH 3/3] fixed typo --- src/topics.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/topics.js b/src/topics.js index 6b77b31d14..66af3845d3 100644 --- a/src/topics.js +++ b/src/topics.js @@ -33,7 +33,7 @@ var async = require('async'), return callback(new Error('no-privileges')); } else if (!cid) { return callback(new Error('invalid-cid')); - } else if (!title || title.length < parseInt(meta.config.minimumTitleLength, 10) { + } else if (!title || title.length < parseInt(meta.config.minimumTitleLength, 10)) { return callback(new Error('title-too-short'), null); } else if(title.length > parseInt(meta.config.maximumTitleLength, 10)) { return callback(new Error('title-too-long'), null);