CometPlugin.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.'/plugins/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. function __construct($server=null, $username=null, $password=null, $prefix=null)
  51. {
  52. $this->server = $server;
  53. $this->username = $username;
  54. $this->password = $password;
  55. $this->prefix = $prefix;
  56. parent::__construct();
  57. }
  58. function _getScripts()
  59. {
  60. $scripts = parent::_getScripts();
  61. $ours = array('js/jquery.comet.js', 'js/cometupdate.js');
  62. foreach ($ours as $script) {
  63. $scripts[] = $this->path($script);
  64. }
  65. return $scripts;
  66. }
  67. function _updateInitialize($timeline, $user_id)
  68. {
  69. $script = parent::_updateInitialize($timeline, $user_id);
  70. return $script." CometUpdate.init(\"$this->server\", \"$timeline\", $user_id, \"$this->replyurl\", \"$this->favorurl\", \"$this->deleteurl\");";
  71. }
  72. function _connect()
  73. {
  74. require_once INSTALLDIR.'/plugins/Comet/extlib/Bayeux/Bayeux.class.php';
  75. // Bayeux? Comet? Huh? These terms confuse me
  76. $this->bay = new Bayeux($this->server, $this->user, $this->password);
  77. }
  78. function _publish($timeline, $json)
  79. {
  80. $this->bay->publish($timeline, $json);
  81. }
  82. function _disconnect()
  83. {
  84. unset($this->bay);
  85. }
  86. function _pathToChannel($path)
  87. {
  88. if (!empty($this->prefix)) {
  89. array_unshift($path, $this->prefix);
  90. }
  91. return '/' . implode('/', $path);
  92. }
  93. function onPluginVersion(array &$versions)
  94. {
  95. $versions[] = array('name' => 'Comet',
  96. 'version' => self::PLUGIN_VERSION,
  97. 'author' => 'Evan Prodromou',
  98. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Comet',
  99. 'rawdescription' =>
  100. // TRANS: Plugin description message. Bayeux is a protocol for transporting asynchronous messages
  101. // TRANS: and Comet is a web application model.
  102. _m('Plugin to make updates using Comet and Bayeux.'));
  103. return true;
  104. }
  105. }