99
1010namespace OCA \DAV \CalDAV \Reminder ;
1111
12+ use OCA \DAV \DAV \Sharing \Plugin ;
13+ use OCA \DAV \Db \PropertyMapper ;
1214use OCP \AppFramework \Utility \ITimeFactory ;
1315use OCP \IDBConnection ;
16+ use Sabre \Uri ;
1417
1518/**
1619 * Class Backend
1922 */
2023class Backend {
2124
25+ /**
26+ * WebDAV property a sharee sets on their own view of a shared calendar
27+ * to mute reminders, stored via the generic custom-properties backend.
28+ */
29+ private const string PROPERTY_DISABLE_ALARM_NOTIFICATIONS = '{ ' . Plugin::NS_NEXTCLOUD . '}disable-alarm-notifications ' ;
30+
2231 /**
2332 * Backend constructor.
2433 *
2534 * @param IDBConnection $db
2635 * @param ITimeFactory $timeFactory
36+ * @param PropertyMapper $propertyMapper
2737 */
2838 public function __construct (
2939 protected IDBConnection $ db ,
3040 protected ITimeFactory $ timeFactory ,
41+ private PropertyMapper $ propertyMapper ,
3142 ) {
3243 }
3344
@@ -39,12 +50,12 @@ public function __construct(
3950 */
4051 public function getRemindersToProcess ():array {
4152 $ query = $ this ->db ->getQueryBuilder ();
42- $ query ->select (['cr.id ' , 'cr.calendar_id ' ,'cr.object_id ' ,'cr.is_recurring ' ,'cr.uid ' ,'cr.recurrence_id ' ,'cr.is_recurrence_exception ' ,'cr.event_hash ' ,'cr.alarm_hash ' ,'cr.type ' ,'cr.is_relative ' ,'cr.notification_date ' ,'cr.is_repeat_based ' ,'co.calendardata ' , 'c.displayname ' , 'c.principaluri ' ])
53+ $ query ->select (['cr.id ' , 'cr.calendar_id ' ,'cr.object_id ' ,'cr.is_recurring ' ,'cr.uid ' ,'cr.recurrence_id ' ,'cr.is_recurrence_exception ' ,'cr.event_hash ' ,'cr.alarm_hash ' ,'cr.type ' ,'cr.is_relative ' ,'cr.notification_date ' ,'cr.is_repeat_based ' ,'co.calendardata ' , 'c.displayname ' , 'c.principaluri ' , ' c.uri ' , ' c.disable_alarm_notifications ' ])
4354 ->from ('calendar_reminders ' , 'cr ' )
4455 ->where ($ query ->expr ()->lte ('cr.notification_date ' , $ query ->createNamedParameter ($ this ->timeFactory ->getTime ())))
4556 ->join ('cr ' , 'calendarobjects ' , 'co ' , $ query ->expr ()->eq ('cr.object_id ' , 'co.id ' ))
4657 ->join ('cr ' , 'calendars ' , 'c ' , $ query ->expr ()->eq ('cr.calendar_id ' , 'c.id ' ))
47- ->groupBy ('cr.event_hash ' , 'cr.notification_date ' , 'cr.type ' , 'cr.id ' , 'cr.calendar_id ' , 'cr.object_id ' , 'cr.is_recurring ' , 'cr.uid ' , 'cr.recurrence_id ' , 'cr.is_recurrence_exception ' , 'cr.alarm_hash ' , 'cr.is_relative ' , 'cr.is_repeat_based ' , 'co.calendardata ' , 'c.displayname ' , 'c.principaluri ' );
58+ ->groupBy ('cr.event_hash ' , 'cr.notification_date ' , 'cr.type ' , 'cr.id ' , 'cr.calendar_id ' , 'cr.object_id ' , 'cr.is_recurring ' , 'cr.uid ' , 'cr.recurrence_id ' , 'cr.is_recurrence_exception ' , 'cr.alarm_hash ' , 'cr.is_relative ' , 'cr.is_repeat_based ' , 'co.calendardata ' , 'c.displayname ' , 'c.principaluri ' , ' c.uri ' , ' c.disable_alarm_notifications ' );
4859 $ stmt = $ query ->executeQuery ();
4960
5061 return array_map (
@@ -53,6 +64,35 @@ public function getRemindersToProcess():array {
5364 );
5465 }
5566
67+ /**
68+ * Whether the given principal has muted reminders for this reminder's calendar.
69+ *
70+ * The calendar owner's setting is stored directly on the `calendars` row (already
71+ * part of $reminder). A sharee's setting is their own WebDAV property on their view
72+ * of the shared calendar, stored via the generic custom-properties backend.
73+ *
74+ * @param array $reminder A row as returned by getRemindersToProcess()
75+ */
76+ public function isReminderMutedForPrincipal (array $ reminder , string $ principalUri ): bool {
77+ if ($ principalUri === $ reminder ['principaluri ' ]) {
78+ return $ reminder ['disable_alarm_notifications ' ];
79+ }
80+
81+ [, $ shareeUid ] = Uri \split ($ principalUri );
82+ [, $ ownerUid ] = Uri \split ($ reminder ['principaluri ' ]);
83+ if ($ shareeUid === null || $ ownerUid === null ) {
84+ return false ;
85+ }
86+
87+ $ path = 'calendars/ ' . $ shareeUid . '/ ' . $ reminder ['uri ' ] . '_shared_by_ ' . $ ownerUid ;
88+ $ properties = $ this ->propertyMapper ->findPropertyByPathAndName ($ shareeUid , $ path , self ::PROPERTY_DISABLE_ALARM_NOTIFICATIONS );
89+ if (empty ($ properties )) {
90+ return false ;
91+ }
92+
93+ return $ properties [0 ]->getPropertyvalue () === '1 ' ;
94+ }
95+
5696 /**
5797 * Get all scheduled reminders for an event
5898 *
@@ -192,6 +232,9 @@ private function fixRowTyping(array $row): array {
192232 $ row ['is_relative ' ] = (bool )$ row ['is_relative ' ];
193233 $ row ['notification_date ' ] = (int )$ row ['notification_date ' ];
194234 $ row ['is_repeat_based ' ] = (bool )$ row ['is_repeat_based ' ];
235+ if (array_key_exists ('disable_alarm_notifications ' , $ row )) {
236+ $ row ['disable_alarm_notifications ' ] = (bool )$ row ['disable_alarm_notifications ' ];
237+ }
195238
196239 return $ row ;
197240 }
0 commit comments