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)
{
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);
$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);
+ }
+ }
}