hardlounge/server/plugins/irc-events/list.ts
Max Leiter dd05ee3a65
TypeScript and Vue 3 (#4559)
Co-authored-by: Eric Nemchik <eric@nemchik.com>
Co-authored-by: Pavel Djundik <xPaw@users.noreply.github.com>
2022-06-18 17:25:21 -07:00

65 lines
1.3 KiB
TypeScript

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 () {
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,
});
}
}
};