Files
NodeBB/src/socket.io/admin.js

140 lines
3.2 KiB
JavaScript
Raw Normal View History

2014-01-13 12:05:13 -05:00
"use strict";
2014-01-10 16:00:03 -05:00
var groups = require('../groups'),
meta = require('../meta'),
plugins = require('../plugins'),
widgets = require('../widgets'),
2014-01-10 16:00:03 -05:00
user = require('../user'),
topics = require('../topics'),
2014-01-13 12:05:13 -05:00
categories = require('../categories'),
2014-01-10 16:00:03 -05:00
CategoryTools = require('../categoryTools'),
2014-01-13 12:05:13 -05:00
logger = require('../logger'),
events = require('../events'),
db = require('../database'),
2014-01-10 16:00:03 -05:00
async = require('async'),
2014-01-13 12:01:42 -05:00
winston = require('winston'),
2014-01-16 17:32:33 -05:00
index = require('./index'),
2014-01-10 16:00:03 -05:00
SocketAdmin = {
2014-04-15 02:40:18 -04:00
user: require('./admin/user'),
categories: require('./admin/categories'),
groups: require('./admin/groups'),
themes: {},
plugins: {},
widgets: {},
config: {},
settings: {}
};
2014-01-10 16:00:03 -05:00
2014-01-16 15:00:49 -05:00
SocketAdmin.before = function(socket, next) {
user.isAdministrator(socket.uid, function(err, isAdmin) {
if (!err && isAdmin) {
2014-01-13 12:01:42 -05:00
next();
} else {
2014-01-16 15:00:49 -05:00
winston.warn('[socket.io] Call to admin method blocked (accessed by uid ' + socket.uid + ')');
2014-01-13 12:01:42 -05:00
}
});
};
2014-02-22 03:11:13 -05:00
SocketAdmin.restart = function(socket, data, callback) {
meta.restart();
};
SocketAdmin.getVisitorCount = function(socket, data, callback) {
var terms = {
day: 86400000,
week: 604800000,
month: 2592000000
};
var now = Date.now();
async.parallel({
day: function(next) {
db.sortedSetCount('ip:recent', now - terms.day, now, next);
},
week: function(next) {
db.sortedSetCount('ip:recent', now - terms.week, now, next);
},
month: function(next) {
db.sortedSetCount('ip:recent', now - terms.month, now, next);
},
alltime: function(next) {
db.sortedSetCount('ip:recent', 0, now, next);
}
}, callback);
};
SocketAdmin.fireEvent = function(socket, data, callback) {
index.server.sockets.emit(data.name, data.payload || {});
};
2014-01-16 15:13:22 -05:00
SocketAdmin.themes.getInstalled = function(socket, data, callback) {
2014-01-17 12:42:19 -05:00
meta.themes.get(callback);
2014-01-10 16:00:03 -05:00
};
2014-01-17 12:42:19 -05:00
SocketAdmin.themes.set = function(socket, data, callback) {
if(!data) {
2014-04-09 21:26:37 -04:00
return callback(new Error('[[error:invalid-data]]'));
2014-01-17 12:42:19 -05:00
}
2014-03-31 16:19:57 -04:00
widgets.reset(function(err) {
meta.themes.set(data, function() {
callback();
});
});
};
2014-01-10 16:00:03 -05:00
SocketAdmin.plugins.toggleActive = function(socket, plugin_id, callback) {
plugins.toggleActive(plugin_id, callback);
};
SocketAdmin.plugins.toggleInstall = function(socket, plugin_id, callback) {
plugins.toggleInstall(plugin_id, callback);
2014-01-10 16:00:03 -05:00
};
SocketAdmin.widgets.set = function(socket, data, callback) {
if(!data) {
2014-04-09 21:26:37 -04:00
return callback(new Error('[[error:invalid-data]]'));
}
widgets.setArea(data, callback);
};
2014-01-16 15:13:22 -05:00
SocketAdmin.config.get = function(socket, data, callback) {
2014-01-16 18:06:19 -05:00
meta.configs.list(callback);
2014-01-13 12:05:13 -05:00
};
2014-01-16 15:13:22 -05:00
SocketAdmin.config.set = function(socket, data, callback) {
2014-01-17 12:42:19 -05:00
if(!data) {
2014-04-09 21:26:37 -04:00
return callback(new Error('[[error:invalid-data]]'));
2014-01-17 12:42:19 -05:00
}
2014-01-13 12:05:13 -05:00
meta.configs.set(data.key, data.value, function(err) {
2014-01-16 18:06:19 -05:00
if(err) {
return callback(err);
2014-01-13 12:05:13 -05:00
}
2014-01-16 18:06:19 -05:00
callback(null);
plugins.fireHook('action:config.set', {
key: data.key,
value: data.value
});
2014-01-16 17:32:33 -05:00
logger.monitorConfig({io: index.server}, data);
2014-01-13 12:05:13 -05:00
});
};
2014-01-16 15:13:22 -05:00
SocketAdmin.config.remove = function(socket, key) {
2014-01-13 12:05:13 -05:00
meta.configs.remove(key);
};
SocketAdmin.settings.get = function(socket, data, callback) {
meta.settings.get(data.hash, callback);
};
SocketAdmin.settings.set = function(socket, data, callback) {
meta.settings.set(data.hash, data.values, callback);
};
2014-02-28 12:58:05 -05:00
module.exports = SocketAdmin;