generate birthday calendars in a background job after admin enabled them

Signed-off-by: Georg Ehrke <developer@georgehrke.com>
pull/6884/head
Georg Ehrke 7 years ago
parent a87d986041
commit 2b51d84b98
No known key found for this signature in database
GPG Key ID: 9D98FD9380A1CB43

@ -11,6 +11,7 @@ return array(
'OCA\\DAV\\Avatars\\AvatarHome' => $baseDir . '/../lib/Avatars/AvatarHome.php',
'OCA\\DAV\\Avatars\\AvatarNode' => $baseDir . '/../lib/Avatars/AvatarNode.php',
'OCA\\DAV\\Avatars\\RootCollection' => $baseDir . '/../lib/Avatars/RootCollection.php',
'OCA\\DAV\\BackgroundJob\\GenerateBirthdayCalendarBackgroundJob' => $baseDir . '/../lib/BackgroundJob/GenerateBirthdayCalendarBackgroundJob.php',
'OCA\\DAV\\CalDAV\\Activity\\Backend' => $baseDir . '/../lib/CalDAV/Activity/Backend.php',
'OCA\\DAV\\CalDAV\\Activity\\Filter\\Calendar' => $baseDir . '/../lib/CalDAV/Activity/Filter/Calendar.php',
'OCA\\DAV\\CalDAV\\Activity\\Filter\\Todo' => $baseDir . '/../lib/CalDAV/Activity/Filter/Todo.php',

@ -26,6 +26,7 @@ class ComposerStaticInitDAV
'OCA\\DAV\\Avatars\\AvatarHome' => __DIR__ . '/..' . '/../lib/Avatars/AvatarHome.php',
'OCA\\DAV\\Avatars\\AvatarNode' => __DIR__ . '/..' . '/../lib/Avatars/AvatarNode.php',
'OCA\\DAV\\Avatars\\RootCollection' => __DIR__ . '/..' . '/../lib/Avatars/RootCollection.php',
'OCA\\DAV\\BackgroundJob\\GenerateBirthdayCalendarBackgroundJob' => __DIR__ . '/..' . '/../lib/BackgroundJob/GenerateBirthdayCalendarBackgroundJob.php',
'OCA\\DAV\\CalDAV\\Activity\\Backend' => __DIR__ . '/..' . '/../lib/CalDAV/Activity/Backend.php',
'OCA\\DAV\\CalDAV\\Activity\\Filter\\Calendar' => __DIR__ . '/..' . '/../lib/CalDAV/Activity/Filter/Calendar.php',
'OCA\\DAV\\CalDAV\\Activity\\Filter\\Todo' => __DIR__ . '/..' . '/../lib/CalDAV/Activity/Filter/Todo.php',

@ -0,0 +1,69 @@
<?php
/**
* @copyright 2017 Georg Ehrke <oc.list@georgehrke.com>
*
* @author Georg Ehrke <oc.list@georgehrke.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\DAV\BackgroundJob;
use OC\BackgroundJob\QueuedJob;
use OCA\DAV\CalDAV\BirthdayService;
use OCP\IConfig;
class GenerateBirthdayCalendarBackgroundJob extends QueuedJob {
/** @var BirthdayService */
private $birthdayService;
/** @var IConfig */
private $config;
/**
* GenerateAllBirthdayCalendarsBackgroundJob constructor.
*
* @param BirthdayService $birthdayService
* @param IConfig $config
*/
public function __construct(BirthdayService $birthdayService,
IConfig $config) {
$this->birthdayService = $birthdayService;
$this->config = $config;
}
/**
* @param array $arguments
*/
public function run($arguments) {
$userId = $arguments['userId'];
// make sure admin didn't change his mind
$isGloballyEnabled = $this->config->getAppValue('dav', 'generateBirthdayCalendar', 'yes');
if ($isGloballyEnabled !== 'yes') {
return;
}
// did the user opt out?
$isUserEnabled = $this->config->getUserValue($userId, 'dav', 'generateBirthdayCalendar', 'yes');
if ($isUserEnabled !== 'yes') {
return;
}
$this->birthdayService->syncUser($userId);
}
}

@ -2076,6 +2076,22 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
}
}
/**
* deletes all birthday calendars
*/
public function deleteAllBirthdayCalendars() {
$query = $this->db->getQueryBuilder();
$result = $query->select(['id'])->from('calendars')
->where($query->expr()->eq('uri',
$query->createNamedParameter(BirthdayService::BIRTHDAY_CALENDAR_URI)))
->execute();
$ids = $result->fetchAll();
foreach($ids as $id) {
$this->deleteCalendar($id['id']);
}
}
/**
* read VCalendar data into a VCalendar object
*

@ -23,12 +23,17 @@
namespace OCA\DAV\Controller;
use OCA\DAV\BackgroundJob\GenerateBirthdayCalendarBackgroundJob;
use OCA\DAV\CalDAV\CalDavBackend;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\Response;
use OCP\BackgroundJob\IJobList;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserManager;
class BirthdayCalendarController extends Controller {
@ -42,6 +47,21 @@ class BirthdayCalendarController extends Controller {
*/
protected $config;
/**
* @var IUserManager
*/
protected $userManager;
/**
* @var CalDavBackend
*/
protected $caldavBackend;
/**
* @var IJobList
*/
protected $jobList;
/**
* BirthdayCalendar constructor.
*
@ -49,12 +69,21 @@ class BirthdayCalendarController extends Controller {
* @param IRequest $request
* @param IDBConnection $db
* @param IConfig $config
* @param IJobList $jobList
* @param IUserManager $userManager
* @param CalDavBackend $calDavBackend
*/
public function __construct($appName, IRequest $request,
IDBConnection $db, IConfig $config){
IDBConnection $db, IConfig $config,
IJobList $jobList,
IUserManager $userManager,
CalDavBackend $calDavBackend){
parent::__construct($appName, $request);
$this->db = $db;
$this->config = $config;
$this->userManager = $userManager;
$this->jobList = $jobList;
$this->caldavBackend = $calDavBackend;
}
/**
@ -63,7 +92,12 @@ class BirthdayCalendarController extends Controller {
public function enable() {
$this->config->setAppValue($this->appName, 'generateBirthdayCalendar', 'yes');
// TODO schedule background job to regenerate
// add background job for each user
$this->userManager->callForAllUsers(function(IUser $user) {
$this->jobList->add(GenerateBirthdayCalendarBackgroundJob::class, [
'userId' => $user->getUID(),
]);
});
return new JSONResponse([]);
}
@ -74,7 +108,8 @@ class BirthdayCalendarController extends Controller {
public function disable() {
$this->config->setAppValue($this->appName, 'generateBirthdayCalendar', 'no');
// TODO delete all birthday calendars
$this->jobList->remove(GenerateBirthdayCalendarBackgroundJob::class);
$this->caldavBackend->deleteAllBirthdayCalendars();
return new JSONResponse([]);
}

@ -0,0 +1,103 @@
<?php
/**
* @copyright Copyright (c) 2017, Georg Ehrke
*
* @author Georg Ehrke <oc.list@georgehrke.com>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\DAV\Tests\unit\BackgroundJob;
use OCA\DAV\BackgroundJob\GenerateBirthdayCalendarBackgroundJob;
use OCA\DAV\CalDAV\BirthdayService;
use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\CalDAV\CalendarHome;
use OCP\IConfig;
use Sabre\DAV\MkCol;
use Test\TestCase;
class GenerateBirthdayCalendarBackgroundJobTest extends TestCase {
/** @var BirthdayService | \PHPUnit_Framework_MockObject_MockObject */
private $birthdayService;
/** @var IConfig | \PHPUnit_Framework_MockObject_MockObject */
private $config;
/** @var \OCA\DAV\BackgroundJob\GenerateBirthdayCalendarBackgroundJob */
private $backgroundJob;
protected function setUp() {
parent::setUp();
$this->birthdayService = $this->createMock(BirthdayService::class);
$this->config = $this->createMock(IConfig::class);
$this->backgroundJob = new GenerateBirthdayCalendarBackgroundJob(
$this->birthdayService, $this->config);
}
public function testRun() {
$this->config->expects($this->once())
->method('getAppValue')
->with('dav', 'generateBirthdayCalendar', 'yes')
->will($this->returnValue('yes'));
$this->config->expects($this->once())
->method('getUserValue')
->with('user123', 'dav', 'generateBirthdayCalendar', 'yes')
->will($this->returnValue('yes'));
$this->birthdayService->expects($this->once())
->method('syncUser')
->with('user123');
$this->backgroundJob->run(['userId' => 'user123']);
}
public function testRunGloballyDisabled() {
$this->config->expects($this->once())
->method('getAppValue')
->with('dav', 'generateBirthdayCalendar', 'yes')
->will($this->returnValue('no'));
$this->config->expects($this->never())
->method('getUserValue');
$this->birthdayService->expects($this->never())
->method('syncUser');
$this->backgroundJob->run(['userId' => 'user123']);
}
public function testRunUserDisabled() {
$this->config->expects($this->once())
->method('getAppValue')
->with('dav', 'generateBirthdayCalendar', 'yes')
->will($this->returnValue('yes'));
$this->config->expects($this->once())
->method('getUserValue')
->with('user123', 'dav', 'generateBirthdayCalendar', 'yes')
->will($this->returnValue('no'));
$this->birthdayService->expects($this->never())
->method('syncUser');
$this->backgroundJob->run(['userId' => 'user123']);
}
}

@ -23,10 +23,15 @@
namespace OCA\DAV\Tests\Unit\DAV\Controller;
use OCA\DAV\BackgroundJob\GenerateBirthdayCalendarBackgroundJob;
use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\Controller\BirthdayCalendarController;
use OCP\BackgroundJob\IJobList;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserManager;
use Test\TestCase;
class BirthdayCalendarControllerTest extends TestCase {
@ -40,6 +45,15 @@ class BirthdayCalendarControllerTest extends TestCase {
/** @var IDBConnection|\PHPUnit_Framework_MockObject_MockObject */
private $db;
/** @var IJobList|\PHPUnit_Framework_MockObject_MockObject */
private $jobList;
/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
private $userManager;
/** @var CalDavBackend|\PHPUnit_Framework_MockObject_MockObject */
private $caldav;
/** @var BirthdayCalendarController|\PHPUnit_Framework_MockObject_MockObject */
private $controller;
@ -49,9 +63,13 @@ class BirthdayCalendarControllerTest extends TestCase {
$this->config = $this->createMock(IConfig::class);
$this->request = $this->createMock(IRequest::class);
$this->db = $this->createMock(IDBConnection::class);
$this->jobList = $this->createMock(IJobList::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->caldav = $this->createMock(CalDavBackend::class);
$this->controller = new BirthdayCalendarController('dav',
$this->request, $this->db, $this->config);
$this->request, $this->db, $this->config, $this->jobList,
$this->userManager, $this->caldav);
}
public function testEnable() {
@ -59,6 +77,31 @@ class BirthdayCalendarControllerTest extends TestCase {
->method('setAppValue')
->with('dav', 'generateBirthdayCalendar', 'yes');
$this->userManager->expects($this->once())
->method('callForAllUsers')
->will($this->returnCallback(function($closure) {
$user1 = $this->createMock(IUser::class);
$user1->method('getUID')->will($this->returnValue('uid1'));
$user2 = $this->createMock(IUser::class);
$user2->method('getUID')->will($this->returnValue('uid2'));
$user3 = $this->createMock(IUser::class);
$user3->method('getUID')->will($this->returnValue('uid3'));
$closure($user1);
$closure($user2);
$closure($user3);
}));
$this->jobList->expects($this->at(0))
->method('add')
->with(GenerateBirthdayCalendarBackgroundJob::class, ['userId' => 'uid1']);
$this->jobList->expects($this->at(1))
->method('add')
->with(GenerateBirthdayCalendarBackgroundJob::class, ['userId' => 'uid2']);
$this->jobList->expects($this->at(2))
->method('add')
->with(GenerateBirthdayCalendarBackgroundJob::class, ['userId' => 'uid3']);
$response = $this->controller->enable();
$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $response);
}
@ -67,6 +110,11 @@ class BirthdayCalendarControllerTest extends TestCase {
$this->config->expects($this->once())
->method('setAppValue')
->with('dav', 'generateBirthdayCalendar', 'no');
$this->jobList->expects($this->once())
->method('remove')
->with(GenerateBirthdayCalendarBackgroundJob::class);
$this->caldav->expects($this->once())
->method('deleteAllBirthdayCalendars');
$response = $this->controller->disable();
$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $response);

Loading…
Cancel
Save