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;
}
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);
}
}
}

View File

@ -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