|
|
|
|
@ -23,6 +23,7 @@ namespace OCA\DAV\CalDAV; |
|
|
|
|
|
|
|
|
|
use Exception; |
|
|
|
|
use OCA\DAV\CardDAV\CardDavBackend; |
|
|
|
|
use OCA\DAV\DAV\GroupPrincipalBackend; |
|
|
|
|
use Sabre\VObject\Component\VCalendar; |
|
|
|
|
use Sabre\VObject\Reader; |
|
|
|
|
|
|
|
|
|
@ -30,15 +31,20 @@ class BirthdayService { |
|
|
|
|
|
|
|
|
|
const BIRTHDAY_CALENDAR_URI = 'contact_birthdays'; |
|
|
|
|
|
|
|
|
|
/** @var GroupPrincipalBackend */ |
|
|
|
|
private $principalBackend; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* BirthdayService constructor. |
|
|
|
|
* |
|
|
|
|
* @param CalDavBackend $calDavBackEnd |
|
|
|
|
* @param CardDavBackend $cardDavBackEnd |
|
|
|
|
* @param GroupPrincipalBackend $principalBackend |
|
|
|
|
*/ |
|
|
|
|
public function __construct($calDavBackEnd, $cardDavBackEnd) { |
|
|
|
|
public function __construct($calDavBackEnd, $cardDavBackEnd, $principalBackend) { |
|
|
|
|
$this->calDavBackEnd = $calDavBackEnd; |
|
|
|
|
$this->cardDavBackEnd = $cardDavBackEnd; |
|
|
|
|
$this->principalBackend = $principalBackend; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -48,22 +54,26 @@ class BirthdayService { |
|
|
|
|
*/ |
|
|
|
|
public function onCardChanged($addressBookId, $cardUri, $cardData) { |
|
|
|
|
|
|
|
|
|
$targetPrincipals = $this->getAllAffectedPrincipals($addressBookId); |
|
|
|
|
|
|
|
|
|
$book = $this->cardDavBackEnd->getAddressBookById($addressBookId); |
|
|
|
|
$principalUri = $book['principaluri']; |
|
|
|
|
$calendar = $this->ensureCalendarExists($principalUri); |
|
|
|
|
$objectUri = $book['uri'] . '-' . $cardUri. '.ics'; |
|
|
|
|
$calendarData = $this->buildBirthdayFromContact($cardData); |
|
|
|
|
$existing = $this->calDavBackEnd->getCalendarObject($calendar['id'], $objectUri); |
|
|
|
|
if (is_null($calendarData)) { |
|
|
|
|
if (!is_null($existing)) { |
|
|
|
|
$this->calDavBackEnd->deleteCalendarObject($calendar['id'], $objectUri); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
if (is_null($existing)) { |
|
|
|
|
$this->calDavBackEnd->createCalendarObject($calendar['id'], $objectUri, $calendarData->serialize()); |
|
|
|
|
$targetPrincipals[] = $book['principaluri']; |
|
|
|
|
foreach ($targetPrincipals as $principalUri) { |
|
|
|
|
$calendar = $this->ensureCalendarExists($principalUri); |
|
|
|
|
$objectUri = $book['uri'] . '-' . $cardUri. '.ics'; |
|
|
|
|
$calendarData = $this->buildBirthdayFromContact($cardData); |
|
|
|
|
$existing = $this->calDavBackEnd->getCalendarObject($calendar['id'], $objectUri); |
|
|
|
|
if (is_null($calendarData)) { |
|
|
|
|
if (!is_null($existing)) { |
|
|
|
|
$this->calDavBackEnd->deleteCalendarObject($calendar['id'], $objectUri); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
if ($this->birthdayEvenChanged($existing['calendardata'], $calendarData)) { |
|
|
|
|
$this->calDavBackEnd->updateCalendarObject($calendar['id'], $objectUri, $calendarData->serialize()); |
|
|
|
|
if (is_null($existing)) { |
|
|
|
|
$this->calDavBackEnd->createCalendarObject($calendar['id'], $objectUri, $calendarData->serialize()); |
|
|
|
|
} else { |
|
|
|
|
if ($this->birthdayEvenChanged($existing['calendardata'], $calendarData)) { |
|
|
|
|
$this->calDavBackEnd->updateCalendarObject($calendar['id'], $objectUri, $calendarData->serialize()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -74,11 +84,14 @@ class BirthdayService { |
|
|
|
|
* @param string $cardUri |
|
|
|
|
*/ |
|
|
|
|
public function onCardDeleted($addressBookId, $cardUri) { |
|
|
|
|
$targetPrincipals = $this->getAllAffectedPrincipals($addressBookId); |
|
|
|
|
$book = $this->cardDavBackEnd->getAddressBookById($addressBookId); |
|
|
|
|
$principalUri = $book['principaluri']; |
|
|
|
|
$calendar = $this->ensureCalendarExists($principalUri); |
|
|
|
|
$objectUri = $book['uri'] . '-' . $cardUri. '.ics'; |
|
|
|
|
$this->calDavBackEnd->deleteCalendarObject($calendar['id'], $objectUri); |
|
|
|
|
$targetPrincipals[] = $book['principaluri']; |
|
|
|
|
foreach ($targetPrincipals as $principalUri) { |
|
|
|
|
$calendar = $this->ensureCalendarExists($principalUri); |
|
|
|
|
$objectUri = $book['uri'] . '-' . $cardUri . '.ics'; |
|
|
|
|
$this->calDavBackEnd->deleteCalendarObject($calendar['id'], $objectUri); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -190,4 +203,24 @@ class BirthdayService { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @param $addressBookId |
|
|
|
|
* @return mixed |
|
|
|
|
*/ |
|
|
|
|
protected function getAllAffectedPrincipals($addressBookId) { |
|
|
|
|
$targetPrincipals = []; |
|
|
|
|
$shares = $this->cardDavBackEnd->getShares($addressBookId); |
|
|
|
|
foreach ($shares as $share) { |
|
|
|
|
if ($share['{http://owncloud.org/ns}group-share']) { |
|
|
|
|
$users = $this->principalBackend->getGroupMemberSet($share['{http://owncloud.org/ns}principal']); |
|
|
|
|
foreach ($users as $user) { |
|
|
|
|
$targetPrincipals[] = $user['uri']; |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
$targetPrincipals[] = $share['{http://owncloud.org/ns}principal']; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return array_values(array_unique($targetPrincipals, SORT_STRING)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|