Completed
Push — master ( 5262ba...247b25 )
by
unknown
30:03 queued 13s
created
apps/dav/lib/CalDAV/Federation/FederatedCalendarImpl.php 1 patch
Indentation   +43 added lines, -43 removed lines patch added patch discarded remove patch
@@ -17,57 +17,57 @@
 block discarded – undo
17 17
 use OCP\Constants;
18 18
 
19 19
 class FederatedCalendarImpl implements ICalendar, ICalendarIsShared, ICalendarIsWritable, ICalendarIsEnabled {
20
-	public function __construct(
21
-		private readonly array $calendarInfo,
22
-		private readonly CalDavBackend $calDavBackend,
23
-	) {
24
-	}
20
+    public function __construct(
21
+        private readonly array $calendarInfo,
22
+        private readonly CalDavBackend $calDavBackend,
23
+    ) {
24
+    }
25 25
 
26
-	public function getKey(): string {
27
-		return (string)$this->calendarInfo['id'];
28
-	}
26
+    public function getKey(): string {
27
+        return (string)$this->calendarInfo['id'];
28
+    }
29 29
 
30
-	public function getUri(): string {
31
-		return $this->calendarInfo['uri'];
32
-	}
30
+    public function getUri(): string {
31
+        return $this->calendarInfo['uri'];
32
+    }
33 33
 
34
-	public function getDisplayName(): ?string {
35
-		return $this->calendarInfo['{DAV:}displayname'];
36
-	}
34
+    public function getDisplayName(): ?string {
35
+        return $this->calendarInfo['{DAV:}displayname'];
36
+    }
37 37
 
38
-	public function getDisplayColor(): ?string {
39
-		return $this->calendarInfo['{http://apple.com/ns/ical/}calendar-color'];
40
-	}
38
+    public function getDisplayColor(): ?string {
39
+        return $this->calendarInfo['{http://apple.com/ns/ical/}calendar-color'];
40
+    }
41 41
 
42
-	public function search(string $pattern, array $searchProperties = [], array $options = [], ?int $limit = null, ?int $offset = null): array {
43
-		return $this->calDavBackend->search(
44
-			$this->calendarInfo,
45
-			$pattern,
46
-			$searchProperties,
47
-			$options,
48
-			$limit,
49
-			$offset,
50
-		);
51
-	}
42
+    public function search(string $pattern, array $searchProperties = [], array $options = [], ?int $limit = null, ?int $offset = null): array {
43
+        return $this->calDavBackend->search(
44
+            $this->calendarInfo,
45
+            $pattern,
46
+            $searchProperties,
47
+            $options,
48
+            $limit,
49
+            $offset,
50
+        );
51
+    }
52 52
 
53
-	public function getPermissions(): int {
54
-		// TODO: implement read-write sharing
55
-		return Constants::PERMISSION_READ;
56
-	}
53
+    public function getPermissions(): int {
54
+        // TODO: implement read-write sharing
55
+        return Constants::PERMISSION_READ;
56
+    }
57 57
 
58
-	public function isDeleted(): bool {
59
-		return false;
60
-	}
58
+    public function isDeleted(): bool {
59
+        return false;
60
+    }
61 61
 
62
-	public function isShared(): bool {
63
-		return true;
64
-	}
62
+    public function isShared(): bool {
63
+        return true;
64
+    }
65 65
 
66
-	public function isWritable(): bool {
67
-		return false;
68
-	}
66
+    public function isWritable(): bool {
67
+        return false;
68
+    }
69 69
 
70
-	public function isEnabled(): bool {
71
-		return $this->calendarInfo['{http://owncloud.org/ns}calendar-enabled'] ?? true;
72
-	}
70
+    public function isEnabled(): bool {
71
+        return $this->calendarInfo['{http://owncloud.org/ns}calendar-enabled'] ?? true;
72
+    }
73 73
 }
Please login to merge, or discard this patch.
apps/dav/lib/CalDAV/Federation/CalendarFederationNotifier.php 1 patch
Indentation   +43 added lines, -43 removed lines patch added patch discarded remove patch
@@ -17,52 +17,52 @@
 block discarded – undo
17 17
 use OCP\OCM\Exceptions\OCMProviderException;
18 18
 
19 19
 class CalendarFederationNotifier {
20
-	public const NOTIFICATION_SYNC_CALENDAR = 'SYNC_CALENDAR';
20
+    public const NOTIFICATION_SYNC_CALENDAR = 'SYNC_CALENDAR';
21 21
 
22
-	public const PROP_SYNC_CALENDAR_SHARE_WITH = 'shareWith';
23
-	public const PROP_SYNC_CALENDAR_CALENDAR_URL = 'calendarUrl';
22
+    public const PROP_SYNC_CALENDAR_SHARE_WITH = 'shareWith';
23
+    public const PROP_SYNC_CALENDAR_CALENDAR_URL = 'calendarUrl';
24 24
 
25
-	public function __construct(
26
-		private readonly ICloudFederationFactory $federationFactory,
27
-		private readonly ICloudFederationProviderManager $federationManager,
28
-		private readonly IURLGenerator $url,
29
-	) {
30
-	}
25
+    public function __construct(
26
+        private readonly ICloudFederationFactory $federationFactory,
27
+        private readonly ICloudFederationProviderManager $federationManager,
28
+        private readonly IURLGenerator $url,
29
+    ) {
30
+    }
31 31
 
32
-	/**
33
-	 * Notify a remote server to sync a calendar.
34
-	 *
35
-	 * @param ICloudId $shareWith The cloud id of the remote sharee.
36
-	 * @return IResponse
37
-	 *
38
-	 * @throws OCMProviderException If sending the notification fails.
39
-	 */
40
-	public function notifySyncCalendar(
41
-		ICloudId $shareWith,
42
-		string $calendarOwner,
43
-		string $calendarName,
44
-		string $sharedSecret,
45
-	): IResponse {
46
-		$sharedWithEncoded = base64_encode($shareWith->getId());
47
-		$relativeCalendarUrl = "remote-calendars/$sharedWithEncoded/{$calendarName}_shared_by_$calendarOwner";
48
-		$calendarUrl = $this->url->linkTo('', 'remote.php') . "/dav/$relativeCalendarUrl";
49
-		$calendarUrl = $this->url->getAbsoluteURL($calendarUrl);
32
+    /**
33
+     * Notify a remote server to sync a calendar.
34
+     *
35
+     * @param ICloudId $shareWith The cloud id of the remote sharee.
36
+     * @return IResponse
37
+     *
38
+     * @throws OCMProviderException If sending the notification fails.
39
+     */
40
+    public function notifySyncCalendar(
41
+        ICloudId $shareWith,
42
+        string $calendarOwner,
43
+        string $calendarName,
44
+        string $sharedSecret,
45
+    ): IResponse {
46
+        $sharedWithEncoded = base64_encode($shareWith->getId());
47
+        $relativeCalendarUrl = "remote-calendars/$sharedWithEncoded/{$calendarName}_shared_by_$calendarOwner";
48
+        $calendarUrl = $this->url->linkTo('', 'remote.php') . "/dav/$relativeCalendarUrl";
49
+        $calendarUrl = $this->url->getAbsoluteURL($calendarUrl);
50 50
 
51
-		$notification = $this->federationFactory->getCloudFederationNotification();
52
-		$notification->setMessage(
53
-			self::NOTIFICATION_SYNC_CALENDAR,
54
-			CalendarFederationProvider::CALENDAR_RESOURCE,
55
-			CalendarFederationProvider::PROVIDER_ID,
56
-			[
57
-				'sharedSecret' => $sharedSecret,
58
-				self::PROP_SYNC_CALENDAR_SHARE_WITH => $shareWith->getId(),
59
-				self::PROP_SYNC_CALENDAR_CALENDAR_URL => $calendarUrl,
60
-			],
61
-		);
51
+        $notification = $this->federationFactory->getCloudFederationNotification();
52
+        $notification->setMessage(
53
+            self::NOTIFICATION_SYNC_CALENDAR,
54
+            CalendarFederationProvider::CALENDAR_RESOURCE,
55
+            CalendarFederationProvider::PROVIDER_ID,
56
+            [
57
+                'sharedSecret' => $sharedSecret,
58
+                self::PROP_SYNC_CALENDAR_SHARE_WITH => $shareWith->getId(),
59
+                self::PROP_SYNC_CALENDAR_CALENDAR_URL => $calendarUrl,
60
+            ],
61
+        );
62 62
 
63
-		return $this->federationManager->sendCloudNotification(
64
-			$shareWith->getRemote(),
65
-			$notification,
66
-		);
67
-	}
63
+        return $this->federationManager->sendCloudNotification(
64
+            $shareWith->getRemote(),
65
+            $notification,
66
+        );
67
+    }
68 68
 }
Please login to merge, or discard this patch.
apps/dav/lib/CalDAV/Federation/FederatedCalendarSyncService.php 1 patch
Indentation   +50 added lines, -50 removed lines patch added patch discarded remove patch
@@ -15,62 +15,62 @@
 block discarded – undo
15 15
 use Psr\Log\LoggerInterface;
16 16
 
17 17
 class FederatedCalendarSyncService {
18
-	private const SYNC_TOKEN_PREFIX = 'http://sabre.io/ns/sync/';
18
+    private const SYNC_TOKEN_PREFIX = 'http://sabre.io/ns/sync/';
19 19
 
20
-	public function __construct(
21
-		private readonly FederatedCalendarMapper $federatedCalendarMapper,
22
-		private readonly LoggerInterface $logger,
23
-		private readonly CalDavSyncService $syncService,
24
-		private readonly ICloudIdManager $cloudIdManager,
25
-	) {
26
-	}
20
+    public function __construct(
21
+        private readonly FederatedCalendarMapper $federatedCalendarMapper,
22
+        private readonly LoggerInterface $logger,
23
+        private readonly CalDavSyncService $syncService,
24
+        private readonly ICloudIdManager $cloudIdManager,
25
+    ) {
26
+    }
27 27
 
28
-	/**
29
-	 * @return int Downloaded event count (created or updated).
30
-	 *
31
-	 * @throws ClientExceptionInterface If syncing the calendar fails.
32
-	 */
33
-	public function syncOne(FederatedCalendarEntity $calendar): int {
34
-		[,, $sharedWith] = explode('/', $calendar->getPrincipaluri());
35
-		$calDavUser = $this->cloudIdManager->getCloudId($sharedWith, null)->getId();
36
-		$remoteUrl = $calendar->getRemoteUrl();
37
-		$syncToken = $calendar->getSyncTokenForSabre();
28
+    /**
29
+     * @return int Downloaded event count (created or updated).
30
+     *
31
+     * @throws ClientExceptionInterface If syncing the calendar fails.
32
+     */
33
+    public function syncOne(FederatedCalendarEntity $calendar): int {
34
+        [,, $sharedWith] = explode('/', $calendar->getPrincipaluri());
35
+        $calDavUser = $this->cloudIdManager->getCloudId($sharedWith, null)->getId();
36
+        $remoteUrl = $calendar->getRemoteUrl();
37
+        $syncToken = $calendar->getSyncTokenForSabre();
38 38
 
39
-		// Need to encode the cloud id as it might contain a colon which is not allowed in basic
40
-		// auth according to RFC 7617
41
-		$calDavUser = base64_encode($calDavUser);
39
+        // Need to encode the cloud id as it might contain a colon which is not allowed in basic
40
+        // auth according to RFC 7617
41
+        $calDavUser = base64_encode($calDavUser);
42 42
 
43
-		$syncResponse = $this->syncService->syncRemoteCalendar(
44
-			$remoteUrl,
45
-			$calDavUser,
46
-			$calendar->getToken(),
47
-			$syncToken,
48
-			$calendar,
49
-		);
43
+        $syncResponse = $this->syncService->syncRemoteCalendar(
44
+            $remoteUrl,
45
+            $calDavUser,
46
+            $calendar->getToken(),
47
+            $syncToken,
48
+            $calendar,
49
+        );
50 50
 
51
-		$newSyncToken = $syncResponse->getSyncToken();
51
+        $newSyncToken = $syncResponse->getSyncToken();
52 52
 
53
-		// Check sync token format and extract the actual sync token integer
54
-		$matches = [];
55
-		if (!preg_match('/^http:\/\/sabre\.io\/ns\/sync\/([0-9]+)$/', $newSyncToken, $matches)) {
56
-			$this->logger->error("Failed to sync federated calendar at $remoteUrl: New sync token has unexpected format: $newSyncToken", [
57
-				'calendar' => $calendar->toCalendarInfo(),
58
-				'newSyncToken' => $newSyncToken,
59
-			]);
60
-			return 0;
61
-		}
53
+        // Check sync token format and extract the actual sync token integer
54
+        $matches = [];
55
+        if (!preg_match('/^http:\/\/sabre\.io\/ns\/sync\/([0-9]+)$/', $newSyncToken, $matches)) {
56
+            $this->logger->error("Failed to sync federated calendar at $remoteUrl: New sync token has unexpected format: $newSyncToken", [
57
+                'calendar' => $calendar->toCalendarInfo(),
58
+                'newSyncToken' => $newSyncToken,
59
+            ]);
60
+            return 0;
61
+        }
62 62
 
63
-		$newSyncToken = (int)$matches[1];
64
-		if ($newSyncToken !== $calendar->getSyncToken()) {
65
-			$this->federatedCalendarMapper->updateSyncTokenAndTime(
66
-				$calendar->getId(),
67
-				$newSyncToken,
68
-			);
69
-		} else {
70
-			$this->logger->debug("Sync Token for $remoteUrl unchanged from previous sync");
71
-			$this->federatedCalendarMapper->updateSyncTime($calendar->getId());
72
-		}
63
+        $newSyncToken = (int)$matches[1];
64
+        if ($newSyncToken !== $calendar->getSyncToken()) {
65
+            $this->federatedCalendarMapper->updateSyncTokenAndTime(
66
+                $calendar->getId(),
67
+                $newSyncToken,
68
+            );
69
+        } else {
70
+            $this->logger->debug("Sync Token for $remoteUrl unchanged from previous sync");
71
+            $this->federatedCalendarMapper->updateSyncTime($calendar->getId());
72
+        }
73 73
 
74
-		return $syncResponse->getDownloadedEvents();
75
-	}
74
+        return $syncResponse->getDownloadedEvents();
75
+    }
76 76
 }
Please login to merge, or discard this patch.
apps/dav/lib/CalDAV/Federation/FederatedCalendar.php 1 patch
Indentation   +16 added lines, -16 removed lines patch added patch discarded remove patch
@@ -17,22 +17,22 @@
 block discarded – undo
17 17
 use Sabre\CalDAV\Backend;
18 18
 
19 19
 class FederatedCalendar extends Calendar {
20
-	public function __construct(
21
-		Backend\BackendInterface $caldavBackend,
22
-		$calendarInfo,
23
-		IL10N $l10n,
24
-		IConfig $config,
25
-		LoggerInterface $logger,
26
-		private readonly FederatedCalendarMapper $federatedCalendarMapper,
27
-	) {
28
-		parent::__construct($caldavBackend, $calendarInfo, $l10n, $config, $logger);
29
-	}
20
+    public function __construct(
21
+        Backend\BackendInterface $caldavBackend,
22
+        $calendarInfo,
23
+        IL10N $l10n,
24
+        IConfig $config,
25
+        LoggerInterface $logger,
26
+        private readonly FederatedCalendarMapper $federatedCalendarMapper,
27
+    ) {
28
+        parent::__construct($caldavBackend, $calendarInfo, $l10n, $config, $logger);
29
+    }
30 30
 
31
-	public function delete() {
32
-		$this->federatedCalendarMapper->deleteById($this->getResourceId());
33
-	}
31
+    public function delete() {
32
+        $this->federatedCalendarMapper->deleteById($this->getResourceId());
33
+    }
34 34
 
35
-	protected function getCalendarType(): int {
36
-		return CalDavBackend::CALENDAR_TYPE_FEDERATED;
37
-	}
35
+    protected function getCalendarType(): int {
36
+        return CalDavBackend::CALENDAR_TYPE_FEDERATED;
37
+    }
38 38
 }
Please login to merge, or discard this patch.
apps/dav/lib/CalDAV/Federation/FederatedCalendarMapper.php 1 patch
Indentation   +188 added lines, -188 removed lines patch added patch discarded remove patch
@@ -18,192 +18,192 @@
 block discarded – undo
18 18
 
19 19
 /** @template-extends QBMapper<FederatedCalendarEntity>  */
20 20
 class FederatedCalendarMapper extends QBMapper {
21
-	public const TABLE_NAME = 'calendars_federated';
22
-
23
-	public function __construct(
24
-		IDBConnection $db,
25
-		private readonly ITimeFactory $time,
26
-	) {
27
-		parent::__construct($db, self::TABLE_NAME, FederatedCalendarEntity::class);
28
-	}
29
-
30
-	/**
31
-	 * @throws DoesNotExistException If there is no federated calendar with the given id.
32
-	 */
33
-	public function find(int $id): FederatedCalendarEntity {
34
-		$qb = $this->db->getQueryBuilder();
35
-		$qb->select('*')
36
-			->from(self::TABLE_NAME)
37
-			->where($qb->expr()->eq(
38
-				'id',
39
-				$qb->createNamedParameter($id, IQueryBuilder::PARAM_INT),
40
-				IQueryBuilder::PARAM_INT,
41
-			));
42
-		return $this->findEntity($qb);
43
-	}
44
-
45
-	/**
46
-	 * @return FederatedCalendarEntity[]
47
-	 */
48
-	public function findByPrincipalUri(string $principalUri): array {
49
-		$qb = $this->db->getQueryBuilder();
50
-		$qb->select('*')
51
-			->from(self::TABLE_NAME)
52
-			->where($qb->expr()->eq(
53
-				'principaluri',
54
-				$qb->createNamedParameter($principalUri, IQueryBuilder::PARAM_STR),
55
-				IQueryBuilder::PARAM_STR,
56
-			));
57
-		return $this->findEntities($qb);
58
-	}
59
-
60
-	public function findByUri(string $principalUri, string $uri): ?FederatedCalendarEntity {
61
-		$qb = $this->db->getQueryBuilder();
62
-		$qb->select('*')
63
-			->from(self::TABLE_NAME)
64
-			->where($qb->expr()->eq(
65
-				'principaluri',
66
-				$qb->createNamedParameter($principalUri, IQueryBuilder::PARAM_STR),
67
-				IQueryBuilder::PARAM_STR,
68
-			))
69
-			->andWhere($qb->expr()->eq(
70
-				'uri',
71
-				$qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR),
72
-				IQueryBuilder::PARAM_STR,
73
-			));
74
-
75
-		try {
76
-			return $this->findEntity($qb);
77
-		} catch (DoesNotExistException $e) {
78
-			return null;
79
-		} catch (MultipleObjectsReturnedException $e) {
80
-			// Should never happen
81
-			return null;
82
-		}
83
-	}
84
-
85
-	/**
86
-	 * @return FederatedCalendarEntity[]
87
-	 */
88
-	public function findUnsyncedSinceBefore(int $beforeTimestamp): array {
89
-		$qb = $this->db->getQueryBuilder();
90
-		$qb->select('*')
91
-			->from(self::TABLE_NAME)
92
-			->where($qb->expr()->lt(
93
-				'last_sync',
94
-				$qb->createNamedParameter($beforeTimestamp, IQueryBuilder::PARAM_INT),
95
-				IQueryBuilder::PARAM_INT,
96
-			))
97
-			// Omit unsynced calendars for now as they are synced by a separate job
98
-			->andWhere($qb->expr()->isNotNull('last_sync'));
99
-		return $this->findEntities($qb);
100
-	}
101
-
102
-	public function deleteById(int $id): void {
103
-		$qb = $this->db->getQueryBuilder();
104
-		$qb->delete(self::TABLE_NAME)
105
-			->where($qb->expr()->eq(
106
-				'id',
107
-				$qb->createNamedParameter($id, IQueryBuilder::PARAM_INT),
108
-				IQueryBuilder::PARAM_INT,
109
-			));
110
-		$qb->executeStatement();
111
-	}
112
-
113
-	public function updateSyncTime(int $id): void {
114
-		$now = $this->time->getTime();
115
-
116
-		$qb = $this->db->getQueryBuilder();
117
-		$qb->update(self::TABLE_NAME)
118
-			->set('last_sync', $qb->createNamedParameter($now, IQueryBuilder::PARAM_INT))
119
-			->where($qb->expr()->eq(
120
-				'id',
121
-				$qb->createNamedParameter($id, IQueryBuilder::PARAM_INT),
122
-				IQueryBuilder::PARAM_INT,
123
-			));
124
-		$qb->executeStatement();
125
-	}
126
-
127
-	public function updateSyncTokenAndTime(int $id, int $syncToken): void {
128
-		$now = $this->time->getTime();
129
-
130
-		$qb = $this->db->getQueryBuilder();
131
-		$qb->update(self::TABLE_NAME)
132
-			->set('sync_token', $qb->createNamedParameter($syncToken, IQueryBuilder::PARAM_INT))
133
-			->set('last_sync', $qb->createNamedParameter($now, IQueryBuilder::PARAM_INT))
134
-			->where($qb->expr()->eq(
135
-				'id',
136
-				$qb->createNamedParameter($id, IQueryBuilder::PARAM_INT),
137
-				IQueryBuilder::PARAM_INT,
138
-			));
139
-		$qb->executeStatement();
140
-	}
141
-
142
-	/**
143
-	 * @return \Generator<mixed, FederatedCalendarEntity>
144
-	 */
145
-	public function findAll(): \Generator {
146
-		$qb = $this->db->getQueryBuilder();
147
-		$qb->select('*')
148
-			->from(self::TABLE_NAME);
149
-
150
-		$result = $qb->executeQuery();
151
-		while ($row = $result->fetch()) {
152
-			yield $this->mapRowToEntity($row);
153
-		}
154
-		$result->closeCursor();
155
-	}
156
-
157
-	public function countAll(): int {
158
-		$qb = $this->db->getQueryBuilder();
159
-		$qb->select($qb->func()->count('*'))
160
-			->from(self::TABLE_NAME);
161
-		$result = $qb->executeQuery();
162
-		$count = (int)$result->fetchOne();
163
-		$result->closeCursor();
164
-		return $count;
165
-	}
166
-
167
-	public function deleteByUri(string $principalUri, string $uri): void {
168
-		$qb = $this->db->getQueryBuilder();
169
-		$qb->delete(self::TABLE_NAME)
170
-			->where($qb->expr()->eq(
171
-				'principaluri',
172
-				$qb->createNamedParameter($principalUri, IQueryBuilder::PARAM_STR),
173
-				IQueryBuilder::PARAM_STR,
174
-			))
175
-			->andWhere($qb->expr()->eq(
176
-				'uri',
177
-				$qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR),
178
-				IQueryBuilder::PARAM_STR,
179
-			));
180
-
181
-		$qb->executeStatement();
182
-	}
183
-
184
-	/**
185
-	 * @return FederatedCalendarEntity[]
186
-	 */
187
-	public function findByRemoteUrl(string $remoteUrl, string $principalUri, string $token): array {
188
-		$qb = $this->db->getQueryBuilder();
189
-		$qb->select('*')
190
-			->from(self::TABLE_NAME)
191
-			->where($qb->expr()->eq(
192
-				'remote_url',
193
-				$qb->createNamedParameter($remoteUrl, IQueryBuilder::PARAM_STR),
194
-				IQueryBuilder::PARAM_STR,
195
-			))
196
-			->andWhere($qb->expr()->eq(
197
-				'principaluri',
198
-				$qb->createNamedParameter($principalUri, IQueryBuilder::PARAM_STR),
199
-				IQueryBuilder::PARAM_STR,
200
-			))
201
-			->andWhere($qb->expr()->eq(
202
-				'token',
203
-				$qb->createNamedParameter($token, IQueryBuilder::PARAM_STR),
204
-				IQueryBuilder::PARAM_STR,
205
-			));
206
-
207
-		return $this->findEntities($qb);
208
-	}
21
+    public const TABLE_NAME = 'calendars_federated';
22
+
23
+    public function __construct(
24
+        IDBConnection $db,
25
+        private readonly ITimeFactory $time,
26
+    ) {
27
+        parent::__construct($db, self::TABLE_NAME, FederatedCalendarEntity::class);
28
+    }
29
+
30
+    /**
31
+     * @throws DoesNotExistException If there is no federated calendar with the given id.
32
+     */
33
+    public function find(int $id): FederatedCalendarEntity {
34
+        $qb = $this->db->getQueryBuilder();
35
+        $qb->select('*')
36
+            ->from(self::TABLE_NAME)
37
+            ->where($qb->expr()->eq(
38
+                'id',
39
+                $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT),
40
+                IQueryBuilder::PARAM_INT,
41
+            ));
42
+        return $this->findEntity($qb);
43
+    }
44
+
45
+    /**
46
+     * @return FederatedCalendarEntity[]
47
+     */
48
+    public function findByPrincipalUri(string $principalUri): array {
49
+        $qb = $this->db->getQueryBuilder();
50
+        $qb->select('*')
51
+            ->from(self::TABLE_NAME)
52
+            ->where($qb->expr()->eq(
53
+                'principaluri',
54
+                $qb->createNamedParameter($principalUri, IQueryBuilder::PARAM_STR),
55
+                IQueryBuilder::PARAM_STR,
56
+            ));
57
+        return $this->findEntities($qb);
58
+    }
59
+
60
+    public function findByUri(string $principalUri, string $uri): ?FederatedCalendarEntity {
61
+        $qb = $this->db->getQueryBuilder();
62
+        $qb->select('*')
63
+            ->from(self::TABLE_NAME)
64
+            ->where($qb->expr()->eq(
65
+                'principaluri',
66
+                $qb->createNamedParameter($principalUri, IQueryBuilder::PARAM_STR),
67
+                IQueryBuilder::PARAM_STR,
68
+            ))
69
+            ->andWhere($qb->expr()->eq(
70
+                'uri',
71
+                $qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR),
72
+                IQueryBuilder::PARAM_STR,
73
+            ));
74
+
75
+        try {
76
+            return $this->findEntity($qb);
77
+        } catch (DoesNotExistException $e) {
78
+            return null;
79
+        } catch (MultipleObjectsReturnedException $e) {
80
+            // Should never happen
81
+            return null;
82
+        }
83
+    }
84
+
85
+    /**
86
+     * @return FederatedCalendarEntity[]
87
+     */
88
+    public function findUnsyncedSinceBefore(int $beforeTimestamp): array {
89
+        $qb = $this->db->getQueryBuilder();
90
+        $qb->select('*')
91
+            ->from(self::TABLE_NAME)
92
+            ->where($qb->expr()->lt(
93
+                'last_sync',
94
+                $qb->createNamedParameter($beforeTimestamp, IQueryBuilder::PARAM_INT),
95
+                IQueryBuilder::PARAM_INT,
96
+            ))
97
+            // Omit unsynced calendars for now as they are synced by a separate job
98
+            ->andWhere($qb->expr()->isNotNull('last_sync'));
99
+        return $this->findEntities($qb);
100
+    }
101
+
102
+    public function deleteById(int $id): void {
103
+        $qb = $this->db->getQueryBuilder();
104
+        $qb->delete(self::TABLE_NAME)
105
+            ->where($qb->expr()->eq(
106
+                'id',
107
+                $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT),
108
+                IQueryBuilder::PARAM_INT,
109
+            ));
110
+        $qb->executeStatement();
111
+    }
112
+
113
+    public function updateSyncTime(int $id): void {
114
+        $now = $this->time->getTime();
115
+
116
+        $qb = $this->db->getQueryBuilder();
117
+        $qb->update(self::TABLE_NAME)
118
+            ->set('last_sync', $qb->createNamedParameter($now, IQueryBuilder::PARAM_INT))
119
+            ->where($qb->expr()->eq(
120
+                'id',
121
+                $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT),
122
+                IQueryBuilder::PARAM_INT,
123
+            ));
124
+        $qb->executeStatement();
125
+    }
126
+
127
+    public function updateSyncTokenAndTime(int $id, int $syncToken): void {
128
+        $now = $this->time->getTime();
129
+
130
+        $qb = $this->db->getQueryBuilder();
131
+        $qb->update(self::TABLE_NAME)
132
+            ->set('sync_token', $qb->createNamedParameter($syncToken, IQueryBuilder::PARAM_INT))
133
+            ->set('last_sync', $qb->createNamedParameter($now, IQueryBuilder::PARAM_INT))
134
+            ->where($qb->expr()->eq(
135
+                'id',
136
+                $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT),
137
+                IQueryBuilder::PARAM_INT,
138
+            ));
139
+        $qb->executeStatement();
140
+    }
141
+
142
+    /**
143
+     * @return \Generator<mixed, FederatedCalendarEntity>
144
+     */
145
+    public function findAll(): \Generator {
146
+        $qb = $this->db->getQueryBuilder();
147
+        $qb->select('*')
148
+            ->from(self::TABLE_NAME);
149
+
150
+        $result = $qb->executeQuery();
151
+        while ($row = $result->fetch()) {
152
+            yield $this->mapRowToEntity($row);
153
+        }
154
+        $result->closeCursor();
155
+    }
156
+
157
+    public function countAll(): int {
158
+        $qb = $this->db->getQueryBuilder();
159
+        $qb->select($qb->func()->count('*'))
160
+            ->from(self::TABLE_NAME);
161
+        $result = $qb->executeQuery();
162
+        $count = (int)$result->fetchOne();
163
+        $result->closeCursor();
164
+        return $count;
165
+    }
166
+
167
+    public function deleteByUri(string $principalUri, string $uri): void {
168
+        $qb = $this->db->getQueryBuilder();
169
+        $qb->delete(self::TABLE_NAME)
170
+            ->where($qb->expr()->eq(
171
+                'principaluri',
172
+                $qb->createNamedParameter($principalUri, IQueryBuilder::PARAM_STR),
173
+                IQueryBuilder::PARAM_STR,
174
+            ))
175
+            ->andWhere($qb->expr()->eq(
176
+                'uri',
177
+                $qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR),
178
+                IQueryBuilder::PARAM_STR,
179
+            ));
180
+
181
+        $qb->executeStatement();
182
+    }
183
+
184
+    /**
185
+     * @return FederatedCalendarEntity[]
186
+     */
187
+    public function findByRemoteUrl(string $remoteUrl, string $principalUri, string $token): array {
188
+        $qb = $this->db->getQueryBuilder();
189
+        $qb->select('*')
190
+            ->from(self::TABLE_NAME)
191
+            ->where($qb->expr()->eq(
192
+                'remote_url',
193
+                $qb->createNamedParameter($remoteUrl, IQueryBuilder::PARAM_STR),
194
+                IQueryBuilder::PARAM_STR,
195
+            ))
196
+            ->andWhere($qb->expr()->eq(
197
+                'principaluri',
198
+                $qb->createNamedParameter($principalUri, IQueryBuilder::PARAM_STR),
199
+                IQueryBuilder::PARAM_STR,
200
+            ))
201
+            ->andWhere($qb->expr()->eq(
202
+                'token',
203
+                $qb->createNamedParameter($token, IQueryBuilder::PARAM_STR),
204
+                IQueryBuilder::PARAM_STR,
205
+            ));
206
+
207
+        return $this->findEntities($qb);
208
+    }
209 209
 }
Please login to merge, or discard this patch.
apps/dav/lib/CalDAV/Federation/Protocol/CalendarFederationProtocolV1.php 1 patch
Indentation   +112 added lines, -112 removed lines patch added patch discarded remove patch
@@ -10,116 +10,116 @@
 block discarded – undo
10 10
 namespace OCA\DAV\CalDAV\Federation\Protocol;
11 11
 
12 12
 class CalendarFederationProtocolV1 implements ICalendarFederationProtocol {
13
-	public const VERSION = 'v1';
14
-
15
-	public const PROP_URL = 'url';
16
-	public const PROP_DISPLAY_NAME = 'displayName';
17
-	public const PROP_COLOR = 'color';
18
-	public const PROP_ACCESS = 'access';
19
-	public const PROP_COMPONENTS = 'components';
20
-
21
-	private string $url = '';
22
-	private string $displayName = '';
23
-	private ?string $color = null;
24
-	private int $access = 0;
25
-	private string $components = '';
26
-
27
-	/**
28
-	 * @throws CalendarProtocolParseException If parsing the raw protocol array fails.
29
-	 */
30
-	public static function parse(array $rawProtocol): self {
31
-		if ($rawProtocol[self::PROP_VERSION] !== self::VERSION) {
32
-			throw new CalendarProtocolParseException('Unknown protocol version');
33
-		}
34
-
35
-		$url = $rawProtocol[self::PROP_URL] ?? null;
36
-		if (!is_string($url)) {
37
-			throw new CalendarProtocolParseException('URL is missing or not a string');
38
-		}
39
-
40
-		$displayName = $rawProtocol[self::PROP_DISPLAY_NAME] ?? null;
41
-		if (!is_string($displayName)) {
42
-			throw new CalendarProtocolParseException('Display name is missing or not a string');
43
-		}
44
-
45
-		$color = $rawProtocol[self::PROP_COLOR] ?? null;
46
-		if (!is_string($color) && $color !== null) {
47
-			throw new CalendarProtocolParseException('Color is set but not a string');
48
-		}
49
-
50
-		$access = $rawProtocol[self::PROP_ACCESS] ?? null;
51
-		if (!is_int($access)) {
52
-			throw new CalendarProtocolParseException('Access is missing or not an integer');
53
-		}
54
-
55
-		$components = $rawProtocol[self::PROP_COMPONENTS] ?? null;
56
-		if (!is_string($components)) {
57
-			throw new CalendarProtocolParseException('Supported calendar components are missing or not a string');
58
-		}
59
-
60
-		$protocol = new self();
61
-		$protocol->setUrl($url);
62
-		$protocol->setDisplayName($displayName);
63
-		$protocol->setColor($color);
64
-		$protocol->setAccess($access);
65
-		$protocol->setComponents($components);
66
-		return $protocol;
67
-	}
68
-
69
-	#[\Override]
70
-	public function toProtocol(): array {
71
-		return [
72
-			self::PROP_VERSION => $this->getVersion(),
73
-			self::PROP_URL => $this->getUrl(),
74
-			self::PROP_DISPLAY_NAME => $this->getDisplayName(),
75
-			self::PROP_COLOR => $this->getColor(),
76
-			self::PROP_ACCESS => $this->getAccess(),
77
-			self::PROP_COMPONENTS => $this->getComponents(),
78
-		];
79
-	}
80
-
81
-	#[\Override]
82
-	public function getVersion(): string {
83
-		return self::VERSION;
84
-	}
85
-
86
-	public function getUrl(): string {
87
-		return $this->url;
88
-	}
89
-
90
-	public function setUrl(string $url): void {
91
-		$this->url = $url;
92
-	}
93
-
94
-	public function getDisplayName(): string {
95
-		return $this->displayName;
96
-	}
97
-
98
-	public function setDisplayName(string $displayName): void {
99
-		$this->displayName = $displayName;
100
-	}
101
-
102
-	public function getColor(): ?string {
103
-		return $this->color;
104
-	}
105
-
106
-	public function setColor(?string $color): void {
107
-		$this->color = $color;
108
-	}
109
-
110
-	public function getAccess(): int {
111
-		return $this->access;
112
-	}
113
-
114
-	public function setAccess(int $access): void {
115
-		$this->access = $access;
116
-	}
117
-
118
-	public function getComponents(): string {
119
-		return $this->components;
120
-	}
121
-
122
-	public function setComponents(string $components): void {
123
-		$this->components = $components;
124
-	}
13
+    public const VERSION = 'v1';
14
+
15
+    public const PROP_URL = 'url';
16
+    public const PROP_DISPLAY_NAME = 'displayName';
17
+    public const PROP_COLOR = 'color';
18
+    public const PROP_ACCESS = 'access';
19
+    public const PROP_COMPONENTS = 'components';
20
+
21
+    private string $url = '';
22
+    private string $displayName = '';
23
+    private ?string $color = null;
24
+    private int $access = 0;
25
+    private string $components = '';
26
+
27
+    /**
28
+     * @throws CalendarProtocolParseException If parsing the raw protocol array fails.
29
+     */
30
+    public static function parse(array $rawProtocol): self {
31
+        if ($rawProtocol[self::PROP_VERSION] !== self::VERSION) {
32
+            throw new CalendarProtocolParseException('Unknown protocol version');
33
+        }
34
+
35
+        $url = $rawProtocol[self::PROP_URL] ?? null;
36
+        if (!is_string($url)) {
37
+            throw new CalendarProtocolParseException('URL is missing or not a string');
38
+        }
39
+
40
+        $displayName = $rawProtocol[self::PROP_DISPLAY_NAME] ?? null;
41
+        if (!is_string($displayName)) {
42
+            throw new CalendarProtocolParseException('Display name is missing or not a string');
43
+        }
44
+
45
+        $color = $rawProtocol[self::PROP_COLOR] ?? null;
46
+        if (!is_string($color) && $color !== null) {
47
+            throw new CalendarProtocolParseException('Color is set but not a string');
48
+        }
49
+
50
+        $access = $rawProtocol[self::PROP_ACCESS] ?? null;
51
+        if (!is_int($access)) {
52
+            throw new CalendarProtocolParseException('Access is missing or not an integer');
53
+        }
54
+
55
+        $components = $rawProtocol[self::PROP_COMPONENTS] ?? null;
56
+        if (!is_string($components)) {
57
+            throw new CalendarProtocolParseException('Supported calendar components are missing or not a string');
58
+        }
59
+
60
+        $protocol = new self();
61
+        $protocol->setUrl($url);
62
+        $protocol->setDisplayName($displayName);
63
+        $protocol->setColor($color);
64
+        $protocol->setAccess($access);
65
+        $protocol->setComponents($components);
66
+        return $protocol;
67
+    }
68
+
69
+    #[\Override]
70
+    public function toProtocol(): array {
71
+        return [
72
+            self::PROP_VERSION => $this->getVersion(),
73
+            self::PROP_URL => $this->getUrl(),
74
+            self::PROP_DISPLAY_NAME => $this->getDisplayName(),
75
+            self::PROP_COLOR => $this->getColor(),
76
+            self::PROP_ACCESS => $this->getAccess(),
77
+            self::PROP_COMPONENTS => $this->getComponents(),
78
+        ];
79
+    }
80
+
81
+    #[\Override]
82
+    public function getVersion(): string {
83
+        return self::VERSION;
84
+    }
85
+
86
+    public function getUrl(): string {
87
+        return $this->url;
88
+    }
89
+
90
+    public function setUrl(string $url): void {
91
+        $this->url = $url;
92
+    }
93
+
94
+    public function getDisplayName(): string {
95
+        return $this->displayName;
96
+    }
97
+
98
+    public function setDisplayName(string $displayName): void {
99
+        $this->displayName = $displayName;
100
+    }
101
+
102
+    public function getColor(): ?string {
103
+        return $this->color;
104
+    }
105
+
106
+    public function setColor(?string $color): void {
107
+        $this->color = $color;
108
+    }
109
+
110
+    public function getAccess(): int {
111
+        return $this->access;
112
+    }
113
+
114
+    public function setAccess(int $access): void {
115
+        $this->access = $access;
116
+    }
117
+
118
+    public function getComponents(): string {
119
+        return $this->components;
120
+    }
121
+
122
+    public function setComponents(string $components): void {
123
+        $this->components = $components;
124
+    }
125 125
 }
Please login to merge, or discard this patch.
apps/dav/lib/CalDAV/Federation/Protocol/ICalendarFederationProtocol.php 1 patch
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -10,16 +10,16 @@
 block discarded – undo
10 10
 namespace OCA\DAV\CalDAV\Federation\Protocol;
11 11
 
12 12
 interface ICalendarFederationProtocol {
13
-	public const PROP_VERSION = 'version';
13
+    public const PROP_VERSION = 'version';
14 14
 
15
-	/**
16
-	 * Get the version of this protocol implementation.
17
-	 */
18
-	public function getVersion(): string;
15
+    /**
16
+     * Get the version of this protocol implementation.
17
+     */
18
+    public function getVersion(): string;
19 19
 
20
-	/**
21
-	 * Convert the protocol to an associative array to be sent to a remote instance.
22
-	 * The resulting array still needs to be merged with the base protocol from the share!
23
-	 */
24
-	public function toProtocol(): array;
20
+    /**
21
+     * Convert the protocol to an associative array to be sent to a remote instance.
22
+     * The resulting array still needs to be merged with the base protocol from the share!
23
+     */
24
+    public function toProtocol(): array;
25 25
 }
Please login to merge, or discard this patch.
apps/dav/lib/CalDAV/Federation/FederatedCalendarAuth.php 1 patch
Indentation   +51 added lines, -51 removed lines patch added patch discarded remove patch
@@ -18,64 +18,64 @@
 block discarded – undo
18 18
 use Sabre\HTTP\ResponseInterface;
19 19
 
20 20
 class FederatedCalendarAuth implements BackendInterface {
21
-	private readonly string $realm;
21
+    private readonly string $realm;
22 22
 
23
-	public function __construct(
24
-		private readonly SharingMapper $sharingMapper,
25
-	) {
26
-		$defaults = new Defaults();
27
-		$this->realm = $defaults->getName();
28
-	}
23
+    public function __construct(
24
+        private readonly SharingMapper $sharingMapper,
25
+    ) {
26
+        $defaults = new Defaults();
27
+        $this->realm = $defaults->getName();
28
+    }
29 29
 
30
-	/**
31
-	 * @return string|null A principal uri if the given combination of user and pass is valid and null otherwise.
32
-	 */
33
-	private function validateUserPass(
34
-		string $requestPath,
35
-		string $username,
36
-		string $password,
37
-	): ?string {
38
-		$remoteUserPrincipalUri = RemoteUserPrincipalBackend::PRINCIPAL_PREFIX . "/$username";
39
-		[, $remoteUserPrincipalId] = \Sabre\Uri\split($remoteUserPrincipalUri);
30
+    /**
31
+     * @return string|null A principal uri if the given combination of user and pass is valid and null otherwise.
32
+     */
33
+    private function validateUserPass(
34
+        string $requestPath,
35
+        string $username,
36
+        string $password,
37
+    ): ?string {
38
+        $remoteUserPrincipalUri = RemoteUserPrincipalBackend::PRINCIPAL_PREFIX . "/$username";
39
+        [, $remoteUserPrincipalId] = \Sabre\Uri\split($remoteUserPrincipalUri);
40 40
 
41
-		$rows = $this->sharingMapper->getSharedCalendarsForRemoteUser(
42
-			$remoteUserPrincipalUri,
43
-			$password,
44
-		);
41
+        $rows = $this->sharingMapper->getSharedCalendarsForRemoteUser(
42
+            $remoteUserPrincipalUri,
43
+            $password,
44
+        );
45 45
 
46
-		// Is the requested calendar actually shared with the remote user?
47
-		foreach ($rows as $row) {
48
-			$ownerPrincipalUri = $row['principaluri'];
49
-			[, $ownerUserId] = \Sabre\Uri\split($ownerPrincipalUri);
50
-			$shareUri = $row['uri'] . '_shared_by_' . $ownerUserId;
51
-			if (str_starts_with($requestPath, "remote-calendars/$remoteUserPrincipalId/$shareUri")) {
52
-				// Yes? -> return early
53
-				return $remoteUserPrincipalUri;
54
-			}
55
-		}
46
+        // Is the requested calendar actually shared with the remote user?
47
+        foreach ($rows as $row) {
48
+            $ownerPrincipalUri = $row['principaluri'];
49
+            [, $ownerUserId] = \Sabre\Uri\split($ownerPrincipalUri);
50
+            $shareUri = $row['uri'] . '_shared_by_' . $ownerUserId;
51
+            if (str_starts_with($requestPath, "remote-calendars/$remoteUserPrincipalId/$shareUri")) {
52
+                // Yes? -> return early
53
+                return $remoteUserPrincipalUri;
54
+            }
55
+        }
56 56
 
57
-		return null;
58
-	}
57
+        return null;
58
+    }
59 59
 
60
-	public function check(RequestInterface $request, ResponseInterface $response): array {
61
-		if (!str_starts_with($request->getPath(), 'remote-calendars/')) {
62
-			return [false, 'This request is not for a federated calendar'];
63
-		}
60
+    public function check(RequestInterface $request, ResponseInterface $response): array {
61
+        if (!str_starts_with($request->getPath(), 'remote-calendars/')) {
62
+            return [false, 'This request is not for a federated calendar'];
63
+        }
64 64
 
65
-		$auth = new BasicAuth($this->realm, $request, $response);
66
-		$userpass = $auth->getCredentials();
67
-		if ($userpass === null || count($userpass) !== 2) {
68
-			return [false, "No 'Authorization: Basic' header found. Either the client didn't send one, or the server is misconfigured"];
69
-		}
70
-		$principal = $this->validateUserPass($request->getPath(), $userpass[0], $userpass[1]);
71
-		if ($principal === null) {
72
-			return [false, 'Username or password was incorrect'];
73
-		}
65
+        $auth = new BasicAuth($this->realm, $request, $response);
66
+        $userpass = $auth->getCredentials();
67
+        if ($userpass === null || count($userpass) !== 2) {
68
+            return [false, "No 'Authorization: Basic' header found. Either the client didn't send one, or the server is misconfigured"];
69
+        }
70
+        $principal = $this->validateUserPass($request->getPath(), $userpass[0], $userpass[1]);
71
+        if ($principal === null) {
72
+            return [false, 'Username or password was incorrect'];
73
+        }
74 74
 
75
-		return [true, $principal];
76
-	}
75
+        return [true, $principal];
76
+    }
77 77
 
78
-	public function challenge(RequestInterface $request, ResponseInterface $response): void {
79
-		// No special challenge is needed here
80
-	}
78
+    public function challenge(RequestInterface $request, ResponseInterface $response): void {
79
+        // No special challenge is needed here
80
+    }
81 81
 }
Please login to merge, or discard this patch.
apps/dav/lib/CalDAV/Federation/CalendarFederationConfig.php 1 patch
Indentation   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -12,12 +12,12 @@
 block discarded – undo
12 12
 use OCP\AppFramework\Services\IAppConfig;
13 13
 
14 14
 class CalendarFederationConfig {
15
-	public function __construct(
16
-		private readonly IAppConfig $appConfig,
17
-	) {
18
-	}
15
+    public function __construct(
16
+        private readonly IAppConfig $appConfig,
17
+    ) {
18
+    }
19 19
 
20
-	public function isFederationEnabled(): bool {
21
-		return $this->appConfig->getAppValueBool('enableCalendarFederation', true);
22
-	}
20
+    public function isFederationEnabled(): bool {
21
+        return $this->appConfig->getAppValueBool('enableCalendarFederation', true);
22
+    }
23 23
 }
Please login to merge, or discard this patch.