]> git.mxchange.org Git - friendica.git/blob - src/Model/PushSubscriber.php
Move mod/viewsrc to src/Module/ItemBody
[friendica.git] / src / Model / PushSubscriber.php
1 <?php
2 /**
3  * @file src/Model/PushSubscriber.php
4  */
5 namespace Friendica\Model;
6
7 use Friendica\Core\Logger;
8 use Friendica\Core\Worker;
9 use Friendica\Database\DBA;
10 use Friendica\Util\DateTimeFormat;
11
12 class PushSubscriber
13 {
14         /**
15          * @brief Send subscription notifications for the given user
16          *
17          * @param integer $uid User ID
18          * @param int     $default_priority
19          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
20          */
21         public static function publishFeed($uid, $default_priority = PRIORITY_HIGH)
22         {
23                 $condition = ['push' => 0, 'uid' => $uid];
24                 DBA::update('push_subscriber', ['push' => 1, 'next_try' => DBA::NULL_DATETIME], $condition);
25
26                 self::requeue($default_priority);
27         }
28
29         /**
30          * @brief start workers to transmit the feed data
31          *
32          * @param int $default_priority
33          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
34          */
35         public static function requeue($default_priority = PRIORITY_HIGH)
36         {
37                 // We'll push to each subscriber that has push > 0,
38                 // i.e. there has been an update (set in notifier.php).
39                 $subscribers = DBA::select('push_subscriber', ['id', 'push', 'callback_url', 'nickname'], ["`push` > 0 AND `next_try` < UTC_TIMESTAMP()"]);
40
41                 while ($subscriber = DBA::fetch($subscribers)) {
42                         // We always handle retries with low priority
43                         if ($subscriber['push'] > 1) {
44                                 $priority = PRIORITY_LOW;
45                         } else {
46                                 $priority = $default_priority;
47                         }
48
49                         Logger::log('Publish feed to ' . $subscriber['callback_url'] . ' for ' . $subscriber['nickname'] . ' with priority ' . $priority, Logger::DEBUG);
50                         Worker::add($priority, 'PubSubPublish', (int)$subscriber['id']);
51                 }
52
53                 DBA::close($subscribers);
54         }
55
56         /**
57          * @brief Renew the feed subscription
58          *
59          * @param integer $uid          User ID
60          * @param string  $nick         Priority for push workers
61          * @param integer $subscribe    Subscribe (Unsubscribe = false)
62          * @param string  $hub_callback Callback address
63          * @param string  $hub_topic    Feed topic
64          * @param string  $hub_secret   Subscription secret
65          * @throws \Exception
66          */
67         public static function renew($uid, $nick, $subscribe, $hub_callback, $hub_topic, $hub_secret)
68         {
69                 // fetch the old subscription if it exists
70                 $subscriber = DBA::selectFirst('push_subscriber', ['last_update', 'push'], ['callback_url' => $hub_callback]);
71
72                 // delete old subscription if it exists
73                 DBA::delete('push_subscriber', ['callback_url' => $hub_callback]);
74
75                 if ($subscribe) {
76                         // if we are just updating an old subscription, keep the
77                         // old values for last_update but reset the push
78                         if (DBA::isResult($subscriber)) {
79                                 $last_update = $subscriber['last_update'];
80                                 $push_flag = min($subscriber['push'], 1);
81                         } else {
82                                 $last_update = DateTimeFormat::utcNow();
83                                 $push_flag = 0;
84                         }
85
86                         // subscribe means adding the row to the table
87                         $fields = ['uid' => $uid, 'callback_url' => $hub_callback,
88                                 'topic' => $hub_topic, 'nickname' => $nick, 'push' => $push_flag,
89                                 'last_update' => $last_update, 'renewed' => DateTimeFormat::utcNow(),
90                                 'secret' => $hub_secret];
91                         DBA::insert('push_subscriber', $fields);
92
93                         Logger::log("Successfully subscribed [$hub_callback] for $nick");
94                 } else {
95                         Logger::log("Successfully unsubscribed [$hub_callback] for $nick");
96                         // we do nothing here, since the row was already deleted
97                 }
98         }
99
100         /**
101          * @brief Delay the push subscriber
102          *
103          * @param integer $id Subscriber ID
104          * @throws \Exception
105          */
106         public static function delay($id)
107         {
108                 $subscriber = DBA::selectFirst('push_subscriber', ['push', 'callback_url', 'renewed', 'nickname'], ['id' => $id]);
109                 if (!DBA::isResult($subscriber)) {
110                         return;
111                 }
112
113                 $retrial = $subscriber['push'];
114
115                 if ($retrial > 14) {
116                         // End subscriptions if they weren't renewed for more than two months
117                         $days = round((time() -  strtotime($subscriber['renewed'])) / (60 * 60 * 24));
118
119                         if ($days > 60) {
120                                 DBA::update('push_subscriber', ['push' => -1, 'next_try' => DBA::NULL_DATETIME], ['id' => $id]);
121                                 Logger::log('Delivery error: Subscription ' . $subscriber['callback_url'] . ' for ' . $subscriber['nickname'] . ' is marked as ended.', Logger::DEBUG);
122                         } else {
123                                 DBA::update('push_subscriber', ['push' => 0, 'next_try' => DBA::NULL_DATETIME], ['id' => $id]);
124                                 Logger::log('Delivery error: Giving up ' . $subscriber['callback_url'] . ' for ' . $subscriber['nickname'] . ' for now.', Logger::DEBUG);
125                         }
126                 } else {
127                         // Calculate the delay until the next trial
128                         $delay = (($retrial + 3) ** 4) + (rand(1, 30) * ($retrial + 1));
129                         $next = DateTimeFormat::utc('now + ' . $delay . ' seconds');
130
131                         $retrial = $retrial + 1;
132
133                         DBA::update('push_subscriber', ['push' => $retrial, 'next_try' => $next], ['id' => $id]);
134                         Logger::log('Delivery error: Next try (' . $retrial . ') ' . $subscriber['callback_url'] . ' for ' . $subscriber['nickname'] . ' at ' . $next, Logger::DEBUG);
135                 }
136         }
137
138         /**
139          * @brief Reset the push subscriber
140          *
141          * @param integer $id          Subscriber ID
142          * @param string  $last_update Date of last transmitted item
143          * @throws \Exception
144          */
145         public static function reset($id, $last_update)
146         {
147                 $subscriber = DBA::selectFirst('push_subscriber', ['callback_url', 'nickname'], ['id' => $id]);
148                 if (!DBA::isResult($subscriber)) {
149                         return;
150                 }
151
152                 // set last_update to the 'created' date of the last item, and reset push=0
153                 $fields = ['push' => 0, 'next_try' => DBA::NULL_DATETIME, 'last_update' => $last_update];
154                 DBA::update('push_subscriber', $fields, ['id' => $id]);
155                 Logger::log('Subscriber ' . $subscriber['callback_url'] . ' for ' . $subscriber['nickname'] . ' is marked as vital', Logger::DEBUG);
156         }
157 }