Activitypub_notice.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * GNU social - a federating social network
  4. *
  5. * ActivityPubPlugin implementation for GNU Social
  6. *
  7. * LICENCE: This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @category Plugin
  21. * @package GNUsocial
  22. * @author Daniel Supernault <danielsupernault@gmail.com>
  23. * @author Diogo Cordeiro <diogo@fc.up.pt>
  24. * @copyright 2018 Free Software Foundation http://fsf.org
  25. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  26. * @link https://www.gnu.org/software/social/
  27. */
  28. if (!defined ('GNUSOCIAL')) {
  29. exit (1);
  30. }
  31. /**
  32. * ActivityPub notice representation
  33. *
  34. * @category Plugin
  35. * @package GNUsocial
  36. * @author Daniel Supernault <danielsupernault@gmail.com>
  37. * @author Diogo Cordeiro <diogo@fc.up.pt>
  38. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  39. * @link http://www.gnu.org/software/social/
  40. */
  41. class Activitypub_notice extends Managed_DataObject
  42. {
  43. /**
  44. * Generates a pretty notice from a Notice object
  45. *
  46. * @param Notice $notice
  47. * @return pretty array to be used in a response
  48. */
  49. public static function notice_to_array ($notice)
  50. {
  51. $attachments = array ();
  52. foreach($notice->attachments () as $attachment) {
  53. $attachments[] = Activitypub_attachment::attachment_to_array ($attachment);
  54. }
  55. $tags = array ();
  56. foreach($notice->getTags()as $tag) {
  57. if ($tag != "") { // Hacky workaround to avoid stupid outputs
  58. $tags[] = Activitypub_tag::tag_to_array ($tag);
  59. }
  60. }
  61. $to = array ();
  62. foreach ($notice->getAttentionProfileIDs () as $to_id) {
  63. $to[] = Profile::getById ($to_id)->getUri ();
  64. }
  65. if (!is_null($to)) {
  66. $to = array ("https://www.w3.org/ns/activitystreams#Public");
  67. }
  68. $item = [
  69. 'id' => $notice->getUrl (),
  70. 'type' => 'Notice',
  71. 'actor' => $notice->getProfile ()->getUrl (),
  72. 'published' => $notice->getCreated (),
  73. 'to' => $to,
  74. 'content' => $notice->getContent (),
  75. 'url' => $notice->getUrl (),
  76. 'reply_to' => empty($notice->reply_to) ? null : Notice::getById($notice->reply_to)->getUrl (),
  77. 'is_local' => $notice->isLocal (),
  78. 'conversation' => intval ($notice->conversation),
  79. 'attachment' => $attachments,
  80. 'tag' => $tags
  81. ];
  82. return $item;
  83. }
  84. }