1
0
mirror of https://github.com/chylex/Nextcloud-News.git synced 2025-04-09 10:15:44 +02:00

order by pubdate

This commit is contained in:
Bernhard Posselt 2014-05-29 22:58:30 +02:00
parent b91ef01fdb
commit cba949fb07
20 changed files with 347 additions and 317 deletions

View File

@ -11,13 +11,12 @@
namespace OCA\News\Controller;
use \OCP\AppFramework\Http\IResponseSerializer;
use \OCP\AppFramework\Http\Response;
use \OCA\News\Db\IAPI;
class EntityApiSerializer implements IResponseSerializer {
class EntityApiSerializer {
public function __construct($level) {
@ -35,10 +34,6 @@ class EntityApiSerializer implements IResponseSerializer {
*/
public function serialize($data) {
if($data === null || $data instanceof Response) {
return $data;
}
if($data instanceof IAPI) {
return [$this->level => [$data->toAPI()]];
}
@ -58,14 +53,14 @@ class EntityApiSerializer implements IResponseSerializer {
foreach($entities as $entity) {
if($entity instanceof IAPI) {
$converted[] = $entity->toAPI();
$converted[] = $entity->toAPI();
// break if it contains anything else than entities
} else {
return $entities;
}
}
return $converted;
}

View File

@ -33,6 +33,7 @@ class FeedApiController extends ApiController {
private $userId;
private $logger;
private $loggerParams;
private $serializer;
public function __construct($appName,
IRequest $request,
@ -47,7 +48,7 @@ class FeedApiController extends ApiController {
$this->userId = $userId;
$this->logger = $logger;
$this->loggerParams = $loggerParams;
$this->registerSerializer(new EntityApiSerializer('feeds'));
$this->serializer = new EntityApiSerializer('feeds');
}
@ -64,14 +65,14 @@ class FeedApiController extends ApiController {
'feeds' => $this->feedService->findAll($this->userId)
];
try {
$result['newestItemId'] = $this->itemService->getNewestItemId($this->userId);
// in case there are no items, ignore
} catch(ServiceNotFoundException $ex) {}
return $result;
return $this->serializer->serialize($result);
}
@ -96,7 +97,7 @@ class FeedApiController extends ApiController {
// in case there are no items, ignore
} catch(ServiceNotFoundException $ex) {}
return $result;
return $this->serializer->serialize($result);
} catch(ServiceConflictException $ex) {
return $this->error($ex, Http::STATUS_CONFLICT);
@ -110,7 +111,7 @@ class FeedApiController extends ApiController {
* @NoAdminRequired
* @NoCSRFRequired
* @CORS
*
*
* @param int $feedId
*/
public function delete($feedId) {
@ -179,7 +180,7 @@ class FeedApiController extends ApiController {
foreach ($feeds as $feed) {
$result['feeds'][] = [
'id' => $feed->getId(),
'id' => $feed->getId(),
'userId' => $feed->getUserId()
];
}

View File

@ -31,6 +31,7 @@ class FolderApiController extends ApiController {
private $folderService;
private $itemService;
private $userId;
private $serializer;
public function __construct($appName,
IRequest $request,
@ -41,7 +42,7 @@ class FolderApiController extends ApiController {
$this->folderService = $folderService;
$this->itemService = $itemService;
$this->userId = $userId;
$this->registerSerializer(new EntityApiSerializer('folders'));
$this->serializer = new EntityApiSerializer('folders');
}
@ -51,7 +52,9 @@ class FolderApiController extends ApiController {
* @CORS
*/
public function index() {
return $this->folderService->findAll($this->userId);
return $this->serializer->serialize(
$this->folderService->findAll($this->userId)
);
}
@ -65,7 +68,9 @@ class FolderApiController extends ApiController {
public function create($name) {
try {
$this->folderService->purgeDeleted($this->userId, false);
return $this->folderService->create($name, $this->userId);
return $this->serializer->serialize(
$this->folderService->create($name, $this->userId)
);
} catch(ServiceValidationException $ex) {
return $this->error($ex, Http::STATUS_UNPROCESSABLE_ENTITY);
} catch(ServiceConflictException $ex) {

View File

@ -34,12 +34,12 @@ class FolderController extends Controller {
private $itemService;
private $userId;
public function __construct($appName,
IRequest $request,
public function __construct($appName,
IRequest $request,
FolderService $folderService,
FeedService $feedService,
ItemService $itemService,
$userId){
$userId) {
parent::__construct($appName, $request);
$this->folderService = $folderService;
$this->feedService = $feedService;
@ -51,23 +51,23 @@ class FolderController extends Controller {
/**
* @NoAdminRequired
*/
public function index(){
public function index() {
$folders = $this->folderService->findAll($this->userId);
return ['folders' => $folders];
}
private function setOpened($isOpened, $folderId){
private function setOpened($isOpened, $folderId) {
$this->folderService->open($folderId, $isOpened, $this->userId);
}
/**
* @NoAdminRequired
*
*
* @param int $folderId
*/
public function open($folderId){
public function open($folderId) {
try {
$this->setOpened(true, $folderId);
} catch(ServiceNotFoundException $ex) {
@ -81,7 +81,7 @@ class FolderController extends Controller {
*
* @param int $folderId
*/
public function collapse($folderId){
public function collapse($folderId) {
try {
$this->setOpened(false, $folderId);
} catch(ServiceNotFoundException $ex) {
@ -95,9 +95,9 @@ class FolderController extends Controller {
*
* @param string $folderName
*/
public function create($folderName){
public function create($folderName) {
try {
// we need to purge deleted folders if a folder is created to
// we need to purge deleted folders if a folder is created to
// prevent already exists exceptions
$this->folderService->purgeDeleted($this->userId, false);
$folder = $this->folderService->create($folderName, $this->userId);
@ -109,16 +109,16 @@ class FolderController extends Controller {
} catch(ServiceValidationException $ex) {
return $this->error($ex, Http::STATUS_UNPROCESSABLE_ENTITY);
}
}
/**
* @NoAdminRequired
*
*
* @param int $folderId
*/
public function delete($folderId){
public function delete($folderId) {
try {
$this->folderService->markDeleted($folderId, $this->userId);
} catch (ServiceNotFoundException $ex){
@ -133,9 +133,9 @@ class FolderController extends Controller {
* @param string $folderName
* @param int $folderId
*/
public function rename($folderName, $folderId){
public function rename($folderName, $folderId) {
try {
$folder = $this->folderService->rename($folderId, $folderName,
$folder = $this->folderService->rename($folderId, $folderName,
$this->userId);
return ['folders' => [$folder]];
@ -143,7 +143,7 @@ class FolderController extends Controller {
} catch(ServiceConflictException $ex) {
return $this->error($ex, Http::STATUS_CONFLICT);
} catch(ServiceValidationException $ex) {
return $this->error($ex, Http::STATUS_UNPROCESSABLE_ENTITY);
return $this->error($ex, Http::STATUS_UNPROCESSABLE_ENTITY);
} catch (ServiceNotFoundException $ex){
return $this->error($ex, Http::STATUS_NOT_FOUND);
}
@ -155,7 +155,7 @@ class FolderController extends Controller {
* @param int $folderId
* @param int $highestItemId
*/
public function read($folderId, $highestItemId){
public function read($folderId, $highestItemId) {
$this->itemService->readFolder($folderId, $highestItemId, $this->userId);
return ['feeds' => $this->feedService->findAll($this->userId)];
@ -164,10 +164,10 @@ class FolderController extends Controller {
/**
* @NoAdminRequired
*
*
* @param int $folderId
*/
public function restore($folderId){
public function restore($folderId) {
try {
$this->folderService->unmarkDeleted($folderId, $this->userId);
} catch (ServiceNotFoundException $ex){

View File

@ -26,6 +26,7 @@ class ItemApiController extends ApiController {
private $itemService;
private $userId;
private $serializer;
public function __construct($appName,
IRequest $request,
@ -34,7 +35,7 @@ class ItemApiController extends ApiController {
parent::__construct($appName, $request);
$this->itemService = $itemService;
$this->userId = $userId;
$this->registerSerializer(new EntityApiSerializer('items'));
$this->serializer = new EntityApiSerializer('items');
}
@ -42,16 +43,22 @@ class ItemApiController extends ApiController {
* @NoAdminRequired
* @NoCSRFRequired
* @CORS
*
*
* @param int $type
* @param int $id
* @param bool $getRead
* @param int $batchSize
* @param int $offset
* @param int $oldestFirst
*/
public function index($type, $id, $getRead, $batchSize=20, $offset=0) {
return $this->itemService->findAll($id, $type, $batchSize, $offset,
$getRead, $this->userId);
public function index($type, $id, $getRead, $batchSize=20, $offset=0,
$oldestFirst=false) {
return $this->serializer->serialize(
$this->itemService->findAll(
$id, $type, $batchSize, $offset, $getRead, $oldestFirst,
$this->userId
)
);
}
@ -59,14 +66,16 @@ class ItemApiController extends ApiController {
* @NoAdminRequired
* @NoCSRFRequired
* @CORS
*
*
* @param int $type
* @param int $id
* @param int $lastModified
*/
public function updated($type, $id, $lastModified=0) {
return $this->itemService->findAllNew($id, $type, $lastModified,
true, $this->userId);
return $this->serializer->serialize(
$this->itemService->findAllNew($id, $type, $lastModified,
true, $this->userId)
);
}
@ -188,7 +197,7 @@ class ItemApiController extends ApiController {
private function setMultipleStarred($isStarred, $items) {
foreach($items as $item) {
try {
$this->itemService->star($item['feedId'], $item['guidHash'],
$this->itemService->star($item['feedId'], $item['guidHash'],
$isStarred, $this->userId);
} catch(ServiceNotFoundException $ex) {
continue;
@ -213,7 +222,7 @@ class ItemApiController extends ApiController {
* @NoAdminRequired
* @NoCSRFRequired
* @CORS
*
*
* @param int[] item ids
*/
public function unstarMultiple($items) {

View File

@ -80,7 +80,8 @@ class ItemController extends Controller {
}
$params['items'] = $this->itemService->findAll(
$id, $type, $limit, $offset, $showAll, $this->userId, $oldestFirst
$id, $type, $limit, $offset, $showAll, $oldestFirst,
$this->userId
);
// this gets thrown if there are no items

View File

@ -41,7 +41,7 @@ class ItemMapper extends Mapper implements IMapper {
'ON `folders`.`id` = `feeds`.`folder_id` ' .
'WHERE `feeds`.`folder_id` = 0 ' .
'OR `folders`.`deleted_at` = 0 ' .
'ORDER BY `items`.`id` ' . $ordering;
'ORDER BY `items`.`pub_date`, `items`.`id` ' . $ordering;
}
private function makeSelectQueryStatus($prependTo, $status, $oldestFirst=false) {
@ -174,39 +174,28 @@ class ItemMapper extends Mapper implements IMapper {
}
}
public function findAllFeed($id, $limit, $offset, $status, $userId, $oldestFirst=false){
public function findAllFeed($id, $limit, $offset, $status, $oldestFirst,
$userId){
$params = [$userId, $id];
$sql = 'AND `items`.`feed_id` = ? ';
if($offset !== 0){
$sql .= 'AND `items`.`id` ' . $this->getOperator($oldestFirst) . ' ? ';
$params[] = $offset;
}
$sql = $this->makeSelectQueryStatus($sql, $status, $oldestFirst);
return $this->findEntities($sql, $params, $limit);
return $this->findEntities($sql, $params, $limit, $offset);
}
public function findAllFolder($id, $limit, $offset, $status, $userId, $oldestFirst=false){
public function findAllFolder($id, $limit, $offset, $status, $oldestFirst,
$userId){
$params = [$userId, $id];
$sql = 'AND `feeds`.`folder_id` = ? ';
if($offset !== 0){
$sql .= 'AND `items`.`id` ' . $this->getOperator($oldestFirst) . ' ? ';
$params[] = $offset;
}
$sql = $this->makeSelectQueryStatus($sql, $status, $oldestFirst);
return $this->findEntities($sql, $params, $limit);
return $this->findEntities($sql, $params, $limit, $offset);
}
public function findAll($limit, $offset, $status, $userId, $oldestFirst=false){
public function findAll($limit, $offset, $status, $oldestFirst, $userId){
$params = [$userId];
$sql = '';
if($offset !== 0){
$sql .= 'AND `items`.`id` ' . $this->getOperator($oldestFirst) . ' ? ';
$params[] = $offset;
}
$sql = $this->makeSelectQueryStatus($sql, $status, $oldestFirst);
return $this->findEntities($sql, $params, $limit);
$sql = $this->makeSelectQueryStatus('', $status, $oldestFirst);
return $this->findEntities($sql, $params, $limit, $offset);
}
@ -235,7 +224,7 @@ class ItemMapper extends Mapper implements IMapper {
public function deleteReadOlderThanThreshold($threshold){
$status = StatusFlag::STARRED | StatusFlag::UNREAD;
$sql = 'SELECT COUNT(*) - `feeds`.`articles_per_update` AS `size`, ' .
'`items`.`feed_id` AS `feed_id` ' .
'`items`.`feed_id` AS `feed_id` ' .
'FROM `*PREFIX*news_items` `items` ' .
'JOIN `*PREFIX*news_feeds` `feeds` ' .
'ON `feeds`.`id` = `items`.`feed_id` ' .
@ -282,7 +271,7 @@ class ItemMapper extends Mapper implements IMapper {
* @param string $userId the name of the user
*/
public function deleteUser($userId) {
$sql = 'DELETE FROM `*PREFIX*news_items` ' .
$sql = 'DELETE FROM `*PREFIX*news_items` ' .
'WHERE `feed_id` IN (' .
'SELECT `feeds`.`id` FROM `*PREFIX*news_feeds` `feeds` ' .
'WHERE `feeds`.`user_id` = ?' .

View File

@ -189,19 +189,23 @@ var $__build_47_app__ = function () {
return ItemResource.getAll();
};
this.toggleStar = function (itemId) {
console.log(itemId);
ItemResource.toggleStar(itemId);
};
this.markRead = function (itemId) {
console.log(itemId);
ItemResource.markItemRead(itemId);
var item = ItemResource.get(itemId);
FeedResource.markItemOfFeedRead(item.feedId);
};
this.getFeed = function (feedId) {
console.log(feedId);
return FeedResource.getById(feedId);
};
this.keepUnread = function (itemId) {
console.log(itemId);
};
this.isContentView = function () {
console.log('tbd');
this.toggleKeepUnread = function (itemId) {
var item = ItemResource.get(itemId);
if (!item.unread) {
FeedResource.markItemOfFeedUnread(item.feedId);
ItemResource.markItemRead(itemId, false);
}
item.keepUnread = !item.keepUnread;
};
this.orderBy = function () {
if (SettingsResource.get('oldestFirst')) {
@ -210,9 +214,18 @@ var $__build_47_app__ = function () {
return 'id';
}
};
this.isCompactView = function () {
return SettingsResource.get('compact');
};
this.getRelativeDate = function (timestamp) {
console.log(timestamp);
};
this.autoPage = function () {
console.log('hi');
};
this.scrollRead = function (itemIds) {
console.log(itemIds);
};
}
]);
app.controller('NavigationController', [
@ -422,6 +435,9 @@ var $__build_47_app__ = function () {
return this.values.filter(function (v) {
return v.folderId === folderId;
});
},
getById: function (feedId) {
return this.ids[$traceurRuntime.toProperty(feedId)];
}
}, {}, Resource);
return new FeedResource($http, BASE_URL);
@ -457,21 +473,9 @@ var $__build_47_app__ = function () {
BASE_URL
]);
this.starredCount = 0;
this.highestId = 0;
this.lowestId = 0;
};
var $ItemResource = ItemResource;
$traceurRuntime.createClass(ItemResource, {
add: function (obj) {
var id = obj[$traceurRuntime.toProperty(this.id)];
if (this.highestId < id) {
this.highestId = id;
}
if (this.lowestId === 0 || this.lowestId > id) {
this.lowestId = id;
}
$traceurRuntime.superCall(this, $ItemResource.prototype, 'add', [obj]);
},
receive: function (value, channel) {
switch (channel) {
case 'newestItemId':
@ -509,6 +513,13 @@ var $__build_47_app__ = function () {
data: { isStarred: isStarred }
});
},
toggleStar: function (itemId) {
if (this.get(itemId).starred) {
this.star(itemId, false);
} else {
this.star(itemId, true);
}
},
markItemRead: function (itemId) {
var isRead = arguments[1] !== void 0 ? arguments[1] : true;
this.get(itemId).unread = !isRead;
@ -547,19 +558,7 @@ var $__build_47_app__ = function () {
}
return this.http.post(this.BASE_URL + '/items/read');
},
getHighestId: function () {
return this.highestId;
},
getLowestId: function () {
return this.lowestId;
},
keepUnread: function (itemId) {
this.get(itemId).keepUnread = true;
return this.markItemRead(itemId, false);
},
clear: function () {
this.highestId = 0;
this.lowestId = 0;
$traceurRuntime.superCall(this, $ItemResource.prototype, 'clear', []);
}
}, {}, Resource);

View File

@ -20,25 +20,29 @@ function (Publisher, FeedResource, ItemResource, SettingsResource, data) {
return ItemResource.getAll();
};
// TBD
this.toggleStar = (itemId) => {
console.log(itemId);
ItemResource.toggleStar(itemId);
};
this.markRead = (itemId) => {
console.log(itemId);
ItemResource.markItemRead(itemId);
let item = ItemResource.get(itemId);
FeedResource.markItemOfFeedRead(item.feedId);
};
this.getFeed = (feedId) => {
console.log(feedId);
return FeedResource.getById(feedId);
};
this.keepUnread = (itemId) => {
console.log(itemId);
};
this.toggleKeepUnread = (itemId) => {
let item = ItemResource.get(itemId);
if (!item.unread) {
FeedResource.markItemOfFeedUnread(item.feedId);
ItemResource.markItemRead(itemId, false);
}
this.isContentView = () => {
console.log('tbd');
item.keepUnread = !item.keepUnread;
};
this.orderBy = () => {
@ -49,7 +53,21 @@ function (Publisher, FeedResource, ItemResource, SettingsResource, data) {
}
};
this.isCompactView = () => {
return SettingsResource.get('compact');
};
// TBD
this.getRelativeDate = (timestamp) => {
console.log(timestamp);
};
this.autoPage = () => {
console.log('hi');
};
this.scrollRead = (itemIds) => {
console.log(itemIds);
};
});

View File

@ -109,7 +109,9 @@ app.factory('FeedResource', (Resource, $http, BASE_URL) => {
return this.values.filter(v => v.folderId === folderId);
}
getById (feedId) {
return this.ids[feedId];
}
}
return new FeedResource($http, BASE_URL);

View File

@ -16,23 +16,6 @@ app.factory('ItemResource', (Resource, $http, BASE_URL) => {
constructor ($http, BASE_URL) {
super($http, BASE_URL);
this.starredCount = 0;
this.highestId = 0;
this.lowestId = 0;
}
add (obj) {
let id = obj[this.id];
if (this.highestId < id) {
this.highestId = id;
}
if (this.lowestId === 0 || this.lowestId > id) {
this.lowestId = id;
}
super.add(obj);
}
@ -84,6 +67,15 @@ app.factory('ItemResource', (Resource, $http, BASE_URL) => {
}
toggleStar (itemId) {
if (this.get(itemId).starred) {
this.star(itemId, false);
} else {
this.star(itemId, true);
}
}
markItemRead (itemId, isRead=true) {
this.get(itemId).unread = !isRead;
return this.http({
@ -112,25 +104,7 @@ app.factory('ItemResource', (Resource, $http, BASE_URL) => {
}
getHighestId () {
return this.highestId;
}
getLowestId () {
return this.lowestId;
}
keepUnread (itemId) {
this.get(itemId).keepUnread = true;
return this.markItemRead(itemId, false);
}
clear () {
this.highestId = 0;
this.lowestId = 0;
super.clear();
}

View File

@ -63,4 +63,126 @@ describe('ContentController', () => {
expect(ctrl.orderBy()).toBe('-id');
}));
it('should mark read', inject(($controller,
ItemResource, FeedResource, Publisher) => {
Publisher.subscribe(ItemResource).toChannels('items');
ItemResource.markItemRead = jasmine.createSpy('markRead');
FeedResource.markItemOfFeedRead = jasmine.createSpy('markRead');
let ctrl = $controller('ContentController', {
ItemResource: ItemResource,
FeedResource: FeedResource,
data: {
'items': [{
id: 3,
feedId: 4
}]
},
});
ctrl.markRead(3);
expect(ItemResource.markItemRead).toHaveBeenCalledWith(3);
expect(FeedResource.markItemOfFeedRead).toHaveBeenCalledWith(4);
}));
it('should toggle keep unread when unread', inject(($controller,
ItemResource, FeedResource, Publisher) => {
Publisher.subscribe(ItemResource).toChannels('items');
let ctrl = $controller('ContentController', {
ItemResource: ItemResource,
FeedResource: FeedResource,
data: {
'items': [{
id: 3,
feedId: 4,
unread: true
}]
},
});
ctrl.toggleKeepUnread(3);
expect(ItemResource.get(3).keepUnread).toBe(true);
}));
it('should toggle keep unread when read', inject(($controller,
ItemResource, FeedResource, Publisher) => {
Publisher.subscribe(ItemResource).toChannels('items');
ItemResource.markItemRead = jasmine.createSpy('markRead');
FeedResource.markItemOfFeedUnread = jasmine.createSpy('markRead');
let ctrl = $controller('ContentController', {
ItemResource: ItemResource,
FeedResource: FeedResource,
data: {
'items': [{
id: 3,
feedId: 4,
unread: false,
keepUnread: true
}]
},
});
ctrl.toggleKeepUnread(3);
expect(ItemResource.get(3).keepUnread).toBe(false);
expect(ItemResource.markItemRead).toHaveBeenCalledWith(3, false);
expect(FeedResource.markItemOfFeedUnread).toHaveBeenCalledWith(4);
}));
it('should get a feed', inject(($controller, FeedResource, Publisher) => {
Publisher.subscribe(FeedResource).toChannels('feeds');
let ctrl = $controller('ContentController', {
FeedResource: FeedResource,
data: {
'feeds': [{
id: 3,
url: 4
}]
},
});
expect(ctrl.getFeed(3).url).toBe(4);
}));
it('should toggle starred', inject(($controller, ItemResource) => {
ItemResource.toggleStar = jasmine.createSpy('star');
let ctrl = $controller('ContentController', {
ItemResource: ItemResource,
data: {},
});
ctrl.toggleStar(3);
expect(ItemResource.toggleStar).toHaveBeenCalledWith(3);
}));
it('should publish compactview', inject(($controller, SettingsResource) => {
SettingsResource.set('compact', true);
let ctrl = $controller('ContentController', {
SettingsResource: SettingsResource,
data: {},
});
expect(ctrl.isCompactView()).toBe(true);
}));
});

View File

@ -35,31 +35,6 @@ describe('ItemResource', () => {
}));
it ('should keep item unread', inject((ItemResource) => {
http.expectPOST('base/items/3/read', {isRead: false}).respond(200, {});
ItemResource.receive([
{
id: 3,
feedId: 4,
unread: false
},
{
id: 4,
feedId: 3,
unread: false
}
], 'items');
ItemResource.keepUnread(3);
http.flush();
expect(ItemResource.get(3).keepUnread).toBe(true);
expect(ItemResource.get(3).unread).toBe(true);
}));
it ('should mark item as read', inject((ItemResource) => {
http.expectPOST('base/items/3/read', {isRead: true}).respond(200, {});
@ -172,57 +147,25 @@ describe('ItemResource', () => {
}));
it ('should remember the highest id', inject((ItemResource) => {
it ('toggle star', inject((ItemResource) => {
ItemResource.receive([
{
id: 3,
starred: true
},
{
id: 5,
},
{
id: 4,
starred: false
}
], 'items');
expect(ItemResource.getHighestId()).toBe(5);
}));
ItemResource.star = jasmine.createSpy('star');
ItemResource.toggleStar(3);
expect(ItemResource.star).toHaveBeenCalledWith(3, false);
it ('should remember the lowest id', inject((ItemResource) => {
ItemResource.receive([
{
id: 3,
},
{
id: 5,
},
{
id: 4,
}
], 'items');
expect(ItemResource.getLowestId()).toBe(3);
}));
it ('should clear the highest and lowest id', inject((ItemResource) => {
ItemResource.receive([
{
id: 3,
},
{
id: 5,
},
{
id: 4,
}
], 'items');
ItemResource.clear();
expect(ItemResource.getHighestId()).toBe(0);
expect(ItemResource.getLowestId()).toBe(0);
ItemResource.toggleStar(5);
expect(ItemResource.star).toHaveBeenCalledWith(5, true);
}));

View File

@ -29,9 +29,9 @@ class ItemService extends Service {
private $timeFactory;
private $itemMapper;
public function __construct(ItemMapper $itemMapper,
public function __construct(ItemMapper $itemMapper,
StatusFlag $statusFlag,
$timeFactory,
$timeFactory,
Config $config){
parent::__construct($itemMapper);
$this->statusFlag = $statusFlag;
@ -76,28 +76,28 @@ class ItemService extends Service {
* @param int $id the id of the feed, 0 for starred or all items
* @param int $type the type of the feed
* @param int $limit how many items should be returned
* @param int $offset only items lower than this id are returned, 0 for no offset
* @param int $offset the offset
* @param boolean $showAll if unread items should also be returned
* @param string $userId the name of the user
* @param boolean $oldestFirst if it should be ordered by oldest first
* @param string $userId the name of the user
* @return array of items
*/
public function findAll($id, $type, $limit, $offset, $showAll, $userId,
$oldestFirst=false){
public function findAll($id, $type, $limit, $offset, $showAll, $oldestFirst,
$userId){
$status = $this->statusFlag->typeToStatus($type, $showAll);
switch($type){
case FeedType::FEED:
return $this->itemMapper->findAllFeed(
$id, $limit, $offset,$status, $userId, $oldestFirst
$id, $limit, $offset, $status, $oldestFirst, $userId
);
case FeedType::FOLDER:
return $this->itemMapper->findAllFolder(
$id, $limit, $offset, $status, $userId, $oldestFirst
$id, $limit, $offset, $status, $oldestFirst, $userId
);
default:
return $this->itemMapper->findAll(
$limit, $offset, $status, $userId, $oldestFirst
$limit, $offset, $status, $oldestFirst, $userId
);
}
}

View File

@ -90,7 +90,7 @@ class FeedApiControllerTest extends \PHPUnit_Framework_TestCase {
$response = $this->feedAPI->index();
$this->assertEquals([
'feeds' => $feeds,
'feeds' => [$feeds[0]->toAPI()],
'starredCount' => $starredCount,
'newestItemId' => $newestItemId
], $response);
@ -117,7 +117,7 @@ class FeedApiControllerTest extends \PHPUnit_Framework_TestCase {
$response = $this->feedAPI->index();
$this->assertEquals([
'feeds' => $feeds,
'feeds' => [$feeds[0]->toAPI()],
'starredCount' => $starredCount,
], $response);
}
@ -167,7 +167,7 @@ class FeedApiControllerTest extends \PHPUnit_Framework_TestCase {
$response = $this->feedAPI->create('url', 3);
$this->assertEquals([
'feeds' => $feeds,
'feeds' => [$feeds[0]->toAPI()],
'newestItemId' => 3
], $response);
}
@ -193,7 +193,7 @@ class FeedApiControllerTest extends \PHPUnit_Framework_TestCase {
$response = $this->feedAPI->create('ho', 3);
$this->assertEquals([
'feeds' => $feeds
'feeds' => [$feeds[0]->toAPI()]
], $response);
}

View File

@ -73,7 +73,9 @@ class FolderApiControllerTest extends \PHPUnit_Framework_TestCase {
$response = $this->folderAPI->index();
$this->assertEquals($folders, $response);
$this->assertEquals([
'folders' => [$folders[0]->toAPI()]
], $response);
}
@ -81,7 +83,7 @@ class FolderApiControllerTest extends \PHPUnit_Framework_TestCase {
$folderName = 'test';
$folder = new Folder();
$folder->setName($folderName);
$this->folderService->expects($this->once())
->method('purgeDeleted')
->with($this->equalTo($this->user), $this->equalTo(false));
@ -92,7 +94,9 @@ class FolderApiControllerTest extends \PHPUnit_Framework_TestCase {
$response = $this->folderAPI->create($folderName);
$this->assertEquals($folder, $response);
$this->assertEquals([
'folders' => [$folder->toAPI()]
], $response);
}

View File

@ -61,14 +61,17 @@ class ItemApiControllerTest extends \PHPUnit_Framework_TestCase {
$this->equalTo(1),
$this->equalTo(30),
$this->equalTo(20),
$this->equalTo(false),
$this->equalTo(true),
$this->equalTo(true),
$this->equalTo($this->user)
)
->will($this->returnValue($items));
$response = $this->itemAPI->index(1, 2, false, 30, 20);
$response = $this->itemAPI->index(1, 2, true, 30, 20, true);
$this->assertEquals($items, $response);
$this->assertEquals([
'items' => [$items[0]->toApi()]
], $response);
}
@ -83,13 +86,16 @@ class ItemApiControllerTest extends \PHPUnit_Framework_TestCase {
$this->equalTo(20),
$this->equalTo(0),
$this->equalTo(false),
$this->equalTo(false),
$this->equalTo($this->user)
)
->will($this->returnValue($items));
$response = $this->itemAPI->index(1, 2, false);
$this->assertEquals($items, $response);
$this->assertEquals([
'items' => [$items[0]->toApi()]
], $response);
}
@ -109,7 +115,9 @@ class ItemApiControllerTest extends \PHPUnit_Framework_TestCase {
$response = $this->itemAPI->updated(1, 2, 30);
$this->assertEquals($items, $response);
$this->assertEquals([
'items' => [$items[0]->toApi()]
], $response);
}

View File

@ -196,8 +196,8 @@ class ItemControllerTest extends \PHPUnit_Framework_TestCase {
$this->equalTo(3),
$this->equalTo(0),
$this->equalTo(true),
$this->equalTo($this->user),
$this->equalTo(false))
$this->equalTo(false),
$this->equalTo($this->user))
->will($this->returnValue($result['items']));
$response = $this->controller->index(FeedType::FEED, 2, 3);
@ -217,8 +217,8 @@ class ItemControllerTest extends \PHPUnit_Framework_TestCase {
$this->equalTo(3),
$this->equalTo(10),
$this->equalTo(true),
$this->equalTo($this->user),
$this->equalTo(true))
$this->equalTo(true),
$this->equalTo($this->user))
->will($this->returnValue($result['items']));
$this->feedService->expects($this->never())

View File

@ -79,7 +79,7 @@ class ItemMapperTest extends \OCP\AppFramework\Db\MapperTestUtility {
'ON `folders`.`id` = `feeds`.`folder_id` ' .
'WHERE `feeds`.`folder_id` = 0 ' .
'OR `folders`.`deleted_at` = 0 ' .
'ORDER BY `items`.`id` ' . $ordering;
'ORDER BY `items`.`pub_date`, `items`.`id` ' . $ordering;
}
private function makeSelectQueryStatus($prependTo, $status, $oldestFirst=false) {
@ -220,118 +220,77 @@ class ItemMapperTest extends \OCP\AppFramework\Db\MapperTestUtility {
public function testFindAllFeed(){
$sql = 'AND `items`.`feed_id` = ? ' .
'AND `items`.`id` < ? ';
$sql = 'AND `items`.`feed_id` = ? ';
$sql = $this->makeSelectQueryStatus($sql, $this->status);
$params = [$this->user, $this->id, $this->offset];
$this->setMapperResult($sql, $params, $this->rows);
$params = [$this->user, $this->id];
$this->setMapperResult($sql, $params, $this->rows, $this->limit, $this->offset);
$result = $this->mapper->findAllFeed($this->id, $this->limit,
$this->offset, $this->status, $this->user);
$this->offset, $this->status, false, $this->user);
$this->assertEquals($this->items, $result);
}
public function testFindAllFeedOldestFirst(){
$sql = 'AND `items`.`feed_id` = ? ' .
'AND `items`.`id` > ? ';
$sql = $this->makeSelectQueryStatus($sql, $this->status, true);
$params = [$this->user, $this->id, $this->offset];
$this->setMapperResult($sql, $params, $this->rows);
$result = $this->mapper->findAllFeed($this->id, $this->limit,
$this->offset, $this->status, $this->user, true);
$this->assertEquals($this->items, $result);
}
public function testFindAllFeedOffsetZero(){
$sql = 'AND `items`.`feed_id` = ? ';
$sql = $this->makeSelectQueryStatus($sql, $this->status);
$sql = $this->makeSelectQueryStatus($sql, $this->status, true);
$params = [$this->user, $this->id];
$this->setMapperResult($sql, $params, $this->rows);
$this->setMapperResult($sql, $params, $this->rows, $this->limit, $this->offset);
$result = $this->mapper->findAllFeed($this->id, $this->limit,
0, $this->status, $this->user);
$this->offset, $this->status, true, $this->user);
$this->assertEquals($this->items, $result);
}
public function testFindAllFolder(){
$sql = 'AND `feeds`.`folder_id` = ? ' .
'AND `items`.`id` < ? ';
$sql = 'AND `feeds`.`folder_id` = ? ';
$sql = $this->makeSelectQueryStatus($sql, $this->status);
$params = [$this->user, $this->id, $this->offset];
$this->setMapperResult($sql, $params, $this->rows);
$params = [$this->user, $this->id];
$this->setMapperResult($sql, $params, $this->rows, $this->limit, $this->offset);
$result = $this->mapper->findAllFolder($this->id, $this->limit,
$this->offset, $this->status, $this->user);
$this->offset, $this->status, false, $this->user);
$this->assertEquals($this->items, $result);
}
public function testFindAllFolderOldestFirst(){
$sql = 'AND `feeds`.`folder_id` = ? ' .
'AND `items`.`id` > ? ';
$sql = $this->makeSelectQueryStatus($sql, $this->status, true);
$params = [$this->user, $this->id, $this->offset];
$this->setMapperResult($sql, $params, $this->rows);
$result = $this->mapper->findAllFolder($this->id, $this->limit,
$this->offset, $this->status, $this->user, true);
$this->assertEquals($this->items, $result);
}
public function testFindAllFolderOffsetZero(){
$sql = 'AND `feeds`.`folder_id` = ? ';
$sql = $this->makeSelectQueryStatus($sql, $this->status);
$sql = $this->makeSelectQueryStatus($sql, $this->status, true);
$params = [$this->user, $this->id];
$this->setMapperResult($sql, $params, $this->rows);
$this->setMapperResult($sql, $params, $this->rows, $this->limit, $this->offset);
$result = $this->mapper->findAllFolder($this->id, $this->limit,
0, $this->status, $this->user);
$this->offset, $this->status, true, $this->user);
$this->assertEquals($this->items, $result);
}
public function testFindAll(){
$sql = 'AND `items`.`id` < ? ';
$sql = '';
$sql = $this->makeSelectQueryStatus($sql, $this->status);
$params = [$this->user, $this->offset];
$this->setMapperResult($sql, $params, $this->rows);
$params = [$this->user];
$this->setMapperResult($sql, $params, $this->rows, $this->limit, $this->offset);
$result = $this->mapper->findAll($this->limit,
$this->offset, $this->status, $this->user);
$this->offset, $this->status, false, $this->user);
$this->assertEquals($this->items, $result);
}
public function testFindAllOldestFirst(){
$sql = 'AND `items`.`id` > ? ';
$sql = '';
$sql = $this->makeSelectQueryStatus($sql, $this->status, true);
$params = [$this->user, $this->offset];
$this->setMapperResult($sql, $params, $this->rows);
$result = $this->mapper->findAll($this->limit,
$this->offset, $this->status, $this->user, true);
$this->assertEquals($this->items, $result);
}
public function testFindAllOffsetZero(){
$sql = $this->makeSelectQueryStatus('', $this->status);
$params = [$this->user];
$this->setMapperResult($sql, $params, $this->rows);
$this->setMapperResult($sql, $params, $this->rows, $this->limit, $this->offset);
$result = $this->mapper->findAll($this->limit,
0, $this->status, $this->user);
$this->offset, $this->status, true, $this->user);
$this->assertEquals($this->items, $result);
}
public function testFindByGuidHash(){
$hash = md5('test');
$feedId = 3;
@ -349,7 +308,7 @@ class ItemMapperTest extends \OCP\AppFramework\Db\MapperTestUtility {
public function testDeleteReadOlderThanThresholdDoesNotDeleteBelowThreshold(){
$status = StatusFlag::STARRED | StatusFlag::UNREAD;
$sql = 'SELECT COUNT(*) - `feeds`.`articles_per_update` AS `size`, ' .
'`items`.`feed_id` AS `feed_id` ' .
'`items`.`feed_id` AS `feed_id` ' .
'FROM `*PREFIX*news_items` `items` ' .
'JOIN `*PREFIX*news_feeds` `feeds` ' .
'ON `feeds`.`id` = `items`.`feed_id` ' .
@ -373,7 +332,7 @@ class ItemMapperTest extends \OCP\AppFramework\Db\MapperTestUtility {
$status = StatusFlag::STARRED | StatusFlag::UNREAD;
$sql1 = 'SELECT COUNT(*) - `feeds`.`articles_per_update` AS `size`, ' .
'`items`.`feed_id` AS `feed_id` ' .
'`items`.`feed_id` AS `feed_id` ' .
'FROM `*PREFIX*news_items` `items` ' .
'JOIN `*PREFIX*news_feeds` `feeds` ' .
'ON `feeds`.`id` = `items`.`feed_id` ' .
@ -430,7 +389,7 @@ class ItemMapperTest extends \OCP\AppFramework\Db\MapperTestUtility {
public function testDeleteFromUser(){
$userId = 'john';
$sql = 'DELETE FROM `*PREFIX*news_items` ' .
$sql = 'DELETE FROM `*PREFIX*news_items` ' .
'WHERE `feed_id` IN (' .
'SELECT `feeds`.`id` FROM `*PREFIX*news_feeds` `feeds` ' .
'WHERE `feeds`.`user_id` = ?' .

View File

@ -127,14 +127,14 @@ class ItemServiceTest extends \PHPUnit_Framework_TestCase {
$this->equalTo($this->limit),
$this->equalTo($this->offset),
$this->equalTo($this->status),
$this->equalTo($this->user),
$this->equalTo(false))
$this->equalTo(false),
$this->equalTo($this->user))
->will($this->returnValue($this->response));
$result = $this->itemService->findAll(
$this->id, $type, $this->limit,
$this->offset, $this->showAll,
$this->user);
$this->id, $type, $this->limit, $this->offset,
$this->showAll, false, $this->user
);
$this->assertEquals($this->response, $result);
}
@ -147,13 +147,14 @@ class ItemServiceTest extends \PHPUnit_Framework_TestCase {
$this->equalTo($this->limit),
$this->equalTo($this->offset),
$this->equalTo($this->status),
$this->equalTo(true),
$this->equalTo($this->user))
->will($this->returnValue($this->response));
$result = $this->itemService->findAll(
$this->id, $type, $this->limit,
$this->offset, $this->showAll,
$this->user);
$this->id, $type, $this->limit, $this->offset,
$this->showAll, true, $this->user
);
$this->assertEquals($this->response, $result);
}
@ -165,14 +166,14 @@ class ItemServiceTest extends \PHPUnit_Framework_TestCase {
->with( $this->equalTo($this->limit),
$this->equalTo($this->offset),
$this->equalTo($this->status),
$this->equalTo($this->user),
$this->equalTo(true))
$this->equalTo(true),
$this->equalTo($this->user))
->will($this->returnValue($this->response));
$result = $this->itemService->findAll(
$this->id, $type, $this->limit,
$this->offset, $this->showAll,
$this->user, true);
$this->id, $type, $this->limit, $this->offset,
$this->showAll, true, $this->user
);
$this->assertEquals($this->response, $result);
}