thelounge/src/plugins/irc-events/join.ts
2022-06-02 20:02:24 -07:00

61 lines
1.4 KiB
TypeScript

import Msg, {MessageType} from "../../models/msg";
import User from "../../models/user";
import type {IrcEventHandler} from "../../client";
import {ChanState} from "../../models/chan";
export default <IrcEventHandler>function (irc, network) {
const client = this;
irc.on("join", function (data) {
let chan = network.getChannel(data.channel);
if (typeof chan === "undefined") {
chan = client.createChannel({
name: data.channel,
state: ChanState.JOINED,
});
client.emit("join", {
network: network.uuid,
chan: chan.getFilteredClone(true),
index: network.addChannel(chan),
});
client.save();
chan.loadMessages(client, network);
// Request channels' modes
network.irc.raw("MODE", chan.name);
} else if (data.nick === irc.user.nick) {
chan.state = ChanState.JOINED;
// Don't react to our own join
if (data.batch?.type !== "chathistory") {
network.irc.chatHistory.latest(chan.name);
}
client.emit("channel:state", {
chan: chan.id,
state: chan.state,
});
}
const user = new User({nick: data.nick});
const msg = new Msg({
time: data.time,
from: user,
hostmask: data.ident + "@" + data.hostname,
gecos: data.gecos,
account: data.account,
type: MessageType.JOIN,
self: data.nick === irc.user.nick,
});
chan.pushMessage(client, msg);
chan.setUser(new User({nick: data.nick}));
client.emit("users", {
chan: chan.id,
});
});
};