apistatusesfavs.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. * Show up to 100 favs of a notice
  18. *
  19. * @category API
  20. * @package GNUsocial
  21. * @author Hannes Mannerheim <h@nnesmannerhe.im>
  22. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  23. */
  24. defined('GNUSOCIAL') || die();
  25. /**
  26. * Show up to 100 favs of a notice
  27. *
  28. * @package GNUsocial
  29. * @author Hannes Mannerheim <h@nnesmannerhe.im>
  30. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  31. */
  32. class ApiStatusesFavsAction extends ApiAuthAction
  33. {
  34. const MAXCOUNT = 100;
  35. // Notice object for which to retrieve favs
  36. public $original = null;
  37. public $cnt = self::MAXCOUNT;
  38. /**
  39. * Take arguments for running
  40. *
  41. * @param array $args $_REQUEST args
  42. *
  43. * @return boolean success flag
  44. */
  45. protected function prepare(array $args = [])
  46. {
  47. parent::prepare($args);
  48. if ($this->format !== 'json') {
  49. $this->clientError('This method currently only serves JSON.', 415);
  50. }
  51. $id = $this->trimmed('id');
  52. $this->original = Notice::getKV('id', $id);
  53. if (!($this->original instanceof Notice)) {
  54. // TRANS: Client error displayed trying to display redents of a non-exiting notice.
  55. $this->clientError(_('No such notice.'), 400);
  56. }
  57. $cnt = $this->trimmed('count');
  58. if (empty($cnt) || !is_integer($cnt)) {
  59. $cnt = 100;
  60. } else {
  61. $this->cnt = min((int)$cnt, self::MAXCOUNT);
  62. }
  63. return true;
  64. }
  65. /**
  66. * Handle the request
  67. *
  68. * Get favs and return them as json object
  69. *
  70. * @param array $args $_REQUEST data (unused)
  71. *
  72. * @return void
  73. */
  74. protected function handle()
  75. {
  76. parent::handle();
  77. $fave = new Fave();
  78. $fave->selectAdd();
  79. $fave->selectAdd('user_id');
  80. $fave->notice_id = $this->original->id;
  81. $fave->orderBy('modified, user_id');
  82. if (!is_null($this->cnt)) {
  83. $fave->limit(0, $this->cnt);
  84. }
  85. $ids = $fave->fetchAll('user_id');
  86. // Get nickname and profile image.
  87. $ids_with_profile_data = [];
  88. foreach (array_values($ids) as $i => $id) {
  89. $profile = Profile::getKV('id', $id);
  90. $ids_with_profile_data[$i]['user_id'] = $id;
  91. $ids_with_profile_data[$i]['nickname'] = $profile->nickname;
  92. $ids_with_profile_data[$i]['fullname'] = $profile->fullname;
  93. $ids_with_profile_data[$i]['profileurl'] = $profile->profileurl;
  94. $profile = new Profile();
  95. $profile->id = $id;
  96. $avatarurl = $profile->avatarUrl(24);
  97. $ids_with_profile_data[$i]['avatarurl'] = $avatarurl;
  98. }
  99. $this->initDocument('json');
  100. $this->showJsonObjects($ids_with_profile_data);
  101. $this->endDocument('json');
  102. }
  103. /**
  104. * Return true if read only.
  105. *
  106. * MAY override
  107. *
  108. * @param array $args other arguments
  109. *
  110. * @return boolean is read only action?
  111. */
  112. public function isReadOnly($args)
  113. {
  114. return true;
  115. }
  116. }