sup.php 2.7 KB

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