]> git.mxchange.org Git - friendica.git/blob - src/Model/PushSubscriber.php
Merge pull request #8939 from MrPetovan/task/8906-frio-viewas-redesign
[friendica.git] / src / Model / PushSubscriber.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Model;
23
24 use Friendica\Core\Logger;
25 use Friendica\Core\Worker;
26 use Friendica\Database\DBA;
27 use Friendica\Util\DateTimeFormat;
28
29 class PushSubscriber
30 {
31         /**
32          * Send subscription notifications for the given user
33          *
34          * @param integer $uid User ID
35          * @param int     $default_priority
36          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
37          */
38         public static function publishFeed($uid, $default_priority = PRIORITY_HIGH)
39         {
40                 $condition = ['push' => 0, 'uid' => $uid];
41                 DBA::update('push_subscriber', ['push' => 1, 'next_try' => DBA::NULL_DATETIME], $condition);
42
43                 self::requeue($default_priority);
44         }
45
46         /**
47          * start workers to transmit the feed data
48          *
49          * @param int $default_priority
50          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
51          */
52         public static function requeue($default_priority = PRIORITY_HIGH)
53         {
54                 // We'll push to each subscriber that has push > 0,
55                 // i.e. there has been an update (set in notifier.php).
56                 $subscribers = DBA::select('push_subscriber', ['id', 'push', 'callback_url', 'nickname'], ["`push` > 0 AND `next_try` < UTC_TIMESTAMP()"]);
57
58                 while ($subscriber = DBA::fetch($subscribers)) {
59                         // We always handle retries with low priority
60                         if ($subscriber['push'] > 1) {
61                                 $priority = PRIORITY_LOW;
62                         } else {
63                                 $priority = $default_priority;
64                         }
65
66                         Logger::log('Publish feed to ' . $subscriber['callback_url'] . ' for ' . $subscriber['nickname'] . ' with priority ' . $priority, Logger::DEBUG);
67                         Worker::add($priority, 'PubSubPublish', (int)$subscriber['id']);
68                 }
69
70                 DBA::close($subscribers);
71         }
72
73         /**
74          * Renew the feed subscription
75          *
76          * @param integer $uid          User ID
77          * @param string  $nick         Priority for push workers
78          * @param integer $subscribe    Subscribe (Unsubscribe = false)
79          * @param string  $hub_callback Callback address
80          * @param string  $hub_topic    Feed topic
81          * @param string  $hub_secret   Subscription secret
82          * @throws \Exception
83          */
84         public static function renew($uid, $nick, $subscribe, $hub_callback, $hub_topic, $hub_secret)
85         {
86                 // fetch the old subscription if it exists
87                 $subscriber = DBA::selectFirst('push_subscriber', ['last_update', 'push'], ['callback_url' => $hub_callback]);
88
89                 // delete old subscription if it exists
90                 DBA::delete('push_subscriber', ['callback_url' => $hub_callback]);
91
92                 if ($subscribe) {
93                         // if we are just updating an old subscription, keep the
94                         // old values for last_update but reset the push
95                         if (DBA::isResult($subscriber)) {
96                                 $last_update = $subscriber['last_update'];
97                                 $push_flag = min($subscriber['push'], 1);
98                         } else {
99                                 $last_update = DateTimeFormat::utcNow();
100                                 $push_flag = 0;
101                         }
102
103                         // subscribe means adding the row to the table
104                         $fields = ['uid' => $uid, 'callback_url' => $hub_callback,
105                                 'topic' => $hub_topic, 'nickname' => $nick, 'push' => $push_flag,
106                                 'last_update' => $last_update, 'renewed' => DateTimeFormat::utcNow(),
107                                 'secret' => $hub_secret];
108                         DBA::insert('push_subscriber', $fields);
109
110                         Logger::log("Successfully subscribed [$hub_callback] for $nick");
111                 } else {
112                         Logger::log("Successfully unsubscribed [$hub_callback] for $nick");
113                         // we do nothing here, since the row was already deleted
114                 }
115         }
116
117         /**
118          * Delay the push subscriber
119          *
120          * @param integer $id Subscriber ID
121          * @throws \Exception
122          */
123         public static function delay($id)
124         {
125                 $subscriber = DBA::selectFirst('push_subscriber', ['push', 'callback_url', 'renewed', 'nickname'], ['id' => $id]);
126                 if (!DBA::isResult($subscriber)) {
127                         return;
128                 }
129
130                 $retrial = $subscriber['push'];
131
132                 if ($retrial > 14) {
133                         // End subscriptions if they weren't renewed for more than two months
134                         $days = round((time() -  strtotime($subscriber['renewed'])) / (60 * 60 * 24));
135
136                         if ($days > 60) {
137                                 DBA::update('push_subscriber', ['push' => -1, 'next_try' => DBA::NULL_DATETIME], ['id' => $id]);
138                                 Logger::log('Delivery error: Subscription ' . $subscriber['callback_url'] . ' for ' . $subscriber['nickname'] . ' is marked as ended.', Logger::DEBUG);
139                         } else {
140                                 DBA::update('push_subscriber', ['push' => 0, 'next_try' => DBA::NULL_DATETIME], ['id' => $id]);
141                                 Logger::log('Delivery error: Giving up ' . $subscriber['callback_url'] . ' for ' . $subscriber['nickname'] . ' for now.', Logger::DEBUG);
142                         }
143                 } else {
144                         // Calculate the delay until the next trial
145                         $delay = (($retrial + 3) ** 4) + (rand(1, 30) * ($retrial + 1));
146                         $next = DateTimeFormat::utc('now + ' . $delay . ' seconds');
147
148                         $retrial = $retrial + 1;
149
150                         DBA::update('push_subscriber', ['push' => $retrial, 'next_try' => $next], ['id' => $id]);
151                         Logger::log('Delivery error: Next try (' . $retrial . ') ' . $subscriber['callback_url'] . ' for ' . $subscriber['nickname'] . ' at ' . $next, Logger::DEBUG);
152                 }
153         }
154
155         /**
156          * Reset the push subscriber
157          *
158          * @param integer $id          Subscriber ID
159          * @param string  $last_update Date of last transmitted item
160          * @throws \Exception
161          */
162         public static function reset($id, $last_update)
163         {
164                 $subscriber = DBA::selectFirst('push_subscriber', ['callback_url', 'nickname'], ['id' => $id]);
165                 if (!DBA::isResult($subscriber)) {
166                         return;
167                 }
168
169                 // set last_update to the 'created' date of the last item, and reset push=0
170                 $fields = ['push' => 0, 'next_try' => DBA::NULL_DATETIME, 'last_update' => $last_update];
171                 DBA::update('push_subscriber', $fields, ['id' => $id]);
172                 Logger::log('Subscriber ' . $subscriber['callback_url'] . ' for ' . $subscriber['nickname'] . ' is marked as vital', Logger::DEBUG);
173         }
174 }