/
message-model.js
138 lines (122 loc) · 3.91 KB
/
message-model.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* eslint-disable import/no-unresolved */
import SimpleSchema from 'simpl-schema';
/* eslint-enable import/no-unresolved */
export default ({ Meteor, LinkableModel, LinkParent, ServerTime, MessagesCollection }) => {
if (MessagesCollection.configureRedisOplog) {
MessagesCollection.configureRedisOplog({
mutation(options, { selector, doc }) {
const namespaces = [MessagesCollection._name];
let conversationId = (selector && selector.conversationId) || (doc && doc.conversationId);
if (!conversationId && selector._id) {
const participant = MessagesCollection.findOne({ _id: selector._id }, { fields: { conversationId: 1 } });
conversationId = participant && participant.conversationId;
}
if (conversationId) {
namespaces.push(conversationId);
}
Object.assign(options, {
namespaces,
});
},
cursor(options, selector) {
const namespace = (selector && selector.conversationId) || MessagesCollection._name;
Object.assign(options, {
namespace,
});
},
});
}
/**
* The Message Class
* @class Message
*/
class Message extends LinkParent {
/**
* Get the user that wrote the message
* @returns {User} The user who wrote the message
*/
user() {
return Meteor.users.findOne({ _id: this.userId });
}
/**
* The message timestamp
* @returns {String} A string representing the time when the message was sent
*/
timestamp() {
const now = new Date();
let stamp = '';
if (now.toLocaleDateString() !== this.createdAt.toLocaleDateString()) {
stamp += `${this.createdAt.toLocaleDateString()} `;
}
stamp += this.createdAt.toLocaleTimeString();
return stamp;
}
/**
* The message's inFlight status
* @returns {Boolean} whether the message has been received yet
*/
isInFlight() {
return this.inFlight;
}
}
Message.attachCollection(MessagesCollection);
LinkableModel.registerParentModel(Message);
// Create our message schema
MessagesCollection.attachSchema(new SimpleSchema({
userId: {
type: String,
regEx: SimpleSchema.RegEx.Id,
autoValue() {
if (this.isInsert && (!this.isFromTrustedCode || !this.isSet)) {
return this.userId;
}
return undefined;
},
index: 1,
denyUpdate: true,
},
conversationId: {
type: String,
regEx: SimpleSchema.RegEx.Id,
index: 1,
denyUpdate: true,
},
body: {
type: SimpleSchema.oneOf(String, Object),
},
createdAt: {
type: Date,
autoValue() {
if (this.isInsert) {
return ServerTime.date();
}
return undefined;
},
index: -1,
denyUpdate: true,
},
// Latest update date
updatedAt: {
type: Date,
optional: true,
autoValue() {
return ServerTime.date();
},
index: -1,
},
inFlight: {
type: Boolean,
autoValue() {
if (this.isInsert) {
if (Meteor.isServer) {
return false;
}
return true;
}
return undefined;
},
denyUpdate: true,
},
}));
return Message;
};