apactoroutbox.php 4.4 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 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. * Inbox Request Handler
  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 apActorOutboxAction extends ManagedAction
  35. {
  36. protected $needLogin = false;
  37. protected $canPost = true;
  38. /**
  39. * Handle the Outbox request
  40. *
  41. * @author Daniel Supernault <danielsupernault@gmail.com>
  42. */
  43. protected function handle()
  44. {
  45. try {
  46. $profile = Profile::getByID($this->trimmed('id'));
  47. $profile_id = $profile->getID();
  48. } catch (Exception $e) {
  49. ActivityPubReturn::error('Invalid Actor URI.', 404);
  50. }
  51. if (!$profile->isLocal()) {
  52. ActivityPubReturn::error("This is not a local user.", 403);
  53. }
  54. if (!isset($_GET["page"])) {
  55. $page = 0;
  56. } else {
  57. $page = intval($this->trimmed('page'));
  58. }
  59. if ($page < 0) {
  60. ActivityPubReturn::error('Invalid page number.');
  61. }
  62. $since = ($page - 1) * PROFILES_PER_MINILIST;
  63. $limit = (($page - 1) == 0 ? 1 : $page) * PROFILES_PER_MINILIST;
  64. /* Calculate total items */
  65. $total_notes = $profile->noticeCount();
  66. $total_pages = ceil($total_notes / PROFILES_PER_MINILIST);
  67. $res = [
  68. '@context' => [
  69. "https://www.w3.org/ns/activitystreams",
  70. "https://w3id.org/security/v1",
  71. ],
  72. 'id' => common_local_url('apActorOutbox', ['id' => $profile_id]).(($page != 0) ? '?page='.$page : ''),
  73. 'type' => ($page == 0 ? 'OrderedCollection' : 'OrderedCollectionPage'),
  74. 'totalItems' => $total_notes
  75. ];
  76. if ($page == 0) {
  77. $res['first'] = common_local_url('apActorOutbox', ['id' => $profile_id]).'?page=1';
  78. } else {
  79. $res['orderedItems'] = $this->generate_outbox($profile);
  80. $res['partOf'] = common_local_url('apActorOutbox', ['id' => $profile_id]);
  81. if ($page+1 < $total_pages) {
  82. $res['next'] = common_local_url('apActorOutbox', ['id' => $profile_id]).'page='.($page+1 == 1 ? 2 : $page+1);
  83. }
  84. if ($page > 1) {
  85. $res['prev'] = common_local_url('apActorOutbox', ['id' => $profile_id]).'?page='.($page-1 <= 0 ? 1 : $page-1);
  86. }
  87. }
  88. ActivityPubReturn::answer($res);
  89. }
  90. /**
  91. * Generates a list of people following given profile.
  92. *
  93. * @param Profile $profile
  94. * @return array of Notices
  95. * @throws EmptyPkeyValueException
  96. * @throws InvalidUrlException
  97. * @throws ServerException
  98. * @author Daniel Supernault <danielsupernault@gmail.com>
  99. */
  100. public function generate_outbox($profile)
  101. {
  102. /* Fetch Notices */
  103. $notices = [];
  104. $notice = $profile->getNotices();
  105. while ($notice->fetch()) {
  106. $note = $notice;
  107. // TODO: Handle other types
  108. if ($note->object_type == 'http://activitystrea.ms/schema/1.0/note') {
  109. $notices[] = Activitypub_create::create_to_array(
  110. ActivityPubPlugin::actor_uri($note->getProfile()),
  111. Activitypub_notice::notice_to_array($note)
  112. );
  113. }
  114. }
  115. return $notices;
  116. }
  117. }