Refactor channel packing

This commit is contained in:
Calvin Montgomery 2014-05-23 23:09:36 -07:00
parent 02ac983fba
commit f3eb999a76
9 changed files with 112 additions and 46 deletions

View file

@ -613,4 +613,42 @@ Channel.prototype.broadcastAll = function (msg, data) {
this._broadcast(msg, data, this.name);
};
Channel.prototype.packInfo = function (isAdmin) {
var data = {
name: this.name,
usercount: this.users.length,
users: [],
registered: this.is(Flags.C_REGISTERED)
};
for (var i = 0; i < this.users.length; i++) {
if (this.users[i].name !== "") {
var name = this.users[i].getName();
var rank = this.users[i].account.effectiveRank;
if (rank >= 255) {
name = "!" + name;
} else if (rank >= 4) {
name = "~" + name;
} else if (rank >= 3) {
name = "&" + name;
} else if (rank >= 2) {
name = "@" + name;
}
data.users.push(name);
}
}
if (isAdmin) {
data.activeLockCount = this.activeLock.count;
}
var self = this;
var keys = Object.keys(this.modules);
keys.forEach(function (k) {
self.modules[k].packInfo(data, isAdmin);
});
return data;
};
module.exports = Channel;

View file

@ -62,6 +62,10 @@ ChatModule.prototype.save = function (data) {
data.chatmuted = Array.prototype.slice.call(this.muted);
};
ChatModule.prototype.packInfo = function (data, isAdmin) {
data.chat = Array.prototype.slice.call(this.buffer);
};
ChatModule.prototype.onUserPostJoin = function (user) {
var self = this;
user.waitFlag(Flags.U_LOGGED_IN, function () {

View file

@ -107,6 +107,12 @@ EmoteModule.prototype.save = function (data) {
data.emotes = this.emotes.pack();
};
EmoteModule.prototype.packInfo = function (data, isAdmin) {
if (isAdmin) {
data.emoteCount = this.emotes.emotes.length;
}
};
EmoteModule.prototype.onUserPostJoin = function (user) {
user.socket.on("updateEmote", this.handleUpdateEmote.bind(this, user));
user.socket.on("importEmotes", this.handleImportEmotes.bind(this, user));

View file

@ -171,6 +171,12 @@ ChatFilterModule.prototype.save = function (data) {
data.filters = this.filters.pack();
};
ChatFilterModule.prototype.packInfo = function (data, isAdmin) {
if (isAdmin) {
data.chatFilterCount = this.filters.filters.length;
}
};
ChatFilterModule.prototype.onUserPostJoin = function (user) {
user.socket.on("updateFilter", this.handleUpdateFilter.bind(this, user));
user.socket.on("importFilters", this.handleImportFilters.bind(this, user));

View file

@ -22,6 +22,13 @@ ChannelModule.prototype = {
},
/**
* Called to pack info, e.g. for channel detail view
*/
packInfo: function (data, isAdmin) {
},
/**
* Called when a user is attempting to join a channel.
*

View file

@ -41,6 +41,14 @@ OptionsModule.prototype.save = function (data) {
data.opts = this.opts;
};
OptionsModule.prototype.packInfo = function (data, isAdmin) {
data.pagetitle = this.opts.pagetitle;
data.public = this.opts.show_public;
if (isAdmin) {
data.hasPassword = this.opts.password !== false;
}
};
OptionsModule.prototype.get = function (key) {
return this.opts[key];
};

View file

@ -171,6 +171,28 @@ PlaylistModule.prototype.unload = function () {
this.channel = null;
};
PlaylistModule.prototype.packInfo = function (data, isAdmin) {
if (this.current) {
data.mediatitle = this.current.media.title;
if (isAdmin) {
data.mediaLink = util.formatLink(this.current.media.id, this.current.media.type);
}
} else {
data.mediatitle = "(Nothing Playing)";
if (isAdmin) {
data.mediaLink = "#";
}
}
if (isAdmin) {
if (this.leader) {
data.leader = this.leader.getName();
} else {
data.leader = "[server]";
}
}
};
PlaylistModule.prototype.onUserPostJoin = function (user) {
this.sendPlaylist([user]);
this.sendChangeMedia([user]);