]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
pre-fill avatars for Profiles in a notice list
authorEvan Prodromou <evan@status.net>
Tue, 2 Aug 2011 15:54:27 +0000 (11:54 -0400)
committerEvan Prodromou <evan@status.net>
Tue, 2 Aug 2011 15:54:27 +0000 (11:54 -0400)
classes/Avatar.php
classes/Notice.php
classes/Profile.php
lib/noticelist.php
lib/threadednoticelist.php

index 0b5141ba53965420a4de1c9070118888c717fbd5..bdf3739bbf86b6541853f7e2594cc4b330c7a9e5 100644 (file)
@@ -27,6 +27,11 @@ class Avatar extends Memcached_DataObject
     /* the code above is auto generated do not remove the tag below */
     ###END_AUTOCODE
 
+       static function pivotGet($keyCol, $keyVals, $otherCols)
+       {
+           return Memcached_DataObject::pivotGet('Avatar', $keyCol, $keyVals, $otherCols);
+       }
+       
     // We clean up the file, too
 
     function delete()
index 952194d43d1522bf2197ceb8826717e4ea470d02..918190a24ce723c3770b4aea4d9aafe03a2914c4 100644 (file)
@@ -2481,24 +2481,26 @@ class Notice extends Memcached_DataObject
 
        static function fillProfiles($notices)
        {
-               $authors = array();
+               $map = self::getProfiles($notices);
                
                foreach ($notices as $notice) {
-                       if (array_key_exists($notice->profile_id, $authors)) {
-                           $authors[$notice->profile_id][] = $notice;
-                       } else {
-                           $authors[$notice->profile_id] = array($notice);
-                       }       
+                       if (array_key_exists($notice->profile_id, $map)) {
+                               $notice->_setProfile($map[$notice->profile_id]);    
+                       }
                }
                
-               $profile = Profile::multiGet('id', array_keys($authors));
+               return array_values($map);
+       }
+       
+       static function getProfiles(&$notices)
+       {
+               $ids = array();
+               foreach ($notices as $notice) {
+                       $ids[] = $notice->profile_id;
+               }
                
-               $profiles = $profile->fetchAll();
+               $ids = array_unique($ids);
                
-               foreach ($profiles as $p) {
-                       foreach ($authors[$p->id] as $notice) {
-                               $notice->_setProfile($p);    
-                       }
-               }
+               return Memcached_DataObject::pivotGet('Profile', 'id', $ids); 
        }
 }
index 5eebd64a0d39401fd75a2e1db38af1f8abc80b71..d0dad48d57d7d7f97ce14e3abb3dce9d548b992c 100644 (file)
@@ -68,12 +68,18 @@ class Profile extends Memcached_DataObject
         return $this->_user;
     }
 
+       protected $_avatars = array();
+       
     function getAvatar($width, $height=null)
     {
         if (is_null($height)) {
             $height = $width;
         }
 
+               if (array_key_exists($width, $this->_avatars)) {
+                       return $this->_avatars[$width];
+               }
+               
         $avatar = null;
 
         if (Event::handle('StartProfileGetAvatar', array($this, $width, &$avatar))) {
@@ -83,9 +89,16 @@ class Profile extends Memcached_DataObject
             Event::handle('EndProfileGetAvatar', array($this, $width, &$avatar));
         }
 
+               $this->_avatars[$width] = $avatar;
+               
         return $avatar;
     }
 
+       function _fillAvatar($width, $avatar)
+       {
+               $this->_avatars[$width] = $avatar;    
+       }
+       
     function getOriginalAvatar()
     {
         $avatar = DB_DataObject::factory('avatar');
@@ -1353,7 +1366,26 @@ class Profile extends Memcached_DataObject
     function __sleep()
     {
         $vars = parent::__sleep();
-        $skip = array('_user');
+        $skip = array('_user', '_avatars');
         return array_diff($vars, $skip);
     }
+    
+    static function fillAvatars(&$profiles, $width)
+    {
+       $ids = array();
+       foreach ($profiles as $profile) {
+           $ids[] = $profile->id;
+       }
+       
+       common_debug('Got here');
+       
+       $avatars = Avatar::pivotGet('profile_id', $ids, array('width' => $width,
+                                                                                                                         'height' => $width));
+       
+       common_debug(sprintf('Got %d avatars for %d profiles', count($avatars), count($ids)));
+       
+       foreach ($profiles as $profile) {
+           $profile->_fillAvatar($width, $avatars[$profile->id]);
+       }
+    }
 }
index 3bd7b05b4adb8c446d40d2225f86ef848c78b531..aaa3b3c98691e8eb127683bdaa1179e1d728088c 100644 (file)
@@ -125,6 +125,7 @@ class NoticeList extends Widget
     function prefill(&$notices)
     {
        // Prefill the profiles
-       Notice::fillProfiles($notices);
+       $profiles = Notice::fillProfiles($notices);
+       Profile::fillAvatars($profiles, AVATAR_STREAM_SIZE);
     }
 }
index ab25e85e9bda0e955e132e3b29980f637cbc3eb1..a63d25110f10fa926e49a36b258725ec852a7b5d 100644 (file)
@@ -254,7 +254,8 @@ class ThreadedNoticeListItem extends NoticeListItem
     function prefill(&$notices)
     {       
        // Prefill the profiles
-       Notice::fillProfiles($notices);
+       $profiles = Notice::fillProfiles($notices);
+       Profile::fillAvatars($profiles, AVATAR_MINI_SIZE);
     }
 }