apactorfollowers.php 4.4 KB

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