activitypubqueuehandler.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 Bruno Casteleiro <brunoccast@fc.up.pt>
  21. * @copyright 2019 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 2019 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 ActivityPubQueueHandler 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';
  39. }
  40. /**
  41. * Notice distribution handler.
  42. *
  43. * @param Notice $notice notice to be distributed.
  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($notice): bool
  51. {
  52. if (!($notice instanceof Notice)) {
  53. common_log(LOG_ERR, "Got a bogus notice, not distributing");
  54. return true;
  55. }
  56. $profile = $notice->getProfile();
  57. if (!$profile->isLocal()) {
  58. return true;
  59. }
  60. // Ignore activity/non-post/share-verb notices
  61. $is_valid_verb = ($notice->verb == ActivityVerb::POST ||
  62. $notice->verb == ActivityVerb::SHARE);
  63. if ($notice->source == 'activity' || !$is_valid_verb) {
  64. common_log(LOG_ERR, "Ignoring distribution of notice:{$notice->id}: activity source or invalid Verb");
  65. return true;
  66. }
  67. $other = Activitypub_profile::from_profile_collection(
  68. $notice->getAttentionProfiles()
  69. );
  70. // Handling a reply?
  71. if ($notice->reply_to) {
  72. try {
  73. $parent_notice = $notice->getParent();
  74. try {
  75. $other[] = Activitypub_profile::from_profile($parent_notice->getProfile());
  76. } catch (Exception $e) {
  77. // Local user can be ignored
  78. }
  79. foreach ($parent_notice->getAttentionProfiles() as $mention) {
  80. try {
  81. $other[] = Activitypub_profile::from_profile($mention);
  82. } catch (Exception $e) {
  83. // Local user can be ignored
  84. }
  85. }
  86. } catch (NoParentNoticeException $e) {
  87. // This is not a reply to something (has no parent)
  88. } catch (NoResultException $e) {
  89. // Parent author's profile not found! Complain louder?
  90. common_log(LOG_ERR, "Parent notice's author not found: ".$e->getMessage());
  91. }
  92. }
  93. // Handling an Announce?
  94. if ($notice->isRepeat()) {
  95. $repeated_notice = Notice::getKV('id', $notice->repeat_of);
  96. if ($repeated_notice instanceof Notice) {
  97. $other = array_merge($other,
  98. Activitypub_profile::from_profile_collection(
  99. $repeated_notice->getAttentionProfiles()
  100. ));
  101. try {
  102. $other[] = Activitypub_profile::from_profile($repeated_notice->getProfile());
  103. } catch (Exception $e) {
  104. // Local user can be ignored
  105. }
  106. // That was it
  107. $postman = new Activitypub_postman($profile, $other);
  108. $postman->announce($repeated_notice);
  109. }
  110. // either made the announce or found nothing to repeat
  111. return true;
  112. }
  113. // That was it
  114. $postman = new Activitypub_postman($profile, $other);
  115. $postman->create_note($notice);
  116. return true;
  117. }
  118. }