From 32af68a68e6a00b5538b7db9432d519ff77869ad Mon Sep 17 00:00:00 2001 From: calzoneman Date: Mon, 17 Jun 2013 21:02:48 -0400 Subject: [PATCH] Start working on user aliases --- database.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ user.js | 2 ++ 2 files changed, 54 insertions(+) diff --git a/database.js b/database.js index c734695e..13f5196f 100644 --- a/database.js +++ b/database.js @@ -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; diff --git a/user.js b/user.js index c3d18702..1b461751 100644 --- a/user.js +++ b/user.js @@ -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