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

@ -638,6 +638,8 @@ Database.prototype.clearChannelNameBan = function (channame, name,
/* REGION users */
/* email and profile */
Database.prototype.getUserProfile = function (name, callback) {
var self = this;
if(typeof callback !== "function")
@ -682,6 +684,8 @@ Database.prototype.setUserEmail = function (name, email, callback) {
self.query(query, [email, name], callback);
};
/* password recovery */
Database.prototype.genPasswordReset = function (ip, name, email, callback) {
var self = this;
if(typeof callback !== "function")
@ -791,6 +795,8 @@ Database.prototype.resetUserPassword = function (name, callback) {
});
};
/* user playlists */
Database.prototype.listUserPlaylists = function (name, callback) {
var self = this;
if(typeof callback !== "function")
@ -859,4 +865,92 @@ Database.prototype.saveUserPlaylist = function (pl, username, plname,
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;