apifavsandrepeats.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Returns both favs and repeats for 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. * Ouputs information for a user, specified by ID or screen name.
  31. * The user's most recent status will be returned inline.
  32. */
  33. class ApiFavsAndRepeatsAction extends ApiPrivateAuthAction
  34. {
  35. /**
  36. * Take arguments for running
  37. *
  38. * @param array $args $_REQUEST args
  39. *
  40. * @return boolean success flag
  41. *
  42. */
  43. protected function prepare(array $args=array())
  44. {
  45. parent::prepare($args);
  46. $this->format = 'json';
  47. $this->notice_id = $this->trimmed('notice_id');
  48. $this->original = Notice::getKV('id', $this->notice_id);
  49. if (empty($this->original)) {
  50. // TRANS: Client error displayed trying to display redents of a non-exiting notice.
  51. $this->clientError(_('No such notice.'), 400);
  52. return false;
  53. }
  54. $cnt = $this->trimmed('count');
  55. if (empty($cnt) || !is_integer($cnt)) {
  56. $this->cnt = 100;
  57. } else {
  58. $this->cnt = min((int)$cnt, self::MAXCOUNT);
  59. }
  60. return true;
  61. }
  62. /**
  63. * Handle the request
  64. *
  65. * Check the format and show the user info
  66. *
  67. * @param array $args $_REQUEST data (unused)
  68. *
  69. * @return void
  70. */
  71. protected function handle()
  72. {
  73. parent::handle();
  74. // since this api method is in practice only used when expanding a
  75. // notice, we can assume the user has seen the notice in question,
  76. // an no longer need a notification about it. mark reply/mention-
  77. // notifications tied to this notice and the current profile as read
  78. if($this->auth_user) {
  79. QvitterPlugin::markNotificationAsSeen($this->notice_id,$this->auth_user->id,'mention');
  80. QvitterPlugin::markNotificationAsSeen($this->notice_id,$this->auth_user->id,'reply');
  81. }
  82. // favs
  83. $fave = new Fave();
  84. $fave->selectAdd();
  85. $fave->selectAdd('user_id');
  86. $fave->selectAdd('modified');
  87. $fave->notice_id = $this->original->id;
  88. $fave->orderBy('modified');
  89. if (!is_null($this->cnt)) {
  90. $fave->limit(0, $this->cnt);
  91. }
  92. $fav_ids = $fave->fetchAll('user_id', 'modified');
  93. // get nickname and profile image
  94. $fav_ids_with_profile_data = array();
  95. $i=0;
  96. foreach($fav_ids as $id=>$time) {
  97. $profile = Profile::getKV('id', $id);
  98. $fav_ids_with_profile_data[$i]['user_id'] = $id;
  99. $fav_ids_with_profile_data[$i]['nickname'] = $profile->nickname;
  100. $fav_ids_with_profile_data[$i]['fullname'] = $profile->fullname;
  101. $fav_ids_with_profile_data[$i]['profileurl'] = $profile->profileurl;
  102. $fav_ids_with_profile_data[$i]['time'] = strtotime($time);
  103. $profile = new Profile();
  104. $profile->id = $id;
  105. $avatarurl = $profile->avatarUrl(48);
  106. $fav_ids_with_profile_data[$i]['avatarurl'] = $avatarurl;
  107. $i++;
  108. }
  109. // repeats
  110. $notice = new Notice();
  111. $notice->selectAdd(); // clears it
  112. $notice->selectAdd('profile_id');
  113. $notice->selectAdd('created');
  114. $notice->repeat_of = $this->original->id;
  115. $notice->verb = ActivityVerb::SHARE;
  116. $notice->orderBy('created, id'); // NB: asc!
  117. if (!is_null($this->cnt)) {
  118. $notice->limit(0, $this->cnt);
  119. }
  120. $repeat_ids = $notice->fetchAll('profile_id','created');
  121. // get nickname and profile image
  122. $repeat_ids_with_profile_data = array();
  123. $i=0;
  124. foreach($repeat_ids as $id=>$time) {
  125. $profile = Profile::getKV('id', $id);
  126. $repeat_ids_with_profile_data[$i]['user_id'] = $id;
  127. $repeat_ids_with_profile_data[$i]['nickname'] = $profile->nickname;
  128. $repeat_ids_with_profile_data[$i]['fullname'] = $profile->fullname;
  129. $repeat_ids_with_profile_data[$i]['profileurl'] = $profile->profileurl;
  130. $repeat_ids_with_profile_data[$i]['time'] = strtotime($time);
  131. $profile = new Profile();
  132. $profile->id = $id;
  133. $avatarurl = $profile->avatarUrl(48);
  134. $repeat_ids_with_profile_data[$i]['avatarurl'] = $avatarurl;
  135. $i++;
  136. }
  137. $favs_and_repeats = array('favs'=>$fav_ids_with_profile_data,'repeats'=>$repeat_ids_with_profile_data);
  138. $this->initDocument('json');
  139. $this->showJsonObjects($favs_and_repeats);
  140. $this->endDocument('json');
  141. }
  142. /**
  143. * Return true if read only.
  144. *
  145. * MAY override
  146. *
  147. * @param array $args other arguments
  148. *
  149. * @return boolean is read only action?
  150. */
  151. function isReadOnly($args)
  152. {
  153. return true;
  154. }
  155. }