-
Notifications
You must be signed in to change notification settings - Fork 7
/
models.js
490 lines (457 loc) · 13.2 KB
/
models.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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
var bcrypt = require('bcrypt-nodejs'),
common = require('./static/js/common'),
orm = require('orm');
var NOT_SET_TIMEZONE_OFFSET = 9999999999;
module.exports = function (db, cb) {
var Session = db.define('session', {
sid: {
type: 'text'
},
value: {
type: 'text'
}
});
// Tracks a transfer from provisional user to a chosen-username user
// on login. Does not track signups the user **is** the provisional user
// with a change in username and some other account details.
var UserHistory = db.define('user_history', {
transferDate: {
type: 'date',
time: true
}
});
// Stores a referral at the point of its instantiation
var Referral = db.define('referral', {
refCode: {
type: 'text'
},
destinationPath: {
type: 'text'
},
medium: {
type: 'text'
},
type: {
type: 'text'
},
date: {
type: 'date',
time: true
}
});
// Route impression
var Impression = db.define('impression', {
path: {
type: 'text'
},
requestMethod: {
type: 'text'
},
date: {
type: 'date',
time: true
},
ip: {
type: 'text'
},
userAgent: {
type: 'text'
},
httpRefererUrl: {
type: 'text'
}
});
// Social event, such as a Tweet, Facebook post
var SocialEvent = db.define('social_event', {
eventSourceId: {
type: 'text'
},
path: {
type: 'text'
},
medium: {
type: 'text'
},
type: {
type: 'text'
},
authorSourceId: {
type: 'text'
},
authorSourceUsername: {
type: 'text'
},
content: {
type: 'text'
},
rawEvent: {
type: 'text'
},
refCode: {
type: 'text'
},
date: {
type: 'date',
time: true
},
ip: {
type: 'text'
},
userAgent: {
type: 'text'
}
});
// TODO: Ensure on migrration to MongoDB that default value is set to current timestamp equivalent or that it isn't the cause of an error
var Post = db.define('post', {
title: {
type: 'text'
},
text: {
type: 'text'
},
targetLocality: {
type: 'text'
},
targetLat: {
type: 'number'
},
targetLong: {
type: 'number'
},
automaticLocation: {
type: 'boolean'
},
targetImage: {
type: 'text'
},
targetYoutubeVideoId: {
type: 'text'
},
targetVideoUrl: {
type: 'text'
},
targetDateTimeOccurred: {
type: 'date',
time: true
},
date: {
type: 'date',
time: true
},
author: {
type: 'text'
},
tags: {
type: 'object'
},
updated: {
type: 'date',
time: true
},
viewCount: {
type: 'number'
}
},
{
methods: {
getUpvoteCount: function(){
return this.ratings.filter(function(rating){return rating.isUpvote() && rating.show && common.isUserContentShow(rating.user);}).length;
},
getDownvoteCount: function(){
return this.ratings.filter(function(rating){return rating.isDownvote() && rating.show && common.isUserContentShow(rating.user);}).length;
},
getImportanceCount: function(){
return this.ratings.filter(function(rating){return rating.isImportance() && common.isUserContentShow(rating.user) && rating.show;}).length;
},
isUpvotedBy: function(user){
var ratings = this.ratings.filter(function(rating){return (user.id == rating.user_id) && rating.isUpvote() && common.isUserContentShow(rating.user) && rating.show;});
return ratings.length > 0;
},
isDownvotedBy: function(user){
return this.ratings.filter(function(rating){return (user.id === rating.user_id) && rating.isDownvote() && common.isUserContentShow(rating.user) && rating.show;}).length > 0;
},
isMarkedImportantBy: function(user){
return this.ratings.filter(function(rating){return (user.id == rating.user_id) && rating.isImportance() && common.isUserContentShow(rating.user) && rating.show;}).length > 0;
},
addViewCount: function(){
this.viewCount++;
this.save();
}
}
},
{
validations:{
title:[orm.enforce.notEmptyString("Title Required"), orm.enforce.required("Title Required")]
}
}), Question = db.define('question', {
// knownAnswer: {
// type: 'text'
// },
show: {
type: 'boolean',
defaultValue: '1'
}
},
{
methods: {
getSupportedAnswerCount: function(){
//If the answers are loaded returns amount of them of type Support, else not loaded returns 0!
if(this.answers != undefined){
return this.answers.filter(function(a){return a.isSupport() && a.show && common.isUserContentShow(a.post.user)}).length;
}
else{
return 0;
}
},
getRejectedAnswerCount: function(){
//If the answers are loaded returns amount of them of type Reject, else not loaded returns 0!
if(this.answers != undefined){
return this.answers.filter(function(a){return a.isAgainst() && a.show && common.isUserContentShow(a.post.user)}).length;
}
else{
return 0;
}
}
}
}), Answer = db.define('answer', {
type: {
type: 'enum',
values: ['support', 'reject']
},
show: {
type: 'boolean',
defaultValue: '1'
}
}, {
methods: {
isSupport: function(){
return this.type == "support";
},
isAgainst: function(){
return this.type == "reject";
}
}
}), Comment = db.define('comment',{
date: {
type: 'date',
time: true
},
text: {
type: 'text'
},
updated: {
type: 'date',
time: true
},
show: {
type: 'boolean',
defaultValue: '1'
}
}), QuestionComment = db.define('question_comment', {
},{
autoFetch: true,
autoFetchLimit: 2
}), AnswerComment = db.define('answer_comment', {
},{
autoFetch: true,
autoFetchLimit: 2
}), Rating = db.define('rating', {
type: {
type: 'enum',
values: ['importance', 'upvote', 'downvote']
},
date: {
type: 'date',
time: true
},
author: {
type: 'text'
},
show: {
type: 'boolean',
defaultValue: '1'
}
},
{
methods: {
isUpvote: function(){
return this.type == 'upvote';
},
isDownvote: function(){
return this.type == 'downvote';
},
isImportance: function(){
return this.type == 'importance';
},
}
}
), Local = db.define('local', {
email: {
type: 'text'
},
password: {
type: 'text'
},
resetPasswordToken: {
type: 'text'
},
resetPasswordExpires: {
type: 'date',
time: true
},
verificationToken: {
type: 'text'
},
verified: {
type: 'boolean',
defaultValue: '0'
}
}, {
methods: {
validPassword: function (pass) {
return bcrypt.compareSync(pass, this.password);
},
generateHash: function (pass) {
return bcrypt.hashSync(pass, bcrypt.genSaltSync(8), null);
}
}
}), Facebook = db.define('facebook', {
facebookId: {
type: 'text'
},
token: {
type: 'text'
},
email: {
type: 'text'
}
}), Twitter = db.define('twitter', {
twitterId: {
type: 'text'
},
token: {
type: 'text'
}
}), User = db.define('user', {
name: {
type: 'text',
},
type: {
type: 'enum',
values: ['chosen-username', 'provisional']
},
role: {
type: 'enum',
values: ['editor', 'simple', 'admin']
},
timezoneOffset: {
type: 'number',
defaultValue: NOT_SET_TIMEZONE_OFFSET
},
signupPoints: {
type: 'number',
defaultValue: 0
},
votingPoints: {
type: 'number',
defaultValue: 0
},
postPoints: {
type: 'number',
defaultValue: 0
},
active: {
type: 'boolean',
defaultValue: '1'
},
lastVisit: {
type: 'date',
time: true
}
},
{
methods: {
isEditor: function(){
return this.role === 'editor'
},
//Currently not being used but can be used for a better maintainability
getDisplayName: function(){
return this.name;
},
getTotalPoints: function () {
var points = this.signupPoints + this.votingPoints + this.postPoints;
return points;
}
}
},{validations: {
name: [orm.enforce.unique("name already taken!"),orm.enforce.ranges.length(1, undefined, "missing")],
}}), Crisis = db.define('crisis', {
show: {
type: 'boolean',
defaultValue: '1'
}
});
var Tag = db.define('tag', {
tag_name: {type: "text"}
},{ validations: {
tag_name: [orm.enforce.unique("tag exists")]
}});
var Config = db.define('config', {attr: {type: 'text', key: true}, val: {type: 'text'} }, {});
Post.hasMany('externTags', Tag, {}, {});
User.hasMany('tags', Tag, {}, {reverse: 'user', key:true});
User.hasMany('subscribedCrises', Crisis, {}, {reverse: 'subscribedUsers', key:true});
Answer.hasOne('question', Question, {
reverse: 'answers'
});
Question.hasOne('post', Post, {reverse: 'questions'});
Answer.hasOne('post', Post, {reverse: 'answers'});
Rating.hasOne('post', Post, {reverse: 'ratings'});
Rating.hasOne('user', User, {reverse: 'ratings'});
Comment.hasOne('user', User, {reverse: 'comments'});
QuestionComment.hasOne('question', Question, {
reverse: 'comments'
});
QuestionComment.hasOne('comment', Comment);
Comment.hasOne('answer', Answer, {
reverse: 'comments'
});
AnswerComment.hasOne('comment', Comment, {
reverse: 'answerComment'
});
Crisis.hasOne('post', Post, {reverse: 'crisis'});
Question.hasOne('crisis', Crisis, {
reverse: 'questions'
});
Post.hasOne('user', User, { reverse: 'posts'});
// Referrals and impressions keep their existing users
// if the user logs in and transfers to a chosen-username account.
// A link from a provisional user to a chosen-username user
// is tracked in the UserHistory model.
Referral.hasOne('user', User, {
reverse: 'authoredReferrals'
});
Impression.hasOne('user', User, {
reverse: 'impressions'
});
Impression.hasMany('referrals', Referral, {}, {
key: true, reverse: 'impressions'
});
UserHistory.hasOne('fromUser', User, {
reverse: 'withinFroms'
});
UserHistory.hasOne('toUser', User, {
reverse: 'withinTos'
});
User.hasOne('local', Local, {
reverse: 'users'
});
User.hasOne('facebook', Facebook, {
reverse: 'users'
});
User.hasOne('twitter', Twitter, {
reverse: 'users'
});
if (cb) {
cb();
}
};