From ba6235a446d5cb848e6d9555c5f5d899e4314a83 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 3 Aug 2011 00:04:18 -0400 Subject: [PATCH] Get faves in Notice and pre-fill --- classes/Fave.php | 31 ++-------------------------- classes/Notice.php | 42 ++++++++++++++++++++++++++++++++++---- lib/noticelist.php | 2 ++ lib/threadednoticelist.php | 4 ++-- 4 files changed, 44 insertions(+), 35 deletions(-) diff --git a/classes/Fave.php b/classes/Fave.php index e8fdbffc71..5067185c0e 100644 --- a/classes/Fave.php +++ b/classes/Fave.php @@ -44,7 +44,7 @@ class Fave extends Memcached_DataObject common_log_db_error($fave, 'INSERT', __FILE__); 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)); } @@ -62,7 +62,7 @@ class Fave extends Memcached_DataObject if (Event::handle('StartDisfavorNotice', array($profile, $notice, &$result))) { $result = parent::delete(); - self::blow('fave:by_notice:%d', $this->notice_id); + self::blow('fave:list:notice_id:%d', $this->notice_id); if ($result) { Event::handle('EndDisfavorNotice', array($profile, $notice)); @@ -156,31 +156,4 @@ class Fave extends Memcached_DataObject 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; - } } diff --git a/classes/Notice.php b/classes/Notice.php index 6f5be311d8..eee6c2a502 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -1396,7 +1396,9 @@ class Notice extends Memcached_DataObject } $gis = Memcached_DataObject::listGet('Group_inbox', 'notice_id', array($this->id)); - + + $ids = array(); + foreach ($gis[$this->id] as $gi) { $ids[] = $gi->group_id; @@ -2434,7 +2436,7 @@ class Notice extends Memcached_DataObject function __sleep() { $vars = parent::__sleep(); - $skip = array('_original', '_profile', '_groups'); + $skip = array('_original', '_profile', '_groups', '_attachments', '_faves'); return array_diff($vars, $skip); } @@ -2482,8 +2484,8 @@ class Notice extends Memcached_DataObject $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 ($gi as $g) @@ -2545,4 +2547,36 @@ class Notice extends Memcached_DataObject $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]); + } + } } diff --git a/lib/noticelist.php b/lib/noticelist.php index a15755eef1..148f428edf 100644 --- a/lib/noticelist.php +++ b/lib/noticelist.php @@ -126,6 +126,8 @@ class NoticeList extends Widget { // Prefill attachments Notice::fillAttachments($notices); + // Prefill attachments + Notice::fillFaves($notices); // Prefill the profiles $profiles = Notice::fillProfiles($notices); // Prefill the avatars diff --git a/lib/threadednoticelist.php b/lib/threadednoticelist.php index 407f7bdde3..cf3c0b8943 100644 --- a/lib/threadednoticelist.php +++ b/lib/threadednoticelist.php @@ -470,9 +470,9 @@ class ThreadedNoticeListFavesItem extends NoticeListActorsItem { function getProfiles() { - $fave = Fave::byNotice($this->notice->id); + $faves = $this->notice->getFaves(); $profiles = array(); - while ($fave->fetch()) { + foreach ($faves as $fave) { $profiles[] = $fave->user_id; } return $profiles; -- 2.39.5