mirror of
https://github.com/strapi/strapi.git
synced 2025-11-11 07:39:16 +00:00
Init sender and middleware, ping
Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
parent
6e3de95656
commit
6f2103a402
@ -145,9 +145,7 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// First, check if their is at least one admin
|
// First, check if their is at least one admin
|
||||||
const admins = await strapi
|
const admins = await strapi.query('administrator', 'admin').find({ _limit: 1 });
|
||||||
.query('administrator', 'admin')
|
|
||||||
.find({ _limit: 1 });
|
|
||||||
|
|
||||||
if (admins.length > 0) {
|
if (admins.length > 0) {
|
||||||
return ctx.badRequest(
|
return ctx.badRequest(
|
||||||
@ -159,9 +157,7 @@ module.exports = {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
params.password = await strapi.admin.services.auth.hashPassword(
|
params.password = await strapi.admin.services.auth.hashPassword(params.password);
|
||||||
params.password
|
|
||||||
);
|
|
||||||
|
|
||||||
const admin = await strapi.query('administrator', 'admin').findOne({
|
const admin = await strapi.query('administrator', 'admin').findOne({
|
||||||
email: params.email,
|
email: params.email,
|
||||||
@ -184,7 +180,7 @@ module.exports = {
|
|||||||
|
|
||||||
const jwt = strapi.admin.services.auth.createJwtToken(admin);
|
const jwt = strapi.admin.services.auth.createJwtToken(admin);
|
||||||
|
|
||||||
strapi.emit('didCreateFirstAdmin');
|
await strapi.telemetry.send('didCreateFirstAdmin');
|
||||||
|
|
||||||
ctx.send({
|
ctx.send({
|
||||||
jwt,
|
jwt,
|
||||||
@ -300,9 +296,7 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Find the admin thanks to his email.
|
// Find the admin thanks to his email.
|
||||||
const admin = await strapi
|
const admin = await strapi.query('administrator', 'admin').findOne({ email });
|
||||||
.query('administrator', 'admin')
|
|
||||||
.findOne({ email });
|
|
||||||
|
|
||||||
// admin not found.
|
// admin not found.
|
||||||
if (!admin) {
|
if (!admin) {
|
||||||
@ -355,9 +349,7 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update the admin.
|
// Update the admin.
|
||||||
await strapi
|
await strapi.query('administrator', 'admin').update({ id: admin.id }, { resetPasswordToken });
|
||||||
.query('administrator', 'admin')
|
|
||||||
.update({ id: admin.id }, { resetPasswordToken });
|
|
||||||
|
|
||||||
ctx.send({ ok: true });
|
ctx.send({ ok: true });
|
||||||
},
|
},
|
||||||
|
|||||||
@ -111,7 +111,7 @@ module.exports = {
|
|||||||
ctx.body = await contentManagerService.create(ctx.request.body, { model });
|
ctx.body = await contentManagerService.create(ctx.request.body, { model });
|
||||||
}
|
}
|
||||||
|
|
||||||
strapi.emit('didCreateFirstContentTypeEntry', { model });
|
await strapi.telemetry.send('didCreateFirstContentTypeEntry', { model });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
strapi.log.error(error);
|
strapi.log.error(error);
|
||||||
ctx.badRequest(null, [
|
ctx.badRequest(null, [
|
||||||
|
|||||||
@ -72,9 +72,9 @@ module.exports = {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (_.isEmpty(strapi.api)) {
|
if (_.isEmpty(strapi.api)) {
|
||||||
strapi.emit('didCreateFirstContentType');
|
await strapi.telemetry.send('didCreateFirstContentType');
|
||||||
} else {
|
} else {
|
||||||
strapi.emit('didCreateContentType');
|
await strapi.telemetry.send('didCreateContentType');
|
||||||
}
|
}
|
||||||
|
|
||||||
setImmediate(() => strapi.reload());
|
setImmediate(() => strapi.reload());
|
||||||
@ -82,7 +82,7 @@ module.exports = {
|
|||||||
ctx.send({ data: { uid: component.uid } }, 201);
|
ctx.send({ data: { uid: component.uid } }, 201);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
strapi.log.error(error);
|
strapi.log.error(error);
|
||||||
strapi.emit('didNotCreateContentType', error);
|
await strapi.telemetry.send('didNotCreateContentType', { error: error.message });
|
||||||
ctx.send({ error: error.message }, 400);
|
ctx.send({ error: error.message }, 400);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@ -30,7 +30,7 @@ module.exports = function createComponentBuilder() {
|
|||||||
/**
|
/**
|
||||||
* create a component in the tmpComponent map
|
* create a component in the tmpComponent map
|
||||||
*/
|
*/
|
||||||
createComponent(infos) {
|
async createComponent(infos) {
|
||||||
const uid = this.createComponentUID(infos);
|
const uid = this.createComponentUID(infos);
|
||||||
|
|
||||||
if (this.components.has(uid)) {
|
if (this.components.has(uid)) {
|
||||||
@ -62,9 +62,9 @@ module.exports = function createComponentBuilder() {
|
|||||||
.setAttributes(this.convertAttributes(infos.attributes));
|
.setAttributes(this.convertAttributes(infos.attributes));
|
||||||
|
|
||||||
if (this.components.size === 0) {
|
if (this.components.size === 0) {
|
||||||
strapi.emit('didCreateFirstComponent');
|
strapi.telemetry.send('didCreateFirstComponent');
|
||||||
} else {
|
} else {
|
||||||
strapi.emit('didCreateComponent');
|
strapi.telemetry.send('didCreateComponent');
|
||||||
}
|
}
|
||||||
|
|
||||||
this.components.set(uid, handler);
|
this.components.set(uid, handler);
|
||||||
|
|||||||
@ -3,44 +3,102 @@
|
|||||||
* Strapi telemetry package.
|
* Strapi telemetry package.
|
||||||
* You can learn more at https://strapi.io/documentation/3.0.0-beta.x/global-strapi/usage-information.html#commitment-to-our-users-data-collection
|
* You can learn more at https://strapi.io/documentation/3.0.0-beta.x/global-strapi/usage-information.html#commitment-to-our-users-data-collection
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const fetch = require('node-fetch');
|
|
||||||
const os = require('os');
|
const os = require('os');
|
||||||
|
const isDocker = require('is-docker');
|
||||||
|
const { machineIdSync } = require('node-machine-id');
|
||||||
|
const fetch = require('node-fetch');
|
||||||
|
const ciEnv = require('ci-info');
|
||||||
|
const scheduleJob = require('node-schedule');
|
||||||
|
|
||||||
const sendEvent = async (event, payload) => {
|
const createTelemetryInstance = strapi => {
|
||||||
try {
|
const uuid = strapi.config.uuid;
|
||||||
const res = await fetch('https://analytics.strapi.io/track', {
|
const deviceId = machineIdSync();
|
||||||
method: 'POST',
|
|
||||||
body: JSON.stringify({
|
|
||||||
event,
|
|
||||||
...payload,
|
|
||||||
}),
|
|
||||||
timeout: 1000,
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
});
|
|
||||||
|
|
||||||
return res.ok;
|
const isDisabled = !uuid;
|
||||||
} catch (err) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const createTelemetryInstance = () => {
|
const anonymous_metadata = {
|
||||||
return {
|
environment: strapi.config.environment,
|
||||||
middleware: ctx => {},
|
os: os.type(),
|
||||||
track(event) {
|
osPlatform: os.platform(),
|
||||||
return sendEvent(event, {
|
osRelease: os.release(),
|
||||||
uuid: process.env.STRAPI_UUID,
|
nodeVersion: process.version,
|
||||||
deviceId: process.env.DEVICE_ID,
|
docker: process.env.DOCKER || isDocker(),
|
||||||
properties: {
|
isCI: ciEnv.isCI,
|
||||||
os: os.type(),
|
version: strapi.config.info.strapi,
|
||||||
os_platform: os.platform(),
|
strapiVersion: strapi.config.info.strapi,
|
||||||
os_release: os.release(),
|
};
|
||||||
node_version: process.version,
|
|
||||||
version: process.env.STRAPI_VERSION,
|
const sendEvent = async (event, payload) => {
|
||||||
docker: process.env.DOCKER,
|
// do not send anything when user has disabled analytics
|
||||||
},
|
if (isDisabled) return true;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch('https://analytics.strapi.io/track', {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({
|
||||||
|
event,
|
||||||
|
uuid,
|
||||||
|
deviceId,
|
||||||
|
properties: {
|
||||||
|
...payload,
|
||||||
|
...anonymous_metadata,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
timeout: 1000,
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return res.ok;
|
||||||
|
} catch (err) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const _state = {
|
||||||
|
currentDay: null,
|
||||||
|
counter: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
initPing() {
|
||||||
|
if (isDisabled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
scheduleJob('0 0 12 * * *', () => sendEvent('ping'));
|
||||||
|
},
|
||||||
|
middleware: async (ctx, next) => {
|
||||||
|
if (isDisabled) {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
|
const { url, method } = ctx.request;
|
||||||
|
|
||||||
|
if (!url.includes('.') && ['GET', 'PUT', 'POST', 'DELETE'].includes(method)) {
|
||||||
|
const dayOfMonth = new Date().getDate();
|
||||||
|
|
||||||
|
if (dayOfMonth !== _state.currentDay) {
|
||||||
|
_state.currentDay = dayOfMonth;
|
||||||
|
_state.counter = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send max. 1000 events per day.
|
||||||
|
if (_state.counter < 1000) {
|
||||||
|
await sendEvent('didReceiveRequest', { url: ctx.request.url });
|
||||||
|
|
||||||
|
// Increase counter.
|
||||||
|
_state.counter++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await next();
|
||||||
|
},
|
||||||
|
async send(event, properties) {
|
||||||
|
if (isDisabled) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
await sendEvent(event, properties);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@ -220,7 +220,7 @@ class Strapi extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Emit started event.
|
// Emit started event.
|
||||||
this.emit('server:started');
|
await this.telemetry.send('didStartServer');
|
||||||
|
|
||||||
if (cb && typeof cb === 'function') {
|
if (cb && typeof cb === 'function') {
|
||||||
cb();
|
cb();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user