Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
['name' => 'contact#getContactGroupMembers', 'url' => '/v1/autocompletion/groupmembers', 'verb' => 'POST'],
// Settings
['name' => 'settings#setConfig', 'url' => '/v1/config/{key}', 'verb' => 'POST'],
// Share alarm suppression
['name' => 'shareAlarm#get', 'url' => '/v1/share-alarm', 'verb' => 'GET'],
['name' => 'shareAlarm#toggle', 'url' => '/v1/share-alarm', 'verb' => 'POST'],
// Tools
['name' => 'email#sendEmailPublicLink', 'url' => '/v1/public/sendmail', 'verb' => 'POST'],
],
Expand Down
4 changes: 4 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
use OCA\Calendar\Listener\AppointmentBookedListener;
use OCA\Calendar\Listener\CalendarReferenceListener;
use OCA\Calendar\Listener\NotifyPushListener;
use OCA\Calendar\Listener\SabrePluginAddListener;
use OCA\Calendar\Listener\UserDeletedListener;
use OCA\Calendar\Notification\Notifier;
use OCA\Calendar\Profile\AppointmentsAction;
use OCA\Calendar\Reference\ReferenceProvider;
use OCA\Calendar\UserMigration\Migrator;
use OCA\DAV\Events\SabrePluginAddEvent;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
Expand Down Expand Up @@ -63,6 +65,8 @@
$context->registerEventListener(CalendarObjectUpdatedEvent::class, NotifyPushListener::class);
$context->registerEventListener(CalendarObjectDeletedEvent::class, NotifyPushListener::class);

$context->registerEventListener(SabrePluginAddEvent::class, SabrePluginAddListener::class);

Check failure on line 68 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable32

InvalidArgument

lib/AppInfo/Application.php:68:63: InvalidArgument: Argument 2 of OCP\AppFramework\Bootstrap\IRegistrationContext::registerEventListener expects class-string<OCP\EventDispatcher\IEventListener<OCP\EventDispatcher\Event>>, but OCA\Calendar\Listener\SabrePluginAddListener::class provided (see https://psalm.dev/004)

Check failure on line 68 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable32

UndefinedClass

lib/AppInfo/Application.php:68:35: UndefinedClass: Class, interface or enum named OCA\DAV\Events\SabrePluginAddEvent does not exist (see https://psalm.dev/019)

Check failure on line 68 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

InvalidArgument

lib/AppInfo/Application.php:68:63: InvalidArgument: Argument 2 of OCP\AppFramework\Bootstrap\IRegistrationContext::registerEventListener expects class-string<OCP\EventDispatcher\IEventListener<OCP\EventDispatcher\Event>>, but OCA\Calendar\Listener\SabrePluginAddListener::class provided (see https://psalm.dev/004)

Check failure on line 68 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

UndefinedClass

lib/AppInfo/Application.php:68:35: UndefinedClass: Class, interface or enum named OCA\DAV\Events\SabrePluginAddEvent does not exist (see https://psalm.dev/019)

Check failure on line 68 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable33

InvalidArgument

lib/AppInfo/Application.php:68:63: InvalidArgument: Argument 2 of OCP\AppFramework\Bootstrap\IRegistrationContext::registerEventListener expects class-string<OCP\EventDispatcher\IEventListener<OCP\EventDispatcher\Event>>, but OCA\Calendar\Listener\SabrePluginAddListener::class provided (see https://psalm.dev/004)

Check failure on line 68 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable33

UndefinedClass

lib/AppInfo/Application.php:68:35: UndefinedClass: Class, interface or enum named OCA\DAV\Events\SabrePluginAddEvent does not exist (see https://psalm.dev/019)

$context->registerNotifierService(Notifier::class);

$context->registerUserMigrator(Migrator::class);
Expand Down
145 changes: 145 additions & 0 deletions lib/Controller/ShareAlarmController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Calendar\Controller;

use OCA\Calendar\AppInfo\Application;
use OCA\Calendar\Db\ShareAlarmSetting;
use OCA\Calendar\Db\ShareAlarmSettingMapper;
use OCA\Calendar\Http\JsonResponse;
use OCA\DAV\CalDAV\CalDavBackend;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\IRequest;
use Psr\Log\LoggerInterface;

class ShareAlarmController extends Controller {

public function __construct(
IRequest $request,
private readonly ShareAlarmSettingMapper $mapper,
private readonly CalDavBackend $calDavBackend,

Check failure on line 28 in lib/Controller/ShareAlarmController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable32

UndefinedClass

lib/Controller/ShareAlarmController.php:28:3: UndefinedClass: Class, interface or enum named OCA\DAV\CalDAV\CalDavBackend does not exist (see https://psalm.dev/019)

Check failure on line 28 in lib/Controller/ShareAlarmController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

UndefinedClass

lib/Controller/ShareAlarmController.php:28:3: UndefinedClass: Class, interface or enum named OCA\DAV\CalDAV\CalDavBackend does not exist (see https://psalm.dev/019)

Check failure on line 28 in lib/Controller/ShareAlarmController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable33

UndefinedClass

lib/Controller/ShareAlarmController.php:28:3: UndefinedClass: Class, interface or enum named OCA\DAV\CalDAV\CalDavBackend does not exist (see https://psalm.dev/019)
private readonly LoggerInterface $logger,
private readonly ?string $userId,
) {
parent::__construct(Application::APP_ID, $request);
}

/**
* Get alarm suppression settings for all shares of a calendar
*
* @NoAdminRequired
*
* @param string $calendarUrl The owner's calendar DAV URL
* @return JsonResponse
*/
public function get(string $calendarUrl): JsonResponse {
if ($this->userId === null) {
return JsonResponse::fail(null, Http::STATUS_UNAUTHORIZED);
}

$calendarId = $this->resolveCalendarId($calendarUrl);
if ($calendarId === null) {
return JsonResponse::fail('Calendar not found', Http::STATUS_NOT_FOUND);
}

$settings = $this->mapper->findAllByCalendarId($calendarId);
$result = [];
foreach ($settings as $setting) {
$result[$setting->getPrincipalUri()] = $setting->getSuppressAlarms();
}

return JsonResponse::success($result);
}

/**
* Toggle alarm suppression for a specific share
*
* @NoAdminRequired
*
* @param string $calendarUrl The owner's calendar DAV URL
* @param string $principalUri The sharee's principal URI
* @param bool $suppressAlarms Whether to suppress alarms
* @return JsonResponse
*/
public function toggle(string $calendarUrl, string $principalUri, bool $suppressAlarms): JsonResponse {
if ($this->userId === null) {
return JsonResponse::fail(null, Http::STATUS_UNAUTHORIZED);
}

$calendarId = $this->resolveCalendarId($calendarUrl);
if ($calendarId === null) {
return JsonResponse::fail('Calendar not found', Http::STATUS_NOT_FOUND);
}

try {
$setting = $this->mapper->findByCalendarAndPrincipal($calendarId, $principalUri);
$setting->setSuppressAlarms($suppressAlarms);
$this->mapper->update($setting);
} catch (DoesNotExistException) {
$setting = new ShareAlarmSetting();
$setting->setCalendarId($calendarId);
$setting->setPrincipalUri($principalUri);
$setting->setSuppressAlarms($suppressAlarms);
$this->mapper->insert($setting);
} catch (\Exception $e) {
$this->logger->error('Failed to toggle alarm suppression', [
'exception' => $e,
'calendarUrl' => $calendarUrl,
'principalUri' => $principalUri,
]);
return JsonResponse::fail(null, Http::STATUS_INTERNAL_SERVER_ERROR);
}

return JsonResponse::success(['suppressAlarms' => $suppressAlarms]);
}

/**
* Resolve a calendar DAV URL to its internal integer ID
*
* Parses the URL to extract the owner and calendar URI, then looks up
* the calendar in the CalDAV backend. Also verifies the current user
* owns the calendar.
*
* @param string $calendarUrl The calendar DAV URL (e.g. /remote.php/dav/calendars/owner/calname/)
* @return int|null The internal calendar ID, or null if not found or not owned
*/
private function resolveCalendarId(string $calendarUrl): ?int {
// Extract owner and calendar URI from the URL
// Expected format: .../calendars/{owner}/{calendarUri}/...
if (!preg_match('#/calendars/([^/]+)/([^/]+)#', $calendarUrl, $matches)) {
$this->logger->warning('Could not parse calendar URL', ['calendarUrl' => $calendarUrl]);
return null;
}

$ownerName = $matches[1];
$calendarUri = $matches[2];

// Verify the current user is the calendar owner
if ($ownerName !== $this->userId) {
$this->logger->warning('User attempted to modify alarm settings for a calendar they do not own', [
'userId' => $this->userId,
'ownerName' => $ownerName,
]);
return null;
}

$principalUri = 'principals/users/' . $ownerName;
$calendars = $this->calDavBackend->getCalendarsForUser($principalUri);

Check failure on line 135 in lib/Controller/ShareAlarmController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable32

UndefinedClass

lib/Controller/ShareAlarmController.php:135:16: UndefinedClass: Class, interface or enum named OCA\DAV\CalDAV\CalDavBackend does not exist (see https://psalm.dev/019)

Check failure on line 135 in lib/Controller/ShareAlarmController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

UndefinedClass

lib/Controller/ShareAlarmController.php:135:16: UndefinedClass: Class, interface or enum named OCA\DAV\CalDAV\CalDavBackend does not exist (see https://psalm.dev/019)

Check failure on line 135 in lib/Controller/ShareAlarmController.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable33

UndefinedClass

lib/Controller/ShareAlarmController.php:135:16: UndefinedClass: Class, interface or enum named OCA\DAV\CalDAV\CalDavBackend does not exist (see https://psalm.dev/019)

foreach ($calendars as $calendar) {
if ($calendar['uri'] === $calendarUri) {
return (int)$calendar['id'];
}
}

return null;
}
}
Loading
Loading