pre-fill avatars for Profiles in a notice list

This commit is contained in:
Evan Prodromou 2011-08-02 11:54:27 -04:00
parent e05f423bea
commit 06e2422517
5 changed files with 59 additions and 18 deletions

View File

@ -27,6 +27,11 @@ class Avatar extends Memcached_DataObject
/* the code above is auto generated do not remove the tag below */ /* the code above is auto generated do not remove the tag below */
###END_AUTOCODE ###END_AUTOCODE
static function pivotGet($keyCol, $keyVals, $otherCols)
{
return Memcached_DataObject::pivotGet('Avatar', $keyCol, $keyVals, $otherCols);
}
// We clean up the file, too // We clean up the file, too
function delete() function delete()

View File

@ -2481,24 +2481,26 @@ class Notice extends Memcached_DataObject
static function fillProfiles($notices) static function fillProfiles($notices)
{ {
$authors = array(); $map = self::getProfiles($notices);
foreach ($notices as $notice) { foreach ($notices as $notice) {
if (array_key_exists($notice->profile_id, $authors)) { if (array_key_exists($notice->profile_id, $map)) {
$authors[$notice->profile_id][] = $notice; $notice->_setProfile($map[$notice->profile_id]);
} else {
$authors[$notice->profile_id] = array($notice);
}
}
$profile = Profile::multiGet('id', array_keys($authors));
$profiles = $profile->fetchAll();
foreach ($profiles as $p) {
foreach ($authors[$p->id] as $notice) {
$notice->_setProfile($p);
} }
} }
return array_values($map);
}
static function getProfiles(&$notices)
{
$ids = array();
foreach ($notices as $notice) {
$ids[] = $notice->profile_id;
}
$ids = array_unique($ids);
return Memcached_DataObject::pivotGet('Profile', 'id', $ids);
} }
} }

View File

@ -68,12 +68,18 @@ class Profile extends Memcached_DataObject
return $this->_user; return $this->_user;
} }
protected $_avatars = array();
function getAvatar($width, $height=null) function getAvatar($width, $height=null)
{ {
if (is_null($height)) { if (is_null($height)) {
$height = $width; $height = $width;
} }
if (array_key_exists($width, $this->_avatars)) {
return $this->_avatars[$width];
}
$avatar = null; $avatar = null;
if (Event::handle('StartProfileGetAvatar', array($this, $width, &$avatar))) { if (Event::handle('StartProfileGetAvatar', array($this, $width, &$avatar))) {
@ -83,9 +89,16 @@ class Profile extends Memcached_DataObject
Event::handle('EndProfileGetAvatar', array($this, $width, &$avatar)); Event::handle('EndProfileGetAvatar', array($this, $width, &$avatar));
} }
$this->_avatars[$width] = $avatar;
return $avatar; return $avatar;
} }
function _fillAvatar($width, $avatar)
{
$this->_avatars[$width] = $avatar;
}
function getOriginalAvatar() function getOriginalAvatar()
{ {
$avatar = DB_DataObject::factory('avatar'); $avatar = DB_DataObject::factory('avatar');
@ -1353,7 +1366,26 @@ class Profile extends Memcached_DataObject
function __sleep() function __sleep()
{ {
$vars = parent::__sleep(); $vars = parent::__sleep();
$skip = array('_user'); $skip = array('_user', '_avatars');
return array_diff($vars, $skip); 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]);
}
}
} }

View File

@ -125,6 +125,7 @@ class NoticeList extends Widget
function prefill(&$notices) function prefill(&$notices)
{ {
// Prefill the profiles // Prefill the profiles
Notice::fillProfiles($notices); $profiles = Notice::fillProfiles($notices);
Profile::fillAvatars($profiles, AVATAR_STREAM_SIZE);
} }
} }

View File

@ -254,7 +254,8 @@ class ThreadedNoticeListItem extends NoticeListItem
function prefill(&$notices) function prefill(&$notices)
{ {
// Prefill the profiles // Prefill the profiles
Notice::fillProfiles($notices); $profiles = Notice::fillProfiles($notices);
Profile::fillAvatars($profiles, AVATAR_MINI_SIZE);
} }
} }