2011-07-08 22:52:07 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* StatusNet - the distributed open-source microblogging tool
|
|
|
|
* Copyright (C) 2011, StatusNet, Inc.
|
|
|
|
*
|
|
|
|
* A channel for real-time browser data
|
2011-08-19 16:06:03 +01:00
|
|
|
*
|
2011-07-08 22:52:07 +01:00
|
|
|
* PHP version 5
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @category Realtime
|
|
|
|
* @package StatusNet
|
|
|
|
* @author Evan Prodromou <evan@status.net>
|
|
|
|
* @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
|
2011-08-19 16:06:03 +01:00
|
|
|
*
|
2011-07-08 22:52:07 +01:00
|
|
|
* 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 <evan@status.net>
|
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
|
|
|
|
* @link http://status.net/
|
|
|
|
*
|
|
|
|
* @see DB_DataObject
|
|
|
|
*/
|
|
|
|
class Realtime_channel extends Managed_DataObject
|
|
|
|
{
|
2011-08-19 16:06:03 +01:00
|
|
|
const TIMEOUT = 1800; // 30 minutes
|
|
|
|
|
2011-07-08 22:52:07 +01:00
|
|
|
public $__table = 'realtime_channel'; // table name
|
2011-08-19 16:06:03 +01:00
|
|
|
|
2011-07-08 22:52:07 +01:00
|
|
|
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
|
2011-07-13 21:10:08 +01:00
|
|
|
public $audience; // listener count
|
2011-08-19 16:06:03 +01:00
|
|
|
public $created; // created date
|
2011-07-08 22:52:07 +01:00
|
|
|
public $modified; // modified date
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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',
|
2011-08-19 16:06:03 +01:00
|
|
|
'not null' => false,
|
|
|
|
'description' => 'user viewing page; can be null'),
|
2011-07-08 22:52:07 +01:00
|
|
|
'action' => array('type' => 'varchar',
|
2011-08-19 16:06:03 +01:00
|
|
|
'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'),
|
|
|
|
'audience' => array('type' => 'integer',
|
2011-07-13 21:10:08 +01:00
|
|
|
'not null' => true,
|
|
|
|
'default' => 0,
|
|
|
|
'description' => 'reference count'),
|
2011-07-08 22:52:07 +01:00
|
|
|
'created' => array('type' => 'datetime',
|
2011-08-19 16:06:03 +01:00
|
|
|
'not null' => true,
|
|
|
|
'description' => 'date this record was created'),
|
2011-07-08 22:52:07 +01:00
|
|
|
'modified' => array('type' => 'datetime',
|
2011-08-19 16:06:03 +01:00
|
|
|
'not null' => true,
|
|
|
|
'description' => 'date this record was modified'),
|
2011-07-08 22:52:07 +01:00
|
|
|
),
|
2011-07-11 18:53:55 +01:00
|
|
|
'primary key' => array('channel_key'),
|
|
|
|
'unique keys' => array('realtime_channel_user_page_idx' => array('user_id', 'action', 'arg1', 'arg2')),
|
2011-07-08 22:52:07 +01:00
|
|
|
'foreign keys' => array(
|
|
|
|
'realtime_channel_user_id_fkey' => array('user', array('user_id' => 'id')),
|
|
|
|
),
|
|
|
|
'indexes' => array(
|
|
|
|
'realtime_channel_modified_idx' => array('modified'),
|
2011-07-11 16:31:59 +01:00
|
|
|
'realtime_channel_page_idx' => array('action', 'arg1', 'arg2')
|
2011-07-08 22:52:07 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2011-08-19 16:06:03 +01:00
|
|
|
|
2011-07-08 22:52:07 +01:00
|
|
|
static function saveNew($user_id, $action, $arg1, $arg2)
|
|
|
|
{
|
2011-08-19 16:06:03 +01:00
|
|
|
$channel = new Realtime_channel();
|
|
|
|
|
|
|
|
$channel->user_id = $user_id;
|
|
|
|
$channel->action = $action;
|
|
|
|
$channel->arg1 = $arg1;
|
|
|
|
$channel->arg2 = $arg2;
|
|
|
|
$channel->audience = 1;
|
|
|
|
|
2013-10-21 12:20:30 +01:00
|
|
|
$channel->channel_key = common_random_hexstr(16); // 128-bit key, 32 hex chars
|
2011-08-19 16:06:03 +01:00
|
|
|
|
|
|
|
$channel->created = common_sql_now();
|
|
|
|
$channel->modified = $channel->created;
|
|
|
|
|
|
|
|
$channel->insert();
|
|
|
|
|
|
|
|
return $channel;
|
2011-07-08 22:52:07 +01:00
|
|
|
}
|
2011-08-19 16:06:03 +01:00
|
|
|
|
2011-07-08 22:52:07 +01:00
|
|
|
static function getChannel($user_id, $action, $arg1, $arg2)
|
|
|
|
{
|
2011-08-19 16:06:03 +01:00
|
|
|
$channel = self::fetchChannel($user_id, $action, $arg1, $arg2);
|
|
|
|
|
|
|
|
// Ignore (and delete!) old channels
|
|
|
|
|
|
|
|
if (!empty($channel)) {
|
|
|
|
$modTime = strtotime($channel->modified);
|
|
|
|
if ((time() - $modTime) > self::TIMEOUT) {
|
|
|
|
$channel->delete();
|
|
|
|
$channel = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($channel)) {
|
|
|
|
$channel = self::saveNew($user_id, $action, $arg1, $arg2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $channel;
|
2011-07-08 22:52:07 +01:00
|
|
|
}
|
2011-08-19 16:06:03 +01:00
|
|
|
|
2011-07-11 14:16:16 +01:00
|
|
|
static function getAllChannels($action, $arg1, $arg2)
|
|
|
|
{
|
2011-08-19 16:06:03 +01:00
|
|
|
$channel = new Realtime_channel();
|
|
|
|
|
|
|
|
$channel->action = $action;
|
|
|
|
|
|
|
|
if (is_null($arg1)) {
|
|
|
|
$channel->whereAdd('arg1 is null');
|
|
|
|
} else {
|
|
|
|
$channel->arg1 = $arg1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($arg2)) {
|
|
|
|
$channel->whereAdd('arg2 is null');
|
|
|
|
} else {
|
|
|
|
$channel->arg2 = $arg2;
|
|
|
|
}
|
|
|
|
|
|
|
|
$channel->whereAdd('modified > "' . common_sql_date(time() - self::TIMEOUT) . '"');
|
|
|
|
|
|
|
|
$channels = array();
|
|
|
|
|
|
|
|
if ($channel->find()) {
|
|
|
|
$channels = $channel->fetchAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $channels;
|
2011-07-11 14:16:16 +01:00
|
|
|
}
|
2011-08-19 16:06:03 +01:00
|
|
|
|
2011-07-11 20:52:05 +01:00
|
|
|
static function fetchChannel($user_id, $action, $arg1, $arg2)
|
2011-08-19 16:06:03 +01:00
|
|
|
{
|
|
|
|
$channel = new Realtime_channel();
|
|
|
|
|
|
|
|
if (is_null($user_id)) {
|
|
|
|
$channel->whereAdd('user_id is null');
|
|
|
|
} else {
|
|
|
|
$channel->user_id = $user_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$channel->action = $action;
|
|
|
|
|
|
|
|
if (is_null($arg1)) {
|
|
|
|
$channel->whereAdd('arg1 is null');
|
|
|
|
} else {
|
|
|
|
$channel->arg1 = $arg1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_null($arg2)) {
|
|
|
|
$channel->whereAdd('arg2 is null');
|
|
|
|
} else {
|
|
|
|
$channel->arg2 = $arg2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($channel->find(true)) {
|
2011-07-13 21:10:08 +01:00
|
|
|
$channel->increment();
|
2011-08-19 16:06:03 +01:00
|
|
|
return $channel;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2011-07-11 20:52:05 +01:00
|
|
|
}
|
2011-07-13 18:55:03 +01:00
|
|
|
|
2011-07-13 21:10:08 +01:00
|
|
|
function increment()
|
|
|
|
{
|
|
|
|
// XXX: race
|
|
|
|
$orig = clone($this);
|
|
|
|
$this->audience++;
|
|
|
|
$this->modified = common_sql_now();
|
|
|
|
$this->update($orig);
|
|
|
|
}
|
|
|
|
|
2011-07-13 18:55:03 +01:00
|
|
|
function touch()
|
|
|
|
{
|
2011-07-13 21:10:08 +01:00
|
|
|
// XXX: race
|
2011-07-13 18:55:03 +01:00
|
|
|
$orig = clone($this);
|
|
|
|
$this->modified = common_sql_now();
|
|
|
|
$this->update($orig);
|
|
|
|
}
|
2011-07-13 21:10:08 +01:00
|
|
|
|
|
|
|
function decrement()
|
|
|
|
{
|
|
|
|
// XXX: race
|
|
|
|
if ($this->audience == 1) {
|
|
|
|
$this->delete();
|
|
|
|
} else {
|
|
|
|
$orig = clone($this);
|
|
|
|
$this->audience--;
|
|
|
|
$this->modified = common_sql_now();
|
|
|
|
$this->update($orig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|