diff --git a/package.json b/package.json index 438478b0..6797ef19 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "Calvin Montgomery", "name": "CyTube", "description": "Online media synchronizer and chat", - "version": "3.47.0", + "version": "3.47.1", "repository": { "url": "http://github.com/calzoneman/sync" }, diff --git a/src/server.js b/src/server.js index 216f41af..5392efff 100644 --- a/src/server.js +++ b/src/server.js @@ -74,6 +74,8 @@ var Server = function () { const globalMessageBus = this.initModule.getGlobalMessageBus(); globalMessageBus.on('UserProfileChanged', this.handleUserProfileChange.bind(this)); + globalMessageBus.on('ChannelDeleted', this.handleChannelDelete.bind(this)); + globalMessageBus.on('ChannelRegistered', this.handleChannelRegister.bind(this)); // database init ------------------------------------------------------ var Database = require("./database"); @@ -428,3 +430,57 @@ Server.prototype.handleUserProfileChange = function (event) { LOGGER.error('handleUserProfileChange failed: %s', error); } }; + +Server.prototype.handleChannelDelete = function (event) { + try { + const lname = event.channel.toLowerCase(); + + this.channels.forEach(channel => { + if (channel.dead) return; + + if (channel.uniqueName === lname) { + channel.clearFlag(Flags.C_REGISTERED); + + const users = Array.prototype.slice.call(channel.users); + users.forEach(u => { + u.kick('Channel deleted'); + }); + + if (!channel.dead) { + channel.emit('empty'); + } + + LOGGER.info('Processed deleted channel %s', lname); + } + }); + } catch (error) { + LOGGER.error('handleChannelDelete failed: %s', error); + } +}; + +Server.prototype.handleChannelRegister = function (event) { + try { + const lname = event.channel.toLowerCase(); + + this.channels.forEach(channel => { + if (channel.dead) return; + + if (channel.uniqueName === lname) { + channel.clearFlag(Flags.C_REGISTERED); + + const users = Array.prototype.slice.call(channel.users); + users.forEach(u => { + u.kick('Channel reloading'); + }); + + if (!channel.dead) { + channel.emit('empty'); + } + + LOGGER.info('Processed registered channel %s', lname); + } + }); + } catch (error) { + LOGGER.error('handleChannelRegister failed: %s', error); + } +}; diff --git a/src/web/account.js b/src/web/account.js index 7ef2bc29..7d12bc6a 100644 --- a/src/web/account.js +++ b/src/web/account.js @@ -268,18 +268,10 @@ async function handleNewChannel(req, res) { Logger.eventlog.log("[channel] " + user.name + "@" + req.realIP + " registered channel " + name); - var sv = Server.getServer(); - if (sv.isChannelLoaded(name)) { - var chan = sv.getChannel(name); - var users = Array.prototype.slice.call(chan.users); - users.forEach(function (u) { - u.kick("Channel reloading"); - }); + globalMessageBus.emit('ChannelRegistered', { + channel: name + }); - if (!chan.dead) { - chan.emit("empty"); - } - } channels.push({ name: name }); @@ -338,19 +330,11 @@ async function handleDeleteChannel(req, res) { req.realIP + " deleted channel " + name); } - var sv = Server.getServer(); - if (sv.isChannelLoaded(name)) { - var chan = sv.getChannel(name); - chan.clearFlag(require("../flags").C_REGISTERED); - var users = Array.prototype.slice.call(chan.users); - users.forEach(function (u) { - u.kick("Channel reloading"); - }); - if (!chan.dead) { - chan.emit("empty"); - } - } + globalMessageBus.emit('ChannelDeleted', { + channel: name + }); + db.channels.listUserChannels(user.name, function (err2, channels) { sendPug(res, "account-channels", { channels: err2 ? [] : channels,