diff --git a/public/src/modules/composer.js b/public/src/modules/composer.js index 8b900fd8f0..7b0239aba8 100644 --- a/public/src/modules/composer.js +++ b/public/src/modules/composer.js @@ -401,23 +401,29 @@ define(['taskbar'], function(taskbar) { 'title' : titleEl.val(), 'content' : bodyEl.val(), 'category_id' : postData.cid - }, function() { - composer.discard(post_uuid); + }, function(err) { + if(!err) { + composer.discard(post_uuid); + } }); } else if (parseInt(postData.tid, 10) > 0) { socket.emit('posts.reply', { 'topic_id' : postData.tid, 'content' : bodyEl.val() - }, function() { - composer.discard(post_uuid); + }, function(err) { + if(!err) { + composer.discard(post_uuid); + } }); } else if (parseInt(postData.pid, 10) > 0) { socket.emit('posts.edit', { pid: postData.pid, content: bodyEl.val(), title: titleEl.val() - }, function() { - composer.discard(post_uuid); + }, function(err) { + if(!err) { + composer.discard(post_uuid); + } }); } } diff --git a/src/categories.js b/src/categories.js index 26550a7b96..06394ee47f 100644 --- a/src/categories.js +++ b/src/categories.js @@ -189,6 +189,10 @@ var db = require('./database.js'), db.setAdd('cid:' + cid + ':read_by_uid', uid); }; + Categories.markAsUnreadForAll = function(cid, callback) { + db.delete('cid:' + cid + ':read_by_uid', callback); + }; + Categories.hasReadCategories = function(cids, uid, callback) { var sets = []; diff --git a/src/posts.js b/src/posts.js index 88d9330a56..94ed3724fb 100644 --- a/src/posts.js +++ b/src/posts.js @@ -90,9 +90,7 @@ var db = require('./database'), next(null, postData); }); } - ], function(err, postData) { - callback(err, postData); - }); + ], callback); }; Posts.getPostsByTid = function(tid, start, end, callback) { @@ -222,10 +220,10 @@ var db = require('./database'), post.editorname = editorData.username; post.editorslug = editorData.userslug; - callback(); + callback(null, post); }); } else { - callback(); + callback(null, post); } }); }); diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js index f02fff3117..c721b90f86 100644 --- a/src/socket.io/posts.js +++ b/src/socket.io/posts.js @@ -104,15 +104,15 @@ SocketPosts.edit = function(socket, data, callback) { type: 'warning', timeout: 2000 }); - return; + return callback(new Error('not-logged-in')); } else if(!data || !data.pid || !data.title || !data.content) { return callback(new Error('invalid data')); } else if (!data.title || data.title.length < parseInt(meta.config.minimumTitleLength, 10)) { topics.emitTitleTooShortAlert(socket); - return; + return callback(new Error('title-too-short')); } else if (!data.content || data.content.length < parseInt(meta.config.minimumPostLength, 10)) { module.parent.exports.emitContentTooShortAlert(socket); - return; + return callback(new Error('content-too-short')); } postTools.edit(socket.uid, data.pid, data.title, data.content); diff --git a/src/socket.io/topics.js b/src/socket.io/topics.js index 9b1c8936cd..dc39ca209a 100644 --- a/src/socket.io/topics.js +++ b/src/socket.io/topics.js @@ -16,7 +16,7 @@ SocketTopics.post = function(socket, data, callback) { type: 'danger', timeout: 2000 }); - return; + return callback(new Error('not-logged-in')); } topics.post(socket.uid, data.title, data.content, data.category_id, function(err, result) { @@ -44,7 +44,7 @@ SocketTopics.post = function(socket, data, callback) { timeout: 7500 }); } - return; + return callback(err); } if (result) { diff --git a/src/topics.js b/src/topics.js index ba88cef81b..35e83befeb 100644 --- a/src/topics.js +++ b/src/topics.js @@ -138,88 +138,85 @@ var async = require('async'), }; Topics.reply = function(tid, uid, content, callback) { - threadTools.privileges(tid, uid, function(err, privileges) { - if(err) { - return callback(err); - } + var privileges; - if (content) { - content = content.trim(); - } - - if (!content || content.length < meta.config.minimumPostLength) { - return callback(new Error('content-too-short')); - } else if (!privileges.write) { - return callback(new Error('no-privileges')); - } - - user.getUserField(uid, 'lastposttime', function(err, lastposttime) { - if(err) { - return callback(err); + async.waterfall([ + function(next) { + threadTools.privileges(tid, uid, next); + }, + function(privilegesData, next) { + privileges = privilegesData; + if (!privileges.write) { + return next(new Error('no-privileges')); } - + next(); + }, + function(next) { + user.getUserField(uid, 'lastposttime', next); + }, + function(lastposttime, next) { if(!lastposttime) { lastposttime = 0; } - if (Date.now() - lastposttime < meta.config.postDelay * 1000) { - return callback(new Error('too-many-posts'), null); + if (Date.now() - parseInt(lastposttime, 10) < parseInt(meta.config.postDelay, 10) * 1000) { + return next(new Error('too-many-posts'), null); } - posts.create(uid, tid, content, function(err, postData) { - if(err) { - return callback(err); - } else if(!postData) { - callback(new Error('reply-error'), null); - } - - posts.getCidByPid(postData.pid, function(err, cid) { - if(err) { - return callback(err, null); - } - - db.delete('cid:' + cid + ':read_by_uid', function(err) { - Topics.markAsUnreadForAll(tid, function(err) { - if(err) { - return callback(err, null); - } - - Topics.markAsRead(tid, uid, function(err) { - Topics.pushUnreadCount(null); - }); - }); - }); - }); - - db.getObjectField('tid:lastFeedUpdate', tid, function(err, lastFeedUpdate) { - var now = Date.now(); - if(!lastFeedUpdate || parseInt(lastFeedUpdate, 10) < now - 3600000) { - feed.updateTopic(tid); - db.setObjectField('tid:lastFeedUpdate', tid, now); - } - }); - - feed.updateRecent(); + next(); + }, + function(next) { + if (content) { + content = content.trim(); + } - threadTools.notifyFollowers(tid, uid); + if (!content || content.length < meta.config.minimumPostLength) { + return next(new Error('content-too-short')); + } - user.sendPostNotificationToFollowers(uid, tid, postData.pid); + posts.create(uid, tid, content, next); + }, + function(postData, next) { + db.getObjectField('tid:lastFeedUpdate', tid, function(err, lastFeedUpdate) { + var now = Date.now(); + if(!lastFeedUpdate || parseInt(lastFeedUpdate, 10) < now - 3600000) { + feed.updateTopic(tid); + db.setObjectField('tid:lastFeedUpdate', tid, now); + } + }); - posts.addUserInfoToPost(postData, function(err) { - if(err) { - return callback(err, null); - } + feed.updateRecent(); + threadTools.notifyFollowers(tid, uid); + user.sendPostNotificationToFollowers(uid, tid, postData.pid); - postData.favourited = false; - postData.display_moderator_tools = true; - postData.display_move_tools = privileges.admin || privileges.moderator; - postData.relativeTime = utils.toISOString(postData.timestamp); + Topics.markCategoryUnreadForAll(tid, function(err) { + next(err, postData); + }); + }, + function(postData, next) { + Topics.markAsUnreadForAll(tid, function(err) { + if(err) { + return next(err); + } - callback(null, postData); + Topics.markAsRead(tid, uid, function(err) { + Topics.pushUnreadCount(null); + next(err, postData); }); }); - }); - }); + }, + function(postData, next) { + posts.addUserInfoToPost(postData, next); + }, + function(postData, next) { + postData.favourited = false; + postData.display_moderator_tools = true; + postData.display_move_tools = privileges.admin || privileges.moderator; + postData.relativeTime = utils.toISOString(postData.timestamp); + + next(null, postData); + } + ], callback); } Topics.createTopicFromPosts = function(uid, title, pids, callback) { @@ -970,6 +967,16 @@ var async = require('async'), }); } + Topics.markCategoryUnreadForAll = function(tid, callback) { + Topics.getTopicField(tid, 'cid', function(err, cid) { + if(err) { + return callback(err); + } + + categories.markAsUnreadForAll(cid, callback); + }); + } + Topics.hasReadTopics = function(tids, uid, callback) { var sets = [];