From cb49bdc5aed104b20d986cd001a65db54ef3728f Mon Sep 17 00:00:00 2001 From: Miguel Ribeiro Date: Wed, 9 Oct 2024 22:34:59 +0200 Subject: [PATCH 1/2] feat: add url and notes as variables for the notifications webhook fix: bug with looping multiple subscriptions on the notifications webhook --- endpoints/cronjobs/sendnotifications.php | 31 +++++++++++++++---- .../savewebhooknotifications.php | 8 +++-- includes/version.php | 2 +- scripts/notifications.js | 8 +++-- settings.php | 8 +++-- 5 files changed, 42 insertions(+), 15 deletions(-) diff --git a/endpoints/cronjobs/sendnotifications.php b/endpoints/cronjobs/sendnotifications.php index 7e47bf6af..cbfe5130a 100644 --- a/endpoints/cronjobs/sendnotifications.php +++ b/endpoints/cronjobs/sendnotifications.php @@ -141,7 +141,12 @@ $webhook['payload'] = $row["payload"]; $webhook['iterator'] = $row["iterator"]; if ($webhook['iterator'] === "") { - $webhook['iterator'] = "subscriptions"; + echo "No iterator set for webhook. Skipping.
"; + $webhook['iterator'] = "{{subscriptions}}"; + } else { + if (strpos($webhook['iterator'], "{{") === false) { + $webhook['iterator'] = "{{" . $webhook['iterator'] . "}}"; + } } } @@ -215,6 +220,8 @@ $notify[$rowSubscription['payer_user_id']][$i]['payer'] = $household[$rowSubscription['payer_user_id']]['name']; $notify[$rowSubscription['payer_user_id']][$i]['date'] = $rowSubscription['next_payment']; $notify[$rowSubscription['payer_user_id']][$i]['days'] = $daysToCompare; + $notify[$rowSubscription['payer_user_id']][$i]['url'] = $rowSubscription['url']; + $notify[$rowSubscription['payer_user_id']][$i]['notes'] = $rowSubscription['notes']; $i++; } } @@ -532,10 +539,16 @@ if ($webhookNotificationsEnabled) { // Get webhook payload and turn it into a json object - $payload = str_replace("{{days_until}}", $days, $webhook['payload']); // The default value for all subscriptions + $payload = str_replace("{{days_until}}", $days, $webhook['payload']); + $payload_json = json_decode($payload, true); - $subscription_template = $payload_json["{{subscriptions}}"]; + if ($payload_json === null) { + echo "Error parsing payload JSON
"; + continue; + } + + $subscription_template = $payload_json[$webhook['iterator']]; $subscriptions = []; foreach ($notify as $userId => $perUser) { @@ -560,7 +573,9 @@ $temp_subscription[$key] = str_replace("{{subscription_category}}", $subscription['category'], $temp_subscription[$key]); $temp_subscription[$key] = str_replace("{{subscription_payer}}", $subscription['payer'], $temp_subscription[$key]); $temp_subscription[$key] = str_replace("{{subscription_date}}", $subscription['date'], $temp_subscription[$key]); - $temp_subscription[$key] = str_replace("{{subscription_days_until_payment}}", $subscription['days'], $temp_subscription[$key]); // The de facto value for this subscription + $temp_subscription[$key] = str_replace("{{subscription_days_until_payment}}", $subscription['days'], $temp_subscription[$key]); + $temp_subscription[$key] = str_replace("{{subscription_url}}", $subscription['url'], $temp_subscription[$key]); + $temp_subscription[$key] = str_replace("{{subscription_notes}}", $subscription['notes'], $temp_subscription[$key]); } } $subscriptions[] = $temp_subscription; @@ -568,10 +583,14 @@ } } + // remove {{ and }} from the iterator + $payload_iterator = str_replace("{{", "", $webhook['iterator']); + $payload_iterator = str_replace("}}", "", $payload_iterator); + $payload_json["{{subscriptions}}"] = $subscriptions; - $payload_json[$webhook['iterator']] = $subscriptions; + $payload_json[$payload_iterator] = $subscriptions; unset($payload_json["{{subscriptions}}"]); - + $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $webhook['url']); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $webhook['request_method']); diff --git a/endpoints/notifications/savewebhooknotifications.php b/endpoints/notifications/savewebhooknotifications.php index c3d099e3a..b4815ee44 100644 --- a/endpoints/notifications/savewebhooknotifications.php +++ b/endpoints/notifications/savewebhooknotifications.php @@ -26,6 +26,7 @@ $url = $data["webhook_url"]; $headers = $data["headers"]; $payload = $data["payload"]; + $iterator = $data["iterator"]; $query = "SELECT COUNT(*) FROM webhook_notifications WHERE user_id = :userId"; $stmt = $db->prepare($query); @@ -42,11 +43,11 @@ $row = $result->fetchArray(); $count = $row[0]; if ($count == 0) { - $query = "INSERT INTO webhook_notifications (enabled, url, headers, payload, user_id) - VALUES (:enabled, :url, :headers, :payload, :userId)"; + $query = "INSERT INTO webhook_notifications (enabled, url, headers, payload, iterator, user_id) + VALUES (:enabled, :url, :headers, :payload, :iterator, :userId)"; } else { $query = "UPDATE webhook_notifications - SET enabled = :enabled, url = :url, headers = :headers, payload = :payload WHERE user_id = :userId"; + SET enabled = :enabled, url = :url, headers = :headers, payload = :payload, iterator = :iterator WHERE user_id = :userId"; } $stmt = $db->prepare($query); @@ -54,6 +55,7 @@ $stmt->bindValue(':url', $url, SQLITE3_TEXT); $stmt->bindValue(':headers', $headers, SQLITE3_TEXT); $stmt->bindValue(':payload', $payload, SQLITE3_TEXT); + $stmt->bindValue(':iterator', $iterator, SQLITE3_TEXT); $stmt->bindValue(':userId', $userId, SQLITE3_INTEGER); if ($stmt->execute()) { diff --git a/includes/version.php b/includes/version.php index 9dd847774..e5a44f1e4 100644 --- a/includes/version.php +++ b/includes/version.php @@ -1,3 +1,3 @@ \ No newline at end of file diff --git a/scripts/notifications.js b/scripts/notifications.js index 515f49afd..217c87fd8 100644 --- a/scripts/notifications.js +++ b/scripts/notifications.js @@ -112,12 +112,14 @@ function saveNotificationsWebhookButton() { const webhook_url = document.getElementById("webhookurl").value; const headers = document.getElementById("webhookcustomheaders").value; const payload = document.getElementById("webhookpayload").value; + const iterator = document.getElementById("webhookiteratorkey").value; const data = { enabled: enabled, webhook_url: webhook_url, headers: headers, - payload: payload + payload: payload, + iterator: iterator }; makeFetchCall('endpoints/notifications/savewebhooknotifications.php', data, button); @@ -132,13 +134,15 @@ function testNotificationsWebhookButton() { const url = document.getElementById("webhookurl").value; const customheaders = document.getElementById("webhookcustomheaders").value; const payload = document.getElementById("webhookpayload").value; + const iterator = document.getElementById("webhookiteratorkey").value; const data = { enabled: enabled, requestmethod: requestmethod, url: url, customheaders: customheaders, - payload: payload + payload: payload, + iterator: iterator }; makeFetchCall('endpoints/notifications/testwebhooknotifications.php', data, button); diff --git a/settings.php b/settings.php index 1b1a93f98..b4f2e8e0f 100644 --- a/settings.php +++ b/settings.php @@ -275,8 +275,10 @@ class="thin mobile-grow" /> "currency": "{{subscription_currency}}", "category": "{{subscription_category}}", "date": "{{subscription_date}}", - "payer": "{{subscription_payer}}" - "days": "{{subscription_days_until_payment}}" + "payer": "{{subscription_payer}}", + "days": "{{subscription_days_until_payment}}", + "notes": "{{subscription_notes}}", + "url": "{{subscription_url}}" } ] @@ -617,7 +619,7 @@ class="capitalize">: : {{days_until}}, {{subscription_name}}, {{subscription_price}}, {{subscription_currency}}, {{subscription_category}}, {{subscription_date}}, {{subscription_payer}}, - {{subscription_days_until_payment}} + {{subscription_days_until_payment}}, {{subscription_notes}}, {{subscription_url}}

From 1a055a2898bacbbbfc71c650cbe830cc807ea5d5 Mon Sep 17 00:00:00 2001 From: Miguel Ribeiro Date: Wed, 9 Oct 2024 22:36:59 +0200 Subject: [PATCH 2/2] remove echo --- endpoints/cronjobs/sendnotifications.php | 1 - 1 file changed, 1 deletion(-) diff --git a/endpoints/cronjobs/sendnotifications.php b/endpoints/cronjobs/sendnotifications.php index cbfe5130a..3996d8312 100644 --- a/endpoints/cronjobs/sendnotifications.php +++ b/endpoints/cronjobs/sendnotifications.php @@ -141,7 +141,6 @@ $webhook['payload'] = $row["payload"]; $webhook['iterator'] = $row["iterator"]; if ($webhook['iterator'] === "") { - echo "No iterator set for webhook. Skipping.
"; $webhook['iterator'] = "{{subscriptions}}"; } else { if (strpos($webhook['iterator'], "{{") === false) {