diff --git a/lib/notification/adapter/apprise.js b/lib/notification/adapter/apprise.js index 62433002..1877bb82 100644 --- a/lib/notification/adapter/apprise.js +++ b/lib/notification/adapter/apprise.js @@ -7,15 +7,38 @@ 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, baseUrl }) => { +export const send = async ({ serviceName, newListings, notificationConfig, jobKey, baseUrl }) => { const { server } = notificationConfig.find((adapter) => adapter.id === config.id).fields; const job = getJob(jobKey); const jobName = job == null ? jobKey : job.name; - const promises = newListings.map((newListing) => { + + for (const newListing of newListings) { const title = `${jobName} at ${serviceName}: ${newListing.title}`; const fredyLine = baseUrl && newListing.id ? `\nOpen in Fredy: ${baseUrl}/#/listings/listing/${newListing.id}` : ''; const message = `Address: ${newListing.address}\nSize: ${newListing.size}\nPrice: ${newListing.price}\nLink: ${newListing.link}${fredyLine}`; - return fetch(server, { + + // Try to attach image if available + if (newListing.image && typeof newListing.image === 'string') { + try { + const imgRes = await fetch(newListing.image); + if (imgRes.ok) { + const ab = await imgRes.arrayBuffer(); + const form = new FormData(); + form.append('body', message); + form.append('title', title); + form.append('attachment', new Blob([ab]), 'image.jpg'); + await fetch(server, { + method: 'POST', + body: form, + }); + continue; + } + } catch { + // fail silently, just skip the image + } + } + + await fetch(server, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ @@ -23,8 +46,7 @@ export const send = ({ serviceName, newListings, notificationConfig, jobKey, bas title: title, }), }); - }); - return Promise.all(promises); + } }; export const config = { id: 'apprise',