Activitypub_message.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 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. * ActivityPub direct note representation
  27. *
  28. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  29. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  30. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  31. */
  32. class Activitypub_message
  33. {
  34. /**
  35. * Generates a pretty message from a Notice object
  36. *
  37. * @param Notice $message
  38. * @return array array to be used in a response
  39. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  40. */
  41. public static function message_to_array(Notice $message): array
  42. {
  43. $from = $message->getProfile();
  44. $tags = [];
  45. foreach ($message->getTags() as $tag) {
  46. if ($tag != "") { // Hacky workaround to avoid stupid outputs
  47. $tags[] = Activitypub_tag::tag_to_array($tag);
  48. }
  49. }
  50. $to = [];
  51. foreach ($message->getAttentionProfiles() as $to_profile) {
  52. $to[] = $href = $to_profile->getUri();
  53. $tags[] = Activitypub_mention_tag::mention_tag_to_array_from_values($href, $to_profile->getNickname().'@'.parse_url($href, PHP_URL_HOST));
  54. }
  55. $item = [
  56. '@context' => 'https://www.w3.org/ns/activitystreams',
  57. 'id' => common_local_url('showmessage', ['message' => $message->getID()]),
  58. 'type' => 'Note',
  59. 'published' => str_replace(' ', 'T', $message->created).'Z',
  60. 'attributedTo' => $from->getUri(),
  61. 'to' => $to,
  62. 'cc' => [],
  63. 'content' => $message->getRendered(),
  64. 'attachment' => [],
  65. 'tag' => $tags
  66. ];
  67. return $item;
  68. }
  69. /**
  70. * Create a private Notice via ActivityPub Note Object.
  71. * Returns created Notice.
  72. *
  73. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  74. * @param array $object
  75. * @param Profile $actor_profile
  76. * @return Notice
  77. * @throws Exception
  78. */
  79. public static function create_message(array $object, Profile $actor_profile = null): Notice
  80. {
  81. return Activitypub_notice::create_notice($object, $actor_profile, true);
  82. }
  83. }