From 6cdfa647fb9c207cc68a676c4ce63ac8b15ea1b3 Mon Sep 17 00:00:00 2001 From: calzoneman Date: Sat, 25 May 2013 18:16:44 -0400 Subject: [PATCH] Continue database work --- database-new.js | 102 ++++++++++++++++++++++++++++++++++++++++-- tests/database-new.js | 27 ++++++++++- 2 files changed, 124 insertions(+), 5 deletions(-) diff --git a/database-new.js b/database-new.js index 153e9cc2..a8867a2b 100644 --- a/database-new.js +++ b/database-new.js @@ -304,7 +304,7 @@ function loadChannel(chan) { } function deleteChannel(name) { - if(!name.test(/[a-zA-Z0-9-_]+/)) { + if(!/[a-zA-Z0-9-_]+/.test(name)) { return false; } @@ -314,7 +314,7 @@ function deleteChannel(name) { } var query = "DROP TABLE `chan_?_bans`, `chan_?_ranks`, `chan_?_library`" - .replace("?", name); + .replace(/\?/g, name); var results = db.querySync(query); if(!results) { @@ -385,13 +385,38 @@ function setChannelRank(chan, name, rank) { } var query = createQuery( - "INSERT INTO `?` VALUES (?, ?) ON DUPLICATE KEY UPDATE", - ["chan_"+chan+"_ranks", rank, name] + ["INSERT INTO `?` ", + "(`name`, `rank`) ", + "VALUES ", + "(?, ?) ", + "ON DUPLICATE KEY UPDATE ", + "`rank`=?"].join(""), + ["chan_"+chan+"_ranks", name, rank, rank] ); return db.querySync(query); } +function listChannelRanks(chan) { + var db = getConnection(); + if(!db) { + return []; + } + + var query = createQuery( + "SELECT * FROM `?` WHERE 1", + ["chan_"+chan+"_ranks"] + ); + + var results = db.querySync(query); + if(!results) { + Logger.errlog.log("! Failed to list ranks: " + chan); + return []; + } + + return results.fetchAllSync(); +} + function addToLibrary(chan, media) { var db = getConnection(); if(!db) { @@ -423,6 +448,70 @@ function removeFromLibrary(chan, id) { return db.querySync(query); } +function channelBan(chan, ip, name, banby) { + var db = getConnection(); + if(!db) { + return false; + } + + var query = createQuery( + ["INSERT INTO `?` ", + "(`ip`, `name`, `banner`) ", + "VALUES ", + "(?, ?, ?)"].join(""), + ["chan_"+chan+"_bans", ip, name, banby] + ); + + return db.querySync(query); +} + +function channelUnbanIP(chan, ip) { + var db = getConnection(); + if(!db) { + return false; + } + + var query = createQuery( + "DELETE FROM `?` WHERE `ip`=?", + ["chan_"+chan+"_bans", ip] + ); + + return db.querySync(query); +} + +function channelUnbanName(chan, name) { + var db = getConnection(); + if(!db) { + return false; + } + + var query = createQuery( + "DELETE FROM `?` WHERE `ip`='*' AND `name`=?", + ["chan_"+chan+"_bans", name] + ); + + return db.querySync(query); +} + +/* REGION Users */ + +function setProfile(name, data) { + var db = getConnection(); + if(!db) { + return false; + } + + var query = createQuery( + ["UPDATE `registrations` SET ", + "`profile_image`=?,", + "`profile_text`=? ", + "WHERE uname=?"].join(""), + [data.image, data.text, name] + ); + + return db.querySync(query); +} + exports.setup = setup; exports.getConnection = getConnection; exports.createQuery = createQuery; @@ -436,5 +525,10 @@ exports.loadChannel = loadChannel; exports.deleteChannel = deleteChannel; exports.getChannelRank = getChannelRank; exports.setChannelRank = setChannelRank; +exports.listChannelRanks = listChannelRanks; exports.addToLibrary = addToLibrary; exports.removeFromLibrary = removeFromLibrary; +exports.channelBan = channelBan; +exports.channelUnbanIP = channelUnbanIP; +exports.channelUnbanName = channelUnbanName; +exports.setProfile = setProfile; diff --git a/tests/database-new.js b/tests/database-new.js index 631ca296..809bb154 100644 --- a/tests/database-new.js +++ b/tests/database-new.js @@ -26,6 +26,31 @@ Database.globalUnbanIP("192.167"); Database.refreshGlobalBans(); assert(!Database.checkGlobalBan("192.168.1.12")); assert(!Database.checkGlobalBan("192.167.5.54")); - console.log("[PASS] Global Bans"); + +// Test channel registration +assert(Database.registerChannel("test")); +assert(Database.deleteChannel("test")); +console.log("[PASS] Channel registration"); + +// Test channel ranks +Database.registerChannel("test"); +assert(Database.setChannelRank("test", "a_user", 10)); +assert(Database.getChannelRank("test", "a_user") == 10); +assert(Database.listChannelRanks("test").length == 1); +console.log("[PASS] Channel ranks"); + +// Test library caching +assert(Database.addToLibrary("test", { + id: "abc", + seconds: 123, + title: "Testing", + type: "yt" +})); +assert(db.querySync("SELECT * FROM `chan_test_library` WHERE id='abc'")); +assert(Database.removeFromLibrary("test", "abc")); +assert(db.querySync("SELECT * FROM `chan_test_library` WHERE id='abc'").fetchAllSync().length == 0); +console.log("[PASS] Channel library"); + +db.closeSync(); process.exit(0);