apactorfollowers.php 4.5 KB

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