thelounge/server/plugins/irc-events/list.ts
2024-04-21 15:10:45 +02:00

65 lines
1.3 KiB
TypeScript

import {IrcEventHandler} from "../../client";
import Chan from "../../models/chan";
import {ChanType, SpecialChanType} from "../../../shared/types/chan";
export default <IrcEventHandler>function (irc, network) {
const client = this;
const MAX_CHANS = 500;
irc.on("channel list start", function () {
network.chanCache = [];
updateListStatus({
text: "Loading channel list, this can take a moment...",
});
});
irc.on("channel list", function (channels) {
Array.prototype.push.apply(network.chanCache, channels);
updateListStatus({
text: `Loaded ${network.chanCache.length} channels...`,
});
});
irc.on("channel list end", function () {
updateListStatus(
network.chanCache.sort((a, b) => b.num_users! - a.num_users!).slice(0, MAX_CHANS)
);
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",
data: msg,
});
client.emit("join", {
network: network.uuid,
chan: chan.getFilteredClone(true),
index: network.addChannel(chan),
});
} else {
chan.data = msg;
client.emit("msg:special", {
chan: chan.id,
data: msg,
});
}
}
};