we don't care if notice with max_id or since_id exist or not

this caused infinite scroll in notification stream to stop loading when
it came across a max_id that was removed from the db
This commit is contained in:
Hannes Mannerheim 2015-01-28 01:49:41 +01:00
parent f88be7bf6f
commit 6cc9a309c3
2 changed files with 7 additions and 100 deletions

View File

@ -41,8 +41,13 @@ class NotificationStream
$notification->limit($offset, $limit);
$notification->orderBy('qvitternotification.created DESC');
QvitterNotification::addWhereSinceId($notification, $since_id);
QvitterNotification::addWhereMaxId($notification, $max_id);
if($since_id) {
$notification->whereAdd(sprintf('qvitternotification.id > %d', $notification->escape($since_id)));
}
if($max_id) {
$notification->whereAdd(sprintf('qvitternotification.id <= %d', $notification->escape($max_id)));
}
if (!$notification->find()) {
return array();

View File

@ -54,103 +54,5 @@ class QvitterNotification extends Managed_DataObject
return $result;
}
/**
* Look up the creation timestamp for a given notice ID, even
* if it's been deleted.
*
* @param int $id
* @return mixed string recorded creation timestamp, or false if can't be found
*/
public static function getAsTimestamp($id)
{
if (!$id) {
return false;
}
$notice = QvitterNotification::getKV('id', $id);
if ($notice) {
return $notice->created;
}
return false;
}
/**
* Build an SQL 'where' fragment for timestamp-based sorting from a since_id
* parameter, matching notices posted after the given one (exclusive).
*
* If the referenced notice can't be found, will return false.
*
* @param int $id
* @param string $idField
* @param string $createdField
* @return mixed string or false if no match
*/
public static function whereSinceId($id, $idField='id', $createdField='created')
{
$since = QvitterNotification::getAsTimestamp($id);
if ($since) {
return sprintf("($createdField = '%s' and $idField > %d) or ($createdField > '%s')", $since, $id, $since);
}
return false;
}
/**
* Build an SQL 'where' fragment for timestamp-based sorting from a since_id
* parameter, matching notices posted after the given one (exclusive), and
* if necessary add it to the data object's query.
*
* @param DB_DataObject $obj
* @param int $id
* @param string $idField
* @param string $createdField
* @return mixed string or false if no match
*/
public static function addWhereSinceId(DB_DataObject $obj, $id, $idField='id', $createdField='created')
{
$since = self::whereSinceId($id, $idField, $createdField);
if ($since) {
$obj->whereAdd($since);
}
}
/**
* Build an SQL 'where' fragment for timestamp-based sorting from a max_id
* parameter, matching notices posted before the given one (inclusive).
*
* If the referenced notice can't be found, will return false.
*
* @param int $id
* @param string $idField
* @param string $createdField
* @return mixed string or false if no match
*/
public static function whereMaxId($id, $idField='id', $createdField='created')
{
$max = QvitterNotification::getAsTimestamp($id);
if ($max) {
return sprintf("($createdField < '%s') or ($createdField = '%s' and $idField <= %d)", $max, $max, $id);
}
return false;
}
/**
* Build an SQL 'where' fragment for timestamp-based sorting from a max_id
* parameter, matching notices posted before the given one (inclusive), and
* if necessary add it to the data object's query.
*
* @param DB_DataObject $obj
* @param int $id
* @param string $idField
* @param string $createdField
* @return mixed string or false if no match
*/
public static function addWhereMaxId(DB_DataObject $obj, $id, $idField='id', $createdField='created')
{
$max = self::whereMaxId($id, $idField, $createdField);
if ($max) {
$obj->whereAdd($max);
}
}
}