activitypubfailedqueuehandler.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * ActivityPub queue handler for notice distribution
  18. *
  19. * @package GNUsocial
  20. * @author Diogo Cordeiro <diogo@fc.up.pt>
  21. * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
  22. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  23. */
  24. defined('GNUSOCIAL') || die();
  25. /**
  26. * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
  27. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  28. */
  29. class ActivityPubFailedQueueHandler extends QueueHandler
  30. {
  31. /**
  32. * Getter of the queue transport name.
  33. *
  34. * @return string transport name
  35. */
  36. public function transport(): string
  37. {
  38. return 'activitypub_failed';
  39. }
  40. /**
  41. * Notice distribution handler.
  42. *
  43. * @param array $to_failed [string to, Notice].
  44. * @return bool true on success, false otherwise
  45. * @throws HTTP_Request2_Exception
  46. * @throws InvalidUrlException
  47. * @throws ServerException
  48. * @author Diogo Cordeiro <diogo@fc.up.pt>
  49. */
  50. public function handle($to_failed): bool
  51. {
  52. [$other, $notice] = $to_failed;
  53. if (!($notice instanceof Notice)) {
  54. common_log(LOG_ERR, 'Got a bogus notice, not distributing');
  55. return true;
  56. }
  57. $profile = $notice->getProfile();
  58. if (!$profile->isLocal()) {
  59. return true;
  60. }
  61. if ($notice->source == 'activity') {
  62. common_log(LOG_ERR, "Ignoring distribution of notice:{$notice->id}: activity source");
  63. return true;
  64. }
  65. try {
  66. // Handling a Create?
  67. if (ActivityUtils::compareVerbs($notice->verb, [ActivityVerb::POST, ActivityVerb::SHARE])) {
  68. return $this->handle_create($profile, $notice, $other);
  69. }
  70. // Handling a Like?
  71. if (ActivityUtils::compareVerbs($notice->verb, [ActivityVerb::FAVORITE])) {
  72. return $this->onEndFavorNotice($profile, $notice, $other);
  73. }
  74. // Handling a Delete Note?
  75. if (ActivityUtils::compareVerbs($notice->verb, [ActivityVerb::DELETE])) {
  76. return $this->onStartDeleteOwnNotice($profile, $notice, $other);
  77. }
  78. } catch (Exception $e) {
  79. // Postman already re-enqueues for us
  80. common_debug('ActivityPub Failed Queue Handler:'.$e->getMessage());
  81. }
  82. return true;
  83. }
  84. private function handle_create($profile, $notice, $other)
  85. {
  86. // Handling an Announce?
  87. if ($notice->isRepeat()) {
  88. $repeated_notice = Notice::getKV('id', $notice->repeat_of);
  89. if ($repeated_notice instanceof Notice) {
  90. // That was it
  91. $postman = new Activitypub_postman($profile, $other);
  92. $postman->announce($notice, $repeated_notice);
  93. }
  94. // either made the announce or found nothing to repeat
  95. return true;
  96. }
  97. // That was it
  98. $postman = new Activitypub_postman($profile, $other);
  99. $postman->create_note($notice);
  100. return true;
  101. }
  102. /**
  103. * Notify remote users when their notices get favourited.
  104. *
  105. * @param Profile $profile of local user doing the faving
  106. * @param Notice $notice_liked Notice being favored
  107. * @return bool return value
  108. * @throws HTTP_Request2_Exception
  109. * @throws InvalidUrlException
  110. * @author Diogo Cordeiro <diogo@fc.up.pt>
  111. */
  112. public function onEndFavorNotice(Profile $profile, Notice $notice, $other)
  113. {
  114. $postman = new Activitypub_postman($profile, $other);
  115. $postman->like($notice);
  116. return true;
  117. }
  118. /**
  119. * Notify remote users when their notices get deleted
  120. *
  121. * @param $user
  122. * @param $notice
  123. * @return bool hook flag
  124. * @throws HTTP_Request2_Exception
  125. * @throws InvalidUrlException
  126. * @author Diogo Cordeiro <diogo@fc.up.pt>
  127. */
  128. public function onStartDeleteOwnNotice($profile, $notice, $other)
  129. {
  130. // Handle delete locally either because:
  131. // 1. There's no undo-share logic yet
  132. // 2. The deleting user has privileges to do so (locally)
  133. if ($notice->isRepeat() || ($notice->getProfile()->getID() != $profile->getID())) {
  134. return true;
  135. }
  136. $postman = new Activitypub_postman($profile, $other);
  137. $postman->delete_note($notice);
  138. return true;
  139. }
  140. }