]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Realtime/Realtime_channel.php
a0afd2cd6bc367dfe1dec09663861a51be1d977c
[quix0rs-gnu-social.git] / plugins / Realtime / Realtime_channel.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * A channel for real-time browser data
7  * 
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Realtime
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2011 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 /**
36  * A channel for real-time browser data
37  * 
38  * For each user currently browsing the site, we want to know which page they're on
39  * so we can send real-time updates to their browser.
40  *
41  * @category Realtime
42  * @package  StatusNet
43  * @author   Evan Prodromou <evan@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
45  * @link     http://status.net/
46  *
47  * @see      DB_DataObject
48  */
49
50 class Realtime_channel extends Managed_DataObject
51 {
52         const TIMEOUT = 1800; // 30 minutes
53         
54     public $__table = 'realtime_channel'; // table name
55     
56     public $user_id;       // int -> user.id, can be null
57     public $action;        // string
58     public $arg1;          // argument
59     public $arg2;          // argument, usually null
60     public $channel_key;   // 128-bit shared secret key
61     public $created;       // created date
62     public $modified;      // modified date
63
64     /**
65      * Get an instance by key
66      *
67      * This is a utility method to get a single instance with a given key value.
68      *
69      * @param string $k Key to use to lookup (usually 'user_id' for this class)
70      * @param mixed  $v Value to lookup
71      *
72      * @return Realtime_channel object found, or null for no hits
73      *
74      */
75     function staticGet($k, $v=null)
76     {
77         return Managed_DataObject::staticGet('Realtime_channel', $k, $v);
78     }
79
80     /**
81      * Get an instance by compound key
82      *
83      * @param array $kv array of key-value mappings
84      *
85      * @return Realtime_channel object found, or null for no hits
86      *
87      */
88     function pkeyGet($kv)
89     {
90         return Managed_DataObject::pkeyGet('Realtime_channel', $kv);
91     }
92
93     /**
94      * The One True Thingy that must be defined and declared.
95      */
96     public static function schemaDef()
97     {
98         return array(
99             'description' => 'A channel of realtime notice data',
100             'fields' => array(
101                 'user_id' => array('type' => 'int',
102                                                                    'not null' => false,
103                                                                    'description' => 'user viewing page; can be null'),
104                 'action' => array('type' => 'varchar',
105                                                   'length' => 255,
106                                                                   'not null' => true,
107                                                                   'description' => 'page being viewed'),
108                                 'arg1' => array('type' => 'varchar',
109                                                             'length' => 255,
110                                                             'not null' => false,
111                                                             'description' => 'page argument, like username or tag'),
112                             'arg2' => array('type' => 'varchar',
113                                                         'length' => 255,
114                                                         'not null' => false,
115                                                         'description' => 'second page argument, like tag for showstream'),
116                             'channel_key' => array('type' => 'varchar',
117                                                    'length' => 32,
118                                                    'not null' => true,
119                                                    'description' => 'shared secret key for this channel'),
120                 'created' => array('type' => 'datetime',
121                                                                    'not null' => true,
122                                                                    'description' => 'date this record was created'),
123                 'modified' => array('type' => 'datetime',
124                                                                 'not null' => true,
125                                                             'description' => 'date this record was modified'),
126             ),
127             'primary key' => array('user_id', 'action', 'arg1', 'arg2'),
128             'unique keys' => array('channel_key'),
129             'foreign keys' => array(
130                 'realtime_channel_user_id_fkey' => array('user', array('user_id' => 'id')),
131             ),
132             'indexes' => array(
133                 'realtime_channel_modified_idx' => array('modified'),
134             ),
135         );
136     }
137     
138     static function saveNew($user_id, $action, $arg1, $arg2)
139     {
140         $channel = self::getChannel($user_id, $action, $arg1, $arg2);
141         
142         $channel = new Realtime_channel();
143         
144         $channel->user_id = $user_id;
145         $channel->action  = $action;
146         $channel->arg1    = $arg1;
147         $channel->arg2    = $arg2;
148         
149         $channel->channel_key = common_good_rand(16); // 128-bit key, 32 hex chars
150         
151         $channel->created  = common_sql_now();
152         $channel->modified = $channel->created;
153         
154                 $channel->insert();
155                 
156                 return $channel;
157     }
158     
159     static function getChannel($user_id, $action, $arg1, $arg2)
160     {
161         $channel = self::pkeyGet(array('user_id' => $user_id,
162                                                                    'action' => $action,
163                                                                    'arg1' => $arg1,
164                                                                    'arg2' => $arg2));
165         
166         // Ignore (and delete!) old channels
167                                    
168         if (!empty($channel)) {
169                         $modTime = strtotime($channel->modified);
170                         if ((time() - $modTime) > self::TIMEOUT) {
171                                 $channel->delete();
172                                 $channel = null;
173                         }
174         }
175         
176         return $channel;
177     }
178 }