added socket methods

v1.18.x
barisusakli 9 years ago
parent 54d47e1e24
commit 8d9f7d8e19

@ -1,32 +1,21 @@
"use strict"; "use strict";
var async = require('async'), var async = require('async');
validator = require('validator'), var validator = require('validator');
nconf = require('nconf'), var nconf = require('nconf');
meta = require('../meta'), var meta = require('../meta');
user = require('../user'), var user = require('../user');
posts = require('../posts'), var posts = require('../posts');
topics = require('../topics'), var topics = require('../topics');
categories = require('../categories'), var categories = require('../categories');
privileges = require('../privileges'), var privileges = require('../privileges');
plugins = require('../plugins'), var plugins = require('../plugins');
helpers = require('./helpers'), var widgets = require('../widgets');
widgets = require('../widgets');
var apiController = {}; var apiController = {};
apiController.getConfig = function(req, res, next) { apiController.getConfig = function(req, res, next) {
function filterConfig() {
plugins.fireHook('filter:config.get', config, function(err, config) {
if (res.locals.isAPI) {
res.status(200).json(config);
} else {
next(err, config);
}
});
}
var config = {}; var config = {};
config.environment = process.env.NODE_ENV; config.environment = process.env.NODE_ENV;
config.relative_path = nconf.get('relative_path'); config.relative_path = nconf.get('relative_path');
@ -51,7 +40,6 @@ apiController.getConfig = function(req, res, next) {
config.allowFileUploads = parseInt(meta.config.allowFileUploads, 10) === 1; config.allowFileUploads = parseInt(meta.config.allowFileUploads, 10) === 1;
config.allowTopicsThumbnail = parseInt(meta.config.allowTopicsThumbnail, 10) === 1; config.allowTopicsThumbnail = parseInt(meta.config.allowTopicsThumbnail, 10) === 1;
config.usePagination = parseInt(meta.config.usePagination, 10) === 1; config.usePagination = parseInt(meta.config.usePagination, 10) === 1;
config.disableSocialButtons = parseInt(meta.config.disableSocialButtons, 10) === 1;
config.disableChat = parseInt(meta.config.disableChat, 10) === 1; config.disableChat = parseInt(meta.config.disableChat, 10) === 1;
config.socketioTransports = nconf.get('socket.io:transports') || ['polling', 'websocket']; config.socketioTransports = nconf.get('socket.io:transports') || ['polling', 'websocket'];
config.websocketAddress = nconf.get('socket.io:address') || ''; config.websocketAddress = nconf.get('socket.io:address') || '';
@ -73,27 +61,41 @@ apiController.getConfig = function(req, res, next) {
config.searchEnabled = plugins.hasListeners('filter:search.query'); config.searchEnabled = plugins.hasListeners('filter:search.query');
config.bootswatchSkin = 'default'; config.bootswatchSkin = 'default';
if (!req.user) { async.waterfall([
return filterConfig(); function (next) {
} if (!req.user) {
return next(null, config);
user.getSettings(req.user.uid, function(err, settings) { }
user.getSettings(req.uid, function(err, settings) {
if (err) {
return next(err);
}
config.usePagination = settings.usePagination;
config.topicsPerPage = settings.topicsPerPage;
config.postsPerPage = settings.postsPerPage;
config.notificationSounds = settings.notificationSounds;
config.userLang = req.query.lang || settings.userLang || config.defaultLang;
config.openOutgoingLinksInNewTab = settings.openOutgoingLinksInNewTab;
config.topicPostSort = settings.topicPostSort || config.topicPostSort;
config.categoryTopicSort = settings.categoryTopicSort || config.categoryTopicSort;
config.topicSearchEnabled = settings.topicSearchEnabled || false;
config.bootswatchSkin = settings.bootswatchSkin || config.bootswatchSkin;
next(null, config);
});
},
function (config, next) {
plugins.fireHook('filter:config.get', config, next);
}
], function(err, config) {
if (err) { if (err) {
return next(err); return next(err);
} }
config.usePagination = settings.usePagination; if (res.locals.isAPI) {
config.topicsPerPage = settings.topicsPerPage; res.json(config);
config.postsPerPage = settings.postsPerPage; } else {
config.notificationSounds = settings.notificationSounds; next(null, config);
config.userLang = req.query.lang || settings.userLang || config.defaultLang; }
config.openOutgoingLinksInNewTab = settings.openOutgoingLinksInNewTab;
config.topicPostSort = settings.topicPostSort || config.topicPostSort;
config.categoryTopicSort = settings.categoryTopicSort || config.categoryTopicSort;
config.topicSearchEnabled = settings.topicSearchEnabled || false;
config.bootswatchSkin = settings.bootswatchSkin || config.bootswatchSkin;
filterConfig();
}); });
}; };
@ -126,6 +128,16 @@ apiController.renderWidgets = function(req, res, next) {
}; };
apiController.getObject = function(req, res, next) { apiController.getObject = function(req, res, next) {
apiController.getObjectByType(req.uid, req.params.type, req.params.id, function(err, results) {
if (err) {
return next(err);
}
res.json(results);
});
};
apiController.getObjectByType = function(uid, type, id, callback) {
var methods = { var methods = {
post: { post: {
canRead: privileges.posts.can, canRead: privileges.posts.can,
@ -141,74 +153,101 @@ apiController.getObject = function(req, res, next) {
} }
}; };
if (!methods[req.params.type]) { if (!methods[type]) {
return next(); return callback();
} }
async.parallel({ async.waterfall([
canRead: async.apply(methods[req.params.type].canRead, 'read', req.params.id, req.uid), function (next) {
data: async.apply(methods[req.params.type].data, req.params.id) methods[type].canRead('read', id, uid, next);
}, function(err, results) { },
if (err || !results.data) { function (canRead, next) {
return next(err); if (!canRead) {
} return next(new Error('[[error:no-privileges]]'));
}
if (!results.canRead) { methods[type].data(id, next);
return helpers.notAllowed(req, res);
} }
], callback);
res.json(results.data);
});
}; };
apiController.getUserByUID = function(req, res, next) { apiController.getUserByUID = function(req, res, next) {
var uid = req.params.uid ? req.params.uid : 0; var uid = req.params.uid ? req.params.uid : 0;
getUserByUID(uid, res, next); apiController.getUserDataByUID(req.uid, uid, function(err, data) {
if (err) {
return next(err);
}
res.json(data);
});
}; };
apiController.getUserByUsername = function(req, res, next) { apiController.getUserByUsername = function(req, res, next) {
var username = req.params.username ? req.params.username : 0; var username = req.params.username ? req.params.username : 0;
apiController.getUserDataByUsername(req.uid, username, function(err, data) {
if (err) {
return next(err);
}
res.json(data);
});
};
apiController.getUserByEmail = function(req, res, next) {
var email = req.params.email ? req.params.email : 0;
apiController.getUserDataByEmail(req.uid, email, function(err, data) {
if (err) {
return next(err);
}
res.json(data);
});
};
apiController.getUserDataByUsername = function(callerUid, username, callback) {
async.waterfall([ async.waterfall([
function(next) { function(next) {
user.getUidByUsername(username, next); user.getUidByUsername(username, next);
}, },
function(uid, next) { function(uid, next) {
getUserByUID(uid, res, next); apiController.getUserDataByUID(callerUid, uid, next);
} }
], next); ], callback);
}; };
apiController.getUserByEmail = function(req, res, next) { apiController.getUserDataByEmail = function(callerUid, email, callback) {
var email = req.params.email ? req.params.email : 0;
async.waterfall([ async.waterfall([
function(next) { function(next) {
user.getUidByEmail(email, next); user.getUidByEmail(email, next);
}, },
function(uid, next) { function(uid, next) {
getUserByUID(uid, res, next); apiController.getUserDataByUID(callerUid, uid, next);
} }
], next); ], callback);
}; };
function getUserByUID(uid, res, next) { apiController.getUserDataByUID = function(callerUid, uid, callback) {
if (!parseInt(callerUid, 10) && parseInt(meta.config.privateUserInfo, 10) === 1) {
return callback(new Error('[[error:no-privileges]]'));
}
if (!parseInt(uid, 10)) {
return callback(new Error('[[error:no-user]]'));
}
async.parallel({ async.parallel({
userData: async.apply(user.getUserData, uid), userData: async.apply(user.getUserData, uid),
settings: async.apply(user.getSettings, uid) settings: async.apply(user.getSettings, uid)
}, function(err, results) { }, function(err, results) {
if (err || !results.userData) { if (err || !results.userData) {
return next(err); return callback(err || new Error('[[error:no-user]]'));
} }
results.userData.email = results.settings.showemail ? results.userData.email : undefined; results.userData.email = results.settings.showemail ? results.userData.email : undefined;
results.userData.fullname = results.settings.showfullname ? results.userData.fullname : undefined; results.userData.fullname = results.settings.showfullname ? results.userData.fullname : undefined;
res.json(results.userData); callback(null, results.userData);
}); });
} };
apiController.getModerators = function(req, res, next) { apiController.getModerators = function(req, res, next) {
categories.getModerators(req.params.cid, function(err, moderators) { categories.getModerators(req.params.cid, function(err, moderators) {

@ -6,7 +6,7 @@ var categories = require('../categories');
var privileges = require('../privileges'); var privileges = require('../privileges');
var user = require('../user'); var user = require('../user');
var topics = require('../topics'); var topics = require('../topics');
var apiController = require('../controllers/api');
var SocketCategories = {}; var SocketCategories = {};
@ -192,4 +192,8 @@ SocketCategories.isModerator = function(socket, cid, callback) {
user.isModerator(socket.uid, cid, callback); user.isModerator(socket.uid, cid, callback);
}; };
SocketCategories.getCategory = function(socket, cid, callback) {
apiController.getObjectByType(socket.uid, 'category', cid, callback);
};
module.exports = SocketCategories; module.exports = SocketCategories;

@ -1,18 +1,20 @@
"use strict"; "use strict";
var async = require('async'), var async = require('async');
posts = require('../posts'), var posts = require('../posts');
privileges = require('../privileges'), var privileges = require('../privileges');
meta = require('../meta'), var meta = require('../meta');
topics = require('../topics'), var topics = require('../topics');
user = require('../user'), var user = require('../user');
websockets = require('./index'), var websockets = require('./index');
socketTopics = require('./topics'), var socketTopics = require('./topics');
socketHelpers = require('./helpers'), var socketHelpers = require('./helpers');
utils = require('../../public/src/utils'), var utils = require('../../public/src/utils');
SocketPosts = {}; var apiController = require('../controllers/api');
var SocketPosts = {};
require('./posts/edit')(SocketPosts); require('./posts/edit')(SocketPosts);
@ -77,6 +79,20 @@ SocketPosts.getRawPost = function(socket, pid, callback) {
], callback); ], callback);
}; };
SocketPosts.getPost = function(socket, pid, callback) {
async.waterfall([
function(next) {
apiController.getObjectByType(socket.uid, 'post', 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) { SocketPosts.loadMoreFavourites = function(socket, data, callback) {
loadMorePosts('uid:' + data.uid + ':favourites', socket.uid, data, callback); loadMorePosts('uid:' + data.uid + ':favourites', socket.uid, data, callback);
}; };
@ -119,4 +135,6 @@ SocketPosts.getPidIndex = function(socket, data, callback) {
posts.getPidIndex(data.pid, data.tid, data.topicPostSort, callback); posts.getPidIndex(data.pid, data.tid, data.topicPostSort, callback);
}; };
module.exports = SocketPosts; module.exports = SocketPosts;

@ -1,18 +1,17 @@
'use strict'; 'use strict';
var nconf = require('nconf'), var async = require('async');
async = require('async'), var winston = require('winston');
winston = require('winston'),
topics = require('../topics'), var topics = require('../topics');
privileges = require('../privileges'), var privileges = require('../privileges');
plugins = require('../plugins'), var plugins = require('../plugins');
notifications = require('../notifications'), var websockets = require('./index');
websockets = require('./index'), var user = require('../user');
user = require('../user'), var apiController = require('../controllers/api');
SocketTopics = {}; var SocketTopics = {};
require('./topics/unread')(SocketTopics); require('./topics/unread')(SocketTopics);
require('./topics/move')(SocketTopics); require('./topics/move')(SocketTopics);
@ -126,4 +125,18 @@ SocketTopics.isModerator = function(socket, tid, callback) {
}); });
}; };
SocketTopics.getTopic = function (socket, tid, callback) {
async.waterfall([
function (next) {
apiController.getObjectByType(socket.uid, 'topic', 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; module.exports = SocketTopics;

@ -12,6 +12,7 @@ var meta = require('../meta');
var events = require('../events'); var events = require('../events');
var emailer = require('../emailer'); var emailer = require('../emailer');
var db = require('../database'); var db = require('../database');
var apiController = require('../controllers/api');
var SocketUser = {}; var SocketUser = {};
@ -332,5 +333,17 @@ SocketUser.invite = function(socket, email, callback) {
}; };
SocketUser.getUserByUID = function(socket, uid, callback) {
apiController.getUserDataByUID(socket.uid, uid, callback);
};
SocketUser.getUserByUsername = function(socket, username, callback) {
apiController.getUserDataByUsername(socket.uid, username, callback);
};
SocketUser.getUserByEmail = function(socket, email, callback) {
apiController.getUserDataByEmail(socket.uid, email, callback);
};
module.exports = SocketUser; module.exports = SocketUser;

Loading…
Cancel
Save