Finish refactoring existing functions from database.js

This commit is contained in:
calzoneman 2013-08-15 13:39:32 -05:00
parent 1b9c707bdf
commit d883445ed4

View file

@ -66,7 +66,7 @@ Database.prototype.query = function (query, sub, callback) {
} }
function blackHole() { function blackHole() {
} }
Database.prototype.init = function () { Database.prototype.init = function () {
@ -230,7 +230,7 @@ Database.prototype.isGlobalIPBanned = function (ip, callback) {
var banned = ip in this.global_ipbans || var banned = ip in this.global_ipbans ||
s16 in this.global_ipbans || s16 in this.global_ipbans ||
s24 in this.global_ipbans; s24 in this.global_ipbans;
callback(null, banned); callback(null, banned);
}; };
@ -259,7 +259,7 @@ Database.prototype.setGlobalIPBan = function (ip, reason, callback) {
if(typeof callback !== "function") if(typeof callback !== "function")
callback = blackHole; callback = blackHole;
var query = "INSERT INTO global_bans VALUES (?, ?)" + var query = "INSERT INTO global_bans VALUES (?, ?)" +
" ON DUPLICATE KEY UPDATE note=?"; " ON DUPLICATE KEY UPDATE note=?";
self.query(query, [ip, reason, reason], function (err, res) { self.query(query, [ip, reason, reason], function (err, res) {
if(err) { if(err) {
@ -338,7 +338,7 @@ Database.prototype.registerChannel = function (name, owner, callback) {
callback(err, null); callback(err, null);
return; return;
} }
// Rank table // Rank table
query = ["CREATE TABLE `chan_" + name + "_ranks` (", query = ["CREATE TABLE `chan_" + name + "_ranks` (",
"`name` VARCHAR(32) NOT NULL,", "`name` VARCHAR(32) NOT NULL,",
@ -365,7 +365,7 @@ Database.prototype.registerChannel = function (name, owner, callback) {
callback(err, null); callback(err, null);
return; return;
} }
query = "INSERT INTO channels VALUES (NULL, ?, ?)"; query = "INSERT INTO channels VALUES (NULL, ?, ?)";
self.query(query, [name, owner], function (err, res) { self.query(query, [name, owner], function (err, res) {
callback(err, res); callback(err, res);
@ -393,7 +393,7 @@ Database.prototype.loadChannelData = function (chan, callback) {
callback(err, null); callback(err, null);
return; return;
} }
if(res.length == 0) { if(res.length == 0) {
callback("Channel is unregistered", null); callback("Channel is unregistered", null);
return; return;
@ -402,7 +402,7 @@ Database.prototype.loadChannelData = function (chan, callback) {
if(res[0].name != chan.name) if(res[0].name != chan.name)
chan.name = rows[0].name; chan.name = rows[0].name;
chan.registered = true; chan.registered = true;
// Load bans // Load bans
query = "SELECT * FROM `chan_" + chan.name + "_bans`"; query = "SELECT * FROM `chan_" + chan.name + "_bans`";
self.query(query, function (err, res) { self.query(query, function (err, res) {
@ -494,7 +494,7 @@ Database.prototype.getChannelRank = function (channame, names, callback) {
callback(null, res[0].rank); callback(null, res[0].rank);
return; return;
} }
callback(null, res); callback(null, res);
}); });
}; };
@ -512,7 +512,7 @@ Database.prototype.setChannelRank = function (channame, name, rank, callback) {
var query = "INSERT INTO `chan_" + channame + "_ranks` " + var query = "INSERT INTO `chan_" + channame + "_ranks` " +
"(name, rank) VALUES (?, ?) " + "(name, rank) VALUES (?, ?) " +
"ON DUPLICATE KEY UPDATE rank=?"; "ON DUPLICATE KEY UPDATE rank=?";
self.query(query, [name, rank, rank], callback); self.query(query, [name, rank, rank], callback);
}; };
@ -541,7 +541,7 @@ Database.prototype.addToLibrary = function (channame, media, callback) {
} }
var query = "INSERT INTO `chan_" + channame + "_ranks`" + var query = "INSERT INTO `chan_" + channame + "_ranks`" +
"(id, title, seconds, type) " + "(id, title, seconds, type) " +
"VALUES (?, ?, ?, ?)"; "VALUES (?, ?, ?, ?)";
var params = [ var params = [
media.id, media.id,
@ -600,7 +600,7 @@ Database.prototype.addChannelBan = function (channame, ip, name, banBy,
return; return;
} }
var query = "INSERT INTO `chan_" + channame + "_bans`" + var query = "INSERT INTO `chan_" + channame + "_bans`" +
"(ip, name, banner) VALUES (?, ?, ?)"; "(ip, name, banner) VALUES (?, ?, ?)";
self.query(query, [ip, name, banBy], callback); self.query(query, [ip, name, banBy], callback);
@ -615,7 +615,7 @@ Database.prototype.clearChannelIPBan = function (channame, ip, callback) {
callback("Invalid channel name", null); callback("Invalid channel name", null);
return; return;
} }
var query = "DELETE FROM `chan_" + channame + "_bans` WHERE ip=?"; var query = "DELETE FROM `chan_" + channame + "_bans` WHERE ip=?";
self.query(query, [ip], callback); self.query(query, [ip], callback);
}; };
@ -628,7 +628,7 @@ Database.prototype.clearChannelNameBan = function (channame, name,
return; return;
} }
var query = "DELETE FROM `chan_" + channame + "_bans` WHERE ip='*'" + var query = "DELETE FROM `chan_" + channame + "_bans` WHERE ip='*'" +
"AND name=?"; "AND name=?";
self.query(query, [name], callback); self.query(query, [name], callback);
@ -638,6 +638,8 @@ Database.prototype.clearChannelNameBan = function (channame, name,
/* REGION users */ /* REGION users */
/* email and profile */
Database.prototype.getUserProfile = function (name, callback) { Database.prototype.getUserProfile = function (name, callback) {
var self = this; var self = this;
if(typeof callback !== "function") if(typeof callback !== "function")
@ -682,6 +684,8 @@ Database.prototype.setUserEmail = function (name, email, callback) {
self.query(query, [email, name], callback); self.query(query, [email, name], callback);
}; };
/* password recovery */
Database.prototype.genPasswordReset = function (ip, name, email, callback) { Database.prototype.genPasswordReset = function (ip, name, email, callback) {
var self = this; var self = this;
if(typeof callback !== "function") if(typeof callback !== "function")
@ -693,7 +697,7 @@ Database.prototype.genPasswordReset = function (ip, name, email, callback) {
callback(err, null); callback(err, null);
return; return;
} }
if(res.length == 0) { if(res.length == 0) {
callback("Provided username does not exist", null); callback("Provided username does not exist", null);
return; return;
@ -766,7 +770,7 @@ Database.prototype.resetUserPassword = function (name, callback) {
var self = this; var self = this;
if(typeof callback !== "function") if(typeof callback !== "function")
callback = blackHole; callback = blackHole;
var pwChars = "abcdefghijkmnopqrstuvwxyz023456789"; var pwChars = "abcdefghijkmnopqrstuvwxyz023456789";
var pw = ""; var pw = "";
for(var i = 0; i < 10; i++) for(var i = 0; i < 10; i++)
@ -778,7 +782,7 @@ Database.prototype.resetUserPassword = function (name, callback) {
callback("Password reset failure", null); callback("Password reset failure", null);
return; return;
} }
var query = "UPDATE registrations SET pw=? WHERE uname=?"; var query = "UPDATE registrations SET pw=? WHERE uname=?";
self.query(query, [data, name], function (err, res) { self.query(query, [data, name], function (err, res) {
if(err) { if(err) {
@ -791,6 +795,8 @@ Database.prototype.resetUserPassword = function (name, callback) {
}); });
}; };
/* user playlists */
Database.prototype.listUserPlaylists = function (name, callback) { Database.prototype.listUserPlaylists = function (name, callback) {
var self = this; var self = this;
if(typeof callback !== "function") if(typeof callback !== "function")
@ -835,7 +841,7 @@ Database.prototype.saveUserPlaylist = function (pl, username, plname,
var self = this; var self = this;
if(typeof callback !== "function") if(typeof callback !== "function")
callback = blackHole; callback = blackHole;
var tmp = [], time = 0; var tmp = [], time = 0;
for(var i in pl) { for(var i in pl) {
var e = { var e = {
@ -852,11 +858,99 @@ Database.prototype.saveUserPlaylist = function (pl, username, plname,
var query = "INSERT INTO user_playlists VALUES (?, ?, ?, ?, ?) " + var query = "INSERT INTO user_playlists VALUES (?, ?, ?, ?, ?) " +
"ON DUPLICATE KEY UPDATE contents=?, count=?, time=?"; "ON DUPLICATE KEY UPDATE contents=?, count=?, time=?";
var params = [username, plname, plText, count, time, var params = [username, plname, plText, count, time,
plText, count, time]; plText, count, time];
self.query(query, params, callback); self.query(query, params, callback);
}; };
Database.prototype.deleteUserPlaylist = function (username, plname,
callback) {
var self = this;
if(typeof callback !== "function")
callback = blackHole;
var query = "DELETE FROM user_playlists WHERE user=? AND name=?";
self.query(query, [username, plname], callback);
};
/* user channels */
Database.prototype.listUserChannels = function (username, callback) {
var self = this;
if(typeof callback !== "function")
return;
var query = "SELECT * FROM channels WHERE owner=? ORDER BY id ASC";
self.query(query, [username], callback);
};
/* aliases */
Database.prototype.recordVisit = function (ip, name, callback) {
var self = this;
if(typeof callback !== "function")
callback = blackHole;
var time = Date.now();
var query = "DELETE FROM aliases WHERE ip=? AND name=?;" +
"INSERT INTO aliases VALUES (NULL, ?, ?, ?)";
self.query(query, [ip, name, ip, name, time], function (err, res) {
if(err) {
callback(err, null);
return;
}
callback(null, res);
query = "DELETE FROM aliases WHERE ip=? AND visit_id NOT IN (" +
"SELECT visit_id FROM (" +
"SELECT visit_id, time FROM aliases WHERE ip=?" +
"ORDER BY time DESC LIMIT 5" +
") foo" + // The 'foo' here is actually necessary
")";
self.query(query, [ip, ip]);
});
};
Database.prototype.listAliases = function (ip, callback) {
var self = this;
if(typeof callback !== "function")
return;
var query = "SELECT name FROM aliases WHERE ip=?";
self.query(query, [ip], function (err, res) {
var names = null;
if(!err) {
names = [];
res.forEach(function (row) {
names.append(row.name);
});
}
callback(err, names);
});
};
Database.prototype.listIPsForName = function (name, callback) {
var self = this;
if(typeof callback !== "function")
return;
var query = "SELECT ip FROM aliases WHERE name=?";
self.query(query, [name], function (err, res) {
var ips = null;
if(!err) {
ips = [];
res.forEach(function (row) {
ips.push(row.ip);
});
}
callback(err, ips);
});
};
module.exports = Database; module.exports = Database;