thelounge/server/plugins/irc-events/list.ts

65 lines
1.3 KiB
TypeScript
Raw Normal View History

import {IrcEventHandler} from "../../client";
import Chan, {ChanType, SpecialChanType} from "../../models/chan";
export default <IrcEventHandler>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)
2019-07-17 11:33:59 +02:00
);
2016-05-29 04:07:34 +02:00
network.chanCache = [];
});
function updateListStatus(
msg:
| {
text: string;
}
| Chan[]
) {
let chan = network.getChannel("Channel List");
if (typeof chan === "undefined") {
chan = client.createChannel({
type: ChanType.SPECIAL,
special: SpecialChanType.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,
});
}
}
};