Pre-fill Notice attachments

This commit is contained in:
Evan Prodromou 2011-08-02 23:12:21 -04:00
parent 9a84907d17
commit dfbdd481fa
2 changed files with 62 additions and 54 deletions

View File

@ -755,62 +755,33 @@ class Notice extends Memcached_DataObject
return true; return true;
} }
function getUploadedAttachment() { protected $_attachments = -1;
$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;
}
function attachments() { function attachments() {
$keypart = sprintf('notice:file_ids:%d', $this->id); if ($this->_attachments != -1) {
return $this->_attachments;
}
$idstr = self::cacheGet($keypart); $f2ps = Memcached_DataObject::listGet('File_to_post', 'post_id', array($this->id));
if ($idstr !== false) {
$ids = explode(',', $idstr);
} else {
$ids = array(); $ids = array();
$f2p = new File_to_post;
$f2p->post_id = $this->id; foreach ($f2ps[$this->id] as $f2p) {
if ($f2p->find()) {
while ($f2p->fetch()) {
$ids[] = $f2p->file_id; $ids[] = $f2p->file_id;
} }
}
self::cacheSet($keypart, implode(',', $ids)); $files = Memcached_DataObject::multiGet('File', 'id', $ids);
$this->_attachments = $files->fetchAll();
return $this->_attachments;
} }
$att = array(); function _setAttachments($attachments)
{
foreach ($ids as $id) { $this->_attachments = $attachments;
$f = File::staticGet('id', $id);
if (!empty($f)) {
$att[] = clone($f);
} }
}
return $att;
}
function publicStream($offset=0, $limit=20, $since_id=0, $max_id=0) 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) static function fillGroups(&$notices)
{ {
$ids = array(); $ids = self::_idsOf($notices);
foreach ($notices as $notice) {
$ids[] = $notice->id;
}
$ids = array_unique($ids);
$gis = Memcached_DataObject::listGet('Group_inbox', 'notice_id', $ids); $gis = Memcached_DataObject::listGet('Group_inbox', 'notice_id', $ids);
@ -2539,4 +2506,43 @@ class Notice extends Memcached_DataObject
$notice->_setGroups($grps); $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);
}
}
} }

View File

@ -124,6 +124,8 @@ class NoticeList extends Widget
static function prefill(&$notices, $avatarSize=AVATAR_STREAM_SIZE) static function prefill(&$notices, $avatarSize=AVATAR_STREAM_SIZE)
{ {
// Prefill attachments
Notice::fillAttachments($notices);
// Prefill the profiles // Prefill the profiles
$profiles = Notice::fillProfiles($notices); $profiles = Notice::fillProfiles($notices);
// Prefill the avatars // Prefill the avatars