-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.php
More file actions
154 lines (130 loc) · 5.6 KB
/
Copy pathextension.php
File metadata and controls
154 lines (130 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
* Plugin Name: AlertOnFeed
* Description: Get notifications for selected RSS feeds and send alerts via Pushover.
* Author: Vntgcode
* Version: 0.1.0
*/
class AlertOnFeedExtension extends Minz_Extension {
private array $user_feeds;
private array $selected_feeds = [];
private string $pushover_user = '';
private string $pushover_token = '';
private bool $ignore_read_articles = false;
private string $statusMessage = '';
private bool $bad_status = false;
public function init() {
$this->registerHook(Minz_HookType::EntryBeforeAdd, [$this, 'handleFeedUpdate']);
}
public function getPushoverUser(): string {
return $this->pushover_user;
}
public function getPushoverToken(): string {
return $this->pushover_token;
}
public function getUserFeeds(): array {
return $this->user_feeds;
}
public function getSelectedFeeds(): array {
return $this->selected_feeds;
}
public function getIgnoreReadArticles(): bool {
return $this->ignore_read_articles;
}
private function getFeeds(): array {
$feedDAO = FreshRSS_Factory::createFeedDao();
return $feedDAO->listFeeds();
}
private function loadConfigValues() {
$this->user_feeds = $this->getFeeds();
$this->selected_feeds = array_map('intval', $this->getUserConfigurationValue('aof_feeds') ?? []);
$this->pushover_user = (string)($this->getUserConfigurationValue('pushover_user') ?? '');
$this->pushover_token = (string)($this->getUserConfigurationValue('pushover_token') ?? '');
$this->ignore_read_articles = filter_var(
$this->getUserConfigurationValue('ignore_read_articles') ?? false,
FILTER_VALIDATE_BOOLEAN
);
}
private function sendPushoverMessage($user, $token, $title, $url) {
$data = [
'token' => $token,
'user' => $user,
'title' => $title,
'message' => $title . "\n" . $url,
'url' => $url,
'url_title' => 'View in FreshRSS',
];
$ch = curl_init('https://api.pushover.net/1/messages.json');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
private function sendStatusMessage() {
$url_array = ['c' => 'extension'];
if ($this->bad_status) {
Minz_Request::bad($this->statusMessage, $url_array);
} else {
Minz_Request::good($this->statusMessage, $url_array);
}
}
private function sendTestPushoverMessage() {
if ($this->pushover_user !== '' && $this->pushover_token !== '') {
$this->sendPushoverMessage($this->pushover_user, $this->pushover_token, 'Test AlertOnFeed', 'This is a test message from AlertOnFeed extension.');
$this->statusMessage = 'Test Pushover message sent.';
} else {
$this->statusMessage = 'Please save your Pushover credentials first.';
$this->bad_status = true;
}
$this->sendStatusMessage();
}
public function handleConfigureAction() {
parent::handleConfigureAction();
$this->loadConfigValues();
if (Minz_Request::isPost()) {
if (Minz_Request::paramString('send_test') === '1') {
$this->sendTestPushoverMessage();
} else {
$form_selected_feeds = array_values(array_filter(Minz_Request::paramArray('feeds'), static fn($value) => is_scalar($value) && (string)$value !== ''));
$pushover_user = Minz_Request::paramString('pushover_user');
$pushover_token = Minz_Request::paramString('pushover_token');
$ignore_read_articles = Minz_Request::paramString('ignore_read_articles') === '1';
$this->setUserConfigurationValue('aof_feeds', $form_selected_feeds);
$this->setUserConfigurationValue('pushover_user', $pushover_user);
$this->setUserConfigurationValue('pushover_token', $pushover_token);
$this->setUserConfigurationValue('ignore_read_articles', $ignore_read_articles);
$this->statusMessage = 'Settings saved.';
}
$this->sendStatusMessage();
}
if (!is_array($this->getSelectedFeeds())) {
$this->selected_feeds = [];
}
}
public function handleFeedUpdate(FreshRSS_Entry $article) {
Minz_Log::notice('AlertOnFeed: Checking feed update for entry ID ' . $article->id());
try {
$this->loadConfigValues();
if (empty($this->pushover_user) || empty($this->pushover_token)) {
return;
}
if ($this->ignore_read_articles && $article->isRead()) {
Minz_Log::notice('AlertOnFeed: Skipping read entry ID ' . $article->id());
return;
}
// $article->feedId and $article->title, $article->link are typical properties
$feedId = $article->feedId();
if ($feedId !== '' && in_array($feedId, $this->getSelectedFeeds(), true)) {
$title = $article->title();
$url = $article->link();
$this->sendPushoverMessage($this->pushover_user, $this->pushover_token, $title, $url);
}
} catch (Exception $e) {
Minz_Log::error('AlertOnFeed: Error handling feed update - ' . $e->getMessage());
} finally {
return $article;
}
}
}