From: Evan Prodromou Date: Wed, 3 Aug 2011 03:12:21 +0000 (-0400) Subject: Pre-fill Notice attachments X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=dfbdd481fa195cad5b1a358ae195962123294371;p=quix0rs-gnu-social.git Pre-fill Notice attachments --- diff --git a/classes/Notice.php b/classes/Notice.php index 573437caef..6f5be311d8 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -755,62 +755,33 @@ class Notice extends Memcached_DataObject return true; } - function getUploadedAttachment() { - $post = clone $this; - $query = 'select file.url as up, file.id as i from file join file_to_post on file.id = file_id where post_id=' . $post->escape($post->id) . ' and url like "%/notice/%/file"'; - $post->query($query); - $post->fetch(); - if (empty($post->up) || empty($post->i)) { - $ret = false; - } else { - $ret = array($post->up, $post->i); - } - $post->free(); - return $ret; - } - - function hasAttachments() { - $post = clone $this; - $query = "select count(file_id) as n_attachments from file join file_to_post on (file_id = file.id) join notice on (post_id = notice.id) where post_id = " . $post->escape($post->id); - $post->query($query); - $post->fetch(); - $n_attachments = intval($post->n_attachments); - $post->free(); - return $n_attachments; - } - + protected $_attachments = -1; + function attachments() { - $keypart = sprintf('notice:file_ids:%d', $this->id); - - $idstr = self::cacheGet($keypart); - - if ($idstr !== false) { - $ids = explode(',', $idstr); - } else { - $ids = array(); - $f2p = new File_to_post; - $f2p->post_id = $this->id; - if ($f2p->find()) { - while ($f2p->fetch()) { - $ids[] = $f2p->file_id; - } - } - self::cacheSet($keypart, implode(',', $ids)); + if ($this->_attachments != -1) { + return $this->_attachments; } - - $att = array(); - - foreach ($ids as $id) { - $f = File::staticGet('id', $id); - if (!empty($f)) { - $att[] = clone($f); - } + + $f2ps = Memcached_DataObject::listGet('File_to_post', 'post_id', array($this->id)); + + $ids = array(); + + foreach ($f2ps[$this->id] as $f2p) { + $ids[] = $f2p->file_id; } + + $files = Memcached_DataObject::multiGet('File', 'id', $ids); - return $att; + $this->_attachments = $files->fetchAll(); + + return $this->_attachments; } + function _setAttachments($attachments) + { + $this->_attachments = $attachments; + } function publicStream($offset=0, $limit=20, $since_id=0, $max_id=0) { @@ -2507,11 +2478,7 @@ class Notice extends Memcached_DataObject static function fillGroups(&$notices) { - $ids = array(); - foreach ($notices as $notice) { - $ids[] = $notice->id; - } - $ids = array_unique($ids); + $ids = self::_idsOf($notices); $gis = Memcached_DataObject::listGet('Group_inbox', 'notice_id', $ids); @@ -2539,4 +2506,43 @@ class Notice extends Memcached_DataObject $notice->_setGroups($grps); } } + + static function _idsOf(&$notices) + { + $ids = array(); + foreach ($notices as $notice) { + $ids[] = $notice->id; + } + $ids = array_unique($ids); + return $ids; + } + + static function fillAttachments(&$notices) + { + $ids = self::_idsOf($notices); + + $f2pMap = Memcached_DataObject::listGet('File_to_post', 'post_id', $ids); + + $fileIds = array(); + + foreach ($f2pMap as $noticeId => $f2ps) { + foreach ($f2ps as $f2p) { + $fileIds[] = $f2p->file_id; + } + } + + $fileIds = array_unique($fileIds); + + $fileMap = Memcached_DataObject::pivotGet('File', 'id', $fileIds); + + foreach ($notices as $notice) + { + $files = array(); + $f2ps = $f2pMap[$notice->id]; + foreach ($f2ps as $f2p) { + $files[] = $fileMap[$f2p->file_id]; + } + $notice->_setAttachments($files); + } + } } diff --git a/lib/noticelist.php b/lib/noticelist.php index a4781d9daa..a15755eef1 100644 --- a/lib/noticelist.php +++ b/lib/noticelist.php @@ -124,6 +124,8 @@ class NoticeList extends Widget static function prefill(&$notices, $avatarSize=AVATAR_STREAM_SIZE) { + // Prefill attachments + Notice::fillAttachments($notices); // Prefill the profiles $profiles = Notice::fillProfiles($notices); // Prefill the avatars