sup.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2008, 2009, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('STATUSNET') && !defined('LACONICA')) {
  20. exit(1);
  21. }
  22. // @todo FIXME: documentation needed.
  23. class SupAction extends Action
  24. {
  25. public function handle()
  26. {
  27. parent::handle();
  28. $seconds = $this->trimmed('seconds');
  29. if (!$seconds) {
  30. $seconds = 15;
  31. }
  32. $updates = $this->getUpdates($seconds);
  33. header('Content-Type: application/json; charset=utf-8');
  34. print json_encode(array('updated_time' => date('c'),
  35. 'since_time' => date('c', time() - $seconds),
  36. 'available_periods' => $this->availablePeriods(),
  37. 'period' => $seconds,
  38. 'updates' => $updates));
  39. }
  40. public function availablePeriods()
  41. {
  42. static $periods = array(86400, 43200, 21600, 7200,
  43. 3600, 1800, 600, 300, 120,
  44. 60, 30, 15);
  45. $available = array();
  46. foreach ($periods as $period) {
  47. $available[$period] = common_local_url(
  48. 'sup',
  49. array('seconds' => $period)
  50. );
  51. }
  52. return $available;
  53. }
  54. public function getUpdates($seconds)
  55. {
  56. $notice = new Notice();
  57. // XXX: cache this. Depends on how big this protocol becomes;
  58. // Re-doing this query every 15 seconds isn't the end of the world.
  59. $divider = common_sql_date(time() - $seconds);
  60. $notice->query('SELECT profile_id, max(id) AS max_id ' .
  61. 'FROM ( ' .
  62. 'SELECT profile_id, id FROM notice ' .
  63. ((common_config('db', 'type') == 'pgsql') ?
  64. 'WHERE extract(epoch from created) > (extract(epoch from now()) - ' . $seconds . ') ' :
  65. 'WHERE created > "'.$divider.'" ') .
  66. ') AS latest ' .
  67. 'GROUP BY profile_id');
  68. $updates = array();
  69. while ($notice->fetch()) {
  70. $updates[] = array($notice->profile_id, $notice->max_id);
  71. }
  72. return $updates;
  73. }
  74. public function isReadOnly($args)
  75. {
  76. return true;
  77. }
  78. }