diff --git a/plugins/Realtime/RealtimePlugin.php b/plugins/Realtime/RealtimePlugin.php index 709d65ac5d..68dc374f8c 100644 --- a/plugins/Realtime/RealtimePlugin.php +++ b/plugins/Realtime/RealtimePlugin.php @@ -59,6 +59,27 @@ class RealtimePlugin extends Plugin array('notice' => '0000000000')); return true; } + + function onCheckSchema() + { + $schema = Schema::get(); + $schema->ensureTable('realtime_channel', Realtime_channel::schemaDef()); + return true; + } + + function onAutoload($cls) + { + $dir = dirname(__FILE__); + + switch ($cls) + { + case 'Realtime_channel': + include_once $dir.'/'.$cls.'.php'; + return false; + default: + return true; + } + } function onEndShowScripts($action) { @@ -377,6 +398,10 @@ class RealtimePlugin extends Plugin $action_name = $action->trimmed('action'); + // FIXME: lists + // FIXME: search (!) + // FIXME: + switch ($action_name) { case 'public': $path = array('public'); diff --git a/plugins/Realtime/Realtime_channel.php b/plugins/Realtime/Realtime_channel.php new file mode 100644 index 0000000000..a0afd2cd6b --- /dev/null +++ b/plugins/Realtime/Realtime_channel.php @@ -0,0 +1,178 @@ +. + * + * @category Realtime + * @package StatusNet + * @author Evan Prodromou + * @copyright 2011 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 + * @link http://status.net/ + */ + +if (!defined('STATUSNET')) { + exit(1); +} + +/** + * A channel for real-time browser data + * + * For each user currently browsing the site, we want to know which page they're on + * so we can send real-time updates to their browser. + * + * @category Realtime + * @package StatusNet + * @author Evan Prodromou + * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3 + * @link http://status.net/ + * + * @see DB_DataObject + */ + +class Realtime_channel extends Managed_DataObject +{ + const TIMEOUT = 1800; // 30 minutes + + public $__table = 'realtime_channel'; // table name + + public $user_id; // int -> user.id, can be null + public $action; // string + public $arg1; // argument + public $arg2; // argument, usually null + public $channel_key; // 128-bit shared secret key + public $created; // created date + public $modified; // modified date + + /** + * Get an instance by key + * + * This is a utility method to get a single instance with a given key value. + * + * @param string $k Key to use to lookup (usually 'user_id' for this class) + * @param mixed $v Value to lookup + * + * @return Realtime_channel object found, or null for no hits + * + */ + function staticGet($k, $v=null) + { + return Managed_DataObject::staticGet('Realtime_channel', $k, $v); + } + + /** + * Get an instance by compound key + * + * @param array $kv array of key-value mappings + * + * @return Realtime_channel object found, or null for no hits + * + */ + function pkeyGet($kv) + { + return Managed_DataObject::pkeyGet('Realtime_channel', $kv); + } + + /** + * The One True Thingy that must be defined and declared. + */ + public static function schemaDef() + { + return array( + 'description' => 'A channel of realtime notice data', + 'fields' => array( + 'user_id' => array('type' => 'int', + 'not null' => false, + 'description' => 'user viewing page; can be null'), + 'action' => array('type' => 'varchar', + 'length' => 255, + 'not null' => true, + 'description' => 'page being viewed'), + 'arg1' => array('type' => 'varchar', + 'length' => 255, + 'not null' => false, + 'description' => 'page argument, like username or tag'), + 'arg2' => array('type' => 'varchar', + 'length' => 255, + 'not null' => false, + 'description' => 'second page argument, like tag for showstream'), + 'channel_key' => array('type' => 'varchar', + 'length' => 32, + 'not null' => true, + 'description' => 'shared secret key for this channel'), + 'created' => array('type' => 'datetime', + 'not null' => true, + 'description' => 'date this record was created'), + 'modified' => array('type' => 'datetime', + 'not null' => true, + 'description' => 'date this record was modified'), + ), + 'primary key' => array('user_id', 'action', 'arg1', 'arg2'), + 'unique keys' => array('channel_key'), + 'foreign keys' => array( + 'realtime_channel_user_id_fkey' => array('user', array('user_id' => 'id')), + ), + 'indexes' => array( + 'realtime_channel_modified_idx' => array('modified'), + ), + ); + } + + static function saveNew($user_id, $action, $arg1, $arg2) + { + $channel = self::getChannel($user_id, $action, $arg1, $arg2); + + $channel = new Realtime_channel(); + + $channel->user_id = $user_id; + $channel->action = $action; + $channel->arg1 = $arg1; + $channel->arg2 = $arg2; + + $channel->channel_key = common_good_rand(16); // 128-bit key, 32 hex chars + + $channel->created = common_sql_now(); + $channel->modified = $channel->created; + + $channel->insert(); + + return $channel; + } + + static function getChannel($user_id, $action, $arg1, $arg2) + { + $channel = self::pkeyGet(array('user_id' => $user_id, + 'action' => $action, + 'arg1' => $arg1, + 'arg2' => $arg2)); + + // Ignore (and delete!) old channels + + if (!empty($channel)) { + $modTime = strtotime($channel->modified); + if ((time() - $modTime) > self::TIMEOUT) { + $channel->delete(); + $channel = null; + } + } + + return $channel; + } +} \ No newline at end of file