diff options
Diffstat (limited to 'classes/Inbox.php')
-rw-r--r-- | classes/Inbox.php | 87 |
1 files changed, 66 insertions, 21 deletions
diff --git a/classes/Inbox.php b/classes/Inbox.php index 610b5fceb..e14d4f4e7 100644 --- a/classes/Inbox.php +++ b/classes/Inbox.php @@ -51,38 +51,80 @@ class Inbox extends Memcached_DataObject return array(false, false, false); } - static function insertNotice($user_id, $notice_id) + /** + * Create a new inbox from existing Notice_inbox stuff + */ + + static function initialize($user_id) { + $ids = array(); + + $ni = new Notice_inbox(); + + $ni->user_id = $user_id; + $ni->selectAdd(); + $ni->selectAdd('notice_id'); + $ni->orderBy('notice_id DESC'); + $ni->limit(0, 1024); + + if ($ni->find()) { + while($ni->fetch()) { + $ids[] = $ni->notice_id; + } + } + + $ni->free(); + unset($ni); + $inbox = new Inbox(); - $inbox->query(sprintf('UPDATE inbox '. - 'set notice_ids = concat(cast(0x%08x as binary(4)), '. - 'substr(notice_ids, 1, 4092)) '. - 'WHERE user_id = %d', - $notice_id, $user_id)); + $inbox->user_id = $user_id; + $inbox->notice_ids = call_user_func_array('pack', array_merge(array('N*'), $ids)); + + $result = $inbox->insert(); + + if (!$result) { + common_log_db_error($inbox, 'INSERT', __FILE__); + return null; + } + + return $inbox; } - static function bulkInsert($notice_id, $user_ids) + static function insertNotice($user_id, $notice_id) { - $cnt = count($user_ids); + $inbox = Inbox::staticGet('user_id', $user_id); - for ($off = 0; $off < $cnt; $off += self::BOXCAR) { + if (empty($inbox)) { + $inbox = Inbox::initialize($user_id); + } - $boxcar = array_slice($user_ids, $off, self::BOXCAR); + if (empty($inbox)) { + return false; + } - if (empty($boxcar)) { // jump in, hobo! - break; - } + $result = $inbox->query(sprintf('UPDATE inbox '. + 'set notice_ids = concat(cast(0x%08x as binary(4)), '. + 'substr(notice_ids, 1, 4092)) '. + 'WHERE user_id = %d', + $notice_id, $user_id)); - $inbox = new Inbox(); + if ($result) { + $c = self::memcache(); - $inbox->query(sprintf('UPDATE inbox '. - 'set notice_ids = concat(cast(0x%08x as binary(4)), '. - 'substr(notice_ids, 1, 4092)) '. - 'WHERE user_id in (%s)', - $notice_id, implode(',', $boxcar))); + if (!empty($c)) { + $c->delete(self::cacheKey('inbox', 'user_id', $user_id)); + } + } + + return $result; + } - $inbox->free(); + static function bulkInsert($notice_id, $user_ids) + { + foreach ($user_ids as $user_id) + { + Inbox::insertNotice($user_id, $notice_id); } } @@ -91,7 +133,10 @@ class Inbox extends Memcached_DataObject $inbox = Inbox::staticGet('user_id', $user_id); if (empty($inbox)) { - return array(); + $inbox = Inbox::initialize($user_id); + if (empty($inbox)) { + return array(); + } } $ids = unpack('N*', $inbox->notice_ids); |