Start working on user aliases

This commit is contained in:
calzoneman 2013-06-17 21:02:48 -04:00
parent ca6d2962d8
commit 32af68a68e
2 changed files with 54 additions and 0 deletions

View File

@ -173,6 +173,19 @@ function init() {
if(!results) {
Logger.errlog.log("! Failed to create 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`))",
"ENGINE = MyISAM;"].join("");
results = db.querySync(query);
if(!results) {
Logger.errlog.log("! Failed to create aliases table");
}
}
/* REGION Global Bans */
@ -885,6 +898,44 @@ function deleteUserPlaylist(user, name) {
return results;
}
/* User Aliases */
function recordVisit(ip, name) {
var db = getConnection();
if(!db) {
return false;
}
var time = Date.now();
db.querySync(createQuery(
"DELETE FROM aliases WHERE ip=? AND name=?",
[ip, name]
));
var query = createQuery(
"INSERT INTO aliases VALUES (NULL, ?, ?, ?)",
[ip, name, time]
);
var results = db.querySync(query);
if(!results) {
console.log(results);
Logger.errlog.log("! Failed to record visit");
}
// Keep most recent 5 records per IP
results = db.querySync(createQuery(
["DELETE FROM aliases WHERE visit_id NOT IN (",
"SELECT visit_id FROM (",
"SELECT visit_id,time FROM aliases WHERE ip=? ORDER BY time DESC LIMIT 5",
") foo",
");"].join(""),
[ip]
));
console.log(results);
return results;
}
exports.setup = setup;
exports.getConnection = getConnection;
exports.createQuery = createQuery;
@ -914,3 +965,4 @@ exports.getUserPlaylists = getUserPlaylists;
exports.loadUserPlaylist = loadUserPlaylist;
exports.saveUserPlaylist = saveUserPlaylist;
exports.deleteUserPlaylist = deleteUserPlaylist;
exports.recordVisit = recordVisit;

View File

@ -539,6 +539,7 @@ User.prototype.login = function(name, pw, session) {
lastguestlogin[this.ip] = Date.now();
this.rank = Rank.Guest;
Logger.syslog.log(this.ip + " signed in as " + name);
Database.recordVisit(this.ip, name);
this.name = name;
this.loggedIn = false;
this.socket.emit("login", {
@ -562,6 +563,7 @@ User.prototype.login = function(name, pw, session) {
name: name
});
Logger.syslog.log(this.ip + " logged in as " + name);
Database.recordVisit(this.ip, name);
this.profile = {
image: row.profile_image,
text: row.profile_text