activitypubqueuehandler.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. * @author Diogo Cordeiro <diogo@fc.up.pt>
  22. * @copyright 2019-2020 Free Software Foundation, Inc http://www.fsf.org
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * @copyright 2019-2020 Free Software Foundation, Inc http://www.fsf.org
  28. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  29. */
  30. class ActivityPubQueueHandler extends QueueHandler
  31. {
  32. /**
  33. * Getter of the queue transport name.
  34. *
  35. * @return string transport name
  36. */
  37. public function transport(): string
  38. {
  39. return 'activitypub';
  40. }
  41. /**
  42. * Notice distribution handler.
  43. *
  44. * @param Notice $notice notice to be distributed.
  45. * @return bool true on success, false otherwise
  46. * @throws HTTP_Request2_Exception
  47. * @throws InvalidUrlException
  48. * @throws ServerException
  49. * @author Diogo Cordeiro <diogo@fc.up.pt>
  50. */
  51. public function handle($notice): bool
  52. {
  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. $other = Activitypub_profile::from_profile_collection(
  66. $notice->getAttentionProfiles()
  67. );
  68. try {
  69. // Handling a Create?
  70. if (ActivityUtils::compareVerbs($notice->verb, [ActivityVerb::POST, ActivityVerb::SHARE])) {
  71. return $this->handle_create($profile, $notice, $other);
  72. }
  73. // Handling a Like?
  74. if (ActivityUtils::compareVerbs($notice->verb, [ActivityVerb::FAVORITE])) {
  75. return $this->onEndFavorNotice($profile, $notice, $other);
  76. }
  77. // Handling a Delete Note?
  78. if (ActivityUtils::compareVerbs($notice->verb, [ActivityVerb::DELETE])) {
  79. return $this->onStartDeleteOwnNotice($profile, $notice, $other);
  80. }
  81. } catch (Exception $e) {
  82. // Postman handles issues with the failed queue
  83. common_debug('ActivityPub Queue Handler:'.$e->getMessage());
  84. }
  85. return true;
  86. }
  87. private function handle_create($profile, $notice, $other)
  88. {
  89. // Handling a reply?
  90. if ($notice->reply_to) {
  91. try {
  92. $parent_notice = $notice->getParent();
  93. try {
  94. $other[] = Activitypub_profile::from_profile($parent_notice->getProfile());
  95. } catch (Exception $e) {
  96. // Local user can be ignored
  97. }
  98. foreach ($parent_notice->getAttentionProfiles() as $mention) {
  99. try {
  100. $other[] = Activitypub_profile::from_profile($mention);
  101. } catch (Exception $e) {
  102. // Local user can be ignored
  103. }
  104. }
  105. } catch (NoParentNoticeException $e) {
  106. // This is not a reply to something (has no parent)
  107. } catch (NoResultException $e) {
  108. // Parent author's profile not found! Complain louder?
  109. common_log(
  110. LOG_ERR,
  111. "Parent notice's author not found: " . $e->getMessage()
  112. );
  113. }
  114. }
  115. // Handling an Announce?
  116. if ($notice->isRepeat()) {
  117. $repeated_notice = Notice::getKV('id', $notice->repeat_of);
  118. if ($repeated_notice instanceof Notice) {
  119. $other = array_merge(
  120. $other,
  121. Activitypub_profile::from_profile_collection(
  122. $repeated_notice->getAttentionProfiles()
  123. )
  124. );
  125. try {
  126. $other[] = Activitypub_profile::from_profile(
  127. $repeated_notice->getProfile()
  128. );
  129. } catch (Exception $e) {
  130. // Local user can be ignored
  131. }
  132. // That was it
  133. $postman = new Activitypub_postman($profile, $other);
  134. $postman->announce($notice, $repeated_notice);
  135. }
  136. // either made the announce or found nothing to repeat
  137. return true;
  138. }
  139. // That was it
  140. $postman = new Activitypub_postman($profile, $other);
  141. $postman->create_note($notice);
  142. return true;
  143. }
  144. /**
  145. * Notify remote users when their notices get favourited.
  146. *
  147. * @param Profile $profile of local user doing the faving
  148. * @param Notice $notice_liked Notice being favored
  149. * @return bool return value
  150. * @throws HTTP_Request2_Exception
  151. * @throws InvalidUrlException
  152. * @author Diogo Cordeiro <diogo@fc.up.pt>
  153. */
  154. public function onEndFavorNotice(Profile $profile, Notice $notice, $other)
  155. {
  156. $notice_liked = $notice->getParent();
  157. if ($notice_liked->reply_to) {
  158. try {
  159. $parent_notice = $notice_liked->getParent();
  160. try {
  161. $other[] = Activitypub_profile::from_profile($parent_notice->getProfile());
  162. } catch (Exception $e) {
  163. // Local user can be ignored
  164. }
  165. $other = array_merge(
  166. $other,
  167. Activitypub_profile::from_profile_collection(
  168. $parent_notice->getAttentionProfiles()
  169. )
  170. );
  171. } catch (NoParentNoticeException $e) {
  172. // This is not a reply to something (has no parent)
  173. } catch (NoResultException $e) {
  174. // Parent author's profile not found! Complain louder?
  175. common_log(LOG_ERR, "Parent notice's author not found: " . $e->getMessage());
  176. }
  177. }
  178. $postman = new Activitypub_postman($profile, $other);
  179. $postman->like($notice);
  180. return true;
  181. }
  182. /**
  183. * Notify remote users when their notices get deleted
  184. *
  185. * @param $user
  186. * @param $notice
  187. * @return bool hook flag
  188. * @throws HTTP_Request2_Exception
  189. * @throws InvalidUrlException
  190. * @author Diogo Cordeiro <diogo@fc.up.pt>
  191. */
  192. public function onStartDeleteOwnNotice($profile, $notice, $other)
  193. {
  194. // Handle delete locally either because:
  195. // 1. There's no undo-share logic yet
  196. // 2. The deleting user has privileges to do so (locally)
  197. if ($notice->isRepeat() || ($notice->getProfile()->getID() != $profile->getID())) {
  198. return true;
  199. }
  200. if ($notice->reply_to) {
  201. try {
  202. $parent_notice = $notice->getParent();
  203. try {
  204. $other[] = Activitypub_profile::from_profile($parent_notice->getProfile());
  205. } catch (Exception $e) {
  206. // Local user can be ignored
  207. }
  208. $other = array_merge(
  209. $other,
  210. Activitypub_profile::from_profile_collection(
  211. $parent_notice->getAttentionProfiles()
  212. )
  213. );
  214. } catch (NoParentNoticeException $e) {
  215. // This is not a reply to something (has no parent)
  216. } catch (NoResultException $e) {
  217. // Parent author's profile not found! Complain louder?
  218. common_log(LOG_ERR, "Parent notice's author not found: " . $e->getMessage());
  219. }
  220. }
  221. $postman = new Activitypub_postman($profile, $other);
  222. $postman->delete_note($notice);
  223. return true;
  224. }
  225. }