mirror of https://github.com/calzoneman/sync.git
More database refactoring
This commit is contained in:
parent
fe00fb8c83
commit
47af1d4892
|
@ -515,7 +515,7 @@ Channel.prototype.saveInitialRank = function (user, callback) {
|
||||||
|
|
||||||
Channel.prototype.getIPRank = function (ip, callback) {
|
Channel.prototype.getIPRank = function (ip, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
self.server.db.listAliases(ip, function (err, names) {
|
db.getAliases(ip, function (err, names) {
|
||||||
if (self.dead)
|
if (self.dead)
|
||||||
return;
|
return;
|
||||||
db.users.getGlobalRanks(names, function (err, res) {
|
db.users.getGlobalRanks(names, function (err, res) {
|
||||||
|
@ -1139,7 +1139,7 @@ Channel.prototype.broadcastNewUser = function(user) {
|
||||||
user.socket.emit("channelNotRegistered");
|
user.socket.emit("channelNotRegistered");
|
||||||
user.socket.emit("rank", user.rank);
|
user.socket.emit("rank", user.rank);
|
||||||
}
|
}
|
||||||
db.listAliases(user.ip, function (err, aliases) {
|
db.getAliases(user.ip, function (err, aliases) {
|
||||||
if (self.dead)
|
if (self.dead)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
424
lib/database.js
424
lib/database.js
|
@ -4,12 +4,10 @@ var bcrypt = require("bcrypt");
|
||||||
var $util = require("./utilities");
|
var $util = require("./utilities");
|
||||||
var Logger = require("./logger");
|
var Logger = require("./logger");
|
||||||
|
|
||||||
var cfg = {};
|
|
||||||
var pool = null;
|
var pool = null;
|
||||||
var global_ipbans = {};
|
var global_ipbans = {};
|
||||||
|
|
||||||
module.exports.init = function (cfg) {
|
module.exports.init = function (cfg) {
|
||||||
cfg = cfg;
|
|
||||||
pool = mysql.createPool({
|
pool = mysql.createPool({
|
||||||
host: cfg["mysql-server"],
|
host: cfg["mysql-server"],
|
||||||
user: cfg["mysql-user"],
|
user: cfg["mysql-user"],
|
||||||
|
@ -25,7 +23,7 @@ module.exports.init = function (cfg) {
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
// Refresh global IP bans
|
// Refresh global IP bans
|
||||||
module.exports.listGlobalIPBans();
|
module.exports.listGlobalBans();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -36,6 +34,9 @@ module.exports.init = function (cfg) {
|
||||||
module.exports.channels.init();
|
module.exports.channels.init();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute a database query
|
||||||
|
*/
|
||||||
module.exports.query = function (query, sub, callback) {
|
module.exports.query = function (query, sub, callback) {
|
||||||
// 2nd argument is optional
|
// 2nd argument is optional
|
||||||
if (typeof sub === "function") {
|
if (typeof sub === "function") {
|
||||||
|
@ -43,8 +44,9 @@ module.exports.query = function (query, sub, callback) {
|
||||||
sub = false;
|
sub = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(typeof callback !== "function")
|
if (typeof callback !== "function") {
|
||||||
callback = blackHole;
|
callback = blackHole;
|
||||||
|
}
|
||||||
|
|
||||||
pool.getConnection(function (err, conn) {
|
pool.getConnection(function (err, conn) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -54,8 +56,9 @@ module.exports.query = function (query, sub, callback) {
|
||||||
function cback(err, res) {
|
function cback(err, res) {
|
||||||
if (err) {
|
if (err) {
|
||||||
Logger.errlog.log("! DB query failed: " + query);
|
Logger.errlog.log("! DB query failed: " + query);
|
||||||
if(sub)
|
if (sub) {
|
||||||
Logger.errlog.log("Substitutions: " + sub);
|
Logger.errlog.log("Substitutions: " + sub);
|
||||||
|
}
|
||||||
Logger.errlog.log(err);
|
Logger.errlog.log(err);
|
||||||
callback("Database failure", null);
|
callback("Database failure", null);
|
||||||
} else {
|
} else {
|
||||||
|
@ -77,114 +80,75 @@ function blackHole() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.oldinit = function () {
|
module.exports.initGlobalTables = function () {
|
||||||
var query;
|
var fail = function (table) {
|
||||||
// Create global bans table
|
return function (err) {
|
||||||
query = ["CREATE TABLE IF NOT EXISTS `global_bans` (",
|
|
||||||
"`ip` VARCHAR(15) NOT NULL,",
|
|
||||||
"`note` VARCHAR(255) NOT NULL,",
|
|
||||||
"PRIMARY KEY (`ip`))",
|
|
||||||
"ENGINE = MyISAM ",
|
|
||||||
"CHARACTER SET utf8;"].join("");
|
|
||||||
|
|
||||||
module.exports.query(query, function (err, res) {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
Logger.errlog.log("! Failed to create global ban table");
|
Logger.errlog.log("Failed to initialize " + table);
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
};
|
||||||
|
var query = module.exports.query;
|
||||||
|
query("CREATE TABLE IF NOT EXISTS `global_bans` (" +
|
||||||
|
"`ip` VARCHAR(39) NOT NULL," +
|
||||||
|
"`reason` VARCHAR(255) NOT NULL," +
|
||||||
|
"PRIMARY KEY (`ip`, `name`)) " +
|
||||||
|
"CHARACTER SET utf8",
|
||||||
|
fail("global_bans"));
|
||||||
|
|
||||||
// Create password reset table
|
query("CREATE TABLE IF NOT EXISTS `password_reset` (" +
|
||||||
query = ["CREATE TABLE IF NOT EXISTS `password_reset` (",
|
"`ip` VARCHAR(39) NOT NULL," +
|
||||||
"`ip` VARCHAR(15) NOT NULL,",
|
"`name` VARCHAR(20) NOT NULL," +
|
||||||
"`name` VARCHAR(20) NOT NULL,",
|
"`hash` VARCHAR(64) NOT NULL," +
|
||||||
"`hash` VARCHAR(64) NOT NULL,",
|
"`email` VARCHAR(255) NOT NULL," +
|
||||||
"`email` VARCHAR(255) NOT NULL,",
|
"`expire` BIGINT NOT NULL," +
|
||||||
"`expire` BIGINT NOT NULL,",
|
"PRIMARY KEY (`name`))" +
|
||||||
"PRIMARY KEY (`name`))",
|
"CHARACTER SET utf8",
|
||||||
"ENGINE = MyISAM ",
|
fail("password_reset"));
|
||||||
"CHARACTER SET utf8;"].join("");
|
|
||||||
|
|
||||||
module.exports.query(query, function (err, res) {
|
query("CREATE TABLE IF NOT EXISTS `user_playlists` (" +
|
||||||
if(err) {
|
"`user` VARCHAR(20) NOT NULL," +
|
||||||
Logger.errlog.log("! Failed to create password reset table");
|
"`name` VARCHAR(255) NOT NULL," +
|
||||||
}
|
"`contents` MEDIUMTEXT NOT NULL," +
|
||||||
});
|
"`count` INT NOT NULL," +
|
||||||
|
"`duration` INT NOT NULL," +
|
||||||
|
"PRIMARY KEY (`user`, `name`))" +
|
||||||
|
"CHARACTER SET utf8",
|
||||||
|
fail("user_playlists"));
|
||||||
|
|
||||||
// Create user playlist table
|
query("CREATE TABLE IF NOT EXISTS `aliases` (" +
|
||||||
query = ["CREATE TABLE IF NOT EXISTS `user_playlists` (",
|
"`visit_id` INT NOT NULL AUTO_INCREMENT," +
|
||||||
"`user` VARCHAR(20) NOT NULL,",
|
"`ip` VARCHAR(39) NOT NULL," +
|
||||||
"`name` VARCHAR(255) NOT NULL,",
|
"`name` VARCHAR(20) NOT NULL," +
|
||||||
"`contents` MEDIUMTEXT NOT NULL,",
|
"`time` BIGINT NOT NULL," +
|
||||||
"`count` INT NOT NULL,",
|
|
||||||
"`time` INT NOT NULL,",
|
|
||||||
"PRIMARY KEY (`user`, `name`))",
|
|
||||||
"ENGINE = MyISAM ",
|
|
||||||
"CHARACTER SET utf8;"].join("");
|
|
||||||
|
|
||||||
module.exports.query(query, function (err, res) {
|
|
||||||
if(err) {
|
|
||||||
Logger.errlog.log("! Failed to create user playlist table");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Create user aliases table
|
|
||||||
query = ["CREATE TABLE IF NOT EXISTS `aliases` (",
|
|
||||||
"`visit_id` INT NOT NULL AUTO_INCREMENT,",
|
|
||||||
"`ip` VARCHAR(15) NOT NULL,",
|
|
||||||
"`name` VARCHAR(20) NOT NULL,",
|
|
||||||
"`time` BIGINT NOT NULL,",
|
|
||||||
"PRIMARY KEY (`visit_id`), INDEX (`ip`))",
|
"PRIMARY KEY (`visit_id`), INDEX (`ip`))",
|
||||||
"ENGINE = MyISAM ",
|
fail("aliases"));
|
||||||
"CHARACTER SET utf8;"].join("");
|
|
||||||
|
|
||||||
module.exports.query(query, function (err, res) {
|
// TODO actionlog? or no?
|
||||||
if(err) {
|
|
||||||
Logger.errlog.log("! Failed to create user aliases table");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Create action log table
|
query("CREATE TABLE IF NOT EXISTS `stats` (" +
|
||||||
query = ["CREATE TABLE IF NOT EXISTS `actionlog` (",
|
"`time` BIGINT NOT NULL," +
|
||||||
"`ip` VARCHAR(15) NOT NULL,",
|
"`usercount` INT NOT NULL," +
|
||||||
"`name` VARCHAR(20) NOT NULL,",
|
"`chancount` INT NOT NULL," +
|
||||||
"`action` VARCHAR(255) NOT NULL,",
|
"`mem` INT NOT NULL," +
|
||||||
"`args` TEXT NOT NULL,",
|
"PRIMARY KEY (`time`))" +
|
||||||
"`time` BIGINT NOT NULL,",
|
"CHARACTER SET utf8",
|
||||||
"PRIMARY KEY (`ip`, `time`), INDEX (`action`))",
|
fail("stats"));
|
||||||
"ENGINE = MyISAM ",
|
|
||||||
"CHARACTER SET utf8;"].join("");
|
|
||||||
|
|
||||||
module.exports.query(query, function (err, res) {
|
|
||||||
if(err) {
|
|
||||||
Logger.errlog.log("! Failed to create action log table");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Create stats table
|
|
||||||
query = ["CREATE TABLE IF NOT EXISTS `stats` (",
|
|
||||||
"`time` BIGINT NOT NULL,",
|
|
||||||
"`usercount` INT NOT NULL,",
|
|
||||||
"`chancount` INT NOT NULL,",
|
|
||||||
"`mem` INT NOT NULL,",
|
|
||||||
"PRIMARY KEY (`time`))",
|
|
||||||
"ENGINE = MyISAM ",
|
|
||||||
"CHARACTER SET utf8;"].join("");
|
|
||||||
|
|
||||||
module.exports.query(query, function (err, res) {
|
|
||||||
if(err) {
|
|
||||||
Logger.errlog.log("! Failed to create stats table");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Refresh global IP bans
|
|
||||||
module.exports.listGlobalIPBans();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* REGION global bans */
|
/* REGION global bans */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if an IP address is globally banned
|
||||||
|
*/
|
||||||
module.exports.isGlobalIPBanned = function (ip, callback) {
|
module.exports.isGlobalIPBanned = function (ip, callback) {
|
||||||
if(typeof callback !== "function")
|
if (typeof callback !== "function") {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO account for IPv6
|
||||||
|
// Also possibly just change this to allow arbitrary
|
||||||
|
// ranges instead of only /32, /24, /16
|
||||||
const re = /(\d+)\.(\d+)\.(\d+)\.(\d+)/;
|
const re = /(\d+)\.(\d+)\.(\d+)\.(\d+)/;
|
||||||
// Account for range banning
|
// Account for range banning
|
||||||
var s16 = ip.replace(re, "$1.$2");
|
var s16 = ip.replace(re, "$1.$2");
|
||||||
|
@ -197,9 +161,14 @@ module.exports.isGlobalIPBanned = function (ip, callback) {
|
||||||
callback(null, banned);
|
callback(null, banned);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.listGlobalIPBans = function (callback) {
|
/**
|
||||||
if(typeof callback !== "function")
|
* Retrieve all global bans from the database.
|
||||||
|
* Cache locally in global_bans
|
||||||
|
*/
|
||||||
|
module.exports.listGlobalBans = function (callback) {
|
||||||
|
if (typeof callback !== "function") {
|
||||||
callback = blackHole;
|
callback = blackHole;
|
||||||
|
}
|
||||||
|
|
||||||
module.exports.query("SELECT * FROM global_bans WHERE 1", function (err, res) {
|
module.exports.query("SELECT * FROM global_bans WHERE 1", function (err, res) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -208,34 +177,42 @@ module.exports.listGlobalIPBans = function (callback) {
|
||||||
}
|
}
|
||||||
|
|
||||||
global_ipbans = {};
|
global_ipbans = {};
|
||||||
for(var i in res) {
|
for (var i = 0; i < res.length; i++) {
|
||||||
global_ipbans[res[i].ip] = res[i].note;
|
global_ipbans[res[i].ip] = res[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(null, global_ipbans);
|
callback(null, global_ipbans);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.setGlobalIPBan = function (ip, reason, callback) {
|
/**
|
||||||
if(typeof callback !== "function")
|
* Globally ban by IP
|
||||||
|
*/
|
||||||
|
module.exports.globalBanIP = function (ip, reason, callback) {
|
||||||
|
if (typeof callback !== "function") {
|
||||||
callback = blackHole;
|
callback = blackHole;
|
||||||
|
}
|
||||||
|
|
||||||
var query = "INSERT INTO global_bans VALUES (?, ?)" +
|
var query = "INSERT INTO global_bans (ip, reason) VALUES (?, ?)" +
|
||||||
" ON DUPLICATE KEY UPDATE note=?";
|
" ON DUPLICATE KEY UPDATE reason=?";
|
||||||
module.exports.query(query, [ip, reason, reason], function (err, res) {
|
module.exports.query(query, [ip, reason, reason], function (err, res) {
|
||||||
if(err) {
|
if(err) {
|
||||||
callback(err, null);
|
callback(err, null);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.listGlobalIPBans();
|
module.exports.listGlobalBans();
|
||||||
callback(null, res);
|
callback(null, res);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.clearGlobalIPBan = function (ip, callback) {
|
/**
|
||||||
if(typeof callback !== "function")
|
* Remove a global IP ban
|
||||||
|
*/
|
||||||
|
module.exports.globalUnbanIP = function (ip, callback) {
|
||||||
|
if (typeof callback !== "function") {
|
||||||
callback = blackHole;
|
callback = blackHole;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
var query = "DELETE FROM global_bans WHERE ip=?";
|
var query = "DELETE FROM global_bans WHERE ip=?";
|
||||||
|
@ -245,139 +222,16 @@ module.exports.clearGlobalIPBan = function (ip, callback) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports.listGlobalBans();
|
||||||
callback(null, res);
|
callback(null, res);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/* END REGION */
|
/* END REGION */
|
||||||
|
|
||||||
/* REGION Auth */
|
|
||||||
module.exports.getGlobalRank = function (name, callback) {
|
|
||||||
if(typeof callback !== "function")
|
|
||||||
return;
|
|
||||||
|
|
||||||
var query = "SELECT global_rank FROM registrations WHERE uname=?";
|
|
||||||
|
|
||||||
module.exports.query(query, [name], function (err, res) {
|
|
||||||
if(err) {
|
|
||||||
callback(err, null);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(res.length == 0) {
|
|
||||||
callback(null, 0);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
callback(null, res[0].global_rank);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports.listGlobalRanks = function (names, callback) {
|
|
||||||
if(typeof callback !== "function")
|
|
||||||
return;
|
|
||||||
|
|
||||||
if(typeof names === "string")
|
|
||||||
names = [names];
|
|
||||||
|
|
||||||
// Build the query template (?, ?, ?, ?, ...)
|
|
||||||
var nlist = [];
|
|
||||||
for(var i in names)
|
|
||||||
nlist.push("?");
|
|
||||||
nlist = "(" + nlist.join(",") + ")";
|
|
||||||
|
|
||||||
var query = "SELECT global_rank FROM registrations WHERE uname IN " +
|
|
||||||
nlist;
|
|
||||||
module.exports.query(query, names, function (err, res) {
|
|
||||||
if(err) {
|
|
||||||
callback(err, null);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(res.length == 0) {
|
|
||||||
callback(null, 0);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for(var i in res)
|
|
||||||
res[i] = res[i].global_rank;
|
|
||||||
|
|
||||||
callback(null, res);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
/* END REGION */
|
|
||||||
|
|
||||||
/* REGION users */
|
|
||||||
|
|
||||||
module.exports.searchUser = function (name, callback) {
|
|
||||||
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 ?";
|
|
||||||
|
|
||||||
module.exports.query(query, ["%" + name + "%"], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
/* rank */
|
|
||||||
|
|
||||||
module.exports.setGlobalRank = function (name, rank, callback) {
|
|
||||||
if(typeof callback !== "function")
|
|
||||||
callback = blackHole;
|
|
||||||
|
|
||||||
var query = "UPDATE registrations SET global_rank=? WHERE uname=?";
|
|
||||||
module.exports.query(query, [rank, name], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
/* email and profile */
|
|
||||||
|
|
||||||
module.exports.getUserProfile = function (name, callback) {
|
|
||||||
if(typeof callback !== "function")
|
|
||||||
callback = blackHole;
|
|
||||||
|
|
||||||
var query = "SELECT profile_image, profile_text FROM registrations " +
|
|
||||||
"WHERE uname=?";
|
|
||||||
|
|
||||||
module.exports.query(query, [name], function (err, res) {
|
|
||||||
if(err) {
|
|
||||||
callback(err, null);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var def = {
|
|
||||||
profile_image: "",
|
|
||||||
profile_text: ""
|
|
||||||
};
|
|
||||||
|
|
||||||
callback(null, res.length > 0 ? res[0] : def);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports.setUserProfile = function (name, data, callback) {
|
|
||||||
if(typeof callback !== "function")
|
|
||||||
callback = blackHole;
|
|
||||||
|
|
||||||
var query = "UPDATE registrations SET profile_image=?, profile_text=?" +
|
|
||||||
"WHERE uname=?";
|
|
||||||
|
|
||||||
module.exports.query(query, [data.image, data.text, name], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports.setUserEmail = function (name, email, callback) {
|
|
||||||
if(typeof callback !== "function")
|
|
||||||
callback = blackHole;
|
|
||||||
|
|
||||||
var query = "UPDATE registrations SET email=? WHERE uname=?";
|
|
||||||
|
|
||||||
module.exports.query(query, [email, name], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
/* password recovery */
|
/* password recovery */
|
||||||
|
|
||||||
|
/*
|
||||||
module.exports.genPasswordReset = function (ip, name, email, callback) {
|
module.exports.genPasswordReset = function (ip, name, email, callback) {
|
||||||
if(typeof callback !== "function")
|
if(typeof callback !== "function")
|
||||||
callback = blackHole;
|
callback = blackHole;
|
||||||
|
@ -483,20 +337,29 @@ module.exports.resetUserPassword = function (name, callback) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
*/
|
||||||
|
|
||||||
/* user playlists */
|
/* user playlists */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve all of a user's playlists
|
||||||
|
*/
|
||||||
module.exports.listUserPlaylists = function (name, callback) {
|
module.exports.listUserPlaylists = function (name, callback) {
|
||||||
if(typeof callback !== "function")
|
if (typeof callback !== "function") {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var query = "SELECT name, count, time FROM user_playlists WHERE user=?";
|
var query = "SELECT name, count, duration FROM user_playlists WHERE user=?";
|
||||||
module.exports.query(query, [name], callback);
|
module.exports.query(query, [name], callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve a user playlist by (user, name) pair
|
||||||
|
*/
|
||||||
module.exports.getUserPlaylist = function (username, plname, callback) {
|
module.exports.getUserPlaylist = function (username, plname, callback) {
|
||||||
if(typeof callback !== "function")
|
if (typeof callback !== "function") {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var query = "SELECT contents FROM user_playlists WHERE " +
|
var query = "SELECT contents FROM user_playlists WHERE " +
|
||||||
"user=? AND name=?";
|
"user=? AND name=?";
|
||||||
|
@ -523,10 +386,14 @@ module.exports.getUserPlaylist = function (username, plname, callback) {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.saveUserPlaylist = function (pl, username, plname,
|
/**
|
||||||
callback) {
|
* Saves a user playlist. Overwrites if the playlist keyed by
|
||||||
if(typeof callback !== "function")
|
* (user, name) already exists
|
||||||
|
*/
|
||||||
|
module.exports.saveUserPlaylist = function (pl, username, plname, callback) {
|
||||||
|
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) {
|
||||||
|
@ -543,7 +410,7 @@ module.exports.saveUserPlaylist = function (pl, username, plname,
|
||||||
var plText = JSON.stringify(tmp);
|
var plText = JSON.stringify(tmp);
|
||||||
|
|
||||||
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=?, duration=?";
|
||||||
|
|
||||||
var params = [username, plname, plText, count, time,
|
var params = [username, plname, plText, count, time,
|
||||||
plText, count, time];
|
plText, count, time];
|
||||||
|
@ -551,30 +418,27 @@ module.exports.saveUserPlaylist = function (pl, username, plname,
|
||||||
module.exports.query(query, params, callback);
|
module.exports.query(query, params, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.deleteUserPlaylist = function (username, plname,
|
/**
|
||||||
callback) {
|
* Deletes a user playlist
|
||||||
if(typeof callback !== "function")
|
*/
|
||||||
|
module.exports.deleteUserPlaylist = function (username, plname, callback) {
|
||||||
|
if (typeof callback !== "function") {
|
||||||
callback = blackHole;
|
callback = blackHole;
|
||||||
|
}
|
||||||
|
|
||||||
var query = "DELETE FROM user_playlists WHERE user=? AND name=?";
|
var query = "DELETE FROM user_playlists WHERE user=? AND name=?";
|
||||||
module.exports.query(query, [username, plname], callback);
|
module.exports.query(query, [username, plname], callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
/* user channels */
|
|
||||||
|
|
||||||
module.exports.listUserChannels = function (username, callback) {
|
|
||||||
if(typeof callback !== "function")
|
|
||||||
return;
|
|
||||||
|
|
||||||
var query = "SELECT * FROM channels WHERE owner=? ORDER BY id ASC";
|
|
||||||
module.exports.query(query, [username], callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
/* aliases */
|
/* aliases */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Records a user or guest login in the aliases table
|
||||||
|
*/
|
||||||
module.exports.recordVisit = function (ip, name, callback) {
|
module.exports.recordVisit = function (ip, name, callback) {
|
||||||
if(typeof callback !== "function")
|
if (typeof callback !== "function") {
|
||||||
callback = blackHole;
|
callback = blackHole;
|
||||||
|
}
|
||||||
|
|
||||||
var time = Date.now();
|
var time = Date.now();
|
||||||
var query = "DELETE FROM aliases WHERE ip=? AND name=?;" +
|
var query = "DELETE FROM aliases WHERE ip=? AND name=?;" +
|
||||||
|
@ -583,20 +447,28 @@ module.exports.recordVisit = function (ip, name, callback) {
|
||||||
module.exports.query(query, [ip, name, ip, name, time], callback);
|
module.exports.query(query, [ip, name, ip, name, time], callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deletes alias rows older than the given time
|
||||||
|
*/
|
||||||
module.exports.cleanOldAliases = function (expiration, callback) {
|
module.exports.cleanOldAliases = function (expiration, callback) {
|
||||||
if (typeof callback === "undefined")
|
if (typeof callback === "undefined") {
|
||||||
callback = blackHole;
|
callback = blackHole;
|
||||||
|
}
|
||||||
|
|
||||||
var query = "DELETE FROM aliases WHERE time < ?";
|
var query = "DELETE FROM aliases WHERE time < ?";
|
||||||
module.exports.query(query, [Date.now() - expiration], callback);
|
module.exports.query(query, [Date.now() - expiration], callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.listAliases = function (ip, callback) {
|
/**
|
||||||
if(typeof callback !== "function")
|
* Retrieves a list of aliases for an IP address
|
||||||
|
*/
|
||||||
|
module.exports.getAliases = function (ip, callback) {
|
||||||
|
if (typeof callback !== "function") {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var query = "SELECT name,time FROM aliases WHERE ip";
|
var query = "SELECT name,time FROM aliases WHERE ip";
|
||||||
// Range
|
// if the ip parameter is a /24 range, we want to match accordingly
|
||||||
if(ip.match(/^\d+\.\d+\.\d+$/)) {
|
if(ip.match(/^\d+\.\d+\.\d+$/)) {
|
||||||
query += " LIKE ?";
|
query += " LIKE ?";
|
||||||
ip += ".%";
|
ip += ".%";
|
||||||
|
@ -609,28 +481,26 @@ module.exports.listAliases = function (ip, callback) {
|
||||||
module.exports.query(query, [ip], function (err, res) {
|
module.exports.query(query, [ip], function (err, res) {
|
||||||
var names = null;
|
var names = null;
|
||||||
if(!err) {
|
if(!err) {
|
||||||
names = [];
|
names = res.map(function (row) { return row.name; });
|
||||||
res.forEach(function (row) {
|
|
||||||
names.push(row.name);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(err, names);
|
callback(err, names);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.listIPsForName = function (name, callback) {
|
/**
|
||||||
if(typeof callback !== "function")
|
* Retrieves a list of IPs that a name as logged in from
|
||||||
|
*/
|
||||||
|
module.exports.getIPs = function (name, callback) {
|
||||||
|
if (typeof callback !== "function") {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var query = "SELECT ip FROM aliases WHERE name=?";
|
var query = "SELECT ip FROM aliases WHERE name=?";
|
||||||
module.exports.query(query, [name], function (err, res) {
|
module.exports.query(query, [name], function (err, res) {
|
||||||
var ips = null;
|
var ips = null;
|
||||||
if(!err) {
|
if(!err) {
|
||||||
ips = [];
|
ips = res.map(function (row) { return row.ip; });
|
||||||
res.forEach(function (row) {
|
|
||||||
ips.push(row.ip);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(err, ips);
|
callback(err, ips);
|
||||||
|
@ -641,6 +511,7 @@ module.exports.listIPsForName = function (name, callback) {
|
||||||
|
|
||||||
/* REGION action log */
|
/* REGION action log */
|
||||||
|
|
||||||
|
/*
|
||||||
module.exports.recordAction = function (ip, name, action, args,
|
module.exports.recordAction = function (ip, name, action, args,
|
||||||
callback) {
|
callback) {
|
||||||
if(typeof callback !== "function")
|
if(typeof callback !== "function")
|
||||||
|
@ -724,31 +595,34 @@ module.exports.listActions = function (types, callback) {
|
||||||
var query = "SELECT * FROM actionlog WHERE action IN " + actionlist;
|
var query = "SELECT * FROM actionlog WHERE action IN " + actionlist;
|
||||||
module.exports.query(query, types, callback);
|
module.exports.query(query, types, callback);
|
||||||
};
|
};
|
||||||
|
*/
|
||||||
|
|
||||||
/* END REGION */
|
/* END REGION */
|
||||||
|
|
||||||
/* REGION stats */
|
/* REGION stats */
|
||||||
|
|
||||||
module.exports.addStatPoint = function (time, ucount, ccount, mem,
|
module.exports.addStatPoint = function (time, ucount, ccount, mem, callback) {
|
||||||
callback) {
|
if (typeof callback !== "function") {
|
||||||
if(typeof callback !== "function")
|
|
||||||
callback = blackHole;
|
callback = blackHole;
|
||||||
|
}
|
||||||
|
|
||||||
var query = "INSERT INTO stats VALUES (?, ?, ?, ?)";
|
var query = "INSERT INTO stats VALUES (?, ?, ?, ?)";
|
||||||
module.exports.query(query, [time, ucount, ccount, mem], callback);
|
module.exports.query(query, [time, ucount, ccount, mem], callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.pruneStats = function (before, callback) {
|
module.exports.pruneStats = function (before, callback) {
|
||||||
if(typeof callback !== "function")
|
if (typeof callback !== "function") {
|
||||||
callback = blackHole;
|
callback = blackHole;
|
||||||
|
}
|
||||||
|
|
||||||
var query = "DELETE FROM stats WHERE time < ?";
|
var query = "DELETE FROM stats WHERE time < ?";
|
||||||
module.exports.query(query, [before], callback);
|
module.exports.query(query, [before], callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.listStats = function (callback) {
|
module.exports.listStats = function (callback) {
|
||||||
if(typeof callback !== "function")
|
if (typeof callback !== "function") {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var query = "SELECT * FROM stats ORDER BY time ASC";
|
var query = "SELECT * FROM stats ORDER BY time ASC";
|
||||||
module.exports.query(query, callback);
|
module.exports.query(query, callback);
|
||||||
|
|
|
@ -494,4 +494,15 @@ module.exports = {
|
||||||
|
|
||||||
callback(new Error("recoverPassword is not implemented"), null);
|
callback(new Error("recoverPassword is not implemented"), null);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve a list of channels owned by a user
|
||||||
|
*/
|
||||||
|
getChannels: function (name, callback) {
|
||||||
|
if (typeof callback !== "function") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
db.query("SELECT * FROM `channels` WHERE owner=?", [name], callback);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue