apiqvitterblocks.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3. · ·
  4. · API for getting all blocked profiles for a profile ·
  5. · ·
  6. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  7. · ·
  8. · ·
  9. · Q V I T T E R ·
  10. · ·
  11. · https://git.gnu.io/h2p/Qvitter ·
  12. · ·
  13. · ·
  14. · <o) ·
  15. · /_//// ·
  16. · (____/ ·
  17. · (o< ·
  18. · o> \\\\_\ ·
  19. · \\) \____) ·
  20. · ·
  21. · ·
  22. · ·
  23. · Qvitter is free software: you can redistribute it and / or modify it ·
  24. · under the terms of the GNU Affero General Public License as published by ·
  25. · the Free Software Foundation, either version three of the License or (at ·
  26. · your option) any later version. ·
  27. · ·
  28. · Qvitter is distributed in hope that it will be useful but WITHOUT ANY ·
  29. · WARRANTY; without even the implied warranty of MERCHANTABILTY or FITNESS ·
  30. · FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for ·
  31. · more details. ·
  32. · ·
  33. · You should have received a copy of the GNU Affero General Public License ·
  34. · along with Qvitter. If not, see <http://www.gnu.org/licenses/>. ·
  35. · ·
  36. · Contact h@nnesmannerhe.im if you have any questions. ·
  37. · ·
  38. · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */
  39. if (!defined('STATUSNET')) {
  40. exit(1);
  41. }
  42. class ApiQvitterBlocksAction extends ApiPrivateAuthAction
  43. {
  44. var $profiles = null;
  45. /**
  46. * Take arguments for running
  47. *
  48. * @param array $args $_REQUEST args
  49. *
  50. * @return boolean success flag
  51. */
  52. protected function prepare(array $args=array())
  53. {
  54. parent::prepare($args);
  55. $this->format = 'json';
  56. $this->count = (int)$this->arg('count', 100);
  57. $arg_user = $this->getTargetUser($this->arg('id'));
  58. $this->target = ($this->auth_user) ? $this->auth_user->getProfile() : null;
  59. if (!($this->target instanceof Profile)) {
  60. // TRANS: Client error displayed when requesting a list of followers for a non-existing user.
  61. $this->clientError(_('No such user.'), 404);
  62. } else if($this->auth_user->id != $arg_user->id) {
  63. $this->clientError(_('You are only allowed to view your own blocks.'), 403);
  64. }
  65. $this->profiles = $this->getProfiles();
  66. return true;
  67. }
  68. /**
  69. * Handle the request
  70. *
  71. * Show the profiles
  72. *
  73. * @return void
  74. */
  75. protected function handle()
  76. {
  77. parent::handle();
  78. $this->initDocument('json');
  79. print json_encode($this->showProfiles());
  80. $this->endDocument('json');
  81. }
  82. /**
  83. * Get the user's blocked profiles
  84. *
  85. * @return array Profiles
  86. */
  87. protected function getProfiles()
  88. {
  89. $offset = ($this->page - 1) * $this->count;
  90. $limit = $this->count + 1;
  91. $blocks = null;
  92. $blocks = QvitterBlocked::getBlocked($this->target->id, $offset, $limit);
  93. if($blocks) {
  94. $profiles = array();
  95. while ($blocks->fetch()) {
  96. $this_profile_block = clone($blocks);
  97. $profiles[] = $this->getTargetProfile($this_profile_block->blocked);
  98. }
  99. return $profiles;
  100. } else {
  101. return false;
  102. }
  103. }
  104. /**
  105. * Is this action read only?
  106. *
  107. * @param array $args other arguments
  108. *
  109. * @return boolean true
  110. */
  111. function isReadOnly($args)
  112. {
  113. return true;
  114. }
  115. /**
  116. * When was this feed last modified?
  117. *
  118. * @return string datestamp of the latest profile in the stream
  119. */
  120. function lastModified()
  121. {
  122. if (!empty($this->profiles) && (count($this->profiles) > 0)) {
  123. return strtotime($this->profiles[0]->modified);
  124. }
  125. return null;
  126. }
  127. /**
  128. * An entity tag for this action
  129. *
  130. * Returns an Etag based on the action name, language, user ID, and
  131. * timestamps of the first and last profiles in the subscriptions list
  132. * There's also an indicator to show whether this action is being called
  133. * as /api/statuses/(friends|followers) or /api/(friends|followers)/ids
  134. *
  135. * @return string etag
  136. */
  137. function etag()
  138. {
  139. if (!empty($this->profiles) && (count($this->profiles) > 0)) {
  140. $last = count($this->profiles) - 1;
  141. return '"' . implode(
  142. ':',
  143. array($this->arg('action'),
  144. common_user_cache_hash($this->auth_user),
  145. common_language(),
  146. $this->target->id,
  147. 'Profiles',
  148. strtotime($this->profiles[0]->modified),
  149. strtotime($this->profiles[$last]->modified))
  150. )
  151. . '"';
  152. }
  153. return null;
  154. }
  155. /**
  156. * Show the profiles as Twitter-style useres and statuses
  157. *
  158. * @return void
  159. */
  160. function showProfiles()
  161. {
  162. $user_arrays = array();
  163. foreach ($this->profiles as $profile) {
  164. $user_arrays[] = $this->twitterUserArray($profile, false );
  165. }
  166. return $user_arrays;
  167. }
  168. }