apactorliked.php 4.5 KB

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