This repository was archived by the owner on Jan 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodels.js
More file actions
339 lines (323 loc) · 8.36 KB
/
models.js
File metadata and controls
339 lines (323 loc) · 8.36 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*!
* Copyright 2014, Digium, Inc.
* All rights reserved.
*
* This source code is licensed under The AGPL v3 License found in the
* LICENSE file in the root directory of this source tree.
*
* For all details and documentation: https://www.respoke.io
*/
'use strict';
var mongoose = require('mongoose');
var ObjectId = mongoose.Schema.Types.ObjectId;
var uuid = require('uuid');
var emailValidation = require('email-validation');
var config = require('./config');
var appUtilities = require('./lib/app-utilities');
var logger;
/**
* For the most part, all errors would be passed back and not logged here.
*/
var models = {};
/**
* Account Model
*/
var AccountSchema = new mongoose.Schema({
// _id is also username
_id: {
type: String,
required: true,
unique: true
},
__v: {
type: Number,
select: false
},
email: {
type: String
},
display: {
type: String,
required: true
},
password: {
type: Object,
select: false
},
google: {
type: String,
select: false
},
conf: {
type: String,
select: false
},
settings: {
type: {},
default: {
privateMessageSounds: true,
groupDesktopNotifications: true,
privateDesktopNotifications: true,
groupMessageSounds: true,
offlineNotifications: true,
htmlEmails: true,
notifyOnMention: true,
mutedGroups: [],
theme: 'dark'
},
required: true
},
created: {
type: Date,
default: Date.now,
select: false
}
});
AccountSchema.pre('validate', function (next) {
if (!this._id) {
return next(new Error("Account username (ID) is required."));
}
if (/[^a-z0-9]/g.test(this._id)) {
return next(new Error("Account username (ID) may only have numbers and lowercase letters."));
}
if (!this.display) {
return next(new Error("Display name is required."));
}
if (!this.google) {
if (!this.email) {
return next(new Error("Email is required."));
}
this.email = this.email.toLowerCase();
var emailInvalid = this.isEmailInvalid(this.email);
if (emailInvalid) {
return next(new Error("Email address is not in a valid format."));
}
if (this.isNew || this.isDirectModified('password')) {
if (!this.password) {
return next(new Error("Password is required."));
}
var passwordInvalid = this.isPasswordInvalid(this.password);
if (passwordInvalid) {
return next(new Error(passwordInvalid));
}
}
}
if (this.isNew && !this.google) {
this.conf = 'confirm-' + uuid.v4() + '-' + uuid.v4() + '-' + uuid.v4();
this._id = this._id.toLowerCase();
logger.info('new account conf', config.baseURL + '/conf/' + this._id + '/' + this.conf);
}
if (this.isDirectModified('password')) {
this.password = appUtilities.hash(this.password);
}
next();
});
// pre-validate that the email is unique
AccountSchema.pre('validate', function (next) {
if (!this.isDirectModified('email')) {
return next();
}
var doc = this;
mongoose.model('Account').findOne({ email: doc.email }).exec(function (err, account2) {
if (err) {
return next(err);
}
if (!account2) {
return next();
}
// of course, if this is their email it's ok
if (account2._id.toString() !== doc._id.toString()) {
return next(new Error("That email is already registered."));
}
next();
});
});
// pre-validate that the _id / username is unique
AccountSchema.pre('validate', function (next) {
if (!this.isDirectModified('_id')) {
return next();
}
var doc = this;
mongoose.model('Account').findOne({ _id: doc._id }).exec(function (err, account1) {
if (err) {
return next(err);
}
if (account1) {
return next(new Error("That username is already taken."));
}
next();
});
});
AccountSchema.method('isPasswordInvalid', function (password) {
if (!password) {
return 'Missing password';
}
if (typeof password !== 'string') {
return 'Password must be a string.';
}
//
// Add more password validators here.
//
return null;
});
AccountSchema.method('isEmailInvalid', function (email) {
return !emailValidation.valid(email);
});
AccountSchema.method('passwordReset', function (callback) {
this.conf = 'reset-' + uuid.v4() + '-' + uuid.v4() + '-' + uuid.v4();
this.save(callback);
});
models.Account = mongoose.model('Account', AccountSchema);
/**
* Group Model
* Types of groups: public, private, conversation
*/
var GroupSchema = new mongoose.Schema({
_id: {
type: String,
required: true,
unique: true,
default: uuid.v4
},
owner: {
type: String,
required: true,
ref: 'Account'
},
accounts: {
type: [{
type: String,
ref: 'Account'
}],
default: []
},
created: {
type: Date,
default: Date.now
}
});
function isUUID(input) {
return /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(input);
}
GroupSchema.virtual('isPrivate').get(function () {
return isUUID(this._id) && this.accounts.length;
});
GroupSchema.virtual('isPublic').get(function () {
return !isUUID(this._id) && !this.accounts.length;
});
GroupSchema.virtual('isConversation').get(function () {
return !isUUID(this._id) && this.accounts.length;
});
GroupSchema.pre('validate', function (next) {
if (/[^a-z0-9]/gi.test(this._id)) {
return next(new Error("Group name must be alphanumeric."));
}
if (!this.owner) {
return next(new Error("A group must have an owner."));
}
next();
});
models.Group = mongoose.model('Group', GroupSchema);
/**
* Message model
* These are group and 1+1 messages.
*/
var MessageSchema = new mongoose.Schema({
__v: {
type: Number,
select: false
},
from: {
type: String,
ref: 'Account',
required: true
},
to: {
type: String,
ref: 'Account'
},
group: {
type: String,
ref: 'Group'
},
content: {
type: String,
required: true,
default: '',
description: 'A JSON string'
},
file: {
type: ObjectId,
ref: 'File'
},
created: {
type: Date,
default: Date.now
}
});
MessageSchema.pre('validate', function (next) {
if (!this.from) {
return next(new Error("The message 'from' field is required."));
}
if (!this.to && !this.group) {
return next(new Error("Either message 'group' or 'to' is required."));
}
if (!this.content && !this.file) {
return next(new Error("Message 'content' is required."));
}
if (this.content) {
try {
var test = JSON.parse(this.content);
if (!test.text && !this.file) {
return next(new Error("Message content.text is missing."));
}
}
catch (ignored) {
return next(new Error("Message 'content' is not a valid JSON string."));
}
}
next();
});
models.Message = mongoose.model('Message', MessageSchema);
/**
* File model
*/
var FileSchema = new mongoose.Schema({
__v: {
type: Number,
select: false
},
content: {
type: String,
required: true,
description: 'Base64 content'
},
contentType: {
type: String,
required: true
},
name: {
type: String
},
owner: {
type: String,
ref: 'Account',
required: true
},
created: {
type: Date,
default: Date.now
}
});
models.File = mongoose.model('File', FileSchema);
exports = module.exports = function (initializedLogger) {
logger = initializedLogger;
mongoose.connect(config.mongoURI);
mongoose.connection.on('connected', function () {
logger.info('mongo connected');
});
mongoose.connection.on('error', function (err) {
logger.error('mongo connect error', err);
});
return models;
};