apactorlikedcollection.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. * @author Daniel Supernault <danielsupernault@gmail.com>
  24. * @copyright 2018 Free Software Foundation http://fsf.org
  25. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  26. * @link https://www.gnu.org/software/social/
  27. */
  28. if (!defined ('GNUSOCIAL')) {
  29. exit (1);
  30. }
  31. /**
  32. * Actor's Liked Collection
  33. *
  34. * @category Plugin
  35. * @package GNUsocial
  36. * @author Diogo Cordeiro <diogo@fc.up.pt>
  37. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  38. * @link http://www.gnu.org/software/social/
  39. */
  40. class apActorLikedCollectionAction extends ManagedAction
  41. {
  42. protected $needLogin = false;
  43. protected $canPost = true;
  44. /**
  45. * Handle the Liked Collection request
  46. *
  47. * @return void
  48. */
  49. protected function handle ()
  50. {
  51. $nickname = $this->trimmed ('nickname');
  52. try {
  53. $user = User::getByNickname ($nickname);
  54. $profile = $user->getProfile ();
  55. $url = $profile->profileurl;
  56. } catch (Exception $e) {
  57. ActivityPubReturn::error ('Invalid username.');
  58. }
  59. $limit = intval ($this->trimmed ('limit'));
  60. $since_id = intval ($this->trimmed ('since_id'));
  61. $max_id = intval ($this->trimmed ('max_id'));
  62. $limit = empty ($limit) ? 40 : $limit; // Default is 40
  63. $since_id = empty ($since_id) ? null : $since_id;
  64. $max_id = empty ($max_id) ? null : $max_id;
  65. // Max is 80
  66. if ($limit > 80) {
  67. $limit = 80;
  68. }
  69. $fave = $this->fetch_faves($user->getID(), $limit, $since_id, $max_id);
  70. $faves = array();
  71. while ($fave->fetch ()) {
  72. $faves[] = $this->pretty_fave (clone ($fave));
  73. }
  74. $res = [
  75. '@context' => [
  76. "https://www.w3.org/ns/activitystreams",
  77. [
  78. "@language" => "en"
  79. ]
  80. ],
  81. 'id' => "{$url}/liked.json",
  82. 'type' => 'OrderedCollection',
  83. 'totalItems' => Fave::countByProfile ($profile),
  84. 'orderedItems' => $faves
  85. ];
  86. ActivityPubReturn::answer ($res);
  87. }
  88. /**
  89. * Take a fave object and turns it in a pretty array to be used
  90. * as a plugin answer
  91. *
  92. * @param Fave $fave_object
  93. * @return array pretty array representating a Fave
  94. */
  95. protected function pretty_fave ($fave_object)
  96. {
  97. $res = array("uri" => $fave_object->uri,
  98. "created" => $fave_object->created,
  99. "object" => Activitypub_notice::notice_to_array (Notice::getByID ($fave_object->notice_id)));
  100. return $res;
  101. }
  102. /**
  103. * Fetch faves
  104. *
  105. * @param int32 $user_id
  106. * @param int32 $limit
  107. * @param int32 $since_id
  108. * @param int32 $max_id
  109. * @return Fave fetchable fave collection
  110. */
  111. private static function fetch_faves ($user_id, $limit = 40, $since_id = null,
  112. $max_id = null)
  113. {
  114. $fav = new Fave ();
  115. $fav->user_id = $user_id;
  116. $fav->orderBy ('modified DESC');
  117. if ($since_id != null) {
  118. $fav->whereAdd ("notice_id > {$since_id}");
  119. }
  120. if ($max_id != null) {
  121. $fav->whereAdd ("notice_id < {$max_id}");
  122. }
  123. $fav->limit ($limit);
  124. $fav->find ();
  125. return $fav;
  126. }
  127. }