From: Evan Prodromou Date: Tue, 2 Aug 2011 15:54:27 +0000 (-0400) Subject: pre-fill avatars for Profiles in a notice list X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=06e2422517628bb46fefee71ee0a514756d88864;p=quix0rs-gnu-social.git pre-fill avatars for Profiles in a notice list --- diff --git a/classes/Avatar.php b/classes/Avatar.php index 0b5141ba53..bdf3739bbf 100644 --- a/classes/Avatar.php +++ b/classes/Avatar.php @@ -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() diff --git a/classes/Notice.php b/classes/Notice.php index 952194d43d..918190a24c 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -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); } } diff --git a/classes/Profile.php b/classes/Profile.php index 5eebd64a0d..d0dad48d57 100644 --- a/classes/Profile.php +++ b/classes/Profile.php @@ -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]); + } + } } diff --git a/lib/noticelist.php b/lib/noticelist.php index 3bd7b05b4a..aaa3b3c986 100644 --- a/lib/noticelist.php +++ b/lib/noticelist.php @@ -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); } } diff --git a/lib/threadednoticelist.php b/lib/threadednoticelist.php index ab25e85e9b..a63d25110f 100644 --- a/lib/threadednoticelist.php +++ b/lib/threadednoticelist.php @@ -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); } }