apactorfollowing.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. * Actor's Following Collection
  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 apActorFollowingAction extends ManagedAction
  35. {
  36. protected $needLogin = false;
  37. protected $canPost = true;
  38. /**
  39. * Handle the Following Collection request
  40. *
  41. * @return void
  42. * @throws Exception
  43. * @author Diogo Cordeiro <diogo@fc.up.pt>
  44. */
  45. protected function handle()
  46. {
  47. try {
  48. $profile = Profile::getByID($this->trimmed('id'));
  49. $profile_id = $profile->getID();
  50. } catch (Exception $e) {
  51. ActivityPubReturn::error('Invalid Actor URI.', 404);
  52. }
  53. if (!$profile->isLocal()) {
  54. ActivityPubReturn::error("This is not a local user.", 403);
  55. }
  56. if (!isset($_GET["page"])) {
  57. $page = 0;
  58. } else {
  59. $page = intval($this->trimmed('page'));
  60. }
  61. if ($page < 0) {
  62. ActivityPubReturn::error('Invalid page number.');
  63. }
  64. $since = ($page - 1) * PROFILES_PER_MINILIST;
  65. $limit = PROFILES_PER_MINILIST;
  66. /* Calculate total items */
  67. $total_subs = Activitypub_profile::subscriptionCount($profile);
  68. $total_pages = ceil($total_subs / PROFILES_PER_MINILIST);
  69. $res = [
  70. '@context' => [
  71. "https://www.w3.org/ns/activitystreams",
  72. "https://w3id.org/security/v1",
  73. ],
  74. 'id' => common_local_url('apActorFollowing', ['id' => $profile_id]).(($page != 0) ? '?page='.$page : ''),
  75. 'type' => ($page == 0 ? 'OrderedCollection' : 'OrderedCollectionPage'),
  76. 'totalItems' => $total_subs
  77. ];
  78. if ($page == 0) {
  79. $res['first'] = common_local_url('apActorFollowing', ['id' => $profile_id]).'?page=1';
  80. } else {
  81. $res['orderedItems'] = $this->generate_following($profile, $since, $limit);
  82. $res['partOf'] = common_local_url('apActorFollowing', ['id' => $profile_id]);
  83. if ($page+1 < $total_pages) {
  84. $res['next'] = common_local_url('apActorFollowing', ['id' => $profile_id]).'page='.($page+1 == 1 ? 2 : $page+1);
  85. }
  86. if ($page > 1) {
  87. $res['prev'] = common_local_url('apActorFollowing', ['id' => $profile_id]).'?page='.($page-1 <= 0 ? 1 : $page-1);
  88. }
  89. }
  90. ActivityPubReturn::answer($res);
  91. }
  92. /**
  93. * Generates the list of those a given profile is stalking.
  94. *
  95. * @param Profile $profile
  96. * @param int $since
  97. * @param int $limit
  98. * @return array of URIs
  99. * @throws Exception
  100. * @author Diogo Cordeiro <diogo@fc.up.pt>
  101. */
  102. public function generate_following($profile, $since, $limit)
  103. {
  104. $subs = [];
  105. try {
  106. $sub = Activitypub_profile::getSubscribed($profile, $since, $limit);
  107. /* Get followed' URLs */
  108. foreach ($sub as $s) {
  109. $subs[] = ActivityPubPlugin::actor_uri($s);
  110. }
  111. } catch (NoResultException $e) {
  112. // Just let the exception go on its merry way
  113. }
  114. return $subs;
  115. }
  116. }