1 |
|
2 |
|
3 | class Message {
|
4 | // Represents an incoming message from the chat.
|
5 | //
|
6 | // user - A User instance that sent the message.
|
7 | constructor (user, done) {
|
8 | this.user = user
|
9 | this.done = done || false
|
10 | this.room = this.user.room
|
11 | }
|
12 |
|
13 | // Indicates that no other Listener should be called on this object
|
14 | //
|
15 | // Returns nothing.
|
16 | finish () {
|
17 | this.done = true
|
18 | }
|
19 | }
|
20 |
|
21 | class TextMessage extends Message {
|
22 | // Represents an incoming message from the chat.
|
23 | //
|
24 | // user - A User instance that sent the message.
|
25 | // text - A String message.
|
26 | // id - A String of the message ID.
|
27 | constructor (user, text, id) {
|
28 | super(user)
|
29 | this.text = text
|
30 | this.id = id
|
31 | }
|
32 |
|
33 | // Determines if the message matches the given regex.
|
34 | //
|
35 | // regex - A Regex to check.
|
36 | //
|
37 | // Returns a Match object or null.
|
38 | match (regex) {
|
39 | return this.text.match(regex)
|
40 | }
|
41 |
|
42 | // String representation of a TextMessage
|
43 | //
|
44 | // Returns the message text
|
45 | toString () {
|
46 | return this.text
|
47 | }
|
48 | }
|
49 |
|
50 | // Represents an incoming user entrance notification.
|
51 | //
|
52 | // user - A User instance for the user who entered.
|
53 | // text - Always null.
|
54 | // id - A String of the message ID.
|
55 | class EnterMessage extends Message {}
|
56 |
|
57 | // Represents an incoming user exit notification.
|
58 | //
|
59 | // user - A User instance for the user who left.
|
60 | // text - Always null.
|
61 | // id - A String of the message ID.
|
62 | class LeaveMessage extends Message {}
|
63 |
|
64 | // Represents an incoming topic change notification.
|
65 | //
|
66 | // user - A User instance for the user who changed the topic.
|
67 | // text - A String of the new topic
|
68 | // id - A String of the message ID.
|
69 | class TopicMessage extends TextMessage {}
|
70 |
|
71 | class CatchAllMessage extends Message {
|
72 | // Represents a message that no matchers matched.
|
73 | //
|
74 | // message - The original message.
|
75 | constructor (message) {
|
76 | super(message.user)
|
77 | this.message = message
|
78 | }
|
79 | }
|
80 |
|
81 | module.exports = {
|
82 | Message,
|
83 | TextMessage,
|
84 | EnterMessage,
|
85 | LeaveMessage,
|
86 | TopicMessage,
|
87 | CatchAllMessage
|
88 | }
|