123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php
- if (!defined('STATUSNET')) {
- exit(1);
- }
- class Realtime_channel extends Managed_DataObject
- {
- const TIMEOUT = 1800;
- public $__table = 'realtime_channel';
- public $user_id;
- public $action;
- public $arg1;
- public $arg2;
- public $channel_key;
- public $audience;
- public $created;
- public $modified;
-
- 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'),
- 'audience' => array('type' => 'integer',
- 'not null' => true,
- 'default' => 0,
- 'description' => 'reference count'),
- '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('channel_key'),
- 'unique keys' => array('realtime_channel_user_page_idx' => array('user_id', 'action', 'arg1', 'arg2')),
- 'foreign keys' => array(
- 'realtime_channel_user_id_fkey' => array('user', array('user_id' => 'id')),
- ),
- 'indexes' => array(
- 'realtime_channel_modified_idx' => array('modified'),
- 'realtime_channel_page_idx' => array('action', 'arg1', 'arg2')
- ),
- );
- }
- static function saveNew($user_id, $action, $arg1, $arg2)
- {
- $channel = new Realtime_channel();
- $channel->user_id = $user_id;
- $channel->action = $action;
- $channel->arg1 = $arg1;
- $channel->arg2 = $arg2;
- $channel->audience = 1;
- $channel->channel_key = common_random_hexstr(16);
- $channel->created = common_sql_now();
- $channel->modified = $channel->created;
- $channel->insert();
- return $channel;
- }
- static function getChannel($user_id, $action, $arg1, $arg2)
- {
- $channel = self::fetchChannel($user_id, $action, $arg1, $arg2);
-
- 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;
- }
- static function getAllChannels($action, $arg1, $arg2)
- {
- $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;
- }
- static function fetchChannel($user_id, $action, $arg1, $arg2)
- {
- $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)) {
- $channel->increment();
- return $channel;
- } else {
- return null;
- }
- }
- function increment()
- {
-
- $orig = clone($this);
- $this->audience++;
- $this->modified = common_sql_now();
- $this->update($orig);
- }
- function touch()
- {
-
- $orig = clone($this);
- $this->modified = common_sql_now();
- $this->update($orig);
- }
- function decrement()
- {
-
- if ($this->audience == 1) {
- $this->delete();
- } else {
- $orig = clone($this);
- $this->audience--;
- $this->modified = common_sql_now();
- $this->update($orig);
- }
- }
- }
|