Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
5.3KB

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