This repository was archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathexport-badges.js
74 lines (65 loc) · 1.94 KB
/
export-badges.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
'use strict';
var async = require('async');
var request = require('request');
var bakery = require('openbadges-bakery');
function exportBadges (args, callback) {
var seneca = this;
var user = args.user;
async.waterfall([
loadUsersBadges,
downloadBadgeImages,
bakeBadgeImages
], function (err, res) {
if (err) return callback(null, {error: err.message});
return callback(null, res);
});
function loadUsersBadges (done) {
seneca.act({role: 'cd-profiles', cmd: 'list', query: {userId: user.id}}, function (err, response) {
if (err) return done(err);
var profile = response[0];
return done(null, profile.badges);
});
}
function downloadBadgeImages (badges, done) {
var badgeData = [];
async.each(badges, function (badge, cb) {
if (!badge.assertion) return cb();
download(badge.imageUrl, badge.slug + '.png', function (err, response) {
if (err) return cb(err);
badge.encodedImage = response;
badgeData.push(badge);
cb();
});
}, function (err) {
if (err) return done(err);
return done(null, badgeData);
});
}
function bakeBadgeImages (badgeData, done) {
var bakedBadges = [];
async.eachSeries(badgeData, function (badge, cb) {
var badgeImage = badge.encodedImage;
var badgeAssertion = badge.assertion;
var options = {
image: badgeImage,
assertion: badgeAssertion
};
// bake assertion into image
bakery.bake(options, function (err, data) {
if (err) return done(err);
bakedBadges.push(data);
cb();
});
}, function (err) {
if (err) return done(err);
return done(null, bakedBadges);
});
}
var download = function (uri, filename, callback) {
request.get({uri: uri, encoding: null}, function (err, res, body) {
if (err) return callback(err);
return callback(null, body);
});
};
}
module.exports = exportBadges;