2016-10-09 19:14:02 +00:00
|
|
|
"use strict";
|
|
|
|
|
2017-11-10 20:44:14 +00:00
|
|
|
const Chan = require("../../models/chan");
|
|
|
|
const Msg = require("../../models/msg");
|
2016-03-09 20:04:07 +00:00
|
|
|
|
|
|
|
module.exports = function(irc, network) {
|
2017-11-10 20:44:14 +00:00
|
|
|
const client = this;
|
|
|
|
const MAX_CHANS = 500;
|
2016-03-09 20:04:07 +00:00
|
|
|
|
|
|
|
irc.on("channel list start", function() {
|
2016-05-29 02:07:34 +00:00
|
|
|
network.chanCache = [];
|
2016-03-09 20:04:07 +00:00
|
|
|
|
|
|
|
updateListStatus(new Msg({
|
|
|
|
text: "Loading channel list, this can take a moment...",
|
2017-11-15 06:35:15 +00:00
|
|
|
type: "channel_list_loading",
|
2016-03-09 20:04:07 +00:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
irc.on("channel list", function(channels) {
|
2016-05-29 02:07:34 +00:00
|
|
|
Array.prototype.push.apply(network.chanCache, channels);
|
2016-03-09 20:04:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
irc.on("channel list end", function() {
|
|
|
|
updateListStatus(new Msg({
|
|
|
|
type: "channel_list",
|
2017-04-06 16:22:56 +00:00
|
|
|
channels: network.chanCache.sort(function(a, b) {
|
|
|
|
return b.num_users - a.num_users;
|
2017-11-15 06:35:15 +00:00
|
|
|
}).slice(0, MAX_CHANS),
|
2016-03-09 20:04:07 +00:00
|
|
|
}));
|
|
|
|
|
2016-05-29 02:07:34 +00:00
|
|
|
if (network.chanCache.length > MAX_CHANS) {
|
2016-03-09 20:04:07 +00:00
|
|
|
updateListStatus(new Msg({
|
|
|
|
type: "channel_list_truncated",
|
2017-11-15 06:35:15 +00:00
|
|
|
text: "Channel list is too large: truncated to " + MAX_CHANS + " channels.",
|
2016-03-09 20:04:07 +00:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2016-05-29 02:07:34 +00:00
|
|
|
network.chanCache = [];
|
2016-03-09 20:04:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function updateListStatus(msg) {
|
2017-11-10 20:44:14 +00:00
|
|
|
let chan = network.getChannel("Channel List");
|
|
|
|
|
2016-03-09 20:04:07 +00:00
|
|
|
if (typeof chan === "undefined") {
|
|
|
|
chan = new Chan({
|
|
|
|
type: Chan.Type.SPECIAL,
|
2017-11-15 06:35:15 +00:00
|
|
|
name: "Channel List",
|
2016-03-09 20:04:07 +00:00
|
|
|
});
|
|
|
|
network.channels.push(chan);
|
|
|
|
client.emit("join", {
|
|
|
|
network: network.id,
|
2017-11-15 06:35:15 +00:00
|
|
|
chan: chan,
|
2016-03-09 20:04:07 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
client.emit("msg", {
|
|
|
|
chan: chan.id,
|
2017-11-15 06:35:15 +00:00
|
|
|
msg: msg,
|
2016-03-09 20:04:07 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|