AddressBook.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\ContactsInteraction;
  8. use Exception;
  9. use OCA\ContactsInteraction\AppInfo\Application;
  10. use OCA\ContactsInteraction\Db\RecentContact;
  11. use OCA\ContactsInteraction\Db\RecentContactMapper;
  12. use OCA\DAV\CardDAV\Integration\ExternalAddressBook;
  13. use OCA\DAV\DAV\Sharing\Plugin;
  14. use OCP\AppFramework\Db\DoesNotExistException;
  15. use OCP\IL10N;
  16. use Sabre\DAV\Exception\NotFound;
  17. use Sabre\DAV\PropPatch;
  18. use Sabre\DAVACL\ACLTrait;
  19. use Sabre\DAVACL\IACL;
  20. class AddressBook extends ExternalAddressBook implements IACL {
  21. use ACLTrait;
  22. public const URI = 'recent';
  23. public function __construct(
  24. private RecentContactMapper $mapper,
  25. private IL10N $l10n,
  26. private string $principalUri,
  27. ) {
  28. parent::__construct(Application::APP_ID, self::URI);
  29. }
  30. /**
  31. * @inheritDoc
  32. * @throws Exception
  33. */
  34. public function delete(): void {
  35. throw new Exception('This addressbook is immutable');
  36. }
  37. /**
  38. * @inheritDoc
  39. * @throws Exception
  40. */
  41. public function createFile($name, $data = null) {
  42. throw new Exception('This addressbook is immutable');
  43. }
  44. /**
  45. * @inheritDoc
  46. * @throws NotFound
  47. */
  48. public function getChild($name): Card {
  49. try {
  50. return new Card(
  51. $this->mapper->find(
  52. $this->getUid(),
  53. (int)$name
  54. ),
  55. $this->principalUri,
  56. $this->getACL()
  57. );
  58. } catch (DoesNotExistException $ex) {
  59. throw new NotFound('Contact does not exist: ' . $ex->getMessage(), 0, $ex);
  60. }
  61. }
  62. /**
  63. * @inheritDoc
  64. */
  65. public function getChildren(): array {
  66. return array_map(
  67. function (RecentContact $contact) {
  68. return new Card(
  69. $contact,
  70. $this->principalUri,
  71. $this->getACL()
  72. );
  73. },
  74. $this->mapper->findAll($this->getUid())
  75. );
  76. }
  77. /**
  78. * @inheritDoc
  79. */
  80. public function childExists($name): bool {
  81. try {
  82. $this->mapper->find(
  83. $this->getUid(),
  84. (int)$name
  85. );
  86. return true;
  87. } catch (DoesNotExistException $e) {
  88. return false;
  89. }
  90. }
  91. /**
  92. * @inheritDoc
  93. */
  94. public function getLastModified(): ?int {
  95. return $this->mapper->findLastUpdatedForUserId($this->getUid());
  96. }
  97. /**
  98. * @inheritDoc
  99. * @throws Exception
  100. */
  101. public function propPatch(PropPatch $propPatch) {
  102. throw new Exception('This addressbook is immutable');
  103. }
  104. /**
  105. * @inheritDoc
  106. */
  107. public function getProperties($properties): array {
  108. return [
  109. 'principaluri' => $this->principalUri,
  110. '{DAV:}displayname' => $this->l10n->t('Recently contacted'),
  111. '{' . Plugin::NS_OWNCLOUD . '}read-only' => true,
  112. '{' . \OCA\DAV\CalDAV\Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($this->getLastModified() ?? 0),
  113. ];
  114. }
  115. public function getOwner(): string {
  116. return $this->principalUri;
  117. }
  118. /**
  119. * @inheritDoc
  120. */
  121. public function getACL(): array {
  122. return [
  123. [
  124. 'privilege' => '{DAV:}read',
  125. 'principal' => $this->getOwner(),
  126. 'protected' => true,
  127. ],
  128. ];
  129. }
  130. private function getUid(): string {
  131. [, $uid] = \Sabre\Uri\split($this->principalUri);
  132. return $uid;
  133. }
  134. }