mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-24 14:43:08 +03:00
6161f94910
refs: https://github.com/TryGhost/Toolbox/issues/595 We're rolling out new rules around the node assert library, the first of which is enforcing the use of assert/strict. This means we don't need to use the strict version of methods, as the standard version will work that way by default. This caught some gotchas in our existing usage of assert where the lack of strict mode had unexpected results: - Url matching needs to be done on `url.href` seeaa58b354a4
- Null and undefined are not the same thing, there were a few cases of this being confused - Particularly questionable changes in [PostExporter tests](c1a468744b
) tracked [here](https://github.com/TryGhost/Team/issues/3505). - A typo seeeaac9c293a
Moving forward, using assert strict should help us to catch unexpected behaviour, particularly around nulls and undefineds during implementation.
125 lines
2.7 KiB
JavaScript
125 lines
2.7 KiB
JavaScript
const assert = require('assert/strict');
|
|
|
|
/**
|
|
* @typedef {object} EmailSuppressionInfo
|
|
* @prop {'spam' | 'failed'} reason
|
|
* @prop {Date} timestamp
|
|
*/
|
|
|
|
/**
|
|
* @typedef {object} EmailSuppressedData
|
|
* @prop {true} suppressed
|
|
* @prop {EmailSuppressionInfo} info
|
|
*/
|
|
|
|
/**
|
|
* @typedef {object} EmailNotSuppressedData
|
|
* @prop {false} suppressed
|
|
* @prop {null} info
|
|
*/
|
|
|
|
/**
|
|
* @typedef {EmailSuppressedData | EmailNotSuppressedData} IEmailSuppressionData
|
|
*/
|
|
|
|
/**
|
|
* @typedef {object} IEmailSuppressionList
|
|
* @prop {(email: string) => Promise<EmailSuppressionData>} getSuppressionData
|
|
* @prop {(emails: string[]) => Promise<EmailSuppressionData[]>} getBulkSuppressionData
|
|
* @prop {(email: string) => Promise<boolean>} removeEmail
|
|
*/
|
|
|
|
/**
|
|
* @implements {IEmailSuppressionData}
|
|
*/
|
|
class EmailSuppressionData {
|
|
/** @type {boolean} */
|
|
suppressed;
|
|
/** @type {EmailSuppressionInfo | null} */
|
|
info;
|
|
|
|
constructor(suppressed, info) {
|
|
if (!suppressed) {
|
|
this.suppressed = false;
|
|
this.info = null;
|
|
} else {
|
|
this.suppressed = true;
|
|
assert(info.reason === 'spam' || info.reason === 'fail');
|
|
assert(info.timestamp instanceof Date);
|
|
this.info = {
|
|
reason: info.reason,
|
|
timestamp: info.timestamp
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @abstract
|
|
* @implements {IEmailSuppressionList}
|
|
*/
|
|
class AbstractEmailSuppressionList {
|
|
/**
|
|
* @param {string} email
|
|
* @returns {Promise<boolean>}
|
|
*/
|
|
async removeEmail(email) { // eslint-disable-line
|
|
return Promise.reject();
|
|
}
|
|
|
|
/**
|
|
* @param {string} email
|
|
* @returns {Promise<EmailSuppressionData>}
|
|
*/
|
|
async getSuppressionData(email) { // eslint-disable-line
|
|
return Promise.reject();
|
|
}
|
|
|
|
/**
|
|
* @param {string[]} emails
|
|
* @returns {Promise<EmailSuppressionData[]>}
|
|
*/
|
|
async getBulkSuppressionData(emails) {
|
|
return Promise.all(emails.map(email => this.getSuppressionData(email)));
|
|
}
|
|
}
|
|
|
|
class EmailSuppressedEvent {
|
|
/**
|
|
* @readonly
|
|
* @type {{emailId: string, emailAddress: string, reason: string}}
|
|
*/
|
|
data;
|
|
|
|
/**
|
|
* @readonly
|
|
* @type {Date}
|
|
*/
|
|
timestamp;
|
|
|
|
/**
|
|
* @private
|
|
*/
|
|
constructor({emailAddress, emailId, reason, timestamp}) {
|
|
this.data = {
|
|
emailAddress,
|
|
emailId,
|
|
reason
|
|
};
|
|
this.timestamp = timestamp;
|
|
}
|
|
|
|
static create(data, timestamp) {
|
|
return new EmailSuppressedEvent({
|
|
...data,
|
|
timestamp: timestamp || new Date
|
|
});
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
AbstractEmailSuppressionList,
|
|
EmailSuppressionData,
|
|
EmailSuppressedEvent
|
|
};
|