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");
module.exports = function (Server) {
var db = Server.db;
return {
init: function(user) {
ActionLog.record(user.ip, user.name, "acp-init");
@ -29,64 +30,41 @@ module.exports = function (Server) {
user.socket.on("acp-global-ban", function(data) {
ActionLog.record(user.ip, user.name, "acp-global-ban", data.ip);
Server.db.globalBanIP(data.ip, data.note);
user.socket.emit("acp-global-banlist", Server.db.refreshGlobalBans());
db.setGlobalIPBan(data.ip, data.note, function (err, res) {
db.listGlobalIPBans(function (err, res) {
res = res || [];
user.socket.emit("acp-global-banlist", res);
});
});
});
user.socket.on("acp-global-unban", function(ip) {
ActionLog.record(user.ip, user.name, "acp-global-unban", ip);
Server.db.globalUnbanIP(ip);
user.socket.emit("acp-global-banlist", Server.db.refreshGlobalBans());
db.clearGlobalIPBan(ip, function (err, res) {
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) {
var db = Server.db.getConnection();
if(!db) {
return;
}
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);
db.searchUser(name, function (err, res) {
res = res || [];
user.socket.emit("acp-userdata", res);
});
});
user.socket.on("acp-lookup-channel", function (data) {
var db = Server.db.getConnection();
if(!db) {
return;
}
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);
db.searchChannel(data.field, data.value, function (e, res) {
res = res || [];
user.socket.emit("acp-channeldata", res);
});
});
user.socket.on("acp-reset-password", function(data) {

View file

@ -234,7 +234,7 @@ Database.prototype.isGlobalIPBanned = function (ip, callback) {
callback(null, banned);
};
Database.prototype.getGlobalIPBans = function (callback) {
Database.prototype.listGlobalIPBans = function (callback) {
var self = this;
if(typeof callback !== "function")
callback = blackHole;
@ -292,6 +292,21 @@ Database.prototype.clearGlobalIPBan = function (ip, callback) {
/* END REGION */
/* 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) {
var self = this;
if(typeof callback !== "function")
@ -638,6 +653,20 @@ Database.prototype.clearChannelNameBan = function (channame, name,
/* 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 */
Database.prototype.getUserProfile = function (name, callback) {
@ -953,4 +982,5 @@ Database.prototype.listIPsForName = function (name, callback) {
});
};
/* END REGION */
module.exports = Database;