]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Notice_inbox.php
define LACONICA and accept LACONICA for backwards compatibility
[quix0rs-gnu-social.git] / classes / Notice_inbox.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
22 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
23
24 // We keep 5 pages of inbox notices in memcache, +1 for pagination check
25
26 define('INBOX_CACHE_WINDOW', 101);
27 define('NOTICE_INBOX_GC_BOXCAR', 128);
28 define('NOTICE_INBOX_GC_MAX', 12800);
29 define('NOTICE_INBOX_LIMIT', 1000);
30 define('NOTICE_INBOX_SOFT_LIMIT', 1000);
31
32 define('NOTICE_INBOX_SOURCE_SUB', 1);
33 define('NOTICE_INBOX_SOURCE_GROUP', 2);
34 define('NOTICE_INBOX_SOURCE_REPLY', 3);
35 define('NOTICE_INBOX_SOURCE_GATEWAY', -1);
36
37 class Notice_inbox extends Memcached_DataObject
38 {
39     ###START_AUTOCODE
40     /* the code below is auto generated do not remove the above tag */
41
42     public $__table = 'notice_inbox';                    // table name
43     public $user_id;                         // int(4)  primary_key not_null
44     public $notice_id;                       // int(4)  primary_key not_null
45     public $created;                         // datetime()   not_null
46     public $source;                          // tinyint(1)   default_1
47
48     /* Static get */
49     function staticGet($k,$v=null)
50     { return Memcached_DataObject::staticGet('Notice_inbox',$k,$v); }
51
52     /* the code above is auto generated do not remove the tag below */
53     ###END_AUTOCODE
54
55     function stream($user_id, $offset, $limit, $since_id, $max_id, $since, $own=false)
56     {
57         return Notice::stream(array('Notice_inbox', '_streamDirect'),
58                               array($user_id, $own),
59                               ($own) ? 'notice_inbox:by_user:'.$user_id :
60                               'notice_inbox:by_user_own:'.$user_id,
61                               $offset, $limit, $since_id, $max_id, $since);
62     }
63
64     function _streamDirect($user_id, $own, $offset, $limit, $since_id, $max_id, $since)
65     {
66         $inbox = new Notice_inbox();
67
68         $inbox->user_id = $user_id;
69
70         if (!$own) {
71             $inbox->whereAdd('source != ' . NOTICE_INBOX_SOURCE_GATEWAY);
72         }
73
74         if ($since_id != 0) {
75             $inbox->whereAdd('notice_id > ' . $since_id);
76         }
77
78         if ($max_id != 0) {
79             $inbox->whereAdd('notice_id <= ' . $max_id);
80         }
81
82         if (!is_null($since)) {
83             $inbox->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
84         }
85
86         $inbox->orderBy('notice_id DESC');
87
88         if (!is_null($offset)) {
89             $inbox->limit($offset, $limit);
90         }
91
92         $ids = array();
93
94         if ($inbox->find()) {
95             while ($inbox->fetch()) {
96                 $ids[] = $inbox->notice_id;
97             }
98         }
99
100         return $ids;
101     }
102
103     function &pkeyGet($kv)
104     {
105         return Memcached_DataObject::pkeyGet('Notice_inbox', $kv);
106     }
107
108     static function gc($user_id)
109     {
110         $entry = new Notice_inbox();
111         $entry->user_id = $user_id;
112         $entry->orderBy('created DESC');
113         $entry->limit(NOTICE_INBOX_LIMIT - 1, NOTICE_INBOX_GC_MAX);
114
115         $total = $entry->find();
116
117         if ($total > 0) {
118             $notices = array();
119             $cnt = 0;
120             while ($entry->fetch()) {
121                 $notices[] = $entry->notice_id;
122                 $cnt++;
123                 if ($cnt >= NOTICE_INBOX_GC_BOXCAR) {
124                     self::deleteMatching($user_id, $notices);
125                     $notices = array();
126                     $cnt = 0;
127                 }
128             }
129
130             if ($cnt > 0) {
131                 self::deleteMatching($user_id, $notices);
132                 $notices = array();
133             }
134         }
135     }
136
137     static function deleteMatching($user_id, $notices)
138     {
139         $entry = new Notice_inbox();
140         return $entry->query('DELETE FROM notice_inbox '.
141                              'WHERE user_id = ' . $user_id . ' ' .
142                              'AND notice_id in ('.implode(',', $notices).')');
143     }
144 }