2020-10-09 16:08:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// Pushes events to processing queue
|
|
|
|
|
|
|
|
const Queue = require('bull');
|
|
|
|
const log = require('npmlog');
|
|
|
|
|
|
|
|
let webhooksQueue;
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
DKIM_CREATED: 'dkim.created',
|
|
|
|
DKIM_UPDATED: 'dkim.updated',
|
|
|
|
DKIM_DELETED: 'dkim.deleted',
|
|
|
|
DOMAINALIAS_CREATED: 'domainalias.created',
|
|
|
|
DOMAINALIAS_DELETED: 'domainalias.deleted',
|
|
|
|
ADDRESS_USER_CREATED: 'address.user.created',
|
|
|
|
ADDRESS_USER_DELETED: 'address.user.deleted',
|
|
|
|
ADDRESS_FORWARDED_CREATED: 'address.forwarded.created',
|
|
|
|
ADDRESS_FORWARDED_DELETED: 'address.forwarded.deleted',
|
|
|
|
ADDRESS_DOMAIN_RENAMED: 'address.domain.renamed',
|
|
|
|
FILTER_DELETED: 'filter.deleted',
|
|
|
|
FILTER_CREATED: 'filter.created',
|
|
|
|
ASP_CREATED: 'asp.created',
|
|
|
|
ASP_DELETED: 'asp.deleted',
|
|
|
|
USER_CREATED: 'user.created',
|
|
|
|
USER_PASSWORD_CHANGED: 'user.password.changed',
|
2021-01-08 16:31:43 +08:00
|
|
|
USER_DELETE_STARTED: 'user.delete.started',
|
|
|
|
USER_DELETE_COMPLETED: 'user.delete.completed',
|
2021-01-22 21:52:35 +08:00
|
|
|
USER_DELETE_CANCELLED: 'user.delete.cancelled',
|
2020-10-09 16:08:33 +08:00
|
|
|
AUTOREPLY_USER_ENABLED: 'autoreply.user.enabled',
|
|
|
|
AUTOREPLY_USER_DISABLED: 'autoreply.user.disabled',
|
|
|
|
MFA_TOTP_ENABLED: 'mfa.totp.enabled',
|
|
|
|
MFA_TOTP_DISABLED: 'mfa.totp.disabled',
|
|
|
|
MFA_CUSTOM_ENABLED: 'mfa.custom.enabled',
|
|
|
|
MFA_CUSTOM_DISABLED: 'mfa.custom.disabled',
|
|
|
|
MFA_U2F_ENABLED: 'mfa.u2f.enabled',
|
|
|
|
MFA_U2F_DISABLED: 'mfa.u2f.disabled',
|
|
|
|
MFA_DISABLED: 'mfa.disabled',
|
|
|
|
MAILBOX_CREATED: 'mailbox.created',
|
|
|
|
MAILBOX_RENAMED: 'mailbox.renamed',
|
|
|
|
MAILBOX_DELETED: 'mailbox.deleted',
|
2021-02-26 20:00:13 +08:00
|
|
|
MARKED_SPAM: 'marked.spam',
|
|
|
|
MARKED_HAM: 'marked.ham',
|
2020-10-09 16:08:33 +08:00
|
|
|
|
|
|
|
async publish(redisClient, data) {
|
|
|
|
if (!data || typeof data !== 'object' || !redisClient) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!webhooksQueue) {
|
|
|
|
webhooksQueue = new Queue('webhooks', {
|
|
|
|
createClient: (type /*, config*/) => {
|
|
|
|
if (type === 'bclient') {
|
|
|
|
// most probably never called
|
|
|
|
return redisClient.duplicate();
|
|
|
|
}
|
|
|
|
return redisClient;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
data = Object.assign({ time: Date.now() }, data);
|
|
|
|
Object.keys(data).forEach(key => {
|
|
|
|
if (data[key] && typeof data[key] === 'object' && typeof data[key].toHexString === 'function') {
|
|
|
|
// convert ObjectId to string
|
|
|
|
data[key] = data[key].toHexString();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
let job = await webhooksQueue.add(data, {
|
|
|
|
removeOnComplete: true,
|
|
|
|
removeOnFail: 500,
|
|
|
|
attempts: 5,
|
|
|
|
backoff: {
|
|
|
|
type: 'exponential',
|
|
|
|
delay: 2000
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return job;
|
|
|
|
} catch (err) {
|
|
|
|
// ignore?
|
|
|
|
log.error('Events', err);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|