mirror of
https://github.com/chylex/Nextcloud-News.git
synced 2025-04-12 19:15:43 +02:00
When creating a feed or folder the it should purge the deleted
Edit fix #176
This commit is contained in:
parent
284d428111
commit
031ba0ce52
businesslayer
controller
db
external
tests/unit
@ -261,9 +261,21 @@ class FeedBusinessLayer extends BusinessLayer {
|
||||
}
|
||||
|
||||
|
||||
public function purgeDeleted($userId=null) {
|
||||
$now = $this->timeFactory->getTime();
|
||||
$deleteOlderThan = $now - $this->autoPurgeMinimumInterval;
|
||||
/**
|
||||
* Deletes all deleted feeds
|
||||
* @param string $userId if given it purges only feeds of that user
|
||||
* @param boolean $useInterval defaults to true, if true it only purges
|
||||
* entries in a given interval to give the user a chance to undo the
|
||||
* deletion
|
||||
*/
|
||||
public function purgeDeleted($userId=null, $useInterval=true) {
|
||||
$deleteOlderThan = null;
|
||||
|
||||
if ($useInterval) {
|
||||
$now = $this->timeFactory->getTime();
|
||||
$deleteOlderThan = $now - $this->autoPurgeMinimumInterval;
|
||||
}
|
||||
|
||||
$toDelete = $this->mapper->getToDelete($deleteOlderThan, $userId);
|
||||
|
||||
foreach ($toDelete as $feed) {
|
||||
|
@ -123,11 +123,20 @@ class FolderBusinessLayer extends BusinessLayer {
|
||||
|
||||
|
||||
/**
|
||||
* Purges marked as deleted folders
|
||||
* Deletes all deleted folders
|
||||
* @param string $userId if given it purges only folders of that user
|
||||
* @param boolean $useInterval defaults to true, if true it only purges
|
||||
* entries in a given interval to give the user a chance to undo the
|
||||
* deletion
|
||||
*/
|
||||
public function purgeDeleted($userId=null) {
|
||||
$now = $this->timeFactory->getTime();
|
||||
$deleteOlderThan = $now - $this->autoPurgeMinimumInterval;
|
||||
public function purgeDeleted($userId=null, $useInterval=true) {
|
||||
$deleteOlderThan = null;
|
||||
|
||||
if ($useInterval) {
|
||||
$now = $this->timeFactory->getTime();
|
||||
$deleteOlderThan = $now - $this->autoPurgeMinimumInterval;
|
||||
}
|
||||
|
||||
$toDelete = $this->mapper->getToDelete($deleteOlderThan, $userId);
|
||||
|
||||
foreach ($toDelete as $folder) {
|
||||
|
@ -135,7 +135,7 @@ class FeedController extends Controller {
|
||||
try {
|
||||
// we need to purge deleted feeds if a feed is created to
|
||||
// prevent already exists exceptions
|
||||
$this->feedBusinessLayer->purgeDeleted($userId);
|
||||
$this->feedBusinessLayer->purgeDeleted($userId, false);
|
||||
|
||||
$feed = $this->feedBusinessLayer->create($url, $parentFolderId, $userId);
|
||||
$params = array(
|
||||
|
@ -116,7 +116,7 @@ class FolderController extends Controller {
|
||||
try {
|
||||
// we need to purge deleted folders if a folder is created to
|
||||
// prevent already exists exceptions
|
||||
$this->folderBusinessLayer->purgeDeleted($userId);
|
||||
$this->folderBusinessLayer->purgeDeleted($userId, false);
|
||||
|
||||
$folder = $this->folderBusinessLayer->create($folderName, $userId);
|
||||
|
||||
|
@ -147,15 +147,26 @@ class FeedMapper extends Mapper implements IMapper {
|
||||
}
|
||||
|
||||
|
||||
public function getToDelete($deleteOlderThan, $userId=null) {
|
||||
/**
|
||||
* @param int $deleteOlderThan if given gets all entries with a delete date
|
||||
* older than that timestamp
|
||||
* @param string $userId if given returns only entries from the given user
|
||||
* @return array with the database rows
|
||||
*/
|
||||
public function getToDelete($deleteOlderThan=null, $userId=null) {
|
||||
$sql = 'SELECT * FROM `*PREFIX*news_feeds` ' .
|
||||
'WHERE `deleted_at` > 0 ' .
|
||||
'AND `deleted_at` < ?';
|
||||
$params = array($deleteOlderThan);
|
||||
'WHERE `deleted_at` > 0 ';
|
||||
$params = array();
|
||||
|
||||
// sometimes we want to delete all entries
|
||||
if ($deleteOlderThan !== null) {
|
||||
$sql .= 'AND `deleted_at` < ? ';
|
||||
array_push($params, $deleteOlderThan);
|
||||
}
|
||||
|
||||
// we need to sometimes only delete feeds of a user
|
||||
if($userId !== null) {
|
||||
$sql .= ' AND `user_id` = ?';
|
||||
$sql .= 'AND `user_id` = ?';
|
||||
array_push($params, $userId);
|
||||
}
|
||||
|
||||
|
@ -97,15 +97,26 @@ class FolderMapper extends Mapper implements IMapper {
|
||||
}
|
||||
|
||||
|
||||
public function getToDelete($deleteOlderThan, $userId=null) {
|
||||
/**
|
||||
* @param int $deleteOlderThan if given gets all entries with a delete date
|
||||
* older than that timestamp
|
||||
* @param string $userId if given returns only entries from the given user
|
||||
* @return array with the database rows
|
||||
*/
|
||||
public function getToDelete($deleteOlderThan=null, $userId=null) {
|
||||
$sql = 'SELECT * FROM `*PREFIX*news_folders` ' .
|
||||
'WHERE `deleted_at` > 0 ' .
|
||||
'AND `deleted_at` < ?';
|
||||
$params = array($deleteOlderThan);
|
||||
'WHERE `deleted_at` > 0 ';
|
||||
$params = array();
|
||||
|
||||
// sometimes we want to delete all entries
|
||||
if ($deleteOlderThan !== null) {
|
||||
$sql .= 'AND `deleted_at` < ? ';
|
||||
array_push($params, $deleteOlderThan);
|
||||
}
|
||||
|
||||
// we need to sometimes only delete feeds of a user
|
||||
if($userId !== null) {
|
||||
$sql .= ' AND `user_id` = ?';
|
||||
$sql .= 'AND `user_id` = ?';
|
||||
array_push($params, $userId);
|
||||
}
|
||||
|
||||
|
2
external/feedapi.php
vendored
2
external/feedapi.php
vendored
@ -82,7 +82,7 @@ class FeedAPI extends Controller {
|
||||
$folderId = (int) $this->params('folderId', 0);
|
||||
|
||||
try {
|
||||
$this->feedBusinessLayer->purgeDeleted($userId);
|
||||
$this->feedBusinessLayer->purgeDeleted($userId, false);
|
||||
|
||||
$feed = $this->feedBusinessLayer->create($feedUrl, $folderId, $userId);
|
||||
$result = array(
|
||||
|
2
external/folderapi.php
vendored
2
external/folderapi.php
vendored
@ -72,7 +72,7 @@ class FolderAPI extends Controller {
|
||||
);
|
||||
|
||||
try {
|
||||
$this->folderBusinessLayer->purgeDeleted($userId);
|
||||
$this->folderBusinessLayer->purgeDeleted($userId, false);
|
||||
$folder = $this->folderBusinessLayer->create($folderName, $userId);
|
||||
array_push($result['folders'], $folder->toAPI());
|
||||
|
||||
|
@ -740,4 +740,24 @@ class FeedBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility {
|
||||
}
|
||||
|
||||
|
||||
public function testPurgeDeletedWithoutInterval(){
|
||||
$feed1 = new Feed();
|
||||
$feed1->setId(3);
|
||||
$feed2 = new Feed();
|
||||
$feed2->setId(5);
|
||||
$feeds = array($feed1, $feed2);
|
||||
|
||||
$this->feedMapper->expects($this->once())
|
||||
->method('getToDelete')
|
||||
->with($this->equalTo(null), $this->equalTo($this->user))
|
||||
->will($this->returnValue($feeds));
|
||||
$this->feedMapper->expects($this->at(1))
|
||||
->method('delete')
|
||||
->with($this->equalTo($feed1));
|
||||
$this->feedMapper->expects($this->at(2))
|
||||
->method('delete')
|
||||
->with($this->equalTo($feed2));
|
||||
|
||||
$this->feedBusinessLayer->purgeDeleted($this->user, false);
|
||||
}
|
||||
}
|
||||
|
@ -232,4 +232,25 @@ class FolderBusinessLayerTest extends \OCA\AppFramework\Utility\TestUtility {
|
||||
$this->folderBusinessLayer->purgeDeleted($this->user);
|
||||
}
|
||||
|
||||
|
||||
public function testPurgeDeletedNoInterval(){
|
||||
$folder1 = new Folder();
|
||||
$folder1->setId(3);
|
||||
$folder2 = new Folder();
|
||||
$folder2->setId(5);
|
||||
$feeds = array($folder1, $folder2);
|
||||
|
||||
$this->folderMapper->expects($this->once())
|
||||
->method('getToDelete')
|
||||
->with($this->equalTo(null), $this->equalTo($this->user))
|
||||
->will($this->returnValue($feeds));
|
||||
$this->folderMapper->expects($this->at(1))
|
||||
->method('delete')
|
||||
->with($this->equalTo($folder1));
|
||||
$this->folderMapper->expects($this->at(2))
|
||||
->method('delete')
|
||||
->with($this->equalTo($folder2));
|
||||
|
||||
$this->folderBusinessLayer->purgeDeleted($this->user, false);
|
||||
}
|
||||
}
|
||||
|
@ -318,7 +318,7 @@ class FeedControllerTest extends ControllerTestUtility {
|
||||
->will($this->returnValue($result['newestItemId']));
|
||||
$this->feedBusinessLayer->expects($this->once())
|
||||
->method('purgeDeleted')
|
||||
->with($this->equalTo($this->user));
|
||||
->with($this->equalTo($this->user), $this->equalTo(false));
|
||||
$this->feedBusinessLayer->expects($this->once())
|
||||
->method('create')
|
||||
->with($this->equalTo($post['url']),
|
||||
@ -349,7 +349,7 @@ class FeedControllerTest extends ControllerTestUtility {
|
||||
->will($this->returnValue($this->user));
|
||||
$this->feedBusinessLayer->expects($this->once())
|
||||
->method('purgeDeleted')
|
||||
->with($this->equalTo($this->user));
|
||||
->with($this->equalTo($this->user), $this->equalTo(false));
|
||||
|
||||
$this->itemBusinessLayer->expects($this->once())
|
||||
->method('getNewestItemId')
|
||||
@ -377,7 +377,7 @@ class FeedControllerTest extends ControllerTestUtility {
|
||||
->will($this->returnValue($this->user));
|
||||
$this->feedBusinessLayer->expects($this->once())
|
||||
->method('purgeDeleted')
|
||||
->with($this->equalTo($this->user));
|
||||
->with($this->equalTo($this->user), $this->equalTo(false));
|
||||
$this->feedBusinessLayer->expects($this->once())
|
||||
->method('create')
|
||||
->will($this->throwException($ex));
|
||||
|
@ -240,7 +240,7 @@ class FolderControllerTest extends ControllerTestUtility {
|
||||
->will($this->returnValue($this->user));
|
||||
$this->folderBusinessLayer->expects($this->once())
|
||||
->method('purgeDeleted')
|
||||
->with($this->equalTo($this->user));
|
||||
->with($this->equalTo($this->user), $this->equalTo(false));
|
||||
$this->folderBusinessLayer->expects($this->once())
|
||||
->method('create')
|
||||
->with($this->equalTo($post['folderName']),
|
||||
@ -262,7 +262,7 @@ class FolderControllerTest extends ControllerTestUtility {
|
||||
->will($this->returnValue($this->user));
|
||||
$this->folderBusinessLayer->expects($this->once())
|
||||
->method('purgeDeleted')
|
||||
->with($this->equalTo($this->user));
|
||||
->with($this->equalTo($this->user), $this->equalTo(false));
|
||||
$this->folderBusinessLayer->expects($this->once())
|
||||
->method('create')
|
||||
->will($this->throwException($ex));
|
||||
|
@ -289,7 +289,7 @@ class FeedMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility {
|
||||
$deleteOlderThan = 110;
|
||||
$sql = 'SELECT * FROM `*PREFIX*news_feeds` ' .
|
||||
'WHERE `deleted_at` > 0 ' .
|
||||
'AND `deleted_at` < ?';
|
||||
'AND `deleted_at` < ? ';
|
||||
$this->setMapperResult($sql, array($deleteOlderThan), $rows);
|
||||
$result = $this->mapper->getToDelete($deleteOlderThan);
|
||||
|
||||
@ -314,4 +314,18 @@ class FeedMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility {
|
||||
}
|
||||
|
||||
|
||||
public function testGetAllPurgeDeletedFromUser(){
|
||||
$rows = array(
|
||||
array('id' => $this->feeds[0]->getId()),
|
||||
array('id' => $this->feeds[1]->getId())
|
||||
);
|
||||
$deleteOlderThan = 110;
|
||||
$sql = 'SELECT * FROM `*PREFIX*news_feeds` ' .
|
||||
'WHERE `deleted_at` > 0 ' .
|
||||
'AND `user_id` = ?';
|
||||
$this->setMapperResult($sql, array($this->user), $rows);
|
||||
$result = $this->mapper->getToDelete(null, $this->user);
|
||||
|
||||
$this->assertEquals($this->feeds, $result);
|
||||
}
|
||||
}
|
||||
|
@ -187,7 +187,7 @@ class FolderMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility {
|
||||
$deleteOlderThan = 110;
|
||||
$sql = 'SELECT * FROM `*PREFIX*news_folders` ' .
|
||||
'WHERE `deleted_at` > 0 ' .
|
||||
'AND `deleted_at` < ?';
|
||||
'AND `deleted_at` < ? ';
|
||||
$this->setMapperResult($sql, array($deleteOlderThan), $rows);
|
||||
$result = $this->folderMapper->getToDelete($deleteOlderThan);
|
||||
|
||||
@ -213,4 +213,19 @@ class FolderMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility {
|
||||
}
|
||||
|
||||
|
||||
public function testGetAllPurgeDeletedUser(){
|
||||
$rows = array(
|
||||
array('id' => $this->folders[0]->getId()),
|
||||
array('id' => $this->folders[1]->getId())
|
||||
);
|
||||
$deleteOlderThan = 110;
|
||||
$sql = 'SELECT * FROM `*PREFIX*news_folders` ' .
|
||||
'WHERE `deleted_at` > 0 ' .
|
||||
'AND `user_id` = ?';
|
||||
$this->setMapperResult($sql, array($this->user), $rows);
|
||||
$result = $this->folderMapper->getToDelete(null, $this->user);
|
||||
|
||||
$this->assertEquals($this->folders, $result);
|
||||
}
|
||||
|
||||
}
|
6
tests/unit/external/FeedAPITest.php
vendored
6
tests/unit/external/FeedAPITest.php
vendored
@ -211,7 +211,7 @@ class FeedAPITest extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
$this->feedBusinessLayer->expects($this->once())
|
||||
->method('purgeDeleted')
|
||||
->with($this->equalTo($this->user));
|
||||
->with($this->equalTo($this->user), $this->equalTo(false));
|
||||
$this->feedBusinessLayer->expects($this->once())
|
||||
->method('create')
|
||||
->with(
|
||||
@ -253,7 +253,7 @@ class FeedAPITest extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
$this->feedBusinessLayer->expects($this->once())
|
||||
->method('purgeDeleted')
|
||||
->with($this->equalTo($this->user));
|
||||
->with($this->equalTo($this->user), $this->equalTo(false));
|
||||
$this->feedBusinessLayer->expects($this->once())
|
||||
->method('create')
|
||||
->with(
|
||||
@ -280,7 +280,7 @@ class FeedAPITest extends \PHPUnit_Framework_TestCase {
|
||||
public function testCreateExists() {
|
||||
$this->feedBusinessLayer->expects($this->once())
|
||||
->method('purgeDeleted')
|
||||
->with($this->equalTo($this->user));
|
||||
->with($this->equalTo($this->user), $this->equalTo(false));
|
||||
$this->feedBusinessLayer->expects($this->once())
|
||||
->method('create')
|
||||
->will($this->throwException(new BusinessLayerExistsException($this->msg)));
|
||||
|
4
tests/unit/external/FolderAPITest.php
vendored
4
tests/unit/external/FolderAPITest.php
vendored
@ -114,7 +114,7 @@ class FolderAPITest extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
$this->folderBusinessLayer->expects($this->once())
|
||||
->method('purgeDeleted')
|
||||
->with($this->equalTo($this->user));
|
||||
->with($this->equalTo($this->user), $this->equalTo(false));
|
||||
$this->folderBusinessLayer->expects($this->once())
|
||||
->method('create')
|
||||
->with($this->equalTo($folderName), $this->equalTo($this->user))
|
||||
@ -132,7 +132,7 @@ class FolderAPITest extends \PHPUnit_Framework_TestCase {
|
||||
$msg = 'exists';
|
||||
$this->folderBusinessLayer->expects($this->once())
|
||||
->method('purgeDeleted')
|
||||
->with($this->equalTo($this->user));
|
||||
->with($this->equalTo($this->user), $this->equalTo(false));
|
||||
$this->folderBusinessLayer->expects($this->once())
|
||||
->method('create')
|
||||
->will($this->throwException(new BusinessLayerExistsException($msg)));
|
||||
|
Loading…
Reference in New Issue
Block a user