apistatusesfavs.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Show up to 100 favs of a notice
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category API
  23. * @package GNUsocial
  24. * @author Hannes Mannerheim <h@nnesmannerhe.im>
  25. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  26. * @link http://www.gnu.org/software/social/
  27. */
  28. if (!defined('GNUSOCIAL')) { exit(1); }
  29. /**
  30. * Show up to 100 favs of a notice
  31. *
  32. */
  33. class ApiStatusesFavsAction extends ApiAuthAction
  34. {
  35. const MAXCOUNT = 100;
  36. var $original = null; // Notice object for which to retrieve favs
  37. var $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=array())
  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');
  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 = array();
  88. $i=0;
  89. foreach($ids as $id) {
  90. $profile = Profile::getKV('id', $id);
  91. $ids_with_profile_data[$i]['user_id'] = $id;
  92. $ids_with_profile_data[$i]['nickname'] = $profile->nickname;
  93. $ids_with_profile_data[$i]['fullname'] = $profile->fullname;
  94. $ids_with_profile_data[$i]['profileurl'] = $profile->profileurl;
  95. $profile = new Profile();
  96. $profile->id = $id;
  97. $avatarurl = $profile->avatarUrl(24);
  98. $ids_with_profile_data[$i]['avatarurl'] = $avatarurl;
  99. $i++;
  100. }
  101. $this->initDocument('json');
  102. $this->showJsonObjects($ids_with_profile_data);
  103. $this->endDocument('json');
  104. }
  105. /**
  106. * Return true if read only.
  107. *
  108. * MAY override
  109. *
  110. * @param array $args other arguments
  111. *
  112. * @return boolean is read only action?
  113. */
  114. function isReadOnly($args)
  115. {
  116. return true;
  117. }
  118. }