From 072fa38927ef05ebe66aaa09f53a53108e12e1af Mon Sep 17 00:00:00 2001 From: accalia Date: Thu, 3 Mar 2016 08:34:08 -0500 Subject: [PATCH 1/2] Add API endpoint analogs to websocket includes post.getPost, topic.getTopic, category.getCategory, user.getUserBy* --- src/socket.io/categories.js | 14 ++++++++++++ src/socket.io/posts.js | 20 +++++++++++++++++ src/socket.io/topics.js | 20 +++++++++++++++++ src/socket.io/user.js | 43 ++++++++++++++++++++++++++++++++++++- 4 files changed, 96 insertions(+), 1 deletion(-) diff --git a/src/socket.io/categories.js b/src/socket.io/categories.js index 7e8b6000f3..48ac795a20 100644 --- a/src/socket.io/categories.js +++ b/src/socket.io/categories.js @@ -192,4 +192,18 @@ SocketCategories.isModerator = function(socket, cid, callback) { user.isModerator(socket.uid, cid, callback); }; +SocketCategories.getCategory = function(socket, cid, callback){ + async.waterfall([ + function (next) { + privileges.categories.can('read', cid, socket.uid, next); + }, + function (canRead, next) { + if (!canRead) { + return next(new Error('[[error:no-privileges]]')); + } + categories.getCategoryData(cid, next); + } + ], callback); +}; + module.exports = SocketCategories; diff --git a/src/socket.io/posts.js b/src/socket.io/posts.js index 64b5b75076..2a8c6261d8 100644 --- a/src/socket.io/posts.js +++ b/src/socket.io/posts.js @@ -77,6 +77,26 @@ SocketPosts.getRawPost = function(socket, pid, callback) { ], callback); }; +SocketPosts.getPost = function(socket, pid, callback) { + async.waterfall([ + function(next) { + privileges.posts.can('read', pid, socket.uid, next); + }, + function(canRead, next) { + if (!canRead) { + return next(new Error('[[error:no-privileges]]')); + } + posts.getPostData(pid, next); + }, + function(postData, next) { + if (parseInt(postData.deleted, 10) === 1) { + return next(new Error('[[error:no-post]]')); + } + next(null, postData); + } + ], callback); +}; + SocketPosts.loadMoreFavourites = function(socket, data, callback) { loadMorePosts('uid:' + data.uid + ':favourites', socket.uid, data, callback); }; diff --git a/src/socket.io/topics.js b/src/socket.io/topics.js index 940ed0a470..a7a6e1f685 100644 --- a/src/socket.io/topics.js +++ b/src/socket.io/topics.js @@ -126,4 +126,24 @@ SocketTopics.isModerator = function(socket, tid, callback) { }); }; +SocketTopics.getTopic = function (socket, tid, callback) { + async.waterfall([ + function (next) { + privileges.topics.can('read', tid, socket.uid, next); + }, + function (canRead, next) { + if (!canRead) { + return next(new Error('[[error:no-privileges]]')); + } + topics.getTopicData(tid, next); + }, + function (topicData, next) { + if (parseInt(topicData.deleted, 10) === 1) { + return next(new Error('[[error:no-topic]]')); + } + next(null, topicData); + } + ], callback); +}; + module.exports = SocketTopics; diff --git a/src/socket.io/user.js b/src/socket.io/user.js index be884d4c54..da7f331a21 100644 --- a/src/socket.io/user.js +++ b/src/socket.io/user.js @@ -194,7 +194,7 @@ SocketUser.saveSettings = function(socket, data, callback) { return next(null, true); } user.isAdminOrGlobalMod(socket.uid, next); - }, + }, function(allowed, next) { if (!allowed) { return next(new Error('[[error:no-privileges]]')); @@ -333,4 +333,45 @@ SocketUser.invite = function(socket, email, callback) { }; +SocketUser.getUserByUID = getUserByUID; + +function getUserByUID(socket, uid, callback) { + async.parallel({ + userData: async.apply(user.getUserData, uid), + settings: async.apply(user.getSettings, uid) + }, function(err, results) { + if (err || !results.userData) { + return callback(err||new Error('[[error:no-user]]')); + } + + results.userData.email = results.settings.showemail ? results.userData.email : undefined; + results.userData.fullname = results.settings.showfullname ? results.userData.fullname : undefined; + + callback(null,results.userData); + }); +} + +SocketUser.getUserByUsername = function(socket, username, callback) { + async.waterfall([ + function(next) { + user.getUidByUsername(username || 0, next); + }, + function(uid, next) { + getUserByUID(socket, uid, next); + } + ], callback); +}; + +SocketUser.getUserByEmail = function(socket, email, callback) { + + async.waterfall([ + function(next) { + user.getUidByEmail(email || 0, next); + }, + function(uid, next) { + getUserByUID(socket, uid, next); + } + ], callback); +}; + module.exports = SocketUser; From c13aab39e72b869a648bd5d2af6e09a03a36d4a2 Mon Sep 17 00:00:00 2001 From: accalia Date: Thu, 3 Mar 2016 16:42:10 -0500 Subject: [PATCH 2/2] fix formatting error --- src/socket.io/categories.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/socket.io/categories.js b/src/socket.io/categories.js index 48ac795a20..d1269c5034 100644 --- a/src/socket.io/categories.js +++ b/src/socket.io/categories.js @@ -192,7 +192,7 @@ SocketCategories.isModerator = function(socket, cid, callback) { user.isModerator(socket.uid, cid, callback); }; -SocketCategories.getCategory = function(socket, cid, callback){ +SocketCategories.getCategory = function(socket, cid, callback) { async.waterfall([ function (next) { privileges.categories.can('read', cid, socket.uid, next);