Commit ba01a1b1 authored by Nicolas Frandeboeuf's avatar Nicolas Frandeboeuf Committed by Benjamin Bouvier
Browse files

Do not try to send a notification if there is no notifier/emailer set (fix #978)

parent 5312a660
......@@ -18,12 +18,19 @@ import {
} from '../helpers';
function postSave(userId: number, key: string, value: string) {
let notifier = null;
switch (key) {
case 'email-recipient':
getEmailer().forceReinit(value);
notifier = getEmailer();
if (notifier !== null) {
notifier.forceReinit(value);
}
break;
case 'apprise-url':
getNotifier(userId).forceReinit(value);
notifier = getNotifier(userId);
if (notifier !== null) {
notifier.forceReinit(value);
}
break;
case 'locale':
setupTranslator(value);
......@@ -86,7 +93,13 @@ export async function testEmail(req: express.Request, res: express.Response) {
if (!email) {
throw new KError('Missing email recipient address when sending a test email', 400);
}
await getEmailer().sendTestEmail(email);
const emailer = getEmailer();
if (emailer !== null) {
await emailer.sendTestEmail(email);
} else {
throw new KError('No emailer found');
}
res.status(200).end();
} catch (err) {
asyncErr(res, err, 'when trying to send an email');
......
......@@ -22,18 +22,32 @@ ${$t('server.email.signature')}
userId: number,
{ subject, text }: { subject: string; text: string }
): Promise<void> {
await getNotifier(userId).send(subject, text);
let notificationsSent = false;
const notifier = getNotifier(userId);
if (notifier !== null) {
await notifier.send(subject, text);
notificationsSent = true;
}
const emailer = getEmailer();
if (emailer !== null) {
// Send email notification
const content = this.wrapContent(text);
const fullSubject = `Kresus - ${subject}`;
// Send email notification
const content = this.wrapContent(text);
const fullSubject = `Kresus - ${subject}`;
await emailer.sendToUser(userId, {
subject: fullSubject,
content,
});
await getEmailer().sendToUser(userId, {
subject: fullSubject,
content,
});
notificationsSent = true;
}
log.info('Notification sent.');
if (notificationsSent) {
log.info('Notification sent.');
} else {
log.info('No notifier or email found, no notification sent.');
}
}
async checkAlertsForOperations(
......@@ -42,6 +56,14 @@ ${$t('server.email.signature')}
operations: Transaction[]
): Promise<void> {
try {
const notifier = getNotifier(userId);
const emailer = getEmailer();
if (notifier === null && emailer === null) {
log.info('No notifier or emailer found, skipping transactions alerts check.');
return;
}
// Map account to names
const accessLabel = access.getLabel();
const accounts = await Account.byAccess(userId, access);
......@@ -101,6 +123,14 @@ ${$t('server.email.signature')}
async checkAlertsForAccounts(userId: number, access: Access): Promise<void> {
try {
const notifier = getNotifier(userId);
const emailer = getEmailer();
if (notifier === null && emailer === null) {
log.info('No notifier or emailer found, accounts alerts check.');
return;
}
const accounts = await Account.byAccess(userId, access);
const accessLabel = access.getLabel();
for (const account of accounts) {
......
......@@ -152,8 +152,8 @@ class Emailer {
}
let EMAILER: Emailer | null = null;
function getEmailer(): Emailer {
if (EMAILER === null) {
function getEmailer(): Emailer | null {
if (EMAILER === null && isEmailEnabled()) {
EMAILER = new Emailer();
}
return EMAILER;
......
......@@ -16,10 +16,8 @@ interface SendOptions {
class Notifier {
appriseApiBaseUrl: string | null;
enabled: boolean;
constructor() {
this.enabled = isAppriseApiEnabled();
this.appriseApiBaseUrl =
process.kresus.appriseApiBaseUrl !== null
? resolve(process.kresus.appriseApiBaseUrl, '/notify')
......@@ -31,7 +29,7 @@ class Notifier {
log.warn(`Notification: Subject: ${opts.subject}; Content: ${opts.content}`);
}
if (!this.enabled) {
if (!isAppriseApiEnabled()) {
log.warn("AppriseApiBaseUrl is missing: notifications won't work.");
return;
}
......@@ -68,8 +66,8 @@ class Notifier {
}
let NOTIFIER: Notifier | null = null;
function _getBaseNotifier(): Notifier {
if (NOTIFIER === null) {
function _getBaseNotifier(): Notifier | null {
if (NOTIFIER === null && isAppriseApiEnabled()) {
NOTIFIER = new Notifier();
}
return NOTIFIER;
......@@ -106,21 +104,30 @@ class UserNotifier {
if (!content) {
return log.warn('Notifier.send misuse: content is required');
}
await _getBaseNotifier()._send({ subject, content, appriseUrl: this.appriseUserUrl });
const notifier = _getBaseNotifier();
if (notifier) {
notifier._send({ subject, content, appriseUrl: this.appriseUserUrl });
} else {
assert(false, 'Notifier.send misuse: no notifier available');
}
}
}
const NOTIFIER_PER_USER_ID: { [k: string]: UserNotifier } = {};
function getNotifier(userId: number): UserNotifier {
if (!(userId in NOTIFIER_PER_USER_ID)) {
function getNotifier(userId: number): UserNotifier | null {
if (isAppriseApiEnabled() && !(userId in NOTIFIER_PER_USER_ID)) {
log.info(`Notifier initialized for user ${userId}`);
NOTIFIER_PER_USER_ID[userId] = new UserNotifier(userId);
}
return NOTIFIER_PER_USER_ID[userId];
return NOTIFIER_PER_USER_ID[userId] || null;
}
export async function sendTestNotification(appriseUrl: string): Promise<void> {
return _getBaseNotifier().sendTestNotification(appriseUrl);
const notifier = _getBaseNotifier();
if (notifier) {
await notifier.sendTestNotification(appriseUrl);
}
}
export default getNotifier;
......@@ -24,15 +24,25 @@ const MIN_DURATION_BETWEEN_REPORTS =
class ReportManager {
async sendReport(userId: number, subject: string, content: string): Promise<void> {
await getEmailer().sendToUser(userId, {
subject,
content,
});
log.info('Report sent.');
const emailer = getEmailer();
if (emailer !== null) {
await emailer.sendToUser(userId, {
subject,
content,
});
log.info('Report sent.');
}
}
async manageReports(userId: number): Promise<void> {
try {
const emailer = getEmailer();
if (emailer === null) {
log.info('No emailer found, skipping reports management.');
return;
}
const now = new Date();
await this.prepareReport(userId, 'daily');
// getDay is indexed from 0, meaning Sunday.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment