diff --git a/channel.js b/channel.js index 52b13904..9b20c506 100644 --- a/channel.js +++ b/channel.js @@ -98,12 +98,50 @@ Channel.prototype.createTables = function() { results = db.querySync(query) || results; // Insert into global channel table - var query = 'INSERT INTO channels VALUES (NULL, "{}")' + var query = 'INSERT INTO channels (`id`, `name`) VALUES (NULL, "{}")' .replace(/\{\}/, this.name); + results = db.querySync(query) || results; db.closeSync(); return results; } +Channel.prototype.tryRegister = function(user) { + if(this.registered) { + user.socket.emit('registerChannel', { + success: false, + error: "This channel is already registered" + }); + } + else if(!user.loggedIn) { + user.socket.emit('registerChannel', { + success: false, + error: "You must log in to register a channel" + }); + + } + else if(!Rank.hasPermission(user, "registerChannel")) { + user.socket.emit('registerChannel', { + success: false, + error: "You don't have permission to register this channel" + }); + } + else { + if(this.createTables()) { + this.registered = true; + this.saveRank(user); + user.socket.emit('registerChannel', { + success: true, + }); + } + else { + user.socket.emit('registerChannel', { + success: false, + error: "Unable to register channel, see an admin" + }); + } + } +} + // Retrieves a user's rank from the database Channel.prototype.getRank = function(name) { if(!this.registered) @@ -143,9 +181,7 @@ Channel.prototype.saveRank = function(user) { .replace(/\{1\}/, this.name) .replace(/\{2\}/, user.rank) .replace(/\{3\}/, user.name); - console.log(query); var results = db.querySync(query); - console.log("saveRank update: " + results); // Gonna have to insert a new one, bugger if(!results.fetchAllSync) { var query = 'INSERT INTO chan_{1}_ranks (`name`, `rank`) VALUES ("{2}", "{3}")' @@ -153,7 +189,6 @@ Channel.prototype.saveRank = function(user) { .replace(/\{2\}/, user.name) .replace(/\{3\}/, user.rank); results = db.querySync(query); - console.log("saveRank insert: " + results); } db.closeSync(); return results; @@ -220,7 +255,8 @@ Channel.prototype.userJoin = function(user) { // If the channel is empty and isn't registered, the first person // gets ownership of the channel (temporarily) if(this.users.length == 0 && !this.registered) { - user.rank = (user.rank < Rank.Owner) ? Rank.Owner : user.rank; + user.rank = (user.rank < Rank.Owner) ? Rank.Owner + 7 : user.rank; + user.socket.emit('channelNotRegistered'); } this.users.push(user); if(user.name != "") { diff --git a/rank.js b/rank.js index 6f4ace93..1b91a0ff 100644 --- a/rank.js +++ b/rank.js @@ -14,6 +14,7 @@ exports.Siteadmin = 255; var permissions = { acp: exports.Siteadmin, + registerChannel: exports.Owner, queue: exports.Moderator, assignLeader: exports.Moderator, kick: exports.Moderator, diff --git a/user.js b/user.js index 8f179523..7692990b 100644 --- a/user.js +++ b/user.js @@ -165,6 +165,18 @@ User.prototype.initCallbacks = function() { } }.bind(this)); + this.socket.on('registerChannel', function(data) { + if(this.channel == null) { + this.socket.emit('channelRegistration', { + success: false, + error: "You're not in any channel!" + }); + } + else { + this.channel.tryRegister(this); + } + }.bind(this)); + this.socket.on('adm', function(data) { if(Rank.hasPermission(this, "acp")) { this.handleAdm(data); @@ -283,8 +295,9 @@ User.prototype.login = function(name, sha256) { // Sweet, let's look up our rank var chanrank = (this.channel != null) ? this.channel.getRank(name) : Rank.Guest; - this.rank = (chanrank > row.global_rank) ? chanrank + var rank = (chanrank > row.global_rank) ? chanrank : row.global_rank; + this.rank = (this.rank > rank) ? this.rank : rank; this.socket.emit('rank', { rank: this.rank }); diff --git a/www/assets/js/callbacks.js b/www/assets/js/callbacks.js index 5047dc90..94454f37 100644 --- a/www/assets/js/callbacks.js +++ b/www/assets/js/callbacks.js @@ -15,6 +15,19 @@ function initCallbacks() { .innerHTML = "

Disconnected from server

"; }); + socket.on('channelNotRegistered', function() { + showChannelRegistration(); + }); + + socket.on('registerChannel', function(data) { + if(data.success) { + $('#chregnotice').remove(); + } + else { + alert(data.error); + } + }); + socket.on('rank', function(data) { if(data.rank >= Rank.Moderator) { $('#playlist_controls').css("display", "block"); diff --git a/www/assets/js/functions.js b/www/assets/js/functions.js index f4193973..129d139a 100644 --- a/www/assets/js/functions.js +++ b/www/assets/js/functions.js @@ -493,3 +493,17 @@ function updatePoll(data) { i++; }); } + +function showChannelRegistration() { + var div = $('
').addClass('alert alert-info').attr('id', 'chregnotice') + .insertAfter($('.row')[0]); + $('