-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.js
244 lines (209 loc) · 6.7 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
var crypto = require('crypto'),
Document,
User,
LoginToken;
function defineModels(mongoose, fn) {
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
function changeNumVotes(v) {
this.votes = (this.upVotes || 0) - (this.downVotes || 0);
return v;
}
Application = new Schema({
'name': {type: String, index: true, 'default': 'app'},
'downloads': {type: Number, 'default': 0},
});
// Model: Transcription
Transcription = new Schema({
'title': {type: String, index: true, 'default': 'Unknown'},
'album': {type: String, index: true, 'default': 'Unknown'},
'artist': {type: String, index: true, 'default': 'Unknown'},
'genre': {type: String, index: true, 'default': 'Unknown'},
'instrument': {type: String, index: true, 'default': 'Unknown'},
'description': {type: String, 'default': ''},
'infoLogStr': {type: String, 'default': '{}'},
'uploadTime': {type: Number},
'uploadDateStr': {type: String},
'fileLocation': {type: String},
'url': {type: String},
'upVotes': {type: Number, 'default': 0, set: changeNumVotes},
'downVotes': {type: Number, 'default': 0, set: changeNumVotes},
'votes': {type: Number},
'userId': {type: ObjectId, index: true}
});
// InfoLog Explained:
// InfoLog stores a history of all changes
// Its an array where each element is an obj with the state of the transcription during the last change
// infoLogStr is whats stored in the db, and its stringified in pre
// infoLogObj is what should be in the obj when saving
// infoLog is a convenience method that calls JSON.parse on infoLogStr
Transcription.virtual('id')
.get(function() {
return this._id.toHexString();
});
Transcription.virtual('infoLog')
.get(function() {
return JSON.parse(this.infoLogStr);
});
function makeReadableTime(mill) {
var d = new Date(mill);
var month = d.getMonth() + 1;
var day = d.getDate();
var year = d.getFullYear();
return month + '/' + day + '/' + year;
}
Transcription.pre('save', function(next) {
this.votes = this.upVotes - this.downVotes;
// Convert to readable time
if (!this.uploadTime) {
this.uploadTime = Date.now();
}
this.uploadDateStr = makeReadableTime(this.uploadTime);
// Collapse if only whitespace
if (!(/\S/.test(this.description))) {
// string is all whitespace
this.description = '';
}
//Stringify info log
this.infoLogStr = JSON.stringify(this.infoLogObj);
next();
});
/*
Transcription.virtual('votes')
.get(function() {
this.votes = this.upVotes - this.downVotes;
return this.upVotes - this.downVotes;
});
*/
/**
* Model: User
*/
function validatePresenceOf(value) {
return value && value.length;
}
User = new Schema({
'username': { type: String, validate: [validatePresenceOf, 'a username is required'], index: { unique: true } },
'email': { type: String, lowercase: true, validate: [validatePresenceOf, 'an email is required'], index: { unique: true } },
//'profPicLoc': {type: String}, //index: { unique: true}},
// Array of transcription ids
'upVotes': {type: [String], 'default': []},
'downVotes': {type: [String], 'default': []},
'karmaPoints': {type: Number, 'default': 10},
'personalWebsite': {type: String},
'hashed_password': String,
'registerTime': {type: Number},
'registerDateStr': {type: 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; });
// Returns if the user has voted for a particular
// transcription or not. 0 for no, 1 for downVote,
// 2 for upVote
User.statics.hasVoted = function(userId, trId, cb) {
this.findById(userId, function(err, user) {
if (user.upVotes.indexOf(trId) > -1) {
cb(err, 2);
} else if (user.downVotes.indexOf(trId) > -1) {
cb(err, 1);
} else {
cb(err, 0);
}
});
};
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');
});
// Never have < 0 karmaPoints
User.pre('save', function(next) {
// Convert to readable time
if (!this.registerTime) {
this.registerTime = Date.now();
}
this.registerDateStr = makeReadableTime(this.registerTime);
if (this.karmaPoints < 0) {
this.karmaPoints = 0;
}
next();
});
/*
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({
username: { 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({ username: this.username, token: this.token, series: this.series });
});
/**
* Model: Bounty
*/
Bounty = new Schema({
'hasUploaded': {type: Boolean, 'default': false},
'fulfilled': {type: Boolean, 'default':false},
'filledById': {type: ObjectId},
'transcriptionId': { type: String},
'points': {type: Number},
'title': {type: String, index: true},
'album': {type: String, index: true},
'artist': {type: String, index: true},
'instrument': {type: String, index: true},
'description': {type: String},
'userId': {type: ObjectId, index: true}
});
Bounty.virtual('id')
.get(function() {
return this._id.toHexString();
});
// Create models
mongoose.model('Transcription', Transcription);
mongoose.model('User', User);
mongoose.model('LoginToken', LoginToken);
mongoose.model('Bounty', Bounty);
mongoose.model('Application', Application);
fn();
}
exports.defineModels = defineModels;