Move channel register/delete reload logic to message bus

This commit is contained in:
Calvin Montgomery 2017-08-15 18:55:36 -07:00
parent d16cfb7328
commit 791a712a68
3 changed files with 64 additions and 24 deletions

View File

@ -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"
},

View File

@ -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);
}
};

View File

@ -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,