]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Inbox.php
Merge branch 'master' into testing
[quix0rs-gnu-social.git] / classes / Inbox.php
index de52e1e0c9ecaa1ed5ba55707bb460ad5b9ef8f6..26b27d2b58f71f47c2e6aa816a6cf668ab46cc3e 100644 (file)
@@ -56,6 +56,22 @@ class Inbox extends Memcached_DataObject
      */
 
     static function initialize($user_id)
+    {
+        $inbox = Inbox::fromNoticeInbox($user_id);
+
+        unset($inbox->fake);
+
+        $result = $inbox->insert();
+
+        if (!$result) {
+            common_log_db_error($inbox, 'INSERT', __FILE__);
+            return null;
+        }
+
+        return $inbox;
+    }
+
+    static function fromNoticeInbox($user_id)
     {
         $ids = array();
 
@@ -79,21 +95,15 @@ class Inbox extends Memcached_DataObject
         $inbox = new Inbox();
 
         $inbox->user_id = $user_id;
-        $inbox->notice_ids = pack('N*', $ids);
-
-        $result = $inbox->insert();
-
-        if (!$result) {
-            common_log_db_error($inbox, 'INSERT', __FILE__);
-            return null;
-        }
+        $inbox->notice_ids = call_user_func_array('pack', array_merge(array('N*'), $ids));
+        $inbox->fake = true;
 
         return $inbox;
     }
 
     static function insertNotice($user_id, $notice_id)
     {
-        $inbox = Inbox::staticGet('user_id', $user_id);
+        $inbox = DB_DataObject::staticGet('inbox', 'user_id', $user_id);
 
         if (empty($inbox)) {
             $inbox = Inbox::initialize($user_id);
@@ -110,11 +120,7 @@ class Inbox extends Memcached_DataObject
                                         $notice_id, $user_id));
 
         if ($result) {
-            $c = $this->memcache();
-
-            if (!empty($c)) {
-                $c->delete($this->cacheKey($this->tableName(), 'user_id', $user_id));
-            }
+            self::blow('inbox:user_id:%d', $user_id);
         }
 
         return $result;
@@ -122,25 +128,9 @@ class Inbox extends Memcached_DataObject
 
     static function bulkInsert($notice_id, $user_ids)
     {
-        $cnt = count($user_ids);
-
-        for ($off = 0; $off < $cnt; $off += self::BOXCAR) {
-
-            $boxcar = array_slice($user_ids, $off, self::BOXCAR);
-
-            if (empty($boxcar)) { // jump in, hobo!
-                break;
-            }
-
-            $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 in (%s)',
-                                  $notice_id, implode(',', $boxcar)));
-
-            $inbox->free();
+        foreach ($user_ids as $user_id)
+        {
+            Inbox::insertNotice($user_id, $notice_id);
         }
     }
 
@@ -149,16 +139,35 @@ class Inbox extends Memcached_DataObject
         $inbox = Inbox::staticGet('user_id', $user_id);
 
         if (empty($inbox)) {
-            $inbox = Inbox::initialize($user_id);
+            $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);