зеркало из https://github.com/nextcloud/news.git
autopurge all read items which are not starred if there are more than 1000
This commit is contained in:
Родитель
312796b1ca
Коммит
4b994c97ad
|
@ -35,6 +35,7 @@ class Task {
|
|||
static public function run() {
|
||||
$container = new DIContainer();
|
||||
$container['FeedBl']->updateAll();
|
||||
$container['FeedBl']->autoPurgeOld();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -39,13 +39,16 @@ class FeedBl extends Bl {
|
|||
private $feedFetcher;
|
||||
private $itemMapper;
|
||||
private $api;
|
||||
private $autoPurgeCount;
|
||||
|
||||
public function __construct(FeedMapper $feedMapper, Fetcher $feedFetcher,
|
||||
ItemMapper $itemMapper, API $api){
|
||||
ItemMapper $itemMapper, API $api,
|
||||
$autoPurgeCount=0){
|
||||
parent::__construct($feedMapper);
|
||||
$this->feedFetcher = $feedFetcher;
|
||||
$this->itemMapper = $itemMapper;
|
||||
$this->api = $api;
|
||||
$this->autoPurgeCount = $autoPurgeCount;
|
||||
}
|
||||
|
||||
|
||||
|
@ -154,5 +157,24 @@ class FeedBl extends Bl {
|
|||
$this->mapper->update($feed);
|
||||
}
|
||||
|
||||
// TODO: delete associated items
|
||||
|
||||
/**
|
||||
* This method deletes all unread feeds that are not starred and over the
|
||||
* count of $this->autoPurgeCount starting by the oldest. This is to clean
|
||||
* up the database so that old entries dont spam your db. As criteria for
|
||||
* old, the id is taken
|
||||
*/
|
||||
public function autoPurgeOld(){
|
||||
$readAndNotStarred =
|
||||
$this->mapper->getReadOlderThanThreshold($this->autoPurgeCount);
|
||||
|
||||
// delete entries with a lower id than last item
|
||||
if($this->autoPurgeCount > 0
|
||||
&& isset($readAndNotStarred[$this->autoPurgeCount-1])){
|
||||
$this->mapper->deleteReadOlderThanId(
|
||||
$readAndNotStarred[$this->autoPurgeCount-1]->getId());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -81,6 +81,6 @@ class FolderBl extends Bl {
|
|||
$this->mapper->update($folder);
|
||||
}
|
||||
|
||||
// TODO: delete associated items
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -57,8 +57,8 @@ class FeedMapper extends Mapper implements IMapper {
|
|||
}
|
||||
|
||||
|
||||
private function findAllRows($sql, $params=array()){
|
||||
$result = $this->execute($sql, $params);
|
||||
private function findAllRows($sql, $params=array(), $limit=null){
|
||||
$result = $this->execute($sql, $params, $limit);
|
||||
|
||||
$feeds = array();
|
||||
while($row = $result->fetchRow()){
|
||||
|
@ -121,4 +121,24 @@ class FeedMapper extends Mapper implements IMapper {
|
|||
$this->execute($sql, $params);
|
||||
}
|
||||
|
||||
|
||||
public function getReadOlderThanThreshold($threshold){
|
||||
|
||||
// we want items that are not starred and not unread
|
||||
$status = StatusFlag::STARRED | StatusFlag::UNREAD;
|
||||
$sql = 'SELECT * FROM `*PREFIX*news_items` ' .
|
||||
'WHERE NOT ((`status` & ?) > 0)';
|
||||
|
||||
$params = array($status);
|
||||
return $this->findAllRows($sql, $params, $threshold);
|
||||
}
|
||||
|
||||
|
||||
public function deleteReadOlderThanId($id){
|
||||
$sql = 'DELETE FROM `*PREFIX*news_items` WHERE `id` < ?';
|
||||
$params = array($id);
|
||||
$this->execute($sql, $params);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -61,6 +61,11 @@ class DIContainer extends BaseContainer {
|
|||
// tell parent container about the app name
|
||||
parent::__construct('news');
|
||||
|
||||
/**
|
||||
* Configuration values
|
||||
*/
|
||||
$this['autoPurgeCount'] = 1000;
|
||||
|
||||
|
||||
/**
|
||||
* CONTROLLERS
|
||||
|
@ -101,8 +106,9 @@ class DIContainer extends BaseContainer {
|
|||
});
|
||||
|
||||
$this['FeedBl'] = $this->share(function($c){
|
||||
return new FeedBl($c['FeedMapper'], $c['Fetcher'],
|
||||
$c['ItemMapper'], $c['API']);
|
||||
return new FeedBl($c['FeedMapper'], $c['Fetcher'],
|
||||
$c['ItemMapper'], $c['API'],
|
||||
$c['autoPurgeCount']);
|
||||
});
|
||||
|
||||
$this['ItemBl'] = $this->share(function($c){
|
||||
|
|
|
@ -42,7 +42,7 @@ module.exports = (grunt) ->
|
|||
'<%= meta.pkg.author.name %> <<%= meta.pkg.author.email %>>\n' +
|
||||
' *\n' +
|
||||
' * This file is licensed under the Affero General Public License version 3 or later.\n' +
|
||||
' * See the COPYING-README file\n' +
|
||||
' * See the COPYING file\n' +
|
||||
' *\n' +
|
||||
' */\n\n'
|
||||
build: 'build/'
|
||||
|
|
|
@ -41,15 +41,16 @@ use \OCA\News\Utility\FetcherException;
|
|||
|
||||
class FeedBlTest extends \OCA\AppFramework\Utility\TestUtility {
|
||||
|
||||
protected $api;
|
||||
protected $mapper;
|
||||
protected $bl;
|
||||
protected $user;
|
||||
protected $response;
|
||||
protected $fetcher;
|
||||
protected $itemMapper;
|
||||
private $mapper;
|
||||
private $bl;
|
||||
private $user;
|
||||
private $response;
|
||||
private $fetcher;
|
||||
private $itemMapper;
|
||||
private $threshold;
|
||||
|
||||
protected function setUp(){
|
||||
$this->threshold = 2;
|
||||
$this->api = $this->getAPIMock();
|
||||
$this->mapper = $this->getMockBuilder('\OCA\News\Db\FeedMapper')
|
||||
->disableOriginalConstructor()
|
||||
|
@ -61,9 +62,11 @@ class FeedBlTest extends \OCA\AppFramework\Utility\TestUtility {
|
|||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$this->bl = new FeedBl($this->mapper,
|
||||
$this->fetcher, $this->itemMapper, $this->api);
|
||||
$this->fetcher, $this->itemMapper, $this->api,
|
||||
$this->threshold);
|
||||
$this->user = 'jack';
|
||||
$response = 'hi';
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -302,6 +305,24 @@ class FeedBlTest extends \OCA\AppFramework\Utility\TestUtility {
|
|||
}
|
||||
|
||||
|
||||
public function testAutoPurgeOldWillPurgeOld(){
|
||||
$feed = new Feed();
|
||||
$feed->setId(3);
|
||||
$unread = array(
|
||||
new Feed(), $feed
|
||||
);
|
||||
$this->mapper->expects($this->once())
|
||||
->method('getReadOlderThanThreshold')
|
||||
->with($this->equalTo($this->threshold))
|
||||
->will($this->returnValue($unread));
|
||||
$this->mapper->expects($this->once())
|
||||
->method('deleteReadOlderThanId')
|
||||
->with($this->equalTo($feed->getId()));
|
||||
|
||||
$result = $this->bl->autoPurgeOld();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -249,4 +249,30 @@ class FeedMapperTest extends \OCA\AppFramework\Utility\MapperTestUtility {
|
|||
}
|
||||
|
||||
|
||||
public function testGetReadOlderThanThreshold(){
|
||||
$status = StatusFlag::STARRED | StatusFlag::UNREAD;
|
||||
$sql = 'SELECT * FROM `*PREFIX*news_items` ' .
|
||||
'WHERE NOT ((`status` & ?) > 0)';
|
||||
$threshold = 10;
|
||||
$feed = new Feed();
|
||||
$feed->setId(30);
|
||||
$rows = array(array('id' => 30));
|
||||
$params = array($status);
|
||||
|
||||
$this->setMapperResult($sql, $params, $rows);
|
||||
$result = $this->mapper->getReadOlderThanThreshold($threshold);
|
||||
|
||||
$this->assertEquals($feed->getId(), $result[0]->getId());
|
||||
}
|
||||
|
||||
|
||||
public function testDeleteReadOlderThanId(){
|
||||
$id = 10;
|
||||
$sql = 'DELETE FROM `*PREFIX*news_items` WHERE `id` < ?';
|
||||
$params = array($id);
|
||||
|
||||
$this->setMapperResult($sql, $params);
|
||||
$this->mapper->deleteReadOlderThanId($id);
|
||||
}
|
||||
|
||||
}
|
Загрузка…
Ссылка в новой задаче