More SQL in the conversation notice fetching

This commit is contained in:
Mikael Nordfeldth 2014-05-11 22:46:40 +02:00
parent f9481467f8
commit 4358ac0675

View File

@ -76,35 +76,30 @@ class RawConversationNoticeStream extends NoticeStream
$this->id = $id; $this->id = $id;
} }
function getNotices($offset, $limit, $sinceId = null, $maxId = null)
{
$all = Notice::listGet('conversation', array($this->id));
$notices = $all[$this->id];
// Re-order in reverse-chron
usort($notices, array('RawConversationNoticeStream', '_reverseChron'));
// FIXME: handle since and max
$wanted = array_slice($notices, $offset, $limit);
return new ArrayWrapper($wanted);
}
function getNoticeIds($offset, $limit, $since_id, $max_id) function getNoticeIds($offset, $limit, $since_id, $max_id)
{ {
$notice = $this->getNotices($offset, $limit, $since_id, $max_id); $conv = Conversation::getKV('id', $this->id);
$ids = $notice->fetchAll('id'); if (!$conv instanceof Conversation) {
return $ids; throw new ServerException('Could not find conversation');
} }
$notice = new Notice();
// SELECT
$notice->selectAdd();
$notice->selectAdd('id');
function _reverseChron($a, $b) // WHERE
{ $notice->conversation = $this->id;
$at = strtotime($a->created); if (!empty($since_id)) {
$bt = strtotime($b->created); $notice->whereAdd(sprintf('notice.id > %d', $since_id));
}
if (!empty($max_id)) {
$notice->whereAdd(sprintf('notice.id <= %d', $max_id));
}
$notice->limit($offset, $limit);
if ($at == $bt) { // ORDER BY
return 0; // currently imitates the previously used "_reverseChron" sorting
} else if ($at > $bt) { $notice->orderBy('notice.created DESC');
return -1; return $notice->fetchAll('id');
} else {
return 1;
}
} }
} }