From f3eb999a76532daa1164dea621b65614a3328294 Mon Sep 17 00:00:00 2001 From: Calvin Montgomery Date: Fri, 23 May 2014 23:09:36 -0700 Subject: [PATCH] Refactor channel packing --- lib/channel/channel.js | 38 +++++++++++++++++++++++++++++++++++ lib/channel/chat.js | 4 ++++ lib/channel/emotes.js | 6 ++++++ lib/channel/filters.js | 6 ++++++ lib/channel/module.js | 7 +++++++ lib/channel/opts.js | 8 ++++++++ lib/channel/playlist.js | 22 +++++++++++++++++++++ lib/server.js | 44 ++--------------------------------------- www/js/acp.js | 23 +++++++++++++++++---- 9 files changed, 112 insertions(+), 46 deletions(-) diff --git a/lib/channel/channel.js b/lib/channel/channel.js index 6f38ad0b..5a3f0b42 100644 --- a/lib/channel/channel.js +++ b/lib/channel/channel.js @@ -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; diff --git a/lib/channel/chat.js b/lib/channel/chat.js index 8f5bc187..1775012b 100644 --- a/lib/channel/chat.js +++ b/lib/channel/chat.js @@ -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 () { diff --git a/lib/channel/emotes.js b/lib/channel/emotes.js index 6a870565..f6f70f54 100644 --- a/lib/channel/emotes.js +++ b/lib/channel/emotes.js @@ -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)); diff --git a/lib/channel/filters.js b/lib/channel/filters.js index 25742cd9..95e93380 100644 --- a/lib/channel/filters.js +++ b/lib/channel/filters.js @@ -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)); diff --git a/lib/channel/module.js b/lib/channel/module.js index 0926e9e6..430efab3 100644 --- a/lib/channel/module.js +++ b/lib/channel/module.js @@ -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. * diff --git a/lib/channel/opts.js b/lib/channel/opts.js index 035a9bb3..e79e5e1a 100644 --- a/lib/channel/opts.js +++ b/lib/channel/opts.js @@ -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]; }; diff --git a/lib/channel/playlist.js b/lib/channel/playlist.js index ff9a303c..c9d43fd6 100644 --- a/lib/channel/playlist.js +++ b/lib/channel/playlist.js @@ -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]); diff --git a/lib/server.js b/lib/server.js index 074a1de1..40fdea96 100644 --- a/lib/server.js +++ b/lib/server.js @@ -194,7 +194,7 @@ Server.prototype.unloadChannel = function (chan) { chan.dead = true; }; -Server.prototype.packChannelList = function (publicOnly, showMeta) { +Server.prototype.packChannelList = function (publicOnly, isAdmin) { var channels = this.channels.filter(function (c) { if (!publicOnly) { return true; @@ -205,50 +205,10 @@ Server.prototype.packChannelList = function (publicOnly, showMeta) { var self = this; return channels.map(function (c) { - return self.packChannel(c, showMeta); + return c.packInfo(isAdmin); }); }; -Server.prototype.packChannel = function (c, showMeta) { - var opts = c.modules.options; - var pl = c.modules.playlist; - var chat = c.modules.chat; - var data = { - name: c.name, - pagetitle: opts.pagetitle ? opts.pagetitle : c.name, - mediatitle: pl && pl.current ? pl.current.media.title : "-", - usercount: c.users.length, - users: [], - chat: chat ? Array.prototype.slice.call(chat.buffer) : [], - registered: c.is(Flags.C_REGISTERED), - public: opts && opts.get("show_public") - }; - - for (var i = 0; i < c.users.length; i++) { - if (c.users[i].name !== "") { - var name = c.users[i].getName(); - var rank = c.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); - } - } - - /* Add in some extra data- intended to be site admin only */ - if (showMeta) { - data.activeLockCount = c.activeLock.count; - } - - return data; -}; - Server.prototype.announce = function (data) { if (data == null) { this.announcement = null; diff --git a/www/js/acp.js b/www/js/acp.js index d5d909bd..6356b72b 100644 --- a/www/js/acp.js +++ b/www/js/acp.js @@ -379,8 +379,10 @@ function showChannelDetailModal(c) { $("").text(c.pagetitle).appendTo(tr); tr = $("").appendTo(table); - $("").text("Media Title").appendTo(tr); - $("").text(c.mediatitle).appendTo(tr); + $("").text("Current Media").appendTo(tr); + $("").attr("href", c.mediaLink).text(c.mediatitle).appendTo( + $("").appendTo(tr) + ); tr = $("").appendTo(table); $("").text("User Count").appendTo(tr); @@ -402,6 +404,14 @@ function showChannelDetailModal(c) { $("").text("ActiveLock Count").appendTo(tr); $("").text(c.activeLockCount).appendTo(tr); + tr = $("").appendTo(table); + $("").text("Chat Filter Count").appendTo(tr); + $("").text(c.chatFilterCount).appendTo(tr); + + tr = $("").appendTo(table); + $("").text("Emote Count").appendTo(tr); + $("").text(c.emoteCount).appendTo(tr); + $("

").text("Recent Chat").appendTo(body); $("
").text(c.chat.map(function (data) {
         var msg = "<" + data.username;
@@ -422,6 +432,7 @@ function showChannelDetailModal(c) {
 }
 
 socket.on("acp-list-activechannels", function (channels) {
+console.log(channels[0]);
     var tbl = $("#acp-loaded-channels table");
     tbl.find("tbody").remove();
 
@@ -449,12 +460,16 @@ socket.on("acp-list-activechannels", function (channels) {
         var public = $("").text(c.public).appendTo(tr);
         var controlOuter = $("").appendTo(tr);
         var controlInner = $("
").addClass("btn-group").appendTo(controlOuter); - $("