2023-02-17 13:59:18 +03:00
|
|
|
const got = require('got');
|
|
|
|
const validator = require('@tryghost/validator');
|
|
|
|
const errors = require('@tryghost/errors');
|
|
|
|
const ghostVersion = require('@tryghost/version');
|
|
|
|
const moment = require('moment');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {URL} webhookUrl
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {string} siteUrl
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {import('@tryghost/logging')} logging
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {import('./SlackNotificationsService').ISlackNotifications} ISlackNotifications
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @implements {ISlackNotifications}
|
|
|
|
*/
|
|
|
|
class SlackNotifications {
|
|
|
|
/** @type {URL} */
|
|
|
|
#webhookUrl;
|
|
|
|
|
|
|
|
/** @type {siteUrl} */
|
|
|
|
#siteUrl;
|
|
|
|
|
|
|
|
/** @type {logging} */
|
|
|
|
#logging;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {object} deps
|
|
|
|
* @param {URL} deps.webhookUrl
|
|
|
|
* @param {siteUrl} deps.siteUrl
|
|
|
|
* @param {logging} deps.logging
|
|
|
|
*/
|
|
|
|
constructor(deps) {
|
|
|
|
this.#siteUrl = deps.siteUrl;
|
|
|
|
this.#webhookUrl = deps.webhookUrl;
|
|
|
|
this.#logging = deps.logging;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {object} eventData
|
|
|
|
* @param {import('@tryghost/milestones/lib/InMemoryMilestoneRepository').Milestone} eventData.milestone
|
|
|
|
* @param {object} [eventData.meta]
|
2023-03-13 20:01:11 +03:00
|
|
|
* @param {'import'|'email'|'skipped'|'initial'} [eventData.meta.reason]
|
2023-02-23 17:18:39 +03:00
|
|
|
* @param {number} [eventData.meta.currentValue]
|
2023-02-17 13:59:18 +03:00
|
|
|
*
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
async notifyMilestoneReceived({milestone, meta}) {
|
2023-03-13 20:01:11 +03:00
|
|
|
if (meta?.reason === 'skipped' || meta?.reason === 'initial') {
|
|
|
|
return;
|
|
|
|
}
|
2023-02-17 13:59:18 +03:00
|
|
|
const hasImportedMembers = meta?.reason === 'import' ? 'has imported members' : null;
|
|
|
|
const lastEmailTooSoon = meta?.reason === 'email' ? 'last email too recent' : null;
|
2023-03-13 20:01:11 +03:00
|
|
|
const emailNotSentReason = hasImportedMembers || lastEmailTooSoon;
|
2023-02-17 13:59:18 +03:00
|
|
|
const milestoneTypePretty = milestone.type === 'arr' ? 'ARR' : 'Members';
|
|
|
|
const valueFormatted = this.#getFormattedAmount({amount: milestone.value, currency: milestone?.currency});
|
|
|
|
const emailSentText = milestone?.emailSentAt ? this.#getFormattedDate(milestone?.emailSentAt) : `no / ${emailNotSentReason}`;
|
|
|
|
const title = `:tada: ${milestoneTypePretty} Milestone ${valueFormatted} reached!`;
|
|
|
|
|
|
|
|
let valueSection;
|
|
|
|
|
|
|
|
if (milestone.type === 'arr') {
|
|
|
|
valueSection = {
|
|
|
|
type: 'section',
|
|
|
|
fields: [
|
|
|
|
{
|
|
|
|
type: 'mrkdwn',
|
|
|
|
text: `*Milestone:*\n${valueFormatted}`
|
|
|
|
}
|
|
|
|
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
2023-02-23 17:18:39 +03:00
|
|
|
if (meta?.currentValue) {
|
2023-02-17 13:59:18 +03:00
|
|
|
valueSection.fields.push({
|
|
|
|
type: 'mrkdwn',
|
2023-02-23 17:18:39 +03:00
|
|
|
text: `*Current ARR:*\n${this.#getFormattedAmount({amount: meta.currentValue, currency: milestone?.currency})}`
|
2023-02-17 13:59:18 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
valueSection = {
|
|
|
|
type: 'section',
|
|
|
|
fields: [
|
|
|
|
{
|
|
|
|
type: 'mrkdwn',
|
|
|
|
text: `*Milestone:*\n${valueFormatted}`
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
2023-02-23 17:18:39 +03:00
|
|
|
if (meta?.currentValue) {
|
2023-02-17 13:59:18 +03:00
|
|
|
valueSection.fields.push({
|
|
|
|
type: 'mrkdwn',
|
2023-02-23 17:18:39 +03:00
|
|
|
text: `*Current Members:*\n${this.#getFormattedAmount({amount: meta.currentValue})}`
|
2023-02-17 13:59:18 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const blocks = [
|
|
|
|
{
|
|
|
|
type: 'header',
|
|
|
|
text: {
|
|
|
|
type: 'plain_text',
|
|
|
|
text: title,
|
|
|
|
emoji: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'section',
|
|
|
|
text: {
|
|
|
|
type: 'mrkdwn',
|
|
|
|
text: `New *${milestoneTypePretty} Milestone* achieved for <${this.#siteUrl}|${this.#siteUrl}>`
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'divider'
|
|
|
|
},
|
|
|
|
valueSection,
|
|
|
|
{
|
|
|
|
type: 'section',
|
|
|
|
text: {
|
|
|
|
type: 'mrkdwn',
|
|
|
|
text: `*Email sent:*\n${emailSentText}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
const slackData = {
|
|
|
|
unfurl_links: false,
|
|
|
|
username: 'Ghost Milestone Service',
|
|
|
|
attachments: [
|
|
|
|
{
|
|
|
|
color: '#36a64f',
|
|
|
|
blocks
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
await this.send(slackData, this.#webhookUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {object} slackData
|
|
|
|
* @param {URL} url
|
|
|
|
*
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
|
|
|
async send(slackData, url) {
|
|
|
|
if ((!url || typeof url !== 'string') || !validator.isURL(url)) {
|
|
|
|
const err = new errors.InternalServerError({
|
|
|
|
message: 'URL empty or invalid.',
|
|
|
|
code: 'URL_MISSING_INVALID',
|
|
|
|
context: url
|
|
|
|
});
|
|
|
|
|
|
|
|
return this.#logging.error(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const requestOptions = {
|
|
|
|
body: JSON.stringify(slackData),
|
|
|
|
headers: {
|
|
|
|
'user-agent': 'Ghost/' + ghostVersion.original + ' (https://github.com/TryGhost/Ghost)'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-24 12:46:14 +03:00
|
|
|
if (process.env.NODE_ENV?.startsWith('test')) {
|
|
|
|
requestOptions.retry = 0;
|
|
|
|
}
|
|
|
|
|
2023-02-17 13:59:18 +03:00
|
|
|
return await got.post(url, requestOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {object} options
|
|
|
|
* @param {number} options.amount
|
|
|
|
* @param {string} [options.currency]
|
|
|
|
*
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
#getFormattedAmount({amount = 0, currency}) {
|
|
|
|
if (!currency) {
|
|
|
|
return Intl.NumberFormat().format(amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Intl.NumberFormat('en', {
|
|
|
|
style: 'currency',
|
|
|
|
currency,
|
|
|
|
currencyDisplay: 'symbol'
|
|
|
|
}).format(amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string|Date} date
|
|
|
|
*
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
#getFormattedDate(date) {
|
|
|
|
return moment(date).format('D MMM YYYY');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = SlackNotifications;
|