thelounge/src/models/msg.ts

142 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-05-02 07:56:38 +02:00
import _ from "lodash";
2022-05-23 09:44:01 +02:00
import {LinkPreview} from "../plugins/irc-events/link";
2022-05-05 00:41:57 +02:00
import User from "./user";
export type UserInMessage = Partial<User> & {
mode: string;
};
export enum MessageType {
UNHANDLED = "unhandled",
ACTION = "action",
AWAY = "away",
BACK = "back",
ERROR = "error",
INVITE = "invite",
JOIN = "join",
KICK = "kick",
LOGIN = "login",
LOGOUT = "logout",
MESSAGE = "message",
MODE = "mode",
MODE_CHANNEL = "mode_channel",
MODE_USER = "mode_user", // RPL_UMODEIS
MONOSPACE_BLOCK = "monospace_block",
NICK = "nick",
NOTICE = "notice",
PART = "part",
QUIT = "quit",
CTCP = "ctcp",
CTCP_REQUEST = "ctcp_request",
CHGHOST = "chghost",
TOPIC = "topic",
TOPIC_SET_BY = "topic_set_by",
WHOIS = "whois",
RAW = "raw",
PLUGIN = "plugin",
WALLOPS = "wallops",
}
2022-05-02 07:56:38 +02:00
class Msg {
2022-05-03 03:32:20 +02:00
from!: UserInMessage;
id!: number;
2022-05-23 09:44:01 +02:00
previews!: LinkPreview[];
2022-05-03 03:32:20 +02:00
text!: string;
type!: MessageType;
self!: boolean;
time!: Date;
hostmask!: string;
target!: UserInMessage;
2022-05-02 07:56:38 +02:00
// TODO: new_nick is only on MessageType.NICK,
// we should probably make Msgs that extend this class and use those
// throughout. I'll leave any similar fields below.
2022-05-03 03:32:20 +02:00
new_nick!: string;
highlight?: boolean;
2022-05-03 03:32:20 +02:00
showInActive?: boolean;
new_ident!: string;
new_host!: string;
ctcpMessage!: string;
command!: string;
invitedYou!: boolean;
gecos!: string;
account!: boolean;
// these are all just for error:
2022-05-03 03:32:20 +02:00
error!: string;
nick!: string;
channel!: string;
reason!: string;
2022-05-03 03:32:20 +02:00
raw_modes!: any;
when!: Date;
whois!: any;
2022-05-05 06:10:12 +02:00
users!: UserInMessage[] | string[];
2022-05-03 03:32:20 +02:00
statusmsgGroup!: string;
params!: string[];
2022-05-02 07:56:38 +02:00
2022-06-03 05:02:24 +02:00
batch?: {
id: number;
type: string;
params: string[];
commands: any[];
};
2022-05-03 08:16:34 +02:00
constructor(attr?: Partial<Msg>) {
2022-05-02 07:56:38 +02:00
// Some properties need to be copied in the Msg object instead of referenced
if (attr) {
["from", "target"].forEach((prop) => {
if (attr[prop]) {
this[prop] = {
mode: attr[prop].mode,
nick: attr[prop].nick,
};
}
});
}
_.defaults(this, attr, {
from: {},
id: 0,
previews: [],
text: "",
type: MessageType.MESSAGE,
self: false,
});
2022-05-05 06:10:12 +02:00
if (this.time) {
2022-05-02 07:56:38 +02:00
this.time = new Date(this.time);
} else {
this.time = new Date();
}
}
findPreview(link: string) {
return this.previews.find((preview) => preview.link === link);
}
isLoggable() {
if (this.type === MessageType.TOPIC) {
// Do not log topic that is sent on channel join
return !!this.from.nick;
}
switch (this.type) {
case MessageType.MONOSPACE_BLOCK:
case MessageType.ERROR:
case MessageType.TOPIC_SET_BY:
case MessageType.MODE_CHANNEL:
case MessageType.MODE_USER:
case MessageType.RAW:
case MessageType.WHOIS:
case MessageType.PLUGIN:
return false;
default:
return true;
}
}
}
export default Msg;
2022-05-05 00:41:57 +02:00
export type Message = Msg;