diff --git a/lib/database/accounts.js b/lib/database/accounts.js index fd9d68e8..f2551224 100644 --- a/lib/database/accounts.js +++ b/lib/database/accounts.js @@ -17,6 +17,7 @@ module.exports = function (db) { "`password` VARCHAR(64) NOT NULL," + "`global_rank` INT NOT NULL," + "`email` VARCHAR(255) NOT NULL," + + "`profile` TEXT NOT NULL," + "`ip` VARCHAR(39) NOT NULL," + "`time` BIGINT NOT NULL, " + "PRIMARY KEY(`id`), INDEX(`name`)) " + @@ -404,20 +405,78 @@ module.exports = function (db) { }); }, + /** + * Lookup a user's profile + */ getProfile: function (name, callback) { if (typeof callback !== "function") { return; } - callback(new Error("getProfile is not implemented"), null); - }, - - setProfile: function (name, callback) { - if (typeof callback !== "function") { + if (typeof name !== "string") { + callback(new Error("Invalid username"), null); return; } - callback(new Error("setProfile is not implemented"), null); + db.query("SELECT profile FROM `users` WHERE name=?", [name], + function (err, rows) { + if (err) { + callback(err, null); + } else if (rows.length === 0) { + callback(new Error("User does not exist"), null); + } else { + var userprof = { + image: "", + text: "" + }; + try { + var profile = JSON.parse(rows[0].profile); + userprof.image = profile.image || ""; + userprof.text = profile.text || ""; + callback(null, userprof); + } catch (e) { + callback(e, null); + } + } + }); + }, + + /** + * Updates a user's profile + */ + setProfile: function (name, profile, callback) { + if (typeof callback !== "function") { + callback = blackHole; + } + + if (typeof name !== "string") { + callback(new Error("Invalid username"), null); + return; + } + + if (typeof profile !== "object") { + callback(new Error("Invalid profile"), null); + return; + } + + // Cast to string to guarantee string type + profile.image += ""; + profile.text += ""; + + // Limit size + profile.image = profile.image.substring(0, 255); + profile.text = profile.text.substring(0, 255); + + // Stringify the literal to guarantee I only get the keys I want + var profilejson = JSON.stringify({ + image: profile.image, + text: profile.text + }); + + db.query("UPDATE `users` SET profile=? WHERE name=?", [profilejson, name], + function (err, result) { + callback(err, err ? null : true); + }); }, generatePasswordReset: function (ip, name, email, callback) {