Коммит
c928103ac0
|
@ -0,0 +1,78 @@
|
|||
var async = require('async');
|
||||
var data = require("../lib/data.js");
|
||||
|
||||
var badgekit = require('badgekit-api-client')(
|
||||
process.env.BADGEKIT_API_URL, {
|
||||
key: process.env.BADGEKIT_API_KEY,
|
||||
secret: process.env.BADGEKIT_API_SECRET
|
||||
}
|
||||
);
|
||||
|
||||
exports = module.exports = function pollBadgekitApi() {
|
||||
data.getContributorBadges(function (err, results) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return;
|
||||
}
|
||||
|
||||
async.each(results, function (badge, callback) {
|
||||
var context = {
|
||||
system: badge.systemSlug,
|
||||
badge: badge.badgeSlug
|
||||
};
|
||||
badgekit.getBadgeInstances(context, function (err, instances) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
var latestTimestamp = null;
|
||||
|
||||
async.each(instances, function (instance, innerCallback) {
|
||||
var issuedOn = new Date(instance.issuedOn);
|
||||
|
||||
if (latestTimestamp === null || latestTimestamp < issuedOn) {
|
||||
latestTimestamp = issuedOn;
|
||||
}
|
||||
|
||||
if (issuedOn <= badge.lastPolled) {
|
||||
// if this badge's issuedOn timestamp is earlier than the latest timestamp we've already polled, skip it, as we've already seen it
|
||||
return innerCallback();
|
||||
}
|
||||
|
||||
var entry = {
|
||||
logged_by: 'badgekit-api',
|
||||
contributor_id: instance.email,
|
||||
contribution_date: issuedOn,
|
||||
moz_team: badge.moz_team,
|
||||
data_bucket: badge.data_bucket,
|
||||
description: badge.description,
|
||||
evidence: instance.assertionUrl
|
||||
};
|
||||
|
||||
data.saveItem(entry, function (err, res) {
|
||||
return innerCallback(err);
|
||||
});
|
||||
},
|
||||
function (err) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if (latestTimestamp === null) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
data.updateBadgePollTime(badge.id, new Date(latestTimestamp), function (err) {
|
||||
return callback(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
function (err) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return;
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
35
lib/data.js
35
lib/data.js
|
@ -247,3 +247,38 @@ exports.deleteItem = function saveItem(toDelete, callback) {
|
|||
});
|
||||
});
|
||||
};
|
||||
|
||||
exports.getContributorBadges = function getContributorBadges(callback) {
|
||||
var connection = mysql.createConnection(connectionOptions);
|
||||
connection.connect(function connectionAttempted(err) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
} else {
|
||||
/*jshint multistr: true */
|
||||
var query = 'SELECT id, systemSlug, badgeSlug, moz_team, data_bucket, description, lastPolled FROM contributorBadges';
|
||||
connection.query(query, function (err, result) {
|
||||
connection.end();
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
return callback(null, result);
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
exports.updateBadgePollTime = function updateBadgePollTime(id, timestamp, callback) {
|
||||
var connection = mysql.createConnection(connectionOptions);
|
||||
connection.connect(function connectionAttempted(err) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
} else {
|
||||
/*jshint multistr: true */
|
||||
var query = 'UPDATE contributorBadges SET lastPolled=? WHERE id=?';
|
||||
connection.query(query, [timestamp, id], function (err, result) {
|
||||
connection.end();
|
||||
return callback(err);
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
|
|
@ -31,7 +31,8 @@
|
|||
"express-persona": "~0.1.1",
|
||||
"express3-handlebars": "~0.5.0",
|
||||
"express-sslify": "0.0.1",
|
||||
"helmet": "~0.2.1"
|
||||
"helmet": "~0.2.1",
|
||||
"badgekit-api-client": "https://github.com/mozilla/badgekit-api-client/tarball/v0.2.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"grunt-jsbeautifier": "~0.2.6",
|
||||
|
|
|
@ -11,3 +11,9 @@ DB_PORT=3306
|
|||
DB_NAME=''
|
||||
#DB_SSL='Amazon RDS'
|
||||
## 'Amazon RDS' SSL profile is bundled in mysql module
|
||||
|
||||
# badgekit-api settings
|
||||
BADGEKIT_API_URL='http://api.badgekit.org'
|
||||
BADGEKIT_API_KEY='master'
|
||||
BADGEKIT_API_SECRET='somesecret'
|
||||
BADGEKIT_API_POLLING_FREQUENCY_MINS=15
|
||||
|
|
|
@ -9,3 +9,14 @@ CREATE TABLE `contributions` (
|
|||
`evidence` varchar(2000) DEFAULT NULL,
|
||||
UNIQUE KEY `no_dupes` (`contributor_id`,`contribution_date`,`moz_team`,`data_bucket`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
|
||||
CREATE TABLE `contributorBadges` (
|
||||
`id` int AUTO_INCREMENT PRIMARY KEY,
|
||||
`systemSlug` varchar(255) NOT NULL,
|
||||
`badgeSlug` varchar(255) NOT NULL,
|
||||
`moz_team` varchar(45) NOT NULL,
|
||||
`data_bucket` varchar(45) NOT NULL,
|
||||
`description` varchar(255) NOT NULL,
|
||||
`lastPolled` TIMESTAMP NULL,
|
||||
UNIQUE KEY `no_dupes` (`systemSlug`, `badgeSlug`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
5
web.js
5
web.js
|
@ -7,6 +7,7 @@ var logfmt = require("logfmt");
|
|||
var util = require("./lib/util");
|
||||
var logic = require("./lib/logic");
|
||||
var data = require("./lib/data");
|
||||
var pollBadgekitApi = require("./lib/badgekit-poll.js");
|
||||
var enforce = require('express-sslify');
|
||||
var helmet = require('helmet');
|
||||
var https = require('https');
|
||||
|
@ -264,3 +265,7 @@ app.configure('development', function () {
|
|||
console.log("Express server listening on port " + port);
|
||||
});
|
||||
});
|
||||
|
||||
if (process.env.BADGEKIT_API_POLLING_FREQUENCY_MINS) {
|
||||
setInterval(pollBadgekitApi, process.env.BADGEKIT_API_POLLING_FREQUENCY_MINS * 60 * 1000);
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче