2019-06-07 14:10:44 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
const nconf = require('nconf');
|
|
|
|
|
const winston = require('winston');
|
|
|
|
|
const crypto = require('crypto');
|
|
|
|
|
const cronJob = require('cron').CronJob;
|
|
|
|
|
|
2023-12-18 12:08:34 -05:00
|
|
|
const request = require('../request');
|
2019-06-07 14:10:44 -04:00
|
|
|
const pkg = require('../../package.json');
|
|
|
|
|
|
|
|
|
|
const meta = require('../meta');
|
|
|
|
|
|
|
|
|
|
module.exports = function (Plugins) {
|
|
|
|
|
Plugins.startJobs = function () {
|
2023-12-18 12:08:34 -05:00
|
|
|
new cronJob('0 0 0 * * *', (async () => {
|
|
|
|
|
await Plugins.submitUsageData();
|
2021-02-04 00:01:39 -07:00
|
|
|
}), null, true);
|
2019-06-07 14:10:44 -04:00
|
|
|
};
|
|
|
|
|
|
2023-12-18 12:08:34 -05:00
|
|
|
Plugins.submitUsageData = async function () {
|
2019-06-07 14:10:44 -04:00
|
|
|
if (!meta.config.submitPluginUsage || !Plugins.loadedPlugins.length || global.env !== 'production') {
|
2023-12-18 12:08:34 -05:00
|
|
|
return;
|
2019-06-07 14:10:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const hash = crypto.createHash('sha256');
|
|
|
|
|
hash.update(nconf.get('url'));
|
2023-12-18 12:08:34 -05:00
|
|
|
const url = `${nconf.get('registry') || 'https://packages.nodebb.org'}/api/v1/plugin/usage`;
|
|
|
|
|
try {
|
|
|
|
|
const { response, body } = await request.post(url, {
|
|
|
|
|
body: {
|
|
|
|
|
id: hash.digest('hex'),
|
|
|
|
|
version: pkg.version,
|
|
|
|
|
plugins: Plugins.loadedPlugins,
|
|
|
|
|
},
|
|
|
|
|
timeout: 5000,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
winston.error(`[plugins.submitUsageData] received ${response.status} ${body}`);
|
2019-06-07 14:10:44 -04:00
|
|
|
}
|
2023-12-18 12:08:34 -05:00
|
|
|
} catch (err) {
|
|
|
|
|
winston.error(err.stack);
|
|
|
|
|
}
|
2019-06-07 14:10:44 -04:00
|
|
|
};
|
|
|
|
|
};
|