3 * @file src/Model/PushSubscriber.php
5 namespace Friendica\Model;
7 use Friendica\Core\Worker;
8 use Friendica\Database\DBA;
9 use Friendica\Util\DateTimeFormat;
11 require_once 'include/dba.php';
16 * @brief Send subscription notifications for the given user
18 * @param integer $uid User ID
19 * @param string $priority Priority for push workers
21 public static function publishFeed($uid, $default_priority = PRIORITY_HIGH)
23 $condition = ['push' => 0, 'uid' => $uid];
24 DBA::update('push_subscriber', ['push' => 1, 'next_try' => NULL_DATE], $condition);
26 self::requeue($default_priority);
30 * @brief start workers to transmit the feed data
32 * @param string $priority Priority for push workers
34 public static function requeue($default_priority = PRIORITY_HIGH)
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()"]);
40 while ($subscriber = DBA::fetch($subscribers)) {
41 // We always handle retries with low priority
42 if ($subscriber['push'] > 1) {
43 $priority = PRIORITY_LOW;
45 $priority = $default_priority;
48 logger('Publish feed to ' . $subscriber['callback_url'] . ' for ' . $subscriber['nickname'] . ' with priority ' . $priority, LOGGER_DEBUG);
49 Worker::add($priority, 'PubSubPublish', (int)$subscriber['id']);
52 DBA::close($subscribers);
56 * @brief Renew the feed subscription
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
65 public static function renew($uid, $nick, $subscribe, $hub_callback, $hub_topic, $hub_secret)
67 // fetch the old subscription if it exists
68 $subscriber = DBA::selectFirst('push_subscriber', ['last_update', 'push'], ['callback_url' => $hub_callback]);
70 // delete old subscription if it exists
71 DBA::delete('push_subscriber', ['callback_url' => $hub_callback]);
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);
80 $last_update = DateTimeFormat::utcNow();
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);
91 logger("Successfully subscribed [$hub_callback] for $nick");
93 logger("Successfully unsubscribed [$hub_callback] for $nick");
94 // we do nothing here, since the row was already deleted
99 * @brief Delay the push subscriber
101 * @param integer $id Subscriber ID
103 public static function delay($id)
105 $subscriber = DBA::selectFirst('push_subscriber', ['push', 'callback_url', 'renewed', 'nickname'], ['id' => $id]);
106 if (!DBA::isResult($subscriber)) {
110 $retrial = $subscriber['push'];
113 // End subscriptions if they weren't renewed for more than two months
114 $days = round((time() - strtotime($subscriber['renewed'])) / (60 * 60 * 24));
117 DBA::update('push_subscriber', ['push' => -1, 'next_try' => NULL_DATE], ['id' => $id]);
118 logger('Delivery error: Subscription ' . $subscriber['callback_url'] . ' for ' . $subscriber['nickname'] . ' is marked as ended.', LOGGER_DEBUG);
120 DBA::update('push_subscriber', ['push' => 0, 'next_try' => NULL_DATE], ['id' => $id]);
121 logger('Delivery error: Giving up ' . $subscriber['callback_url'] . ' for ' . $subscriber['nickname'] . ' for now.', LOGGER_DEBUG);
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');
128 $retrial = $retrial + 1;
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);
136 * @brief Reset the push subscriber
138 * @param integer $id Subscriber ID
139 * @param date $last_update Date of last transmitted item
141 public static function reset($id, $last_update)
143 $subscriber = DBA::selectFirst('push_subscriber', ['callback_url', 'nickname'], ['id' => $id]);
144 if (!DBA::isResult($subscriber)) {
148 // set last_update to the 'created' date of the last item, and reset push=0
149 $fields = ['push' => 0, 'next_try' => NULL_DATE, '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);