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

59 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 04:07:34 +02:00
network.chanCache = [];
2018-07-10 11:37:48 +02:00
updateListStatus({
text: "Loading channel list, this can take a moment...",
2018-07-10 11:37:48 +02:00
});
});
irc.on("channel list", function (channels) {
2016-05-29 04:07:34 +02:00
Array.prototype.push.apply(network.chanCache, channels);
2018-07-10 11:37:48 +02:00
updateListStatus({
text: `Loaded ${network.chanCache.length} channels...`,
});
});
irc.on("channel list end", function () {
2019-07-17 11:33:59 +02:00
updateListStatus(
network.chanCache.sort((a, b) => b.num_users - a.num_users).slice(0, MAX_CHANS)
);
2016-05-29 04:07:34 +02: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 11:37:48 +02:00
special: Chan.SpecialType.CHANNELLIST,
name: "Channel List",
2018-07-10 11:37:48 +02:00
data: msg,
});
client.emit("join", {
network: network.uuid,
chan: chan.getFilteredClone(true),
index: network.addChannel(chan),
});
2018-07-10 11:37:48 +02:00
} else {
chan.data = msg;
2018-07-10 11:37:48 +02:00
client.emit("msg:special", {
chan: chan.id,
data: msg,
});
}
}
};