apactoroutbox.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 Diogo Cordeiro <diogo@fc.up.pt>
  23. * @copyright 2018 Free Software Foundation http://fsf.org
  24. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  25. * @link https://www.gnu.org/software/social/
  26. */
  27. if (!defined('GNUSOCIAL')) {
  28. exit(1);
  29. }
  30. /**
  31. * Inbox Request Handler
  32. *
  33. * @category Plugin
  34. * @package GNUsocial
  35. * @author Diogo Cordeiro <diogo@fc.up.pt>
  36. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  37. * @link http://www.gnu.org/software/social/
  38. */
  39. class apActorOutboxAction extends ManagedAction
  40. {
  41. protected $needLogin = false;
  42. protected $canPost = true;
  43. /**
  44. * Handle the Outbox request
  45. *
  46. * @author Daniel Supernault <danielsupernault@gmail.com>
  47. */
  48. protected function handle()
  49. {
  50. try {
  51. $profile = Profile::getByID($this->trimmed('id'));
  52. $profile_id = $profile->getID();
  53. } catch (Exception $e) {
  54. ActivityPubReturn::error('Invalid Actor URI.', 404);
  55. }
  56. if (!$profile->isLocal()) {
  57. ActivityPubReturn::error("This is not a local user.", 403);
  58. }
  59. if (!isset($_GET["page"])) {
  60. $page = 0;
  61. } else {
  62. $page = intval($this->trimmed('page'));
  63. }
  64. if ($page < 0) {
  65. ActivityPubReturn::error('Invalid page number.');
  66. }
  67. $since = ($page - 1) * PROFILES_PER_MINILIST;
  68. $limit = (($page - 1) == 0 ? 1 : $page) * PROFILES_PER_MINILIST;
  69. /* Calculate total items */
  70. $total_notes = $profile->noticeCount();
  71. $total_pages = ceil($total_notes / PROFILES_PER_MINILIST);
  72. $res = [
  73. '@context' => [
  74. "https://www.w3.org/ns/activitystreams",
  75. "https://w3id.org/security/v1",
  76. ],
  77. 'id' => common_local_url('apActorOutbox', ['id' => $profile_id]).(($page != 0) ? '?page='.$page : ''),
  78. 'type' => ($page == 0 ? 'OrderedCollection' : 'OrderedCollectionPage'),
  79. 'totalItems' => $total_notes
  80. ];
  81. if ($page == 0) {
  82. $res['first'] = common_local_url('apActorOutbox', ['id' => $profile_id]).'?page=1';
  83. } else {
  84. $res['orderedItems'] = $this->generate_outbox($profile);
  85. $res['partOf'] = common_local_url('apActorOutbox', ['id' => $profile_id]);
  86. if ($page+1 < $total_pages) {
  87. $res['next'] = common_local_url('apActorOutbox', ['id' => $profile_id]).'page='.($page+1 == 1 ? 2 : $page+1);
  88. }
  89. if ($page > 1) {
  90. $res['prev'] = common_local_url('apActorOutbox', ['id' => $profile_id]).'?page='.($page-1 <= 0 ? 1 : $page-1);
  91. }
  92. }
  93. ActivityPubReturn::answer($res);
  94. }
  95. /**
  96. * Generates a list of people following given profile.
  97. *
  98. * @author Daniel Supernault <danielsupernault@gmail.com>
  99. * @param Profile $profile
  100. * @return Array of Notices
  101. */
  102. public function generate_outbox($profile)
  103. {
  104. /* Fetch Notices */
  105. $notices = [];
  106. $notice = $profile->getNotices();
  107. while ($notice->fetch()) {
  108. $note = $notice;
  109. // TODO: Handle other types
  110. if ($note->object_type == 'http://activitystrea.ms/schema/1.0/note') {
  111. $notices[] = Activitypub_create::create_to_array(
  112. ActivityPubPlugin::actor_uri($note->getProfile()),
  113. Activitypub_notice::notice_to_array($note)
  114. );
  115. }
  116. }
  117. return $notices;
  118. }
  119. }