thelounge/src/models/msg.js
Pavel Djundik 104e270882
Merge pull request #1771 from thelounge/astorije/fix-user-in-msg
Clone instances of `User` in `Msg` to avoid unintentional mutations
2017-11-30 11:29:23 +02:00

66 lines
1.1 KiB
JavaScript

"use strict";
var _ = require("lodash");
var id = 0;
class Msg {
constructor(attr) {
// 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: id++,
previews: [],
text: "",
type: Msg.Type.MESSAGE,
self: false,
});
if (this.time > 0) {
this.time = new Date(this.time);
} else {
this.time = new Date();
}
}
findPreview(link) {
return this.previews.find((preview) => preview.link === link);
}
}
Msg.Type = {
UNHANDLED: "unhandled",
AWAY: "away",
ACTION: "action",
BACK: "back",
ERROR: "error",
INVITE: "invite",
JOIN: "join",
KICK: "kick",
MESSAGE: "message",
MODE: "mode",
MOTD: "motd",
NICK: "nick",
NOTICE: "notice",
PART: "part",
QUIT: "quit",
CTCP: "ctcp",
TOPIC: "topic",
TOPIC_SET_BY: "topic_set_by",
WHOIS: "whois",
BANLIST: "ban_list",
};
module.exports = Msg;