-
Notifications
You must be signed in to change notification settings - Fork 181
/
models.js
129 lines (104 loc) · 2.89 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
var crypto = require('crypto'),
Document,
User,
LoginToken;
function extractKeywords(text) {
if (!text) return [];
return text.
split(/\s+/).
filter(function(v) { return v.length > 2; }).
filter(function(v, i, a) { return a.lastIndexOf(v) === i; });
}
function defineModels(mongoose, fn) {
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
/**
* Model: Document
*/
Document = new Schema({
'title': { type: String, index: true },
'data': String,
'tags': [String],
'keywords': [String],
'user_id': ObjectId
});
Document.virtual('id')
.get(function() {
return this._id.toHexString();
});
Document.pre('save', function(next) {
this.keywords = extractKeywords(this.data);
next();
});
/**
* Model: User
*/
function validatePresenceOf(value) {
return value && value.length;
}
User = new Schema({
'email': { type: String, validate: [validatePresenceOf, 'an email is required'], index: { unique: true } },
'hashed_password': String,
'salt': String
});
User.virtual('id')
.get(function() {
return this._id.toHexString();
});
User.virtual('password')
.set(function(password) {
this._password = password;
this.salt = this.makeSalt();
this.hashed_password = this.encryptPassword(password);
})
.get(function() { return this._password; });
User.method('authenticate', function(plainText) {
return this.encryptPassword(plainText) === this.hashed_password;
});
User.method('makeSalt', function() {
return Math.round((new Date().valueOf() * Math.random())) + '';
});
User.method('encryptPassword', function(password) {
return crypto.createHmac('sha1', this.salt).update(password).digest('hex');
});
User.pre('save', function(next) {
if (!validatePresenceOf(this.password)) {
next(new Error('Invalid password'));
} else {
next();
}
});
/**
* Model: LoginToken
*
* Used for session persistence.
*/
LoginToken = new Schema({
email: { type: String, index: true },
series: { type: String, index: true },
token: { type: String, index: true }
});
LoginToken.method('randomToken', function() {
return Math.round((new Date().valueOf() * Math.random())) + '';
});
LoginToken.pre('save', function(next) {
// Automatically create the tokens
this.token = this.randomToken();
if (this.isNew)
this.series = this.randomToken();
next();
});
LoginToken.virtual('id')
.get(function() {
return this._id.toHexString();
});
LoginToken.virtual('cookieValue')
.get(function() {
return JSON.stringify({ email: this.email, token: this.token, series: this.series });
});
mongoose.model('Document', Document);
mongoose.model('User', User);
mongoose.model('LoginToken', LoginToken);
fn();
}
exports.defineModels = defineModels;