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