added calendar listener
This commit is contained in:
parent
af104a63f2
commit
a6d4368058
3 changed files with 97 additions and 2 deletions
|
@ -5,14 +5,18 @@
|
|||
|
||||
namespace OCA\DavPush\AppInfo;
|
||||
|
||||
use OCA\DavPush\Listener\SabrePluginAddListener;
|
||||
use OCA\DavPush\Listener\CalendarListener;
|
||||
|
||||
use OCP\AppFramework\App;
|
||||
use OCP\AppFramework\Bootstrap\IBootstrap;
|
||||
use OCP\AppFramework\Bootstrap\IRegistrationContext;
|
||||
use OCP\AppFramework\Bootstrap\IBootContext;
|
||||
|
||||
use OCA\DAV\Events\SabrePluginAddEvent;
|
||||
|
||||
use OCA\DavPush\Listener\SabrePluginAddListener;
|
||||
use OCA\DAV\Events\CalendarObjectCreatedEvent;
|
||||
use OCA\DAV\Events\CalendarObjectDeletedEvent;
|
||||
use OCA\DAV\Events\CalendarObjectUpdatedEvent;
|
||||
|
||||
class Application extends App implements IBootstrap {
|
||||
public const APP_ID = 'dav_push';
|
||||
|
@ -23,6 +27,9 @@ class Application extends App implements IBootstrap {
|
|||
|
||||
public function register(IRegistrationContext $context): void {
|
||||
$context->registerEventListener(SabrePluginAddEvent::class, SabrePluginAddListener::class);
|
||||
$context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarListener::class);
|
||||
$context->registerEventListener(CalendarObjectDeletedEvent::class, CalendarListener::class);
|
||||
$context->registerEventListener(CalendarObjectUpdatedEvent::class, CalendarListener::class);
|
||||
}
|
||||
|
||||
public function boot(IBootContext $context): void {
|
||||
|
|
|
@ -32,4 +32,21 @@ class SubscriptionMapper extends QBMapper {
|
|||
|
||||
return $this->findEntity($qb);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $userId
|
||||
* @param string $collectionName
|
||||
* @return Subscription[]
|
||||
*/
|
||||
public function findAll(string $userId, string $collectionName): array {
|
||||
/* @var $qb IQueryBuilder */
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
|
||||
$qb->select('*')
|
||||
->from(self::TABLENAME)
|
||||
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)))
|
||||
->andWhere($qb->expr()->eq('collection_name', $qb->createNamedParameter($collectionName)));
|
||||
|
||||
return $this->findEntities($qb);
|
||||
}
|
||||
}
|
71
lib/Listener/CalendarListener.php
Normal file
71
lib/Listener/CalendarListener.php
Normal file
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @copyright bitfire web engineering GmbH <info@bitfire.at>
|
||||
*
|
||||
* @author bitfire web engineering GmbH <info@bitfire.at>
|
||||
* @author Jonathan Treffler <mail@jonathan-treffler.de>
|
||||
*
|
||||
* @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\DavPush\Listener;
|
||||
|
||||
use OCP\IConfig;
|
||||
use OCP\EventDispatcher\Event;
|
||||
use OCP\EventDispatcher\IEventListener;
|
||||
|
||||
use OCA\DAV\Events\CalendarObjectCreatedEvent;
|
||||
use OCA\DAV\Events\CalendarObjectDeletedEvent;
|
||||
use OCA\DAV\Events\CalendarObjectUpdatedEvent;
|
||||
use OCA\DAV\Events\CardCreatedEvent;
|
||||
use OCA\DAV\Events\CardDeletedEvent;
|
||||
use OCA\DAV\Events\CardUpdatedEvent;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
use OCA\DavPush\Db\SubscriptionMapper;
|
||||
use OCA\DavPush\Transport\TransportManager;
|
||||
|
||||
class CalendarListener implements IEventListener {
|
||||
|
||||
public function __construct(
|
||||
private LoggerInterface $logger,
|
||||
private SubscriptionMapper $subscriptionMapper,
|
||||
private TransportManager $transportManager,
|
||||
private $userId,
|
||||
) {}
|
||||
|
||||
public function handle(Event $event): void {
|
||||
if (!($event instanceOf CalendarObjectCreatedEvent) && !($event instanceOf CalendarObjectDeletedEvent) &&
|
||||
!($event instanceOf CalendarObjectUpdatedEvent)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$collectionName = $event->getCalendarData()['uri'];
|
||||
$subscriptions = $this->subscriptionMapper->findAll($this->userId, $collectionName);
|
||||
|
||||
foreach($subscriptions as $subscription) {
|
||||
$transport = $this->transportManager->getTransport($subscription->getTransport());
|
||||
try {
|
||||
$transport->notify($this->userId, $collectionName, json_decode($subscription->getData(), True));
|
||||
} catch (Error $e) {
|
||||
$this->logger->error("transport " . $subscription->getTransport() . " failed to deliver notification to subscription " . $subscription->getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue