Skip to content
This repository has been archived by the owner on Jul 6, 2020. It is now read-only.

Commit

Permalink
use last_modified column for finding new items (so we also see if the…
Browse files Browse the repository at this point in the history
…y were updated or starred), use offset to paginate rather than item id
  • Loading branch information
BernhardPosselt committed Apr 26, 2013
1 parent 5d41833 commit 5cc47b4
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 152 deletions.
25 changes: 19 additions & 6 deletions businesslayer/itembusinesslayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
namespace OCA\News\BusinessLayer;

use \OCA\AppFramework\Utility\TimeFactory;
use \OCA\AppFramework\Db\DoesNotExistException;

use \OCA\News\Db\Item;
use \OCA\News\Db\ItemMapper;
Expand Down Expand Up @@ -70,22 +71,25 @@ public function findAllNew($id, $type, $updatedSince,
}


public function findAll($id, $type, $limit, $offset,
$showAll, $userId){
public function findAll($id, $type, $limit, $offset, $newestItemId, $showAll,
$userId){

$status = $this->statusFlag->typeToStatus($type, $showAll);

switch($type){
case FeedType::FEED:
$items = $this->mapper->findAllFeed($id, $limit, $offset,
$status, $userId);
$newestItemId, $status,
$userId);
break;
case FeedType::FOLDER:
$items = $this->mapper->findAllFolder($id, $limit, $offset,
$status, $userId);
$newestItemId, $status,
$userId);
break;
default:
$items = $this->mapper->findAll($limit, $offset, $status,
$userId);
$items = $this->mapper->findAll($limit, $offset, $newestItemId,
$status, $userId);
}

return $items;
Expand Down Expand Up @@ -138,4 +142,13 @@ public function autoPurgeOld(){
}


public function getNewestItemId($userId) {
try {
return $this->mapper->getNewestItemId($userId);
} catch(DoesNotExistException $ex) {
throw new BusinessLayerException($ex->getMessage());
}
}


}
44 changes: 23 additions & 21 deletions controller/itemcontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use \OCA\AppFramework\Core\API;
use \OCA\AppFramework\Http\Request;

use \OCA\News\BusinessLayer\BusinessLayerException;
use \OCA\News\BusinessLayer\ItemBusinessLayer;
use \OCA\News\BusinessLayer\FeedBusinessLayer;

Expand Down Expand Up @@ -59,32 +60,33 @@ public function items(){
$limit = $this->params('limit');
$type = (int) $this->params('type');
$id = (int) $this->params('id');
$offset = (int) $this->params('offset', 0);
$newestItemId = (int) $this->params('newestItemId');

$this->api->setUserValue('lastViewedFeedId', $id);
$this->api->setUserValue('lastViewedFeedType', $type);

if($limit !== null){
$offset = (int) $this->params('offset', 0);
$items = $this->itemBusinessLayer->findAll($id, $type, (int) $limit,
$offset, $showAll, $userId);
if($offset === 0) {
$feeds = $this->feedBusinessLayer->findAll($userId);
}
} else {
$updatedSince = (int) $this->params('updatedSince');
$items = $this->itemBusinessLayer->findAllNew($id, $type,
$updatedSince, $showAll, $userId);
}

$params = array(
'items' => $items
);
$params = array();

// we need to pass the newest feeds to not let the unread count get out
// of sync
if(isset($feeds)) {
$params['feeds'] = $feeds;
}
try {

// the offset is 0 if the user clicks on a new feed
// we need to pass the newest feeds to not let the unread count get
// out of sync
if($offset === 0) {
$params['newestItemId'] =
$this->itemBusinessLayer->getNewestItemId($userId);
$newestItemId = $params['newestItemId'];
$params['feeds'] = $this->feedBusinessLayer->findAll($userId);
}

$params['items'] = $this->itemBusinessLayer->findAll(
$id, $type, $limit,
$offset, $newestItemId,
$showAll, $userId);
// this gets thrown if there are no items
// in that case just return an empty array
} catch(BusinessLayerException $ex) {}

return $this->renderJSON($params);
}
Expand Down
80 changes: 44 additions & 36 deletions db/itemmapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,67 +139,65 @@ public function readFeed($feedId, $highestItemId, $userId){
}


public function findAllNewFeed($id, $updatedSince, $status, $userId){
$sql = 'AND `items`.`feed_id` = ? ' .
'AND `items`.`id` >= ?';
$sql = $this->makeSelectQueryStatus($sql, $status);
$params = array($userId, $id, $updatedSince);
public function findAllNew($updatedSince, $status, $userId){
$sql = $this->makeSelectQueryStatus('AND `items`.`last_modified` >= ?', $status);
$params = array($userId, $updatedSince);
return $this->findAllRows($sql, $params);
}


public function findAllNewFolder($id, $updatedSince, $status, $userId){
$sql = 'AND `feeds`.`folder_id` = ? ' .
'AND `items`.`id` >= ?';
'AND `items`.`last_modified` >= ?';
$sql = $this->makeSelectQueryStatus($sql, $status);
$params = array($userId, $id, $updatedSince);
return $this->findAllRows($sql, $params);
}


public function findAllNew($updatedSince, $status, $userId){
$sql = $this->makeSelectQueryStatus('AND `items`.`id` >= ?', $status);
$params = array($userId, $updatedSince);
public function findAllNewFeed($id, $updatedSince, $status, $userId){
$sql = 'AND `items`.`feed_id` = ? ' .
'AND `items`.`last_modified` >= ?';
$sql = $this->makeSelectQueryStatus($sql, $status);
$params = array($userId, $id, $updatedSince);
return $this->findAllRows($sql, $params);
}


public function findAllFeed($id, $limit, $offset, $status, $userId){
$params = array($userId, $id);
$sql = 'AND `items`.`feed_id` = ? ';
if($offset !== 0){
$sql .= 'AND `items`.`id` < ? ';
array_push($params, $offset);
}
$sql .= 'ORDER BY `items`.`id` DESC ';
public function findAll($limit, $offset, $newestItemId, $status, $userId){
$sql = 'AND `items`.`id` < ? ';
$sql .= 'ORDER BY `items`.`pub_date` DESC, `items`.`id` DESC ';
$params = array($userId, $newestItemId);

$sql = $this->makeSelectQueryStatus($sql, $status);
return $this->findAllRows($sql, $params, $limit);
return $this->findAllRows($sql, $params, $limit, $offset);
}


public function findAllFolder($id, $limit, $offset, $status, $userId){
$params = array($userId, $id);
$sql = 'AND `feeds`.`folder_id` = ? ';
if($offset !== 0){
$sql .= 'AND `items`.`id` < ? ';
array_push($params, $offset);
}
$sql .= 'ORDER BY `items`.`id` DESC ';
public function findAllFolder($id, $limit, $offset, $newestItemId, $status,
$userId){

$sql = 'AND `feeds`.`folder_id` = ? ' .
'AND `items`.`id` < ? ' .
'ORDER BY `items`.`pub_date` DESC, `items`.`id` DESC ';
$params = array($userId, $id, $newestItemId);

$sql = $this->makeSelectQueryStatus($sql, $status);
return $this->findAllRows($sql, $params, $limit);

return $this->findAllRows($sql, $params, $limit, $offset);
}


public function findAll($limit, $offset, $status, $userId){
$params = array($userId);
$sql = '';
if($offset !== 0){
$sql .= 'AND `items`.`id` < ? ';
array_push($params, $offset);
}
$sql .= 'ORDER BY `items`.`id` DESC ';
public function findAllFeed($id, $limit, $offset, $newestItemId, $status,
$userId){

$sql = 'AND `items`.`feed_id` = ? ' .
'AND `items`.`id` < ? ' .
'ORDER BY `items`.`pub_date` DESC, `items`.`id` DESC ';
$sql = $this->makeSelectQueryStatus($sql, $status);
return $this->findAllRows($sql, $params, $limit);
$params = array($userId, $id, $newestItemId);

return $this->findAllRows($sql, $params, $limit, $offset);
}


Expand Down Expand Up @@ -248,6 +246,16 @@ public function deleteReadOlderThanThreshold($threshold){
}


public function getNewestItemId($userId) {
$sql = 'SELECT MAX(`items`.`id`) AS `max_id` FROM `*PREFIX*news_items` `items` '.
'JOIN `*PREFIX*news_feeds` `feeds` ' .
'ON `feeds`.`id` = `items`.`feed_id` '.
'AND `feeds`.`user_id` = ?';
$params = array($userId);

$result = $this->findOneQuery($sql, $params);

return $result['max_id'];
}

}
Empty file added templates/part.shortcuts.php
Empty file.
33 changes: 30 additions & 3 deletions tests/unit/businesslayer/ItemBusinessLayerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

require_once(__DIR__ . "/../../classloader.php");

use \OCA\AppFramework\Db\DoesNotExistException;

use \OCA\News\Db\Item;
use \OCA\News\Db\StatusFlag;
Expand All @@ -42,6 +43,7 @@ class ItemBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility {
private $response;
private $status;
private $time;
private $newestItemId;


protected function setUp(){
Expand Down Expand Up @@ -74,6 +76,7 @@ protected function setUp(){
$this->showAll = true;
$this->offset = 5;
$this->limit = 20;
$this->newestItemId = 4;
}


Expand Down Expand Up @@ -134,13 +137,14 @@ public function testFindAllFeed(){
->with($this->equalTo($this->id),
$this->equalTo($this->limit),
$this->equalTo($this->offset),
$this->equalTo($this->newestItemId),
$this->equalTo($this->status),
$this->equalTo($this->user))
->will($this->returnValue($this->response));

$result = $this->itemBusinessLayer->findAll(
$this->id, $type, $this->limit,
$this->offset, $this->showAll,
$this->offset, $this->newestItemId, $this->showAll,
$this->user);
$this->assertEquals($this->response, $result);
}
Expand All @@ -153,13 +157,14 @@ public function testFindAllFolder(){
->with($this->equalTo($this->id),
$this->equalTo($this->limit),
$this->equalTo($this->offset),
$this->equalTo($this->newestItemId),
$this->equalTo($this->status),
$this->equalTo($this->user))
->will($this->returnValue($this->response));

$result = $this->itemBusinessLayer->findAll(
$this->id, $type, $this->limit,
$this->offset, $this->showAll,
$this->offset, $this->newestItemId, $this->showAll,
$this->user);
$this->assertEquals($this->response, $result);
}
Expand All @@ -171,13 +176,14 @@ public function testFindAll(){
->method('findAll')
->with( $this->equalTo($this->limit),
$this->equalTo($this->offset),
$this->equalTo($this->newestItemId),
$this->equalTo($this->status),
$this->equalTo($this->user))
->will($this->returnValue($this->response));

$result = $this->itemBusinessLayer->findAll(
$this->id, $type, $this->limit,
$this->offset, $this->showAll,
$this->offset, $this->newestItemId, $this->showAll,
$this->user);
$this->assertEquals($this->response, $result);
}
Expand Down Expand Up @@ -283,6 +289,27 @@ public function testAutoPurgeOldWillPurgeOld(){
}


public function testGetNewestItemId() {
$this->mapper->expects($this->once())
->method('getNewestItemId')
->with($this->equalTo($this->user))
->will($this->returnValue(12));

$result = $this->itemBusinessLayer->getNewestItemId($this->user);
$this->assertEquals(12, $result);
}


public function testGetNewestItemIdDoesNotExist() {
$this->mapper->expects($this->once())
->method('getNewestItemId')
->with($this->equalTo($this->user))
->will($this->throwException(new DoesNotExistException('There are no items')));

$this->setExpectedException('\OCA\News\BusinessLayer\BusinessLayerException');
$this->itemBusinessLayer->getNewestItemId($this->user);
}

}


Expand Down
Loading

0 comments on commit 5cc47b4

Please sign in to comment.