Get faves in Notice and pre-fill

This commit is contained in:
Evan Prodromou 2011-08-03 00:04:18 -04:00
parent dfbdd481fa
commit ba6235a446
4 changed files with 44 additions and 35 deletions

View File

@ -44,7 +44,7 @@ class Fave extends Memcached_DataObject
common_log_db_error($fave, 'INSERT', __FILE__); common_log_db_error($fave, 'INSERT', __FILE__);
return false; return false;
} }
self::blow('fave:by_notice:%d', $fave->notice_id); self::blow('fave:list:notice_id:%d', $fave->notice_id);
Event::handle('EndFavorNotice', array($profile, $notice)); Event::handle('EndFavorNotice', array($profile, $notice));
} }
@ -62,7 +62,7 @@ class Fave extends Memcached_DataObject
if (Event::handle('StartDisfavorNotice', array($profile, $notice, &$result))) { if (Event::handle('StartDisfavorNotice', array($profile, $notice, &$result))) {
$result = parent::delete(); $result = parent::delete();
self::blow('fave:by_notice:%d', $this->notice_id); self::blow('fave:list:notice_id:%d', $this->notice_id);
if ($result) { if ($result) {
Event::handle('EndDisfavorNotice', array($profile, $notice)); Event::handle('EndDisfavorNotice', array($profile, $notice));
@ -156,31 +156,4 @@ class Fave extends Memcached_DataObject
return $fav; return $fav;
} }
/**
* Grab a list of profile who have favored this notice.
*
* @return ArrayWrapper masquerading as a Fave
*/
static function byNotice($noticeId)
{
$c = self::memcache();
$key = Cache::key('fave:by_notice:' . $noticeId);
$wrapper = $c->get($key);
if (!$wrapper) {
// @fixme caching & scalability!
$fave = new Fave();
$fave->notice_id = $noticeId;
$fave->find();
$list = array();
while ($fave->fetch()) {
$list[] = clone($fave);
}
$wrapper = new ArrayWrapper($list);
$c->set($key, $wrapper);
}
return $wrapper;
}
} }

View File

@ -1396,7 +1396,9 @@ class Notice extends Memcached_DataObject
} }
$gis = Memcached_DataObject::listGet('Group_inbox', 'notice_id', array($this->id)); $gis = Memcached_DataObject::listGet('Group_inbox', 'notice_id', array($this->id));
$ids = array();
foreach ($gis[$this->id] as $gi) foreach ($gis[$this->id] as $gi)
{ {
$ids[] = $gi->group_id; $ids[] = $gi->group_id;
@ -2434,7 +2436,7 @@ class Notice extends Memcached_DataObject
function __sleep() function __sleep()
{ {
$vars = parent::__sleep(); $vars = parent::__sleep();
$skip = array('_original', '_profile', '_groups'); $skip = array('_original', '_profile', '_groups', '_attachments', '_faves');
return array_diff($vars, $skip); return array_diff($vars, $skip);
} }
@ -2482,8 +2484,8 @@ class Notice extends Memcached_DataObject
$gis = Memcached_DataObject::listGet('Group_inbox', 'notice_id', $ids); $gis = Memcached_DataObject::listGet('Group_inbox', 'notice_id', $ids);
common_debug(sprintf("Notice::fillGroups(): got %d results for %d notices", count($gis), count($ids))); $gids = array();
foreach ($gis as $id => $gi) foreach ($gis as $id => $gi)
{ {
foreach ($gi as $g) foreach ($gi as $g)
@ -2545,4 +2547,36 @@ class Notice extends Memcached_DataObject
$notice->_setAttachments($files); $notice->_setAttachments($files);
} }
} }
protected $_faves = -1;
/**
* All faves of this notice
*
* @return array Array of Fave objects
*/
function getFaves()
{
if ($this->_faves != -1) {
return $this->_faves;
}
$faveMap = Memcached_DataObject::listGet('Fave', 'notice_id', array($noticeId));
$this->_faves = $faveMap[$noticeId];
return $this->_faves;
}
function _setFaves($faves)
{
$this->_faves = $faves;
}
static function fillFaves(&$notices)
{
$ids = self::_idsOf($notices);
$faveMap = Memcached_DataObject::listGet('Fave', 'notice_id', $ids);
foreach ($notices as $notice) {
$notice->_setFaves($faveMap[$notice->id]);
}
}
} }

View File

@ -126,6 +126,8 @@ class NoticeList extends Widget
{ {
// Prefill attachments // Prefill attachments
Notice::fillAttachments($notices); Notice::fillAttachments($notices);
// Prefill attachments
Notice::fillFaves($notices);
// Prefill the profiles // Prefill the profiles
$profiles = Notice::fillProfiles($notices); $profiles = Notice::fillProfiles($notices);
// Prefill the avatars // Prefill the avatars

View File

@ -470,9 +470,9 @@ class ThreadedNoticeListFavesItem extends NoticeListActorsItem
{ {
function getProfiles() function getProfiles()
{ {
$fave = Fave::byNotice($this->notice->id); $faves = $this->notice->getFaves();
$profiles = array(); $profiles = array();
while ($fave->fetch()) { foreach ($faves as $fave) {
$profiles[] = $fave->user_id; $profiles[] = $fave->user_id;
} }
return $profiles; return $profiles;