]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Notice_inbox.php
ptiturl.com correct name
[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_FORWARD', 4);
36 define('NOTICE_INBOX_SOURCE_GATEWAY', -1);
37
38 class Notice_inbox extends Memcached_DataObject
39 {
40     ###START_AUTOCODE
41     /* the code below is auto generated do not remove the above tag */
42
43     public $__table = 'notice_inbox';                    // table name
44     public $user_id;                         // int(4)  primary_key not_null
45     public $notice_id;                       // int(4)  primary_key not_null
46     public $created;                         // datetime()   not_null
47     public $source;                          // tinyint(1)   default_1
48
49     /* Static get */
50     function staticGet($k,$v=null)
51     { return Memcached_DataObject::staticGet('Notice_inbox',$k,$v); }
52
53     /* the code above is auto generated do not remove the tag below */
54     ###END_AUTOCODE
55
56     function stream($user_id, $offset, $limit, $since_id, $max_id, $since, $own=false)
57     {
58         return Notice::stream(array('Notice_inbox', '_streamDirect'),
59                               array($user_id, $own),
60                               ($own) ? 'notice_inbox:by_user:'.$user_id :
61                               'notice_inbox:by_user_own:'.$user_id,
62                               $offset, $limit, $since_id, $max_id, $since);
63     }
64
65     function _streamDirect($user_id, $own, $offset, $limit, $since_id, $max_id, $since)
66     {
67         $inbox = new Notice_inbox();
68
69         $inbox->user_id = $user_id;
70
71         if (!$own) {
72             $inbox->whereAdd('source != ' . NOTICE_INBOX_SOURCE_GATEWAY);
73         }
74
75         if ($since_id != 0) {
76             $inbox->whereAdd('notice_id > ' . $since_id);
77         }
78
79         if ($max_id != 0) {
80             $inbox->whereAdd('notice_id <= ' . $max_id);
81         }
82
83         if (!is_null($since)) {
84             $inbox->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
85         }
86
87         $inbox->orderBy('created DESC');
88
89         if (!is_null($offset)) {
90             $inbox->limit($offset, $limit);
91         }
92
93         $ids = array();
94
95         if ($inbox->find()) {
96             while ($inbox->fetch()) {
97                 $ids[] = $inbox->notice_id;
98             }
99         }
100
101         return $ids;
102     }
103
104     function pkeyGet($kv)
105     {
106         return Memcached_DataObject::pkeyGet('Notice_inbox', $kv);
107     }
108
109     /**
110      * Trim inbox for a given user to latest NOTICE_INBOX_LIMIT items
111      * (up to NOTICE_INBOX_GC_MAX will be deleted).
112      *
113      * @param int $user_id
114      * @return int count of notices dropped from the inbox, if any
115      */
116     static function gc($user_id)
117     {
118         $entry = new Notice_inbox();
119         $entry->user_id = $user_id;
120         $entry->orderBy('created DESC');
121         $entry->limit(NOTICE_INBOX_LIMIT - 1, NOTICE_INBOX_GC_MAX);
122
123         $total = $entry->find();
124
125         if ($total > 0) {
126             $notices = array();
127             $cnt = 0;
128             while ($entry->fetch()) {
129                 $notices[] = $entry->notice_id;
130                 $cnt++;
131                 if ($cnt >= NOTICE_INBOX_GC_BOXCAR) {
132                     self::deleteMatching($user_id, $notices);
133                     $notices = array();
134                     $cnt = 0;
135                 }
136             }
137
138             if ($cnt > 0) {
139                 self::deleteMatching($user_id, $notices);
140                 $notices = array();
141             }
142         }
143
144         return $total;
145     }
146
147     static function deleteMatching($user_id, $notices)
148     {
149         $entry = new Notice_inbox();
150         return $entry->query('DELETE FROM notice_inbox '.
151                              'WHERE user_id = ' . $user_id . ' ' .
152                              'AND notice_id in ('.implode(',', $notices).')');
153     }
154
155     static function bulkInsert($notice_id, $created, $ni)
156     {
157         $cnt = 0;
158
159         $qryhdr = 'INSERT INTO notice_inbox (user_id, notice_id, source, created) VALUES ';
160         $qry = $qryhdr;
161
162         foreach ($ni as $id => $source) {
163             if ($cnt > 0) {
164                 $qry .= ', ';
165             }
166             $qry .= '('.$id.', '.$notice_id.', '.$source.", '".$created. "') ";
167             $cnt++;
168             if (rand() % NOTICE_INBOX_SOFT_LIMIT == 0) {
169                 // FIXME: Causes lag in replicated servers
170                 // Notice_inbox::gc($id);
171             }
172             if ($cnt >= MAX_BOXCARS) {
173                 $inbox = new Notice_inbox();
174                 $result = $inbox->query($qry);
175                 if (PEAR::isError($result)) {
176                     common_log_db_error($inbox, $qry);
177                 }
178                 $qry = $qryhdr;
179                 $cnt = 0;
180             }
181         }
182
183         if ($cnt > 0) {
184             $inbox = new Notice_inbox();
185             $result = $inbox->query($qry);
186             if (PEAR::isError($result)) {
187                 common_log_db_error($inbox, $qry);
188             }
189         }
190
191         return;
192     }
193 }