apactorliked.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. * @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. $limit = intval($this->trimmed('limit'));
  61. $since_id = intval($this->trimmed('since_id'));
  62. $max_id = intval($this->trimmed('max_id'));
  63. $limit = empty($limit) ? 40 : $limit; // Default is 40
  64. $since_id = empty($since_id) ? null : $since_id;
  65. $max_id = empty($max_id) ? null : $max_id;
  66. // Max is 80
  67. if ($limit > 80) {
  68. $limit = 80;
  69. }
  70. $fave = $this->fetch_faves($profile_id, $limit, $since_id, $max_id);
  71. $faves = array();
  72. while ($fave->fetch()) {
  73. $faves[] = $this->pretty_fave(clone ($fave));
  74. }
  75. $res = [
  76. '@context' => [
  77. "https://www.w3.org/ns/activitystreams",
  78. "https://w3id.org/security/v1",
  79. ],
  80. 'id' => common_local_url('apActorLiked', ['id' => $profile_id]),
  81. 'type' => 'OrderedCollection',
  82. 'totalItems' => Fave::countByProfile($profile),
  83. 'orderedItems' => $faves
  84. ];
  85. ActivityPubReturn::answer($res);
  86. }
  87. /**
  88. * Take a fave object and turns it in a pretty array to be used
  89. * as a plugin answer
  90. *
  91. * @author Diogo Cordeiro <diogo@fc.up.pt>
  92. * @param Fave $fave_object
  93. * @return array pretty array representating a Fave
  94. */
  95. protected function pretty_fave($fave_object)
  96. {
  97. $res = [
  98. 'created' => $fave_object->created,
  99. 'object' => Activitypub_notice::notice_to_array(Notice::getByID($fave_object->notice_id))
  100. ];
  101. return $res;
  102. }
  103. /**
  104. * Fetch faves
  105. *
  106. * @author Diogo Cordeiro <diogo@fc.up.pt>
  107. * @param int32 $user_id
  108. * @param int32 $limit
  109. * @param int32 $since_id
  110. * @param int32 $max_id
  111. * @return Fave fetchable fave collection
  112. */
  113. private static function fetch_faves(
  114. $user_id,
  115. $limit = 40,
  116. $since_id = null,
  117. $max_id = null
  118. ) {
  119. $fav = new Fave();
  120. $fav->user_id = $user_id;
  121. $fav->orderBy('modified DESC');
  122. if ($since_id != null) {
  123. $fav->whereAdd("notice_id > {$since_id}");
  124. }
  125. if ($max_id != null) {
  126. $fav->whereAdd("notice_id < {$max_id}");
  127. }
  128. $fav->limit($limit);
  129. $fav->find();
  130. return $fav;
  131. }
  132. }