1
0

AddressBook.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\ContactsInteraction;
  26. use Exception;
  27. use OCA\ContactsInteraction\AppInfo\Application;
  28. use OCA\ContactsInteraction\Db\RecentContact;
  29. use OCA\ContactsInteraction\Db\RecentContactMapper;
  30. use OCA\DAV\CardDAV\Integration\ExternalAddressBook;
  31. use OCA\DAV\DAV\Sharing\Plugin;
  32. use OCP\AppFramework\Db\DoesNotExistException;
  33. use OCP\IL10N;
  34. use Sabre\DAV\Exception\NotFound;
  35. use Sabre\DAV\PropPatch;
  36. use Sabre\DAVACL\ACLTrait;
  37. use Sabre\DAVACL\IACL;
  38. class AddressBook extends ExternalAddressBook implements IACL {
  39. use ACLTrait;
  40. public const URI = 'recent';
  41. public function __construct(
  42. private RecentContactMapper $mapper,
  43. private IL10N $l10n,
  44. private string $principalUri,
  45. ) {
  46. parent::__construct(Application::APP_ID, self::URI);
  47. }
  48. /**
  49. * @inheritDoc
  50. * @throws Exception
  51. */
  52. public function delete(): void {
  53. throw new Exception("This addressbook is immutable");
  54. }
  55. /**
  56. * @inheritDoc
  57. * @throws Exception
  58. */
  59. public function createFile($name, $data = null) {
  60. throw new Exception("This addressbook is immutable");
  61. }
  62. /**
  63. * @inheritDoc
  64. * @throws NotFound
  65. */
  66. public function getChild($name): Card {
  67. try {
  68. return new Card(
  69. $this->mapper->find(
  70. $this->getUid(),
  71. (int)$name
  72. ),
  73. $this->principalUri,
  74. $this->getACL()
  75. );
  76. } catch (DoesNotExistException $ex) {
  77. throw new NotFound("Contact does not exist: " . $ex->getMessage(), 0, $ex);
  78. }
  79. }
  80. /**
  81. * @inheritDoc
  82. */
  83. public function getChildren(): array {
  84. return array_map(
  85. function (RecentContact $contact) {
  86. return new Card(
  87. $contact,
  88. $this->principalUri,
  89. $this->getACL()
  90. );
  91. },
  92. $this->mapper->findAll($this->getUid())
  93. );
  94. }
  95. /**
  96. * @inheritDoc
  97. */
  98. public function childExists($name): bool {
  99. try {
  100. $this->mapper->find(
  101. $this->getUid(),
  102. (int)$name
  103. );
  104. return true;
  105. } catch (DoesNotExistException $e) {
  106. return false;
  107. }
  108. }
  109. /**
  110. * @inheritDoc
  111. */
  112. public function getLastModified(): ?int {
  113. return $this->mapper->findLastUpdatedForUserId($this->getUid());
  114. }
  115. /**
  116. * @inheritDoc
  117. * @throws Exception
  118. */
  119. public function propPatch(PropPatch $propPatch) {
  120. throw new Exception("This addressbook is immutable");
  121. }
  122. /**
  123. * @inheritDoc
  124. */
  125. public function getProperties($properties): array {
  126. return [
  127. 'principaluri' => $this->principalUri,
  128. '{DAV:}displayname' => $this->l10n->t('Recently contacted'),
  129. '{' . Plugin::NS_OWNCLOUD . '}read-only' => true,
  130. '{' . \OCA\DAV\CalDAV\Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($this->getLastModified() ?? 0),
  131. ];
  132. }
  133. public function getOwner(): string {
  134. return $this->principalUri;
  135. }
  136. /**
  137. * @inheritDoc
  138. */
  139. public function getACL(): array {
  140. return [
  141. [
  142. 'privilege' => '{DAV:}read',
  143. 'principal' => $this->getOwner(),
  144. 'protected' => true,
  145. ],
  146. ];
  147. }
  148. private function getUid(): string {
  149. [, $uid] = \Sabre\Uri\split($this->principalUri);
  150. return $uid;
  151. }
  152. }