3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, Control Yourself, Inc.
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.
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.
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/>.
20 if (!defined('LACONICA')) { exit(1); }
22 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
24 // We keep 5 pages of inbox notices in memcache, +1 for pagination check
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);
31 define('NOTICE_INBOX_SOURCE_SUB', 1);
32 define('NOTICE_INBOX_SOURCE_GROUP', 2);
33 define('NOTICE_INBOX_SOURCE_REPLY', 3);
34 define('NOTICE_INBOX_SOURCE_GATEWAY', -1);
36 class Notice_inbox extends Memcached_DataObject
39 /* the code below is auto generated do not remove the above tag */
41 public $__table = 'notice_inbox'; // table name
42 public $user_id; // int(4) primary_key not_null
43 public $notice_id; // int(4) primary_key not_null
44 public $created; // datetime() not_null
45 public $source; // tinyint(1) default_1
48 function staticGet($k,$v=null)
49 { return Memcached_DataObject::staticGet('Notice_inbox',$k,$v); }
51 /* the code above is auto generated do not remove the tag below */
54 function stream($user_id, $offset, $limit, $since_id, $max_id, $since, $own=false)
56 return Notice::stream(array('Notice_inbox', '_streamDirect'),
57 array($user_id, $own),
58 ($own) ? 'notice_inbox:by_user:'.$user_id :
59 'notice_inbox:by_user_own:'.$user_id,
60 $offset, $limit, $since_id, $max_id, $since);
63 function _streamDirect($user_id, $own, $offset, $limit, $since_id, $max_id, $since)
65 $inbox = new Notice_inbox();
67 $inbox->user_id = $user_id;
70 $inbox->whereAdd('source != ' . NOTICE_INBOX_SOURCE_GATEWAY);
74 $inbox->whereAdd('notice_id > ' . $since_id);
78 $inbox->whereAdd('notice_id <= ' . $max_id);
81 if (!is_null($since)) {
82 $inbox->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
85 $inbox->orderBy('notice_id DESC');
87 if (!is_null($offset)) {
88 $inbox->limit($offset, $limit);
94 while ($inbox->fetch()) {
95 $ids[] = $inbox->notice_id;
102 function &pkeyGet($kv)
104 return Memcached_DataObject::pkeyGet('Notice_inbox', $kv);
107 static function gc($user_id)
109 $entry = new Notice_inbox();
110 $entry->user_id = $user_id;
111 $entry->orderBy('created DESC');
112 $entry->limit(NOTICE_INBOX_LIMIT - 1, NOTICE_INBOX_GC_MAX);
114 $total = $entry->find();
119 while ($entry->fetch()) {
120 $notices[] = $entry->notice_id;
122 if ($cnt >= NOTICE_INBOX_GC_BOXCAR) {
123 self::deleteMatching($user_id, $notices);
130 self::deleteMatching($user_id, $notices);
136 static function deleteMatching($user_id, $notices)
138 $entry = new Notice_inbox();
139 return $entry->query('DELETE FROM notice_inbox '.
140 'WHERE user_id = ' . $user_id . ' ' .
141 'AND notice_id in ('.implode(',', $notices).')');