3 * @file src/Model/PushSubscriber.php
5 namespace Friendica\Model;
7 use Friendica\Core\Worker;
8 use Friendica\Database\dba;
9 use Friendica\Database\DBM;
10 use Friendica\Util\DateTimeFormat;
12 require_once 'include/dba.php';
17 * @brief Send subscription notifications for the given user
19 * @param integer $uid User ID
20 * @param string $priority Priority for push workers
22 public static function publishFeed($uid, $default_priority = PRIORITY_HIGH)
24 $condition = ['push' => 0, 'uid' => $uid];
25 dba::update('push_subscriber', ['push' => 1, 'next_try' => NULL_DATE], $condition);
27 self::requeue($default_priority);
31 * @brief start workers to transmit the feed data
33 * @param string $priority Priority for push workers
35 public static function requeue($default_priority = PRIORITY_HIGH)
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()"]);
41 while ($subscriber = dba::fetch($subscribers)) {
42 // We always handle retries with low priority
43 if ($subscriber['push'] > 1) {
44 $priority = PRIORITY_LOW;
46 $priority = $default_priority;
49 logger('Publish feed to ' . $subscriber['callback_url'] . ' for ' . $subscriber['nickname'] . ' with priority ' . $priority, LOGGER_DEBUG);
50 Worker::add($priority, 'PubSubPublish', (int)$subscriber['id']);
53 dba::close($subscribers);
57 * @brief Renew the feed subscription
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
66 public static function renew($uid, $nick, $subscribe, $hub_callback, $hub_topic, $hub_secret)
68 // fetch the old subscription if it exists
69 $subscriber = dba::selectFirst('push_subscriber', ['last_update', 'push'], ['callback_url' => $hub_callback]);
71 // delete old subscription if it exists
72 dba::delete('push_subscriber', ['callback_url' => $hub_callback]);
75 // if we are just updating an old subscription, keep the
76 // old values for last_update but reset the push
77 if (DBM::is_result($subscriber)) {
78 $last_update = $subscriber['last_update'];
79 $push_flag = min($subscriber['push'], 1);
81 $last_update = DateTimeFormat::utcNow();
85 // subscribe means adding the row to the table
86 $fields = ['uid' => $uid, 'callback_url' => $hub_callback,
87 'topic' => $hub_topic, 'nickname' => $nick, 'push' => $push_flag,
88 'last_update' => $last_update, 'renewed' => DateTimeFormat::utcNow(),
89 'secret' => $hub_secret];
90 dba::insert('push_subscriber', $fields);
92 logger("Successfully subscribed [$hub_callback] for $nick");
94 logger("Successfully unsubscribed [$hub_callback] for $nick");
95 // we do nothing here, since the row was already deleted
100 * @brief Delay the push subscriber
102 * @param integer $id Subscriber ID
104 public static function delay($id)
106 $subscriber = dba::selectFirst('push_subscriber', ['push', 'callback_url', 'renewed', 'nickname'], ['id' => $id]);
107 if (!DBM::is_result($subscriber)) {
111 $retrial = $subscriber['push'];
114 // End subscriptions if they weren't renewed for more than two months
115 $days = round((time() - strtotime($subscriber['renewed'])) / (60 * 60 * 24));
118 dba::update('push_subscriber', ['push' => -1, 'next_try' => NULL_DATE], ['id' => $id]);
119 logger('Delivery error: Subscription ' . $subscriber['callback_url'] . ' for ' . $subscriber['nickname'] . ' is marked as ended.', LOGGER_DEBUG);
121 dba::update('push_subscriber', ['push' => 0, 'next_try' => NULL_DATE], ['id' => $id]);
122 logger('Delivery error: Giving up ' . $subscriber['callback_url'] . ' for ' . $subscriber['nickname'] . ' for now.', LOGGER_DEBUG);
125 // Calculate the delay until the next trial
126 $delay = (($retrial + 3) ** 4) + (rand(1, 30) * ($retrial + 1));
127 $next = DateTimeFormat::utc('now + ' . $delay . ' seconds');
129 $retrial = $retrial + 1;
131 dba::update('push_subscriber', ['push' => $retrial, 'next_try' => $next], ['id' => $id]);
132 logger('Delivery error: Next try (' . $retrial . ') ' . $subscriber['callback_url'] . ' for ' . $subscriber['nickname'] . ' at ' . $next, LOGGER_DEBUG);
137 * @brief Reset the push subscriber
139 * @param integer $id Subscriber ID
140 * @param date $last_update Date of last transmitted item
142 public static function reset($id, $last_update)
144 $subscriber = dba::selectFirst('push_subscriber', ['callback_url', 'nickname'], ['id' => $id]);
145 if (!DBM::is_result($subscriber)) {
149 // set last_update to the 'created' date of the last item, and reset push=0
150 $fields = ['push' => 0, 'next_try' => NULL_DATE, 'last_update' => $last_update];
151 dba::update('push_subscriber', $fields, ['id' => $id]);
152 logger('Subscriber ' . $subscriber['callback_url'] . ' for ' . $subscriber['nickname'] . ' is marked as vital', LOGGER_DEBUG);