wildduck/lib/events.js

88 lines
2.8 KiB
JavaScript
Raw Normal View History

2020-10-09 16:08:33 +08:00
'use strict';
// Pushes events to processing queue
const { Queue } = require('bullmq');
2020-10-09 16:08:33 +08:00
const log = require('npmlog');
let webhooksQueue;
module.exports = {
DKIM_CREATED: 'dkim.created',
DKIM_UPDATED: 'dkim.updated',
DKIM_DELETED: 'dkim.deleted',
CERT_CREATED: 'cert.created',
CERT_UPDATED: 'cert.updated',
CERT_DELETED: 'cert.deleted',
2020-10-09 16:08:33 +08:00
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',
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',
2022-03-06 06:08:48 +08:00
MFA_FIDO_REGISTERED: 'mfa.webauthn.registered',
MFA_FIDO_REOVED: 'mfa.webauthn.removed',
2020-10-09 16:08:33 +08:00
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
2021-10-04 16:57:43 +08:00
FORWARD_ADDED: 'forward added',
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', {
connection: redisClient,
prefix: `wd:bull`
2020-10-09 16:08:33 +08:00
});
}
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('wehook', data, {
2020-10-09 16:08:33 +08:00
removeOnComplete: true,
removeOnFail: 500,
attempts: 5,
backoff: {
type: 'exponential',
delay: 2000
}
});
return job;
} catch (err) {
// ignore?
log.error('Events', err);
}
return false;
}
};