CometPlugin.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Plugin to do "real time" updates using Comet/Bayeux
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Plugin
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @copyright 2009 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('GNUSOCIAL') && !defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. require_once INSTALLDIR . DIRECTORY_SEPARATOR . 'lib/modules/Realtime/RealtimePlugin.php';
  33. /**
  34. * Plugin to do realtime updates using Comet
  35. *
  36. * @category Plugin
  37. * @package StatusNet
  38. * @author Evan Prodromou <evan@status.net>
  39. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  40. * @link http://status.net/
  41. */
  42. class CometPlugin extends RealtimePlugin
  43. {
  44. const PLUGIN_VERSION = '2.0.0';
  45. public $server = null;
  46. public $username = null;
  47. public $password = null;
  48. public $prefix = null;
  49. protected $bay = null;
  50. public function __construct(
  51. ?string $server = null,
  52. ?string $username = null,
  53. ?string $password = null,
  54. ?string $prefix = null
  55. ) {
  56. $this->server = $server;
  57. $this->username = $username;
  58. $this->password = $password;
  59. $this->prefix = $prefix;
  60. parent::__construct();
  61. }
  62. public function _getScripts(): array
  63. {
  64. $scripts = parent::_getScripts();
  65. $ours = ['js/jquery.comet.js', 'js/cometupdate.js'];
  66. foreach ($ours as $script) {
  67. $scripts[] = $this->path($script);
  68. }
  69. return $scripts;
  70. }
  71. public function _updateInitialize($timeline, int $user_id)
  72. {
  73. $script = parent::_updateInitialize($timeline, $user_id);
  74. return $script." CometUpdate.init(\"$this->server\", \"$timeline\", $user_id, \"$this->replyurl\", \"$this->favorurl\", \"$this->deleteurl\");";
  75. }
  76. public function _connect(): void
  77. {
  78. require_once __DIR__. DIRECTORY_SEPARATOR . 'extlib/Bayeux/Bayeux.class.php';
  79. // Bayeux? Comet? Huh? These terms confuse me
  80. $this->bay = new Bayeux($this->server, $this->user, $this->password);
  81. }
  82. public function _publish($timeline, $json): void
  83. {
  84. $this->bay->publish($timeline, $json);
  85. }
  86. public function _disconnect(): void
  87. {
  88. unset($this->bay);
  89. }
  90. public function _pathToChannel(array $path): string
  91. {
  92. if (!empty($this->prefix)) {
  93. array_unshift($path, $this->prefix);
  94. }
  95. return '/' . implode('/', $path);
  96. }
  97. public function onPluginVersion(array &$versions): bool
  98. {
  99. $versions[] = [
  100. 'name' => 'Comet',
  101. 'version' => self::PLUGIN_VERSION,
  102. 'author' => 'Evan Prodromou',
  103. 'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/Comet',
  104. 'rawdescription' =>
  105. // TRANS: Plugin description message. Bayeux is a protocol for transporting asynchronous messages
  106. // TRANS: and Comet is a web application model.
  107. _m('Plugin to make updates using Comet and Bayeux.')
  108. ];
  109. return true;
  110. }
  111. }