-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
Signed-off-by: Richard Steinmetz <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Calendar\Listener; | ||
|
||
use OCA\DAV\Events\CalendarObjectCreatedEvent; | ||
use OCA\DAV\Events\CalendarObjectDeletedEvent; | ||
use OCA\DAV\Events\CalendarObjectUpdatedEvent; | ||
use OCA\NotifyPush\Queue\IQueue; | ||
use OCP\EventDispatcher\Event; | ||
use OCP\EventDispatcher\IEventListener; | ||
use OCP\IURLGenerator; | ||
use OCP\IUserSession; | ||
|
||
/** | ||
* @template-implements IEventListener<Event|> | ||
*/ | ||
class NotifyPushListener implements IEventListener { | ||
public function __construct( | ||
private readonly IUserSession $userSession, | ||
private readonly IURLGenerator $urlGenerator, | ||
private readonly ?IQueue $queue, | ||
Check failure on line 28 in lib/Listener/NotifyPushListener.php GitHub Actions / static-psalm-analysis dev-masterUndefinedClass
Check failure on line 28 in lib/Listener/NotifyPushListener.php GitHub Actions / static-psalm-analysis dev-stable30UndefinedClass
|
||
) { | ||
} | ||
|
||
/** | ||
* @param CalendarObjectCreatedEvent|CalendarObjectUpdatedEvent|CalendarObjectDeletedEvent> $event | ||
*/ | ||
public function handle(Event $event): void { | ||
Check failure on line 35 in lib/Listener/NotifyPushListener.php GitHub Actions / static-psalm-analysis dev-masterInvalidDocblock
Check failure on line 35 in lib/Listener/NotifyPushListener.php GitHub Actions / static-psalm-analysis dev-stable30InvalidDocblock
|
||
if ($this->queue === null) { | ||
return; | ||
} | ||
|
||
$user = $this->userSession->getUser(); | ||
if ($user === null) { | ||
return; | ||
} | ||
|
||
// TODO: How to generate this in a more safe way? | ||
$webroot = $this->urlGenerator->getWebroot(); | ||
$uid = $user->getUID(); | ||
$uri = $event->getCalendarData()['uri']; | ||
Check failure on line 48 in lib/Listener/NotifyPushListener.php GitHub Actions / static-psalm-analysis dev-masterUndefinedMethod
Check failure on line 48 in lib/Listener/NotifyPushListener.php GitHub Actions / static-psalm-analysis dev-stable30UndefinedMethod
|
||
$this->queue->push('notify_custom', [ | ||
Check failure on line 49 in lib/Listener/NotifyPushListener.php GitHub Actions / static-psalm-analysis dev-masterUndefinedClass
Check failure on line 49 in lib/Listener/NotifyPushListener.php GitHub Actions / static-psalm-analysis dev-stable30UndefinedClass
|
||
'user' => $user->getUID(), | ||
'message' => 'calendar_sync', | ||
'body' => [ | ||
'calendarUrl' => "$webroot/remote.php/dav/calendars/$uid/$uri/", | ||
], | ||
]); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
import { listen } from '@nextcloud/notify_push' | ||
import { loadState } from '@nextcloud/initial-state' | ||
import useCalendarsStore from '../store/calendars.js' | ||
import logger from '../utils/logger.js' | ||
|
||
/** | ||
* Register a notify_push listener to listen for sync requests and sync calendars. | ||
*/ | ||
export function registerNotifyPushSyncListener() { | ||
// TODO: how to actually get the state | ||
/* | ||
const isPushEnabled = loadState('calendar', 'notify_push_available', false) | ||
if (!isPushEnabled) { | ||
return | ||
} | ||
*/ | ||
|
||
const calendarsStore = useCalendarsStore() | ||
listen('calendar_sync', (messageType, messageBody) => { | ||
logger.debug('calendar_sync', { | ||
messageType, | ||
messageBody, | ||
}) | ||
const { calendarUrl } = messageBody | ||
const calendar = calendarsStore.getCalendarByUrl(calendarUrl) | ||
if (!calendar) { | ||
logger.warn(`Requested push sync for unknown calendar: ${calendarUrl}`, { | ||
messageType, | ||
messageBody, | ||
}) | ||
return | ||
} | ||
|
||
logger.debug(`Syncing calendar ${calendarUrl} (requested by notify_push)`) | ||
calendarsStore.syncCalendar({ calendar }) | ||
}) | ||
} |