From e59a1c753daa3226679df95501861780845a6c5e Mon Sep 17 00:00:00 2001 From: simatec Date: Tue, 15 Aug 2023 10:51:53 +0200 Subject: [PATCH] (simatec) Bugfix System-Message --- README.md | 1 + lib/systemCheck.js | 141 ++++++++++++++++++++++++++++++++------------- main.js | 4 +- 3 files changed, 104 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 552e2d70..eeb91cf3 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ When the adapter crashes or another Code error happens, this error message that ### **WORK IN PROGRESS** * (simatec) small Sentry fixes +* (simatec) Bugfix System-Message ### 2.7.0 (2023-08-14) * (simatec) dependencies updated diff --git a/lib/systemCheck.js b/lib/systemCheck.js index e1873717..1f038a4d 100644 --- a/lib/systemCheck.js +++ b/lib/systemCheck.js @@ -39,67 +39,128 @@ async function storageSizeCheck(options, adapterName, log) { }); } -function systemMessage(options, sendTo, sysMessage) { - if (options.notificationEnabled) { - switch (options.notificationsType) { +function systemMessage(options, sysMessage) { + if (options.config.notificationEnabled) { + switch (options.config.notificationsType) { case 'Telegram': - if (options.telegramUser && options.telegramUser === 'allTelegramUsers' && options.telegramInstance) { - sendTo(options.telegramInstance, 'send', { text: 'BackItUp:\n' + sysMessage, disable_notification: options.telegramSilentNotice }); - } else if (options.telegramInstance) { - sendTo(options.telegramInstance, 'send', { user: options.telegramUser, text: 'BackItUp:\n' + sysMessage, disable_notification: options.telegramSilentNotice }); + if (options.config.telegramUser && options.config.telegramUser === 'allTelegramUsers' && options.config.telegramInstance) { + try { + options.sendTo(options.config.telegramInstance, 'send', { + text: `BackItUp:\n${sysMessage}`, + disable_notification: options.config.telegramSilentNotice + }); + } catch (err) { + log.warn(`Error sending Telegram message: ${err}`) + } + } else if (options.config.telegramInstance) { + try { + options.sendTo(options.config.telegramInstance, 'send', { + user: options.config.telegramUser, + text: `BackItUp:\n${sysMessage}`, + disable_notification: options.config.telegramSilentNotice + }); + } catch (err) { + log.warn(`Error sending Telegram message: ${err}`) + } } break; case 'E-Mail': - if (options.emailInstance && options.emailReceiver && options.emailSender) { - sendTo(options.emailInstance, 'send', { text: 'BackItUp:\n' + sysMessage, to: options.emailReceiver, subject: 'Backitup', from: options.emailSender }); + if (options.config.emailInstance && options.config.emailReceiver && options.config.emailSender) { + try { + options.sendTo(options.config.emailInstance, 'send', { + text: `BackItUp:\n${sysMessage}`, + to: options.config.emailReceiver, + subject: 'Backitup', + from: options.config.emailSender + }); + } catch (err) { + log.warn(`Error sending E-Mail message: ${err}`) + } } break; case 'Pushover': - if ((options.pushoverSilentNotice === 'true' || options.pushoverSilentNotice === true) && options.pushoverInstance && options.pushoverDeviceID) { - sendTo(options.pushoverInstance, 'send', { message: 'BackItUp:\n' + sysMessage, sound: '', priority: -1, title: 'Backitup', device: options.pushoverDeviceID }); - } else if (options.pushoverInstance && options.pushoverDeviceID) { - sendTo(options.pushoverInstance, 'send', { message: 'BackItUp:\n' + sysMessage, sound: '', title: 'Backitup', device: options.pushoverDeviceID }); + if ((options.config.pushoverSilentNotice === 'true' || options.config.pushoverSilentNotice === true) && options.config.pushoverInstance && options.config.pushoverDeviceID) { + try { + options.sendTo(options.config.pushoverInstance, 'send', { + message: `BackItUp:\n${sysMessage}`, + sound: '', + priority: -1, + title: 'Backitup', + device: options.config.pushoverDeviceID + }); + } catch (err) { + log.warn(`Error sending Pushover message: ${err}`) + } + } else if (options.config.pushoverInstance && options.config.pushoverDeviceID) { + try { + options.sendTo(options.config.pushoverInstance, 'send', { + message: `BackItUp:\n${sysMessage}`, + sound: '', + title: 'Backitup', + device: options.config.pushoverDeviceID + }); + } catch (err) { + log.warn(`Error sending Pushover message: ${err}`) + } } break; case 'WhatsApp': - if (options.whatsappInstance) { - sendTo(options.whatsappInstance, 'send', { text: 'BackItUp:\n' + sysMessage }); + if (options.config.whatsappInstance) { + try { + options.sendTo(options.config.whatsappInstance, 'send', { + text: `BackItUp:\n${sysMessage}` + }); + } catch (err) { + log.warn(`Error sending WhatsApp message: ${err}`) + } } break; case 'Signal': - if (options.signalInstance) { - sendTo(options.signalInstance, 'send', { text: 'BackItUp:\n' + sysMessage }); + if (options.config.signalInstance) { + try { + options.sendTo(options.config.signalInstance, 'send', { + text: `BackItUp:\n${sysMessage}` + }); + } catch (err) { + log.warn(`Error sending Signal message: ${err}`) + } } break; case 'Matrix': - if (options.matrixInstance) { - sendTo(options.matrixInstance, 'BackItUp:\n' + sysMessage); + if (options.config.matrixInstance) { + try { + options.sendTo(options.config.matrixInstance, { + text: `BackItUp:\n${sysMessage}` + }); + } catch (err) { + log.warn(`Error sending Matrix message: ${err}`) + } } break; case 'Discord': - if (options.discordInstance && options.discordTarget) { - if (options.discordTarget.match(/^\d+$/)) { + if (options.config.discordInstance && options.config.discordTarget) { + if (options.config.discordTarget.match(/^\d+$/)) { // send to a single user - sendTo(options.discordInstance, 'sendMessage', { - userId: options.discordTarget, - content: '**BackItUp**:\n' + sysMessage, - }, (ret) => { - if (ret.err) { - log.warn(`Error sending Discord message: ${ret.err}`); - } - }); - } else if (options.discordTarget.match(/^\d+\/\d+$/)) { + try { + options.sendTo(options.config.discordInstance, 'sendMessage', { + userId: options.config.discordTarget, + content: `BackItUp:\n${sysMessage}`, + }); + } catch (err) { + log.warn(`Error sending Discord message: ${err}`) + } + } else if (options.config.discordTarget.match(/^\d+\/\d+$/)) { // send to a server channel - const [ serverId, channelId ] = options.discordTarget.split('/'); - sendTo(options.discordInstance, 'sendMessage', { - serverId, - channelId, - content: '**BackItUp**:\n' + sysMessage, - }, (ret) => { - if (ret.err) { - log.warn(`Error sending Discord message: ${ret.err}`); - } - }); + const [serverId, channelId] = options.config.discordTarget.split('/'); + try { + options.sendTo(options.config.discordInstance, 'sendMessage', { + serverId, + channelId, + content: `BackItUp:\n${sysMessage}`, + }); + } catch (err) { + log.warn(`Error sending Discord message: ${err}`) + } } } break; diff --git a/main.js b/main.js index 4f5633a2..a88f0899 100644 --- a/main.js +++ b/main.js @@ -143,7 +143,7 @@ function startAdapter(options) { }); } else { adapter.log.error(`A local backup is currently not possible. The storage space is currently only ${sysCheck && sysCheck.diskFree ? sysCheck.diskFree : null} MB`); - systemCheck.systemMessage(adapter.config, adapter.sendTo, tools._('A local backup is currently not possible. Please check your System!', systemLang)); + systemCheck.systemMessage(adapter, tools._('A local backup is currently not possible. Please check your System!', systemLang)); adapter.setState('oneClick.' + type, false, true); adapter.setState('output.line', `[EXIT] ${tools._('A local backup is currently not possible. Please check your System!', systemLang)}`, true); } @@ -565,7 +565,7 @@ function createBackupSchedule() { }); } else { adapter.log.error(`A local backup is currently not possible. The storage space is currently only ${sysCheck && sysCheck.diskFree ? sysCheck.diskFree : null} MB`); - systemCheck.systemMessage(adapter.config, adapter.sendTo, tools._('A local backup is currently not possible. Please check your System!', systemLang)); + systemCheck.systemMessage(adapter, tools._('A local backup is currently not possible. Please check your System!', systemLang)); } });