keepalivechannel.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. /**
  17. * Action periodically pinged by a page to keep a channel alive
  18. *
  19. * @category Realtime
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @copyright 2011 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Action periodically pinged by a page to keep a channel alive
  28. *
  29. * @category Realtime
  30. * @package GNUsocial
  31. * @author Evan Prodromou <evan@status.net>
  32. * @copyright 2011 StatusNet, Inc.
  33. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  34. */
  35. class KeepalivechannelAction extends Action
  36. {
  37. protected $channelKey = null;
  38. protected $channel = null;
  39. /**
  40. * For initializing members of the class.
  41. *
  42. * @param array $args misc. arguments
  43. *
  44. * @return bool true
  45. * @throws ClientException
  46. */
  47. public function prepare(array $args = []): bool
  48. {
  49. parent::prepare($args);
  50. if (!$this->isPost()) {
  51. // TRANS: Client exception. Do not translate POST.
  52. throw new ClientException(_m('You have to POST it.'));
  53. }
  54. $this->channelKey = $this->trimmed('channelkey');
  55. if (empty($this->channelKey)) {
  56. // TRANS: Client exception thrown when the channel key argument is missing.
  57. throw new ClientException(_m('No channel key argument.'));
  58. }
  59. $this->channel = Realtime_channel::getKV('channel_key', $this->channelKey);
  60. if (empty($this->channel)) {
  61. // TRANS: Client exception thrown when referring to a non-existing channel.
  62. throw new ClientException(_m('No such channel.'));
  63. }
  64. return true;
  65. }
  66. /**
  67. * Handler method
  68. *
  69. * @return void
  70. */
  71. public function handle(): void
  72. {
  73. $this->channel->touch();
  74. http_response_code(204);
  75. return;
  76. }
  77. /**
  78. * Return true if read only.
  79. *
  80. * MAY override
  81. *
  82. * @param array $args other arguments
  83. *
  84. * @return bool is read only action?
  85. */
  86. public function isReadOnly($args): bool
  87. {
  88. return false;
  89. }
  90. }