From e420ee5fb65cd10e0d4e86a1da763f861957dc8f Mon Sep 17 00:00:00 2001 From: barisusakli Date: Sat, 2 Aug 2014 19:45:13 -0400 Subject: [PATCH] privilege fix for single category check --- public/src/forum/topic.js | 10 ++++- src/notifications.js | 81 ++++++++++++++++-------------------- src/privileges/categories.js | 12 +++--- src/privileges/helpers.js | 12 ------ src/privileges/topics.js | 8 ++-- src/socket.io/topics.js | 9 +++- src/topics/create.js | 6 --- 7 files changed, 62 insertions(+), 76 deletions(-) diff --git a/public/src/forum/topic.js b/public/src/forum/topic.js index 280a61db44..230cc09f50 100644 --- a/public/src/forum/topic.js +++ b/public/src/forum/topic.js @@ -26,6 +26,7 @@ define('forum/topic', dependencies, function(pagination, infinitescroll, threadT events.removeListeners(); socket.removeListener('event:new_post', onNewPost); + socket.removeListener('event:new_notification', onNewNotification); } }); @@ -65,6 +66,7 @@ define('forum/topic', dependencies, function(pagination, infinitescroll, threadT navigator.init('.posts > .post-row', postCount, Topic.navigatorCallback, Topic.toTop, Topic.toBottom); socket.on('event:new_post', onNewPost); + socket.on('event:new_notification', onNewNotification); $(window).on('scroll', updateTopicTitle); @@ -145,11 +147,17 @@ define('forum/topic', dependencies, function(pagination, infinitescroll, threadT var postcount = $('.user_postcount_' + data.posts[i].uid); postcount.html(parseInt(postcount.html(), 10) + 1); } - socket.emit('topics.markAsRead', tid); createNewPosts(data); } + function onNewNotification(data) { + var tid = ajaxify.variables.get('topic_id'); + if (data && data.tid && parseInt(data.tid, 10) === tid) { + socket.emit('topics.markTopicNotificationsRead', tid); + } + } + function addBlockQuoteHandler() { $('#post-container').on('click', 'blockquote .toggle', function() { var blockQuote = $(this).parent('blockquote'); diff --git a/src/notifications.js b/src/notifications.js index ea96fda52d..655851d480 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -24,53 +24,47 @@ var async = require('async'), }; Notifications.get = function(nid, callback) { - db.exists('notifications:' + nid, function(err, exists) { + db.getObject('notifications:' + nid, function(err, notification) { if (err) { - winston.error('[notifications.get] Could not retrieve nid ' + nid + ': ' + err.message); return callback(err); } - if (!exists) { + if (!notification) { + winston.info('[notifications.get] Could not retrieve nid ' + nid); return callback(null, null); } - db.getObject('notifications:' + nid, function(err, notification) { - if (err) { - return callback(err); - } + // Backwards compatibility for old notification schema + // Remove this block when NodeBB v0.6.0 is released. + if (notification.hasOwnProperty('text')) { + notification.bodyShort = notification.text; + notification.bodyLong = ''; + notification.text = S(notification.text).escapeHTML().s; + } - // Backwards compatibility for old notification schema - // Remove this block when NodeBB v0.6.0 is released. - if (notification.hasOwnProperty('text')) { - notification.bodyShort = notification.text; - notification.bodyLong = ''; - notification.text = S(notification.text).escapeHTML().s; - } + notification.bodyShort = S(notification.bodyShort).escapeHTML().s; + notification.bodyLong = S(notification.bodyLong).escapeHTML().s; - notification.bodyShort = S(notification.bodyShort).escapeHTML().s; - notification.bodyLong = S(notification.bodyLong).escapeHTML().s; - - if (notification.from && !notification.image) { - User.getUserField(notification.from, 'picture', function(err, picture) { - if (err) { - return callback(err); - } - notification.image = picture; - callback(null, notification); - }); - return; - } else if (notification.image) { - switch(notification.image) { - case 'brand:logo': - notification.image = meta.config['brand:logo'] || nconf.get('relative_path') + '/logo.png'; - break; + if (notification.from && !notification.image) { + User.getUserField(notification.from, 'picture', function(err, picture) { + if (err) { + return callback(err); } - - return callback(null, notification); + notification.image = picture; + callback(null, notification); + }); + return; + } else if (notification.image) { + switch(notification.image) { + case 'brand:logo': + notification.image = meta.config['brand:logo'] || nconf.get('relative_path') + '/logo.png'; + break; } - callback(null, notification); - }); + return callback(null, notification); + } + + callback(null, notification); }); }; @@ -207,18 +201,13 @@ var async = require('async'), } Notifications.pushGroup = function(nid, groupName, callback) { - if (!callback) { - callback = function() {}; - } - + callback = callback || function() {}; groups.get(groupName, {}, function(err, groupObj) { - if (!err && groupObj) { - if (groupObj.memberCount > 0) { - Notifications.push(nid, groupObj.members, callback); - } - } else { - callback(err); + if (err || !groupObj || !Array.isArray(groupObj.members) || !groupObj.members.length) { + return callback(err); } + + Notifications.push(nid, groupObj.members, callback); }); }; @@ -229,7 +218,7 @@ var async = require('async'), return callback(); } - Notifications.get(nid, function(err, notificationData) { + db.getObjectFields('notifications:' + nid, ['uniqueId', 'datetime'], function(err, notificationData) { if (err || !notificationData) { return callback(err); } diff --git a/src/privileges/categories.js b/src/privileges/categories.js index b0f89a9fad..d2527b1fa8 100644 --- a/src/privileges/categories.js +++ b/src/privileges/categories.js @@ -16,10 +16,10 @@ module.exports = function(privileges) { privileges.categories.get = function(cid, uid, callback) { async.parallel({ 'topics:create': function(next) { - helpers.allowedTo('topics:create', uid, cid, next); + helpers.allowedTo('topics:create', uid, [cid], next); }, read: function(next) { - helpers.allowedTo('read', uid, cid, next); + helpers.allowedTo('read', uid, [cid], next); }, isAdministrator: function(next) { user.isAdministrator(uid, next); @@ -35,10 +35,10 @@ module.exports = function(privileges) { var editable = results.isAdministrator || results.isModerator; callback(null, { - 'topics:create': results['topics:create'], + 'topics:create': results['topics:create'][0], editable: editable, view_deleted: editable, - read: results.read + read: results.read[0] }); }); }; @@ -55,7 +55,9 @@ module.exports = function(privileges) { helpers.some([ function(next) { - helpers.allowedTo(privilege, uid, cid, next); + helpers.allowedTo(privilege, uid, [cid], function(err, results) { + next(err, Array.isArray(results) && results.length ? results[0] : false); + }); }, function(next) { user.isModerator(uid, cid, next); diff --git a/src/privileges/helpers.js b/src/privileges/helpers.js index c85972b677..ee6583b408 100644 --- a/src/privileges/helpers.js +++ b/src/privileges/helpers.js @@ -21,11 +21,6 @@ helpers.some = function(tasks, callback) { }; helpers.allowedTo = function(privilege, uid, cids, callback) { - - if (!Array.isArray(cids)) { - cids = [cids]; - } - if (parseInt(uid, 10) === 0) { return isGuestAllowedTo(privilege, cids, callback); } @@ -61,9 +56,6 @@ helpers.allowedTo = function(privilege, uid, cids, callback) { result.push((!results.userPrivilegeExists[i] && !results.groupPrivilegeExists[i]) || results.hasUserPrivilege[i] || results.hasGroupPrivilege[i]); } - if (result.length === 1) { - result = result[0]; - } callback(null, result); }); @@ -100,10 +92,6 @@ function isGuestAllowedTo(privilege, cids, callback) { result.push(!results.userPrivilegeExists[i] && groupPriv); } - if (result.length === 1) { - result = result[0]; - } - callback(null, result); }); } diff --git a/src/privileges/topics.js b/src/privileges/topics.js index 6a2d321d97..d96870b3cc 100644 --- a/src/privileges/topics.js +++ b/src/privileges/topics.js @@ -23,10 +23,10 @@ module.exports = function(privileges) { async.parallel({ 'topics:reply': function(next) { - helpers.allowedTo('topics:reply', uid, cid, next); + helpers.allowedTo('topics:reply', uid, [cid], next); }, read: function(next) { - helpers.allowedTo('read', uid, cid, next); + helpers.allowedTo('read', uid, [cid], next); }, isOwner: function(next) { topics.isOwner(tid, uid, next); @@ -53,8 +53,8 @@ module.exports = function(privileges) { var deletable = isAdminOrMod || results.isOwner; callback(null, { - 'topics:reply': results['topics:reply'], - read: results.read, + 'topics:reply': results['topics:reply'][0], + read: results.read[0], view_thread_tools: editable || deletable, editable: editable, deletable: deletable, diff --git a/src/socket.io/topics.js b/src/socket.io/topics.js index 3370db4072..8e71b9b98d 100644 --- a/src/socket.io/topics.js +++ b/src/socket.io/topics.js @@ -73,12 +73,10 @@ SocketTopics.markAsRead = function(socket, tid) { topics.markAsRead(tid, socket.uid, function(err) { topics.pushUnreadCount(socket.uid); - topics.markTopicNotificationsRead(tid, socket.uid); }); }; SocketTopics.markTidsRead = function(socket, tids, callback) { - if (!Array.isArray(tids)) { return callback(new Error('[[error:invalid-data]]')); } @@ -98,6 +96,13 @@ SocketTopics.markTidsRead = function(socket, tids, callback) { }); }; +SocketTopics.markTopicNotificationsRead = function(socket, tid, callback) { + if(!tid || !socket.uid) { + return callback(new Error('[[error:invalid-data]]')); + } + topics.markTopicNotificationsRead(tid, socket.uid); +}; + SocketTopics.markAllRead = function(socket, data, callback) { topics.getUnreadTids(socket.uid, 0, -1, function(err, tids) { if (err) { diff --git a/src/topics/create.js b/src/topics/create.js index 01a334b128..d7a847d94a 100644 --- a/src/topics/create.js +++ b/src/topics/create.js @@ -200,9 +200,6 @@ module.exports = function(Topics) { }, function(data, next) { postData = data; - next(); - }, - function(next) { Topics.markAsUnreadForAll(tid, next); }, function(next) { @@ -218,9 +215,6 @@ module.exports = function(Topics) { function(topicData, next) { topicData.title = validator.escape(topicData.title); postData.topic = topicData; - next(); - }, - function(next) { posts.getPidIndex(postData.pid, next); }, function(index, next) {