hardlounge/src/plugins/irc-events/list.js

57 lines
1.2 KiB
JavaScript
Raw Normal View History

"use strict";
const Chan = require("../../models/chan");
module.exports = function(irc, network) {
const client = this;
const MAX_CHANS = 500;
irc.on("channel list start", function() {
2016-05-29 02:07:34 +00:00
network.chanCache = [];
2018-07-10 09:37:48 +00:00
updateListStatus({
text: "Loading channel list, this can take a moment...",
2018-07-10 09:37:48 +00:00
});
});
irc.on("channel list", function(channels) {
2016-05-29 02:07:34 +00:00
Array.prototype.push.apply(network.chanCache, channels);
2018-07-10 09:37:48 +00:00
updateListStatus({
text: `Loaded ${network.chanCache.length} channels...`,
});
});
irc.on("channel list end", function() {
2018-07-10 09:37:48 +00:00
updateListStatus(network.chanCache.sort((a, b) => b.num_users - a.num_users).slice(0, MAX_CHANS));
2016-05-29 02:07:34 +00:00
network.chanCache = [];
});
function updateListStatus(msg) {
let chan = network.getChannel("Channel List");
if (typeof chan === "undefined") {
chan = client.createChannel({
type: Chan.Type.SPECIAL,
2018-07-10 09:37:48 +00:00
special: Chan.SpecialType.CHANNELLIST,
name: "Channel List",
2018-07-10 09:37:48 +00:00
data: msg,
});
client.emit("join", {
network: network.uuid,
chan: chan.getFilteredClone(true),
index: network.addChannel(chan),
});
2018-07-10 09:37:48 +00:00
} else {
chan.data = msg;
2018-07-10 09:37:48 +00:00
client.emit("msg:special", {
chan: chan.id,
data: msg,
});
}
}
};