3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Model;
24 use Friendica\Core\Logger;
25 use Friendica\Core\Worker;
26 use Friendica\Database\DBA;
27 use Friendica\Util\DateTimeFormat;
32 * Send subscription notifications for the given user
34 * @param integer $uid User ID
35 * @param int $default_priority
36 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
38 public static function publishFeed($uid, $default_priority = PRIORITY_HIGH)
40 $condition = ['push' => 0, 'uid' => $uid];
41 DBA::update('push_subscriber', ['push' => 1, 'next_try' => DBA::NULL_DATETIME], $condition);
43 self::requeue($default_priority);
47 * start workers to transmit the feed data
49 * @param int $default_priority
50 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
52 public static function requeue($default_priority = PRIORITY_HIGH)
54 // We'll push to each subscriber that has push > 0,
55 // i.e. there has been an update (set in notifier.php).
56 $subscribers = DBA::select('push_subscriber', ['id', 'push', 'callback_url', 'nickname'], ["`push` > 0 AND `next_try` < UTC_TIMESTAMP()"]);
58 while ($subscriber = DBA::fetch($subscribers)) {
59 // We always handle retries with low priority
60 if ($subscriber['push'] > 1) {
61 $priority = PRIORITY_LOW;
63 $priority = $default_priority;
66 Logger::log('Publish feed to ' . $subscriber['callback_url'] . ' for ' . $subscriber['nickname'] . ' with priority ' . $priority, Logger::DEBUG);
67 Worker::add($priority, 'PubSubPublish', (int)$subscriber['id']);
70 DBA::close($subscribers);
74 * Renew the feed subscription
76 * @param integer $uid User ID
77 * @param string $nick Priority for push workers
78 * @param integer $subscribe Subscribe (Unsubscribe = false)
79 * @param string $hub_callback Callback address
80 * @param string $hub_topic Feed topic
81 * @param string $hub_secret Subscription secret
84 public static function renew($uid, $nick, $subscribe, $hub_callback, $hub_topic, $hub_secret)
86 // fetch the old subscription if it exists
87 $subscriber = DBA::selectFirst('push_subscriber', ['last_update', 'push'], ['callback_url' => $hub_callback]);
89 // delete old subscription if it exists
90 DBA::delete('push_subscriber', ['callback_url' => $hub_callback]);
93 // if we are just updating an old subscription, keep the
94 // old values for last_update but reset the push
95 if (DBA::isResult($subscriber)) {
96 $last_update = $subscriber['last_update'];
97 $push_flag = min($subscriber['push'], 1);
99 $last_update = DateTimeFormat::utcNow();
103 // subscribe means adding the row to the table
104 $fields = ['uid' => $uid, 'callback_url' => $hub_callback,
105 'topic' => $hub_topic, 'nickname' => $nick, 'push' => $push_flag,
106 'last_update' => $last_update, 'renewed' => DateTimeFormat::utcNow(),
107 'secret' => $hub_secret];
108 DBA::insert('push_subscriber', $fields);
110 Logger::log("Successfully subscribed [$hub_callback] for $nick");
112 Logger::log("Successfully unsubscribed [$hub_callback] for $nick");
113 // we do nothing here, since the row was already deleted
118 * Delay the push subscriber
120 * @param integer $id Subscriber ID
123 public static function delay($id)
125 $subscriber = DBA::selectFirst('push_subscriber', ['push', 'callback_url', 'renewed', 'nickname'], ['id' => $id]);
126 if (!DBA::isResult($subscriber)) {
130 $retrial = $subscriber['push'];
133 // End subscriptions if they weren't renewed for more than two months
134 $days = round((time() - strtotime($subscriber['renewed'])) / (60 * 60 * 24));
137 DBA::update('push_subscriber', ['push' => -1, 'next_try' => DBA::NULL_DATETIME], ['id' => $id]);
138 Logger::log('Delivery error: Subscription ' . $subscriber['callback_url'] . ' for ' . $subscriber['nickname'] . ' is marked as ended.', Logger::DEBUG);
140 DBA::update('push_subscriber', ['push' => 0, 'next_try' => DBA::NULL_DATETIME], ['id' => $id]);
141 Logger::log('Delivery error: Giving up ' . $subscriber['callback_url'] . ' for ' . $subscriber['nickname'] . ' for now.', Logger::DEBUG);
144 // Calculate the delay until the next trial
145 $delay = (($retrial + 3) ** 4) + (rand(1, 30) * ($retrial + 1));
146 $next = DateTimeFormat::utc('now + ' . $delay . ' seconds');
148 $retrial = $retrial + 1;
150 DBA::update('push_subscriber', ['push' => $retrial, 'next_try' => $next], ['id' => $id]);
151 Logger::log('Delivery error: Next try (' . $retrial . ') ' . $subscriber['callback_url'] . ' for ' . $subscriber['nickname'] . ' at ' . $next, Logger::DEBUG);
156 * Reset the push subscriber
158 * @param integer $id Subscriber ID
159 * @param string $last_update Date of last transmitted item
162 public static function reset($id, $last_update)
164 $subscriber = DBA::selectFirst('push_subscriber', ['callback_url', 'nickname'], ['id' => $id]);
165 if (!DBA::isResult($subscriber)) {
169 // set last_update to the 'created' date of the last item, and reset push=0
170 $fields = ['push' => 0, 'next_try' => DBA::NULL_DATETIME, 'last_update' => $last_update];
171 DBA::update('push_subscriber', $fields, ['id' => $id]);
172 Logger::log('Subscriber ' . $subscriber['callback_url'] . ' for ' . $subscriber['nickname'] . ' is marked as vital', Logger::DEBUG);