mirror of https://github.com/calzoneman/sync.git
Implement channel registration
This commit is contained in:
parent
8fdf3f7cd7
commit
18d599a7aa
46
channel.js
46
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 != "") {
|
||||
|
|
1
rank.js
1
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,
|
||||
|
|
15
user.js
15
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
|
||||
});
|
||||
|
|
|
@ -15,6 +15,19 @@ function initCallbacks() {
|
|||
.innerHTML = "<h3>Disconnected from server</h3>";
|
||||
});
|
||||
|
||||
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");
|
||||
|
|
|
@ -493,3 +493,17 @@ function updatePoll(data) {
|
|||
i++;
|
||||
});
|
||||
}
|
||||
|
||||
function showChannelRegistration() {
|
||||
var div = $('<div/>').addClass('alert alert-info').attr('id', 'chregnotice')
|
||||
.insertAfter($('.row')[0]);
|
||||
$('<button/>').addClass('close pull-right').text('×')
|
||||
.appendTo(div)
|
||||
.click(function() { div.remove(); });
|
||||
$('<h3/>').text("This channel isn't registered").appendTo(div);
|
||||
$('<button/>').addClass('btn btn-primary').text('Register it')
|
||||
.appendTo(div)
|
||||
.click(function() {
|
||||
socket.emit('registerChannel');
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue