apiqvitternotifications.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /* · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · ·
  3. · ·
  4. · ·
  5. · Q V I T T E R ·
  6. · ·
  7. · https://git.gnu.io/h2p/Qvitter ·
  8. · ·
  9. · ·
  10. · <o) ·
  11. · /_//// ·
  12. · (____/ ·
  13. · (o< ·
  14. · o> \\\\_\ ·
  15. · \\) \____) ·
  16. · ·
  17. · ·
  18. · ·
  19. · Qvitter is free software: you can redistribute it and / or modify it ·
  20. · under the terms of the GNU Affero General Public License as published by ·
  21. · the Free Software Foundation, either version three of the License or (at ·
  22. · your option) any later version. ·
  23. · ·
  24. · Qvitter is distributed in hope that it will be useful but WITHOUT ANY ·
  25. · WARRANTY; without even the implied warranty of MERCHANTABILTY or FITNESS ·
  26. · FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for ·
  27. · more details. ·
  28. · ·
  29. · You should have received a copy of the GNU Affero General Public License ·
  30. · along with Qvitter. If not, see <http://www.gnu.org/licenses/>. ·
  31. · ·
  32. · Contact h@nnesmannerhe.im if you have any questions. ·
  33. · ·
  34. · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · · */
  35. if (!defined('STATUSNET')) {
  36. exit(1);
  37. }
  38. class ApiQvitterNotificationsAction extends ApiPrivateAuthAction
  39. {
  40. var $notifications = array();
  41. var $notices = null;
  42. var $profiles = null;
  43. /**
  44. * Take arguments for running
  45. *
  46. * @param array $args $_REQUEST args
  47. *
  48. * @return boolean success flag
  49. *
  50. */
  51. protected function prepare(array $args=array())
  52. {
  53. parent::prepare($args);
  54. $this->format = 'json';
  55. $this->notifications = $this->getNotifications();
  56. return true;
  57. }
  58. /**
  59. * Handle the request
  60. *
  61. * Just show the notices
  62. *
  63. * @param array $args $_REQUEST data (unused)
  64. *
  65. * @return void
  66. */
  67. protected function handle()
  68. {
  69. parent::handle();
  70. $this->showTimeline();
  71. }
  72. /**
  73. * Show the timeline of notices
  74. *
  75. * @return void
  76. */
  77. function showTimeline()
  78. {
  79. $notice = null;
  80. $notifications_populated = array();
  81. foreach ($this->notifications as $notification) {
  82. // all but follow has an notice
  83. if ($notification->ntype != 'follow') {
  84. // we need a notice id here, skip this notification if notice id is null
  85. if($notification->notice_id === null) {
  86. continue;
  87. } else {
  88. $notice_object = Notice::getKV($notification->notice_id);
  89. if($notice_object instanceof Notice) {
  90. $notice = self::twitterSimpleStatusArray($notice_object);
  91. } else {
  92. // if the referenced notice is missing, delete this corrupt notification!
  93. $notification->delete();
  94. continue;
  95. }
  96. }
  97. }
  98. $from_profile = Profile::getKV($notification->from_profile_id);
  99. // a user might have deleted their profile, don't show these notifications
  100. if ($from_profile instanceof Profile) {
  101. $notifications_populated[] = array(
  102. 'id'=> $notification->id,
  103. 'from_profile'=> self::twitterUserArray($from_profile),
  104. 'ntype'=> $notification->ntype,
  105. 'notice'=> $notice,
  106. 'created_at'=>self::dateTwitter($notification->created),
  107. 'is_seen'=>$notification->is_seen
  108. );
  109. } else {
  110. // if the referenced from_profile is missing, delete this corrupt notification!
  111. $notification->delete();
  112. }
  113. // mark as seen
  114. if($notification->is_seen == 0) {
  115. $orig = clone($notification);
  116. $notification->is_seen = 1;
  117. $notification->update($orig);
  118. }
  119. }
  120. $this->initDocument('json');
  121. $this->showJsonObjects($notifications_populated);
  122. $this->endDocument('json');
  123. }
  124. /**
  125. * Get notices
  126. *
  127. * @return array notices
  128. */
  129. function getNotifications()
  130. {
  131. $notices = array();
  132. $profile = ($this->auth_user) ? $this->auth_user->getProfile() : null;
  133. if(!$profile instanceof Profile) {
  134. return false;
  135. }
  136. $stream = new NotificationStream($profile);
  137. $notifications = $stream->getNotifications(($this->page - 1) * $this->count,
  138. $this->count,
  139. $this->since_id,
  140. $this->max_id);
  141. $notifications = $notifications->fetchAll();
  142. return $notifications;
  143. }
  144. /**
  145. * Is this action read only?
  146. *
  147. * @param array $args other arguments
  148. *
  149. * @return boolean true
  150. */
  151. function isReadOnly($args)
  152. {
  153. return true;
  154. }
  155. /**
  156. * When was this feed last modified?
  157. *
  158. * @return string datestamp of the latest notice in the stream
  159. */
  160. function lastModified()
  161. {
  162. if (!empty($this->notifications) && (count($this->notifications) > 0)) {
  163. return strtotime($this->notifications[0]->created);
  164. }
  165. return null;
  166. }
  167. /**
  168. * An entity tag for this stream
  169. *
  170. * Returns an Etag based on the action name, language, and
  171. * timestamps of the first and last notice in the timeline
  172. *
  173. * @return string etag
  174. */
  175. function etag()
  176. {
  177. if (!empty($this->notifications) && (count($this->notifications) > 0)) {
  178. $last = count($this->notifications) - 1;
  179. return '"' . implode(
  180. ':',
  181. array($this->arg('action'),
  182. common_user_cache_hash($this->auth_user),
  183. common_language(),
  184. strtotime($this->notifications[0]->created),
  185. strtotime($this->notifications[$last]->created))
  186. )
  187. . '"';
  188. }
  189. return null;
  190. }
  191. }