From 46775c3662b85c1ea77f3d44fba0ebaaeb3a6563 Mon Sep 17 00:00:00 2001 From: Daniel Linsenmeyer Date: Fri, 14 Apr 2023 17:16:08 +0200 Subject: [PATCH] Fix validation and add ntfy as notification adapter (#75) --- lib/notification/adapter/ntfy.js | 51 +++++++++++++++++++ lib/notification/adapter/ntfy.md | 5 ++ .../NotificationAdapterMutator.jsx | 9 ++-- 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 lib/notification/adapter/ntfy.js create mode 100644 lib/notification/adapter/ntfy.md diff --git a/lib/notification/adapter/ntfy.js b/lib/notification/adapter/ntfy.js new file mode 100644 index 0000000..4ffbc6f --- /dev/null +++ b/lib/notification/adapter/ntfy.js @@ -0,0 +1,51 @@ +import { markdown2Html } from '../../services/markdown.js'; +import { getJob } from '../../services/storage/jobStorage.js'; +import fetch from 'node-fetch'; + +export const send = ({ serviceName, newListings, notificationConfig, jobKey }) => { + const { priority, server, topic } = notificationConfig.find((adapter) => adapter.id === 'ntfy').fields; + const job = getJob(jobKey); + const jobName = job == null ? jobKey : job.name; + const promises = newListings.map((newListing) => { + const message = `Address: ${newListing.address} Size: ${newListing.size.replace(/2m/g, '$m^2$')} Price: ${ + newListing.price + }`; + return fetch(server, { + method: 'POST', + body: JSON.stringify({ + topic: topic, + message: message, + title: newListing.title, + tags: [serviceName, jobName], + priority: parseInt(priority), + click: newListing.link, + }), + }); + }); + + return Promise.all(promises); +}; +export const config = { + id: 'ntfy', + name: 'ntfy', + readme: markdown2Html('lib/notification/adapter/ntfy.md'), + description: 'Fredy will send new listings to your ntfy.', + fields: { + priority: { + type: 'number', + label: 'Priority', + description: 'The priority of the send notification.', + }, + server: { + type: 'text', + label: 'Server-URL', + description: 'The server url to the send the notification to.', + }, + topic: { + type: 'text', + label: 'topic', + description: + 'The topic where fredy should send notifications to. The topic is a secret, only known to you, make sure it is something not generic.', + }, + }, +}; diff --git a/lib/notification/adapter/ntfy.md b/lib/notification/adapter/ntfy.md new file mode 100644 index 0000000..18fd391 --- /dev/null +++ b/lib/notification/adapter/ntfy.md @@ -0,0 +1,5 @@ +### ntfy Adapter + +For ntfy, you need to create a topic on your preferred ntfy instance. This is pretty easy. Please visit the steps in the [docs](https://docs.ntfy.sh/publish/) and follow the instructions. + +As a result, you get the URL for configuration in fredy. In addition, the priority must be defined. \ No newline at end of file diff --git a/ui/src/views/jobs/mutation/components/notificationAdapter/NotificationAdapterMutator.jsx b/ui/src/views/jobs/mutation/components/notificationAdapter/NotificationAdapterMutator.jsx index dfea349..70db99b 100644 --- a/ui/src/views/jobs/mutation/components/notificationAdapter/NotificationAdapterMutator.jsx +++ b/ui/src/views/jobs/mutation/components/notificationAdapter/NotificationAdapterMutator.jsx @@ -25,9 +25,12 @@ const validate = (selectedAdapter) => { results.push('All fields are mandatory and must be set.'); continue; } - if (uiElement.type === 'number' && (typeof uiElement.value !== 'number' || uiElement.value < 0)) { - results.push('A number field cannot contain anything else and must be > 0.'); - continue; + if (uiElement.type === 'number') { + const numberValue = parseFloat(uiElement.value); + if(isNaN(numberValue) || numberValue < 0) { + results.push('A number field cannot contain anything else and must be > 0.'); + continue; + } } if (uiElement.type === 'boolean' && typeof uiElement.value !== 'boolean') { results.push('A boolean field cannot be of a different type.');