Start refactoring acp.js database calls

This commit is contained in:
calzoneman 2013-08-15 13:53:58 -05:00
parent d883445ed4
commit 6d84222871
2 changed files with 56 additions and 48 deletions

72
acp.js
View File

@ -13,6 +13,7 @@ var Auth = require("./auth");
var ActionLog = require("./actionlog"); var ActionLog = require("./actionlog");
module.exports = function (Server) { module.exports = function (Server) {
var db = Server.db;
return { return {
init: function(user) { init: function(user) {
ActionLog.record(user.ip, user.name, "acp-init"); ActionLog.record(user.ip, user.name, "acp-init");
@ -29,64 +30,41 @@ module.exports = function (Server) {
user.socket.on("acp-global-ban", function(data) { user.socket.on("acp-global-ban", function(data) {
ActionLog.record(user.ip, user.name, "acp-global-ban", data.ip); ActionLog.record(user.ip, user.name, "acp-global-ban", data.ip);
Server.db.globalBanIP(data.ip, data.note); db.setGlobalIPBan(data.ip, data.note, function (err, res) {
user.socket.emit("acp-global-banlist", Server.db.refreshGlobalBans()); db.listGlobalIPBans(function (err, res) {
res = res || [];
user.socket.emit("acp-global-banlist", res);
});
});
}); });
user.socket.on("acp-global-unban", function(ip) { user.socket.on("acp-global-unban", function(ip) {
ActionLog.record(user.ip, user.name, "acp-global-unban", ip); ActionLog.record(user.ip, user.name, "acp-global-unban", ip);
Server.db.globalUnbanIP(ip); db.clearGlobalIPBan(ip, function (err, res) {
user.socket.emit("acp-global-banlist", Server.db.refreshGlobalBans()); db.listGlobalIPBans(function (err, res) {
res = res || [];
user.socket.emit("acp-global-banlist", res);
});
});
}); });
user.socket.emit("acp-global-banlist", Server.db.refreshGlobalBans()); db.listGlobalIPBans(function (err, res) {
res = res || [];
user.socket.emit("acp-global-banlist", res);
});
user.socket.on("acp-lookup-user", function(name) { user.socket.on("acp-lookup-user", function(name) {
var db = Server.db.getConnection(); db.searchUser(name, function (err, res) {
if(!db) { res = res || [];
return; user.socket.emit("acp-userdata", res);
} });
var query = Server.db.createQuery(
"SELECT id,uname,global_rank,profile_image,profile_text,email FROM registrations WHERE uname LIKE ?",
["%"+name+"%"]
);
var res = db.querySync(query);
if(!res)
return;
var rows = res.fetchAllSync();
user.socket.emit("acp-userdata", rows);
}); });
user.socket.on("acp-lookup-channel", function (data) { user.socket.on("acp-lookup-channel", function (data) {
var db = Server.db.getConnection(); db.searchChannel(data.field, data.value, function (e, res) {
if(!db) { res = res || [];
return; user.socket.emit("acp-channeldata", res);
} });
var query;
if(data.field === "owner") {
query = Server.db.createQuery(
"SELECT * FROM channels WHERE owner LIKE ?",
["%" + data.value + "%"]
);
} else if (data.field === "name") {
query = Server.db.createQuery(
"SELECT * FROM channels WHERE name LIKE ?",
["%" + data.value + "%"]
);
} else {
return;
}
var results = db.querySync(query);
if(!results)
return;
var rows = results.fetchAllSync();
user.socket.emit("acp-channeldata", rows);
}); });
user.socket.on("acp-reset-password", function(data) { user.socket.on("acp-reset-password", function(data) {

View File

@ -234,7 +234,7 @@ Database.prototype.isGlobalIPBanned = function (ip, callback) {
callback(null, banned); callback(null, banned);
}; };
Database.prototype.getGlobalIPBans = function (callback) { Database.prototype.listGlobalIPBans = function (callback) {
var self = this; var self = this;
if(typeof callback !== "function") if(typeof callback !== "function")
callback = blackHole; callback = blackHole;
@ -292,6 +292,21 @@ Database.prototype.clearGlobalIPBan = function (ip, callback) {
/* END REGION */ /* END REGION */
/* REGION channels */ /* REGION channels */
Database.prototype.searchChannel = function (field, value, callback) {
var self = this;
if(typeof callback !== "function")
return;
var query = "SELECT * FROM channels WHERE ";
if(field === "owner")
query += "owner LIKE %?%";
else if(field === "name")
query += "name LIKE %?%";
self.query(query, [value], callback);
};
Database.prototype.channelExists = function (name, callback) { Database.prototype.channelExists = function (name, callback) {
var self = this; var self = this;
if(typeof callback !== "function") if(typeof callback !== "function")
@ -638,6 +653,20 @@ Database.prototype.clearChannelNameBan = function (channame, name,
/* REGION users */ /* REGION users */
Database.prototype.searchUser = function (name, callback) {
var self = this;
if(typeof callback !== "function")
return;
// NOTE: No SELECT * here because I don't want to risk exposing
// the user's password hash
var query = "SELECT id, uname, global_rank, profile_image, " +
"profile_text, email FROM registrations WHERE " +
"uname LIKE %?%";
self.query(query, [name], callback);
};
/* email and profile */ /* email and profile */
Database.prototype.getUserProfile = function (name, callback) { Database.prototype.getUserProfile = function (name, callback) {
@ -953,4 +982,5 @@ Database.prototype.listIPsForName = function (name, callback) {
}); });
}; };
/* END REGION */
module.exports = Database; module.exports = Database;