This repository has been archived by the owner on Jan 14, 2020. It is now read-only.
/
badge.js
230 lines (206 loc) · 5.42 KB
/
badge.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
var db = require('./');
var mongoose = require('mongoose');
var env = require('../lib/environment');
var util = require('../lib/util');
var Issuer = require('./issuer');
var Schema = mongoose.Schema;
function maxLength(field, length) {
function lengthValidator() {
if (!this[field]) return true;
return this[field].length <= length;
}
var msg = 'maxLength';
return [lengthValidator, msg];
}
var BehaviorSchema = new Schema({
shortname: {
type: String,
trim: true,
required: true
},
count: {
type: Number,
min: 0,
required: true
}
});
var BadgeSchema = new Schema({
shortname: {
type: String,
trim: true,
required: true,
unique: true
},
name: {
type: String,
trim: true,
required: true,
unique: true,
validate: maxLength('name', 128)
},
description: {
type: String,
trim: true,
required: true,
validate: maxLength('description', 128)
},
criteria: {
content: {
type: String,
trim: true
},
url: {
type: String,
trim: true
}
},
behaviors: {
type: [BehaviorSchema],
unique: true
},
prerequisites: {
type: [String]
},
image: {
type: Buffer,
required: true,
validate: maxLength('image', 256 * 1024)
}
});
var Badge = db.model('Badge', BadgeSchema);
/**
* Middleware for setting default shortname when one is not provided.
*/
function setShortNameDefault(next) {
if (!this.shortname && this.name)
this.shortname = util.slugify(this.name);
next();
}
BadgeSchema.pre('validate', setShortNameDefault);
/**
* Find a badge by the shortname of a behavior associated with the badge.
*
* @param {String} shortname
*/
Badge.findByBehavior = function findByBehavior(shortnames, callback) {
shortnames = Array.isArray(shortnames) ? shortnames : [shortnames]
var searchTerms = { behaviors: { '$elemMatch': { shortname: {'$in': shortnames }}}};
return Badge.find(searchTerms, callback);
};
/**
* Check if the credits are enough to earn the badge
*
* @param {User} user An object resembling a User object.
* @return {Boolean} whether or not the badge is earned by the credits
*/
Badge.prototype.hasEnoughCredit = function hasEnoughCredit(user) {
return this.behaviors.map(function (behavior) {
var name = behavior.shortname;
var minimum = behavior.count;
return user.credit[name] >= minimum;
}).reduce(function (result, value) {
return result && value;
}, true);
};
/**
* Remove a behavior from the list of required behaviors for the badge
*
* @param {String} shortname
*/
Badge.prototype.removeBehavior = function removeBehavior(shortname) {
var behaviors = this.behaviors.filter(function (behavior) {
if (behavior.shortname === shortname)
return null;
return behavior;
});
this.behaviors = behaviors;
return this;
};
/**
* Get the image buffer as a data URI
*
* @return {String} the data URI representing the image.
*/
Badge.prototype.imageDataURI = function imageDataURI() {
// #TODO: don't hardcode PNG maybe
var base64 = '';
var format = 'data:image/png;base64,%s';
if (this.image)
base64 = this.image.toString('base64');
return util.format('data:image/png;base64,%s', base64);
};
/**
* Get relative URL for a field
*
* @param {String} field Should be either `criteria` or `image`
* @return {String} relative url
*/
Badge.prototype.relativeUrl = function relativeUrl(field) {
var formats = {
criteria: '/badge/criteria/%s',
image: '/badge/image/%s.png'
};
return util.format(formats[field], this.shortname);
};
/**
* Get absolute URL for a field
*
* @param {String} field Should be either `criteria` or `image`
* @return {String} absolute url
*/
Badge.prototype.absoluteUrl = function absoluteUrl(field) {
return env.qualifyUrl(this.relativeUrl(field));
};
/**
* Convert to an assertion compatible object
*
* @return {Object} assertion compatible object.
*/
Badge.prototype.toAssertionObject = function () {
var VERSION = '0.5.0';
return {
version: VERSION,
name: this.name,
description: this.description,
image: this.absoluteUrl('image'),
criteria: this.absoluteUrl('criteria')
};
};
/**
* Generate an assertion from the badge.
*
* @param {Object} details Recipient details:
* - `recipient`: User email
* - `evidence`: URL for badge evidence (optional)
* - `expires`: When the badge expires (optional)
* - `issuedOn`: When the badge was issued (optional)
* - `salt`: Salt for hashing the email (optional)
* @param {Object} options
* - `json`: Return JSON (default: true)
*/
Badge.prototype.makeAssertion = function makeAssertion(details, options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
options = options || { json: true }
var salt = details.salt || util.randomString(64);
var assertion = {};
assertion.recipient = util.sha256(details.recipient, salt);
assertion.salt = salt;
if (details.evidence)
assertion.evidence = details.evidence;
if (details.expires)
assertion.expires = details.expires;
if (details.issuedOn)
assertion.issued_on = details.issuedOn;
var badge = assertion.badge = this.toAssertionObject();
Issuer.getAssertionObject(function (err, issuerObj) {
if (err) return callback(err);
badge.issuer = issuerObj;
if (options.json === true)
return callback(null, JSON.stringify(assertion));
return callback(null, assertion);
});
};
module.exports = Badge;