From 200f179b5f9078a45526746f7ec79a11ad08dbf4 Mon Sep 17 00:00:00 2001 From: Toby Liddicoat <toby@codesure.co.uk> Date: Wed, 26 Feb 2025 14:57:50 +0000 Subject: [PATCH] Refactor GovNotify to enhance API key handling and messaging. Introduced a toggle in the UI to securely display or edit the GOV Notify API key. Updated the backend to include dynamic subject lines and timestamps in notifications to improve clarity and contextual information for recipients. Signed-off-by: Toby Liddicoat <toby@codesure.co.uk> --- server/notification-providers/gov-notify.js | 24 +++++++++++-- src/components/notifications/GovNotify.vue | 38 ++++++++++++++++++--- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/server/notification-providers/gov-notify.js b/server/notification-providers/gov-notify.js index aa49ac903..1fb4188ba 100644 --- a/server/notification-providers/gov-notify.js +++ b/server/notification-providers/gov-notify.js @@ -1,4 +1,5 @@ const NotificationProvider = require("./notification-provider"); +const { DOWN } = require("../../src/util"); const NotifyClient = require("notifications-node-client").NotifyClient; class GovNotify extends NotificationProvider { @@ -13,19 +14,38 @@ class GovNotify extends NotificationProvider { const smsRecipients = (typeof notification.smsRecipients === "string" && notification.smsRecipients.trim()) ? notification.smsRecipients.split(",").map(n => n.trim()).filter(n => n) : []; - const message = notification.messageTemplate || msg; + let message = notification.messageTemplate || msg; const emailTemplateID = notification.emailTemplateId; const smsTemplateID = notification.smsTemplateId; const notifyClient = new NotifyClient(apiKey); + let subject = "⚠️ Test"; + + if (heartbeatJSON !== null) { + subject = (heartbeatJSON["status"] === DOWN) ? "🔴 Down" : "✅ Up"; + } + + const date = new Date(); + const day = date.getDate(); + const month = date.getMonth() + 1; + const year = date.getFullYear(); + const hours = date.getHours(); + const minutes = date.getMinutes(); + + const readableDate = `GMT ${day}/${month}/${year} ${hours}:${minutes}`; + message += `\n${readableDate}`; + // Send Emails for (const email of emailRecipients) { await notifyClient.sendEmail( emailTemplateID, email, { - personalisation: { message }, + personalisation: { + message, + subject, + }, reference: "Uptime-Kuma" }); } diff --git a/src/components/notifications/GovNotify.vue b/src/components/notifications/GovNotify.vue index d1b5a85a0..d377dae1e 100644 --- a/src/components/notifications/GovNotify.vue +++ b/src/components/notifications/GovNotify.vue @@ -1,11 +1,25 @@ <template> <div class="mb-3"> <label class="form-label">GOV Notify API Key</label> - <input - v-model="$parent.notification.apiKey" - type="text" - class="form-control" - /> + <div class="input-group"> + <input + v-if="!showApiKey" + type="text" + class="form-control" + value="************" + disabled + /> + <input + v-else + v-model="newApiKey" + type="text" + class="form-control" + placeholder="Enter new API key" + /> + <button class="btn btn-outline-secondary" type="button" @click="toggleApiKey"> + {{ showApiKey ? "Cancel" : "Change" }} + </button> + </div> </div> <div class="mb-3"> <label class="form-label">Email Recipients (comma-separated)</label> @@ -43,5 +57,19 @@ </template> <script> export default { + data() { + return { + showApiKey: false, + newApiKey: "", + }; + }, + methods: { + toggleApiKey() { + if (this.showApiKey) { + this.newApiKey = ""; + } + this.showApiKey = !this.showApiKey; + }, + }, }; </script>