server: fix time handling

The framework may emit messages which do not have a time stamp.
We tried to unconditionally convert the time field, fix that.

The Msg constructor replaces falsey time fields with the current
date so we can also remove the duplication from that codepath.
This commit is contained in:
Reto Brunner 2024-05-16 22:15:39 +02:00
parent 4de413070d
commit 0d9c184f19

View file

@ -17,7 +17,7 @@ type HandleInput = {
ident: string; ident: string;
target: string; target: string;
type: MessageType; type: MessageType;
time: number; time?: number;
text?: string; text?: string;
from_server?: boolean; from_server?: boolean;
message: string; message: string;
@ -25,7 +25,7 @@ type HandleInput = {
}; };
function convertForHandle(type: MessageType, data: MessageEventArgs): HandleInput { function convertForHandle(type: MessageType, data: MessageEventArgs): HandleInput {
return {...data, time: data.time ? data.time : new Date().getTime(), type: type}; return {...data, type: type};
} }
export default <IrcEventHandler>function (irc, network) { export default <IrcEventHandler>function (irc, network) {
@ -36,19 +36,16 @@ export default <IrcEventHandler>function (irc, network) {
}); });
irc.on("action", function (data) { irc.on("action", function (data) {
data.type = MessageType.ACTION; handleMessage(convertForHandle(MessageType.ACTION, data));
handleMessage(data);
}); });
irc.on("privmsg", function (data) { irc.on("privmsg", function (data) {
data.type = MessageType.MESSAGE; handleMessage(convertForHandle(MessageType.MESSAGE, data));
handleMessage(data);
}); });
irc.on("wallops", function (data) { irc.on("wallops", function (data) {
data.from_server = true; data.from_server = true;
data.type = MessageType.WALLOPS; handleMessage(convertForHandle(MessageType.WALLOPS, data));
handleMessage(data);
}); });
function handleMessage(data: HandleInput) { function handleMessage(data: HandleInput) {
@ -129,7 +126,7 @@ export default <IrcEventHandler>function (irc, network) {
// msg is constructed down here because `from` is being copied in the constructor // msg is constructed down here because `from` is being copied in the constructor
const msg = new Msg({ const msg = new Msg({
type: data.type, type: data.type,
time: new Date(data.time), time: data.time ? new Date(data.time) : undefined,
text: data.message, text: data.message,
self: self, self: self,
from: from, from: from,