Activitypub_announce.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 implementation for GNU social
  18. *
  19. * @package GNUsocial
  20. * @author Diogo Cordeiro <diogo@fc.up.pt>
  21. * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
  22. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  23. * @link http://www.gnu.org/software/social/
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * ActivityPub error representation
  28. *
  29. * @category Plugin
  30. * @package GNUsocial
  31. * @author Diogo Cordeiro <diogo@fc.up.pt>
  32. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  33. */
  34. class Activitypub_announce
  35. {
  36. /**
  37. * Generates an ActivityPub representation of a Announce
  38. *
  39. * @param Profile $actor
  40. * @param Notice $notice
  41. * @param Notice $repeat_of
  42. * @return array pretty array to be used in a response
  43. * @author Diogo Cordeiro <diogo@fc.up.pt>
  44. */
  45. public static function announce_to_array(
  46. Profile $actor,
  47. Notice $notice,
  48. Notice $repeat_of
  49. ): array {
  50. $actor_uri = $actor->getUri();
  51. $to = [common_local_url('apActorFollowers', ['id' => $actor->getID()])];
  52. foreach ($notice->getAttentionProfiles() as $to_profile) {
  53. $to[] = $to_profile->getUri();
  54. }
  55. $cc[]= 'https://www.w3.org/ns/activitystreams#Public';
  56. $res = [
  57. '@context' => 'https://www.w3.org/ns/activitystreams',
  58. 'id' => Activitypub_notice::getUri($notice),
  59. 'type' => 'Announce',
  60. 'actor' => $actor_uri,
  61. 'object' => Activitypub_notice::getUri($repeat_of),
  62. 'to' => $to,
  63. 'cc' => $cc,
  64. ];
  65. return $res;
  66. }
  67. /**
  68. * Convenience function for posting a repeat of an existing message.
  69. *
  70. * @param string $uri
  71. * @param Profile $actor Profile which is doing the repeat
  72. * @param Notice $target
  73. * @return Notice
  74. */
  75. public static function repeat(string $uri, Profile $actor, Notice $target): Notice
  76. {
  77. // TRANS: Message used to repeat a notice. RT is the abbreviation of 'retweet'.
  78. // TRANS: %1$s is the repeated user's name, %2$s is the repeated notice.
  79. $content = sprintf(
  80. _('RT @%1$s %2$s'),
  81. $actor->getNickname(),
  82. $target->getContent()
  83. );
  84. $options = [
  85. 'source' => 'ActivityPub',
  86. 'uri' => $uri,
  87. 'is_local' => ($actor->isLocal() ? Notice::LOCAL_PUBLIC : Notice::REMOTE),
  88. 'repeat_of' => $target->getParent()->getID(),
  89. 'scope' => $target->getScope(),
  90. ];
  91. // Scope is same as this one's
  92. return Notice::saveNew(
  93. $actor->getID(),
  94. $content,
  95. 'ActivityPub',
  96. $options
  97. );
  98. }
  99. }