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

67 lines
1.4 KiB
TypeScript
Raw Permalink Normal View History

import {IrcEventHandler} from "../../client";
2024-02-24 11:13:11 +01:00
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 () {
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),
2024-03-03 11:33:40 +01:00
shouldOpen: false,
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,
});
}
}
};