JavaScript and other changes to make realtime_channel management work
This commit is contained in:
parent
3c3ab128b3
commit
58ea000485
@ -93,16 +93,24 @@ class RealtimePlugin extends Plugin
|
|||||||
*/
|
*/
|
||||||
function onRouterInitialized($m)
|
function onRouterInitialized($m)
|
||||||
{
|
{
|
||||||
// Discovery actions
|
$m->connect('main/channel/:channelkey/keepalive',
|
||||||
$m->connect('main/channel/:channel_key/keepalive',
|
array('action' => 'keepalivechannel'),
|
||||||
array('action' => 'keepalivechannel'));
|
array('channelkey' => '[a-z0-9]{32}'));
|
||||||
$m->connect('main/channel/:channel_key/close',
|
$m->connect('main/channel/:channelkey/close',
|
||||||
array('action' => 'closechannel'));
|
array('action' => 'closechannel'),
|
||||||
|
array('channelkey' => '[a-z0-9]{32}'));
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function onEndShowScripts($action)
|
function onEndShowScripts($action)
|
||||||
{
|
{
|
||||||
$timeline = $this->_getTimeline($action);
|
$channel = $this->_getChannel($action);
|
||||||
|
|
||||||
|
if (empty($channel)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
$timeline = $this->_pathToChannel(array($channel->channel_key));
|
||||||
|
|
||||||
// If there's not a timeline on this page,
|
// If there's not a timeline on this page,
|
||||||
// just return true
|
// just return true
|
||||||
@ -137,7 +145,9 @@ class RealtimePlugin extends Plugin
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$pluginPath = common_path('plugins/Realtime/');
|
$pluginPath = common_path('plugins/Realtime/');
|
||||||
$realtimeUI = ' RealtimeUpdate.initActions("'.$url.'", "'.$timeline.'", "'. $pluginPath .'");';
|
$keepalive = common_local_url('keepalivechannel', array('channelkey' => $channel->channel_key));
|
||||||
|
$close = common_local_url('closechannel', array('channelkey' => $channel->channel_key));
|
||||||
|
$realtimeUI = ' RealtimeUpdate.initActions("'.$url.'", "'.$timeline.'", "'. $pluginPath .'", "'.$keepalive.'", "'.$close.'"); ';
|
||||||
}
|
}
|
||||||
|
|
||||||
$script = ' $(document).ready(function() { '.
|
$script = ' $(document).ready(function() { '.
|
||||||
@ -431,7 +441,18 @@ class RealtimePlugin extends Plugin
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function _getTimeline($action)
|
function _getTimeline($action)
|
||||||
|
{
|
||||||
|
$channel = $this->_getChannel($action);
|
||||||
|
if (empty($channel)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->_pathToChannel(array($channel->channel_key));
|
||||||
|
}
|
||||||
|
|
||||||
|
function _getChannel($action)
|
||||||
{
|
{
|
||||||
$timeline = null;
|
$timeline = null;
|
||||||
$arg1 = null;
|
$arg1 = null;
|
||||||
@ -481,11 +502,7 @@ class RealtimePlugin extends Plugin
|
|||||||
$arg1,
|
$arg1,
|
||||||
$arg2);
|
$arg2);
|
||||||
|
|
||||||
if (!empty($channel)) {
|
return $channel;
|
||||||
$timeline = $this->_pathToChannel(array($channel->channel_key));
|
|
||||||
}
|
|
||||||
|
|
||||||
return $timeline;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function onStartReadWriteTables(&$alwaysRW, &$rwdb)
|
function onStartReadWriteTables(&$alwaysRW, &$rwdb)
|
||||||
|
@ -58,6 +58,7 @@ class Realtime_channel extends Managed_DataObject
|
|||||||
public $arg1; // argument
|
public $arg1; // argument
|
||||||
public $arg2; // argument, usually null
|
public $arg2; // argument, usually null
|
||||||
public $channel_key; // 128-bit shared secret key
|
public $channel_key; // 128-bit shared secret key
|
||||||
|
public $audience; // listener count
|
||||||
public $created; // created date
|
public $created; // created date
|
||||||
public $modified; // modified date
|
public $modified; // modified date
|
||||||
|
|
||||||
@ -117,6 +118,10 @@ class Realtime_channel extends Managed_DataObject
|
|||||||
'length' => 32,
|
'length' => 32,
|
||||||
'not null' => true,
|
'not null' => true,
|
||||||
'description' => 'shared secret key for this channel'),
|
'description' => 'shared secret key for this channel'),
|
||||||
|
'audience' => array('type' => 'integer',
|
||||||
|
'not null' => true,
|
||||||
|
'default' => 0,
|
||||||
|
'description' => 'reference count'),
|
||||||
'created' => array('type' => 'datetime',
|
'created' => array('type' => 'datetime',
|
||||||
'not null' => true,
|
'not null' => true,
|
||||||
'description' => 'date this record was created'),
|
'description' => 'date this record was created'),
|
||||||
@ -144,6 +149,7 @@ class Realtime_channel extends Managed_DataObject
|
|||||||
$channel->action = $action;
|
$channel->action = $action;
|
||||||
$channel->arg1 = $arg1;
|
$channel->arg1 = $arg1;
|
||||||
$channel->arg2 = $arg2;
|
$channel->arg2 = $arg2;
|
||||||
|
$channel->audience = 1;
|
||||||
|
|
||||||
$channel->channel_key = common_good_rand(16); // 128-bit key, 32 hex chars
|
$channel->channel_key = common_good_rand(16); // 128-bit key, 32 hex chars
|
||||||
|
|
||||||
@ -230,18 +236,40 @@ class Realtime_channel extends Managed_DataObject
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($channel->find(true)) {
|
if ($channel->find(true)) {
|
||||||
$channel->touch();
|
$channel->increment();
|
||||||
return $channel;
|
return $channel;
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function increment()
|
||||||
|
{
|
||||||
|
// XXX: race
|
||||||
|
$orig = clone($this);
|
||||||
|
$this->audience++;
|
||||||
|
$this->modified = common_sql_now();
|
||||||
|
$this->update($orig);
|
||||||
|
}
|
||||||
|
|
||||||
function touch()
|
function touch()
|
||||||
{
|
{
|
||||||
// Touch it!
|
// XXX: race
|
||||||
$orig = clone($this);
|
$orig = clone($this);
|
||||||
$this->modified = common_sql_now();
|
$this->modified = common_sql_now();
|
||||||
$this->update($orig);
|
$this->update($orig);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function decrement()
|
||||||
|
{
|
||||||
|
// XXX: race
|
||||||
|
if ($this->audience == 1) {
|
||||||
|
$this->delete();
|
||||||
|
} else {
|
||||||
|
$orig = clone($this);
|
||||||
|
$this->audience--;
|
||||||
|
$this->modified = common_sql_now();
|
||||||
|
$this->update($orig);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -66,7 +66,7 @@ class ClosechannelAction extends Action
|
|||||||
throw new ClientException(_m('You have to POST it.'));
|
throw new ClientException(_m('You have to POST it.'));
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->channelKey = $this->trimmed('channel_key');
|
$this->channelKey = $this->trimmed('channelkey');
|
||||||
|
|
||||||
if (empty($this->channelKey)) {
|
if (empty($this->channelKey)) {
|
||||||
throw new ClientException(_m('No channel key argument.'));
|
throw new ClientException(_m('No channel key argument.'));
|
||||||
@ -91,7 +91,7 @@ class ClosechannelAction extends Action
|
|||||||
|
|
||||||
function handle($argarray=null)
|
function handle($argarray=null)
|
||||||
{
|
{
|
||||||
$this->channel->delete();
|
$this->channel->decrement();
|
||||||
|
|
||||||
header('HTTP/1.1 204 No Content');
|
header('HTTP/1.1 204 No Content');
|
||||||
|
|
||||||
|
@ -66,7 +66,7 @@ class KeepalivechannelAction extends Action
|
|||||||
throw new ClientException(_m('You have to POST it.'));
|
throw new ClientException(_m('You have to POST it.'));
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->channelKey = $this->trimmed('channel_key');
|
$this->channelKey = $this->trimmed('channelkey');
|
||||||
|
|
||||||
if (empty($this->channelKey)) {
|
if (empty($this->channelKey)) {
|
||||||
throw new ClientException(_m('No channel key argument.'));
|
throw new ClientException(_m('No channel key argument.'));
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* StatusNet - a distributed open-source microblogging tool
|
* StatusNet - a distributed open-source microblogging tool
|
||||||
* Copyright (C) 2008, StatusNet, Inc.
|
* Copyright (C) 2009-2011, StatusNet, Inc.
|
||||||
*
|
*
|
||||||
* Add a notice encoded as JSON into the current timeline
|
* Add a notice encoded as JSON into the current timeline
|
||||||
*
|
*
|
||||||
@ -21,7 +21,7 @@
|
|||||||
* @package StatusNet
|
* @package StatusNet
|
||||||
* @author Evan Prodromou <evan@status.net>
|
* @author Evan Prodromou <evan@status.net>
|
||||||
* @author Sarven Capadisli <csarven@status.net>
|
* @author Sarven Capadisli <csarven@status.net>
|
||||||
* @copyright 2009 StatusNet, Inc.
|
* @copyright 2009-2011 StatusNet, Inc.
|
||||||
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
||||||
* @link http://status.net/
|
* @link http://status.net/
|
||||||
*/
|
*/
|
||||||
@ -45,6 +45,8 @@
|
|||||||
RealtimeUpdate = {
|
RealtimeUpdate = {
|
||||||
_userid: 0,
|
_userid: 0,
|
||||||
_showurl: '',
|
_showurl: '',
|
||||||
|
_keepaliveurl: '',
|
||||||
|
_closeurl: '',
|
||||||
_updatecounter: 0,
|
_updatecounter: 0,
|
||||||
_maxnotices: 50,
|
_maxnotices: 50,
|
||||||
_windowhasfocus: true,
|
_windowhasfocus: true,
|
||||||
@ -390,11 +392,28 @@ RealtimeUpdate = {
|
|||||||
*
|
*
|
||||||
* @access private
|
* @access private
|
||||||
*/
|
*/
|
||||||
initActions: function(url, timeline, path)
|
initActions: function(url, timeline, path, keepaliveurl, closeurl)
|
||||||
{
|
{
|
||||||
$('#notices_primary').prepend('<ul id="realtime_actions"><li id="realtime_playpause"></li><li id="realtime_timeline"></li></ul>');
|
$('#notices_primary').prepend('<ul id="realtime_actions"><li id="realtime_playpause"></li><li id="realtime_timeline"></li></ul>');
|
||||||
|
|
||||||
RealtimeUpdate._pluginPath = path;
|
RealtimeUpdate._pluginPath = path;
|
||||||
|
RealtimeUpdate._keepaliveurl = keepaliveurl;
|
||||||
|
RealtimeUpdate._closeurl = closeurl;
|
||||||
|
|
||||||
|
|
||||||
|
// On unload, let the server know we're no longer listening
|
||||||
|
$(window).unload(function() {
|
||||||
|
$.ajax({
|
||||||
|
type: 'POST',
|
||||||
|
url: RealtimeUpdate._closeurl});
|
||||||
|
});
|
||||||
|
|
||||||
|
setInterval(function() {
|
||||||
|
$.ajax({
|
||||||
|
type: 'POST',
|
||||||
|
url: RealtimeUpdate._keepaliveurl});
|
||||||
|
|
||||||
|
}, 15 * 60 * 1000 ); // every 15 min; timeout in 30 min
|
||||||
|
|
||||||
RealtimeUpdate.initPlayPause();
|
RealtimeUpdate.initPlayPause();
|
||||||
RealtimeUpdate.initAddPopup(url, timeline, RealtimeUpdate._pluginPath);
|
RealtimeUpdate.initAddPopup(url, timeline, RealtimeUpdate._pluginPath);
|
||||||
|
2
plugins/Realtime/realtimeupdate.min.js
vendored
2
plugins/Realtime/realtimeupdate.min.js
vendored
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user