Skip to content

Commit ee2436b

Browse files
authored
feat(push): add test push button to validate configuration (#4)
1 parent 35a4e23 commit ee2436b

File tree

1 file changed

+78
-26
lines changed

1 file changed

+78
-26
lines changed

init.php

Lines changed: 78 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,28 @@ public function save() {
2323
echo __("Ntfy settings saved.");
2424
}
2525

26+
public function test_notify() {
27+
$ntfy_server = $_POST["ntfy_server"];
28+
$ntfy_topic = $_POST["ntfy_topic"];
29+
$ntfy_token = $_POST["ntfy_token"];
30+
31+
$message = [
32+
"topic" => $ntfy_topic,
33+
"message" => "This is a test notification from Tiny Tiny RSS",
34+
"title" => "Test Notification",
35+
"tags" => ["mailbox_with_mail"]
36+
];
37+
38+
$error = $this->send_ntfy_message($ntfy_server, $ntfy_token, $message);
39+
40+
41+
if ($error) {
42+
echo __("Error sending test notification:") . " " . $error;
43+
} else {
44+
echo __("Test notification sent successfully");
45+
}
46+
}
47+
2648
public function init($host) {
2749
$this->host = $host;
2850

@@ -53,7 +75,7 @@ public function hook_prefs_tab($args) {
5375

5476
<h2> <?= __('Send feed notification via Ntfy') ?></h2>
5577

56-
<form dojoType="dijit.form.Form">
78+
<form id="ntfyForm" dojoId="ntfyForm" dojoType="dijit.form.Form">
5779
<?= \Controls\pluginhandler_tags($this, "save") ?>
5880
<script type="dojo/method" event="onSubmit" args="evt">
5981
evt.preventDefault();
@@ -78,6 +100,29 @@ public function hook_prefs_tab($args) {
78100

79101
<?= \Controls\submit_tag(__("Save")) ?>
80102

103+
<button dojoType="dijit.form.Button">
104+
<?= __("Test") ?>
105+
<script type="dojo/on" data-dojo-event="click" data-dojo-args="evt" >
106+
require(['dojo/dom-form'], function(domForm) {
107+
Notify.progress('Sending test notification...', true);
108+
const ntfyData = domForm.toObject('ntfyForm')
109+
xhr.post("backend.php", {
110+
...ntfyData,
111+
op: "PluginHandler",
112+
plugin: "ntfy",
113+
method: "test_notify"
114+
}, reply => {
115+
if (reply.errors) {
116+
Notify.error(reply.errors.join('; '));
117+
} else {
118+
Notify.info(reply);
119+
}
120+
});
121+
});
122+
123+
</script>
124+
</button>
125+
81126
</form>
82127
<hr />
83128
<?php
@@ -162,40 +207,47 @@ public function send_notification($article) {
162207
$ntfy_topic = $this->host->get($this, "ntfy_topic");
163208
$ntfy_token = $this->host->get($this, "ntfy_token");
164209

165-
$ch = curl_init();
166-
167210
$content = $this->clean_html($article['content']);
168211
$truncatedContent = strlen($content) > 500
169-
? mb_substr($content, 0, 500, 'UTF-8') . "..."
170-
: $content;
171-
172-
$headers = [];
173-
if (strlen(trim($ntfy_token)) > 0) {
174-
$headers[] = "Authorization: Bearer " . $ntfy_token;
175-
}
212+
? mb_substr($content, 0, 500, 'UTF-8') . "..."
213+
: $content;
176214

177-
// get article feed title
178215
$title = "" . Feeds::_get_title($article["feed"]["id"]) . "" . $article["title"];
179-
$jsonData = json_encode([
180-
"topic" => $ntfy_topic,
181-
"message" => $truncatedContent,
182-
"title" => $title,
183-
"tags" => ["mailbox_with_mail"],
184-
"click" => $article["link"],
185-
]);
186-
curl_setopt($ch, CURLOPT_URL, $ntfy_server);
187-
curl_setopt($ch, CURLOPT_POST, true);
188-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
189-
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
190-
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
191-
curl_exec($ch);
192-
curl_close($ch);
193-
216+
$message = [
217+
"topic" => $ntfy_topic,
218+
"message" => $truncatedContent,
219+
"title" => $title,
220+
"tags" => ["mailbox_with_mail"],
221+
"click" => $article["link"],
222+
];
223+
224+
$this->send_ntfy_message($ntfy_server, $ntfy_token, $message);
194225
array_push($article["tags"], "ntfy");
195226
}
196227
return $article;
197228
}
198229

230+
private function send_ntfy_message($server, $token, $message) {
231+
$ch = curl_init();
232+
233+
$headers = [];
234+
if (strlen(trim($token)) > 0) {
235+
$headers[] = "Authorization: Bearer " . $token;
236+
}
237+
238+
curl_setopt($ch, CURLOPT_URL, $server);
239+
curl_setopt($ch, CURLOPT_POST, true);
240+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
241+
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($message));
242+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
243+
244+
curl_exec($ch);
245+
$error = curl_error($ch);
246+
curl_close($ch);
247+
248+
return $error;
249+
}
250+
199251
public function api_version() {
200252
return 2;
201253
}

0 commit comments

Comments
 (0)