1010namespace OCA \Calendar \Reference ;
1111
1212use OCA \Calendar \AppInfo \Application ;
13- use OCA \ DAV \ CalDAV \ CalDavBackend ;
13+ use OCP \ Calendar \ ICalendar ;
1414use OCP \Calendar \IManager ;
1515use OCP \Collaboration \Reference \ADiscoverableReferenceProvider ;
1616use OCP \Collaboration \Reference \IReference ;
1717use OCP \Collaboration \Reference \Reference ;
1818use OCP \IDateTimeFormatter ;
1919use OCP \IL10N ;
2020use OCP \IURLGenerator ;
21- use Sabre \VObject \Reader ;
2221
2322class EventReferenceProvider extends ADiscoverableReferenceProvider {
2423 public function __construct (
2524 private readonly IL10N $ l10n ,
2625 private readonly IURLGenerator $ urlGenerator ,
2726 private readonly IManager $ calendarManager ,
28- private CalDavBackend $ calDavBackend ,
2927 private readonly IDateTimeFormatter $ dateTimeFormatter ,
3028 private readonly ?string $ userId ,
3129 ) {
@@ -106,24 +104,24 @@ public function resolveReference(string $referenceText): ?IReference {
106104 }
107105
108106 $ calendar = $ this ->getCalendar ($ calendarUri );
109- if ($ calendar === null ) {
107+ if ($ calendar-> isDeleted () ) {
110108 return null ;
111109 }
112110
113- $ eventData = $ this ->getEventData ($ calendar[ ' id ' ] , $ eventFile );
111+ $ eventData = $ this ->getEventData ($ calendar , $ eventFile );
114112 if ($ eventData === null ) {
115113 return null ;
116114 }
117115
118116 $ reference = new Reference ($ referenceText );
119117 $ reference ->setTitle ($ eventData ['title ' ]);
120- $ reference ->setDescription ($ eventData ['date ' ] ?? $ calendar[ ' {DAV:}displayname ' ] ?? '' );
118+ $ reference ->setDescription ($ eventData ['date ' ] ?? $ calendar-> getDisplayName () );
121119 $ reference ->setRichObject (
122120 'calendar_event ' ,
123121 [
124122 'title ' => $ eventData ['title ' ],
125- 'calendarName ' => $ calendar[ ' {DAV:}displayname ' ] ?? '' ,
126- 'calendarColor ' => $ calendar[ ' {http://apple.com/ns/ical/}calendar-color ' ] ?? null ,
123+ 'calendarName ' => $ calendar-> getDisplayName () ,
124+ 'calendarColor ' => $ calendar-> getDisplayColor () ,
127125 'date ' => $ eventData ['date ' ],
128126 'startTimestamp ' => $ eventData ['startTimestamp ' ],
129127 'endTimestamp ' => $ eventData ['endTimestamp ' ],
@@ -148,50 +146,57 @@ private function getObjectIdFromUrl(string $url): ?string {
148146 return null ;
149147 }
150148
151- private function getCalendar (string $ calendarUri ): ? array {
149+ private function getCalendar (string $ calendarUri ): ICalendar {
152150 $ principalUri = 'principals/users/ ' . $ this ->userId ;
153- $ calendar = $ this ->calDavBackend ->getCalendarByUri ($ principalUri , $ calendarUri );
154- if ($ calendar === null || $ calendar ['{http://nextcloud.com/ns}deleted_at ' ] !== null ) {
155- return null ;
156- }
157- return $ calendar ;
151+ $ calendars = $ this ->calendarManager ->getCalendarsForPrincipal ($ principalUri , [$ calendarUri ]);
152+ return $ calendars [0 ];
158153 }
159154
160- private function getEventData (int $ calendarId , string $ eventFile ): ?array {
161- $ object = $ this ->calDavBackend ->getCalendarObject ($ calendarId , $ eventFile );
162- if ($ object === null ) {
155+ private function getEventData (ICalendar $ calendar , string $ eventFile ): ?array {
156+ $ event = null ;
157+ foreach ($ calendar ->search ('' ) as $ result ) {
158+ if (($ result ['uri ' ] ?? null ) === $ eventFile ) {
159+ $ event = $ result ;
160+ break ;
161+ }
162+ }
163+ if ($ event === null ) {
163164 return null ;
164165 }
165166
166- $ vObject = Reader::read ($ object ['calendardata ' ]);
167- $ vEvent = $ vObject ->VEVENT ?? null ;
168- if ($ vEvent === null ) {
167+ $ object = $ event ['objects ' ][0 ] ?? null ;
168+ if ($ object === null ) {
169169 return null ;
170+
170171 }
171172
172173 $ date = null ;
173174 $ startTimestamp = null ;
174- if (isset ($ vEvent ->DTSTART )) {
175- $ dt = $ vEvent ->DTSTART ->getDateTime ();
176- $ date = $ this ->dateTimeFormatter ->formatTimeSpan (\DateTime::createFromInterface ($ dt ));
177- $ startTimestamp = $ dt ->getTimestamp ();
175+ /** @var \DateTimeInterface|null $dtStart */
176+ $ dtStart = $ object ['DTSTART ' ][0 ] ?? null ;
177+ if ($ dtStart instanceof \DateTimeInterface) {
178+ $ date = $ this ->dateTimeFormatter ->formatTimeSpan (\DateTime::createFromInterface ($ dtStart ));
179+ $ startTimestamp = $ dtStart ->getTimestamp ();
178180 }
179181
180182 $ endTimestamp = null ;
181- if (isset ($ vEvent ->DTEND )) {
182- $ dt = $ vEvent ->DTEND ->getDateTime ();
183- $ endTimestamp = $ dt ->getTimestamp ();
184- } elseif (isset ($ vEvent ->DURATION ) && $ startTimestamp !== null ) {
185- $ duration = $ vEvent ->DURATION ->getDateInterval ();
186- $ endTimestamp = (new \DateTime ())->setTimestamp ($ startTimestamp )->add ($ duration )->getTimestamp ();
183+ /** @var \DateTimeInterface|null $dtEnd */
184+ $ dtEnd = $ object ['DTEND ' ][0 ] ?? null ;
185+ if ($ dtEnd instanceof \DateTimeInterface) {
186+ $ endTimestamp = $ dtEnd ->getTimestamp ();
187+ } elseif ($ startTimestamp !== null ) {
188+ $ duration = $ object ['DURATION ' ][0 ] ?? null ;
189+ if ($ duration instanceof \DateInterval) {
190+ $ endTimestamp = (new \DateTime ())->setTimestamp ($ startTimestamp )->add ($ duration )->getTimestamp ();
191+ }
187192 }
188193
189194 return [
190- 'title ' => isset ( $ vEvent -> SUMMARY ) ? ( string ) $ vEvent -> SUMMARY : $ this ->l10n ->t ('Untitled event ' ),
195+ 'title ' => $ object [ ' SUMMARY ' ][ 0 ] ?? $ this ->l10n ->t ('Untitled event ' ),
191196 'date ' => $ date ,
192197 'startTimestamp ' => $ startTimestamp ,
193198 'endTimestamp ' => $ endTimestamp ,
194- 'location ' => isset ( $ vEvent -> LOCATION ) ? ( string ) $ vEvent -> LOCATION : null ,
199+ 'location ' => $ object [ ' LOCATION ' ][ 0 ] ?? null ,
195200 ];
196201 }
197202
0 commit comments