X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=classes%2FInbox.php;h=26b27d2b58f71f47c2e6aa816a6cf668ab46cc3e;hb=663e4e02a1b3b1c104c2c3db19e524a486c3d981;hp=610b5fceb2b9bedb0b1a028d8768cf9c5710b950;hpb=1e7ec69190e089f89b57181d561e2df2c8bdd3b5;p=quix0rs-gnu-social.git diff --git a/classes/Inbox.php b/classes/Inbox.php index 610b5fceb2..26b27d2b58 100644 --- a/classes/Inbox.php +++ b/classes/Inbox.php @@ -51,38 +51,86 @@ 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) { - $inbox = new Inbox(); + $inbox = Inbox::fromNoticeInbox($user_id); + + unset($inbox->fake); + + $result = $inbox->insert(); - $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)); + if (!$result) { + common_log_db_error($inbox, 'INSERT', __FILE__); + return null; + } + + return $inbox; } - static function bulkInsert($notice_id, $user_ids) + static function fromNoticeInbox($user_id) { - $cnt = count($user_ids); + $ids = array(); - for ($off = 0; $off < $cnt; $off += self::BOXCAR) { + $ni = new Notice_inbox(); - $boxcar = array_slice($user_ids, $off, self::BOXCAR); + $ni->user_id = $user_id; + $ni->selectAdd(); + $ni->selectAdd('notice_id'); + $ni->orderBy('notice_id DESC'); + $ni->limit(0, 1024); - if (empty($boxcar)) { // jump in, hobo! - break; + if ($ni->find()) { + while($ni->fetch()) { + $ids[] = $ni->notice_id; } + } - $inbox = new Inbox(); + $ni->free(); + unset($ni); - $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))); + $inbox = new Inbox(); + + $inbox->user_id = $user_id; + $inbox->notice_ids = call_user_func_array('pack', array_merge(array('N*'), $ids)); + $inbox->fake = true; - $inbox->free(); + return $inbox; + } + + static function insertNotice($user_id, $notice_id) + { + $inbox = DB_DataObject::staticGet('inbox', 'user_id', $user_id); + + if (empty($inbox)) { + $inbox = Inbox::initialize($user_id); + } + + if (empty($inbox)) { + return false; + } + + $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)); + + if ($result) { + self::blow('inbox:user_id:%d', $user_id); + } + + return $result; + } + + static function bulkInsert($notice_id, $user_ids) + { + foreach ($user_ids as $user_id) + { + Inbox::insertNotice($user_id, $notice_id); } } @@ -91,13 +139,35 @@ class Inbox extends Memcached_DataObject $inbox = Inbox::staticGet('user_id', $user_id); if (empty($inbox)) { - return array(); + $inbox = Inbox::fromNoticeInbox($user_id); + if (empty($inbox)) { + return array(); + } else { + $inbox->encache(); + } } $ids = unpack('N*', $inbox->notice_ids); - // XXX: handle since_id - // XXX: handle max_id + if (!empty($since_id)) { + $newids = array(); + foreach ($ids as $id) { + if ($id > $since_id) { + $newids[] = $id; + } + } + $ids = $newids; + } + + if (!empty($max_id)) { + $newids = array(); + foreach ($ids as $id) { + if ($id <= $max_id) { + $newids[] = $id; + } + } + $ids = $newids; + } $ids = array_slice($ids, $offset, $limit);