1 | "use strict";
|
2 |
|
3 | var _ = require("lodash");
|
4 |
|
5 | var id = 0;
|
6 |
|
7 | class Msg {
|
8 | constructor(attr) {
|
9 |
|
10 | if (attr) {
|
11 | ["from", "target"].forEach((prop) => {
|
12 | if (attr[prop]) {
|
13 | this[prop] = {
|
14 | mode: attr[prop].mode,
|
15 | nick: attr[prop].nick,
|
16 | };
|
17 | }
|
18 | });
|
19 | }
|
20 |
|
21 | _.defaults(this, attr, {
|
22 | from: {},
|
23 | id: id++,
|
24 | previews: [],
|
25 | text: "",
|
26 | type: Msg.Type.MESSAGE,
|
27 | self: false,
|
28 | });
|
29 |
|
30 | if (this.time > 0) {
|
31 | this.time = new Date(this.time);
|
32 | } else {
|
33 | this.time = new Date();
|
34 | }
|
35 | }
|
36 |
|
37 | findPreview(link) {
|
38 | return this.previews.find((preview) => preview.link === link);
|
39 | }
|
40 |
|
41 | isLoggable() {
|
42 | return this.type !== Msg.Type.MOTD &&
|
43 | this.type !== Msg.Type.BANLIST &&
|
44 | this.type !== Msg.Type.WHOIS;
|
45 | }
|
46 | }
|
47 |
|
48 | Msg.Type = {
|
49 | UNHANDLED: "unhandled",
|
50 | AWAY: "away",
|
51 | ACTION: "action",
|
52 | BACK: "back",
|
53 | ERROR: "error",
|
54 | INVITE: "invite",
|
55 | JOIN: "join",
|
56 | KICK: "kick",
|
57 | MESSAGE: "message",
|
58 | MODE: "mode",
|
59 | MOTD: "motd",
|
60 | NICK: "nick",
|
61 | NOTICE: "notice",
|
62 | PART: "part",
|
63 | QUIT: "quit",
|
64 | CTCP: "ctcp",
|
65 | CHGHOST: "chghost",
|
66 | TOPIC: "topic",
|
67 | TOPIC_SET_BY: "topic_set_by",
|
68 | WHOIS: "whois",
|
69 | BANLIST: "ban_list",
|
70 | };
|
71 |
|
72 | module.exports = Msg;
|