apactorliked.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 Liked 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 apActorLikedAction extends ManagedAction
  35. {
  36. protected $needLogin = false;
  37. protected $canPost = true;
  38. /**
  39. * Handle the Liked Collection request
  40. *
  41. * @return void
  42. * @throws EmptyPkeyValueException
  43. * @throws ServerException
  44. * @author Diogo Cordeiro <diogo@fc.up.pt>
  45. */
  46. protected function handle()
  47. {
  48. try {
  49. $profile = Profile::getByID($this->trimmed('id'));
  50. $profile_id = $profile->getID();
  51. } catch (Exception $e) {
  52. ActivityPubReturn::error('Invalid Actor URI.', 404);
  53. }
  54. if (!$profile->isLocal()) {
  55. ActivityPubReturn::error("This is not a local user.", 403);
  56. }
  57. $limit = intval($this->trimmed('limit'));
  58. $since_id = intval($this->trimmed('since_id'));
  59. $max_id = intval($this->trimmed('max_id'));
  60. $limit = empty($limit) ? 40 : $limit; // Default is 40
  61. $since_id = empty($since_id) ? null : $since_id;
  62. $max_id = empty($max_id) ? null : $max_id;
  63. // Max is 80
  64. if ($limit > 80) {
  65. $limit = 80;
  66. }
  67. $fave = $this->fetch_faves($profile_id, $limit, $since_id, $max_id);
  68. $faves = array();
  69. while ($fave->fetch()) {
  70. $faves[] = $this->pretty_fave(clone ($fave));
  71. }
  72. $res = [
  73. '@context' => [
  74. "https://www.w3.org/ns/activitystreams",
  75. "https://w3id.org/security/v1",
  76. ],
  77. 'id' => common_local_url('apActorLiked', ['id' => $profile_id]),
  78. 'type' => 'OrderedCollection',
  79. 'totalItems' => Fave::countByProfile($profile),
  80. 'orderedItems' => $faves
  81. ];
  82. ActivityPubReturn::answer($res);
  83. }
  84. /**
  85. * Take a fave object and turns it in a pretty array to be used
  86. * as a plugin answer
  87. *
  88. * @param Fave $fave_object
  89. * @return array pretty array representating a Fave
  90. * @throws EmptyPkeyValueException
  91. * @throws ServerException
  92. * @author Diogo Cordeiro <diogo@fc.up.pt>
  93. */
  94. protected function pretty_fave($fave_object)
  95. {
  96. $res = [
  97. 'created' => $fave_object->created,
  98. 'object' => Activitypub_notice::notice_to_array(Notice::getByID($fave_object->notice_id))
  99. ];
  100. return $res;
  101. }
  102. /**
  103. * Fetch faves
  104. *
  105. * @author Diogo Cordeiro <diogo@fc.up.pt>
  106. * @param int $user_id
  107. * @param int $limit
  108. * @param int $since_id
  109. * @param int $max_id
  110. * @return Fave fetchable fave collection
  111. */
  112. private static function fetch_faves(
  113. $user_id,
  114. $limit = 40,
  115. $since_id = null,
  116. $max_id = null
  117. ) {
  118. $fav = new Fave();
  119. $fav->user_id = $user_id;
  120. $fav->orderBy('modified DESC');
  121. if ($since_id != null) {
  122. $fav->whereAdd("notice_id > {$since_id}");
  123. }
  124. if ($max_id != null) {
  125. $fav->whereAdd("notice_id < {$max_id}");
  126. }
  127. $fav->limit($limit);
  128. $fav->find();
  129. return $fav;
  130. }
  131. }