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