]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Pre-fill Notice attachments
authorEvan Prodromou <evan@status.net>
Wed, 3 Aug 2011 03:12:21 +0000 (23:12 -0400)
committerEvan Prodromou <evan@status.net>
Wed, 3 Aug 2011 03:12:21 +0000 (23:12 -0400)
classes/Notice.php
lib/noticelist.php

index 573437caefe4d4c13f328c9ffeacf81549e9854e..6f5be311d84bee90138b57bf2ca303c8e0cce0fc 100644 (file)
@@ -755,62 +755,33 @@ class Notice extends Memcached_DataObject
         return true;
     }
 
-    function getUploadedAttachment() {
-        $post = clone $this;
-        $query = 'select file.url as up, file.id as i from file join file_to_post on file.id = file_id where post_id=' . $post->escape($post->id) . ' and url like "%/notice/%/file"';
-        $post->query($query);
-        $post->fetch();
-        if (empty($post->up) || empty($post->i)) {
-            $ret = false;
-        } else {
-            $ret = array($post->up, $post->i);
-        }
-        $post->free();
-        return $ret;
-    }
-
-    function hasAttachments() {
-        $post = clone $this;
-        $query = "select count(file_id) as n_attachments from file join file_to_post on (file_id = file.id) join notice on (post_id = notice.id) where post_id = " . $post->escape($post->id);
-        $post->query($query);
-        $post->fetch();
-        $n_attachments = intval($post->n_attachments);
-        $post->free();
-        return $n_attachments;
-    }
-
+       protected $_attachments = -1;
+       
     function attachments() {
 
-        $keypart = sprintf('notice:file_ids:%d', $this->id);
-
-        $idstr = self::cacheGet($keypart);
-
-        if ($idstr !== false) {
-            $ids = explode(',', $idstr);
-        } else {
-            $ids = array();
-            $f2p = new File_to_post;
-            $f2p->post_id = $this->id;
-            if ($f2p->find()) {
-                while ($f2p->fetch()) {
-                    $ids[] = $f2p->file_id;
-                }
-            }
-            self::cacheSet($keypart, implode(',', $ids));
+               if ($this->_attachments != -1)  {
+            return $this->_attachments;
         }
-
-        $att = array();
-
-        foreach ($ids as $id) {
-            $f = File::staticGet('id', $id);
-            if (!empty($f)) {
-                $att[] = clone($f);
-            }
+               
+               $f2ps = Memcached_DataObject::listGet('File_to_post', 'post_id', array($this->id));
+               
+               $ids = array();
+               
+               foreach ($f2ps[$this->id] as $f2p) {
+            $ids[] = $f2p->file_id;    
         }
+               
+               $files = Memcached_DataObject::multiGet('File', 'id', $ids);
 
-        return $att;
+               $this->_attachments = $files->fetchAll();
+               
+        return $this->_attachments;
     }
 
+       function _setAttachments($attachments)
+       {
+           $this->_attachments = $attachments;
+       }
 
     function publicStream($offset=0, $limit=20, $since_id=0, $max_id=0)
     {
@@ -2507,11 +2478,7 @@ class Notice extends Memcached_DataObject
        
        static function fillGroups(&$notices)
        {
-               $ids = array();
-               foreach ($notices as $notice) {
-                       $ids[] = $notice->id;
-               }
-               $ids = array_unique($ids);
+        $ids = self::_idsOf($notices);
                
                $gis = Memcached_DataObject::listGet('Group_inbox', 'notice_id', $ids);
                
@@ -2539,4 +2506,43 @@ class Notice extends Memcached_DataObject
                    $notice->_setGroups($grps);
                }
        }
+
+    static function _idsOf(&$notices)
+    {
+               $ids = array();
+               foreach ($notices as $notice) {
+                       $ids[] = $notice->id;
+               }
+               $ids = array_unique($ids);
+        return $ids;
+    }
+
+    static function fillAttachments(&$notices)
+    {
+        $ids = self::_idsOf($notices);
+
+               $f2pMap = Memcached_DataObject::listGet('File_to_post', 'post_id', $ids);
+               
+               $fileIds = array();
+               
+               foreach ($f2pMap as $noticeId => $f2ps) {
+            foreach ($f2ps as $f2p) {
+                $fileIds[] = $f2p->file_id;    
+            }
+        }
+
+        $fileIds = array_unique($fileIds);
+
+               $fileMap = Memcached_DataObject::pivotGet('File', 'id', $fileIds);
+
+               foreach ($notices as $notice)
+               {
+                       $files = array();
+                       $f2ps = $f2pMap[$notice->id];
+                       foreach ($f2ps as $f2p) {
+                           $files[] = $fileMap[$f2p->file_id];
+                       }
+                   $notice->_setAttachments($files);
+               }
+    }
 }
index a4781d9daa7a077cb33917eaad1c931501e94149..a15755eef1a9994f6efcf2a45e008ba1e720f479 100644 (file)
@@ -124,6 +124,8 @@ class NoticeList extends Widget
     
     static function prefill(&$notices, $avatarSize=AVATAR_STREAM_SIZE)
     {
+        // Prefill attachments
+        Notice::fillAttachments($notices);
        // Prefill the profiles
        $profiles = Notice::fillProfiles($notices);
        // Prefill the avatars