1
0

AddressBook.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. private RecentContactMapper $mapper;
  42. private IL10N $l10n;
  43. private string $principalUri;
  44. public function __construct(RecentContactMapper $mapper,
  45. IL10N $l10n,
  46. string $principalUri) {
  47. parent::__construct(Application::APP_ID, self::URI);
  48. $this->mapper = $mapper;
  49. $this->l10n = $l10n;
  50. $this->principalUri = $principalUri;
  51. }
  52. /**
  53. * @inheritDoc
  54. */
  55. public function delete(): void {
  56. throw new Exception("This addressbook is immutable");
  57. }
  58. /**
  59. * @inheritDoc
  60. */
  61. public function createFile($name, $data = null) {
  62. throw new Exception("This addressbook is immutable");
  63. }
  64. /**
  65. * @inheritDoc
  66. * @throws NotFound
  67. */
  68. public function getChild($name): Card {
  69. try {
  70. return new Card(
  71. $this->mapper->find(
  72. $this->getUid(),
  73. (int)$name
  74. ),
  75. $this->principalUri,
  76. $this->getACL()
  77. );
  78. } catch (DoesNotExistException $ex) {
  79. throw new NotFound("Contact does not exist: " . $ex->getMessage(), 0, $ex);
  80. }
  81. }
  82. /**
  83. * @inheritDoc
  84. */
  85. public function getChildren(): array {
  86. return array_map(
  87. function (RecentContact $contact) {
  88. return new Card(
  89. $contact,
  90. $this->principalUri,
  91. $this->getACL()
  92. );
  93. },
  94. $this->mapper->findAll($this->getUid())
  95. );
  96. }
  97. /**
  98. * @inheritDoc
  99. */
  100. public function childExists($name): bool {
  101. try {
  102. $this->mapper->find(
  103. $this->getUid(),
  104. (int)$name
  105. );
  106. return true;
  107. } catch (DoesNotExistException $e) {
  108. return false;
  109. }
  110. }
  111. /**
  112. * @inheritDoc
  113. */
  114. public function getLastModified(): ?int {
  115. return $this->mapper->findLastUpdatedForUserId($this->getUid());
  116. }
  117. /**
  118. * @inheritDoc
  119. */
  120. public function propPatch(PropPatch $propPatch) {
  121. throw new Exception("This addressbook is immutable");
  122. }
  123. /**
  124. * @inheritDoc
  125. */
  126. public function getProperties($properties) {
  127. return [
  128. 'principaluri' => $this->principalUri,
  129. '{DAV:}displayname' => $this->l10n->t('Recently contacted'),
  130. '{' . Plugin::NS_OWNCLOUD . '}read-only' => true,
  131. '{' . \OCA\DAV\CalDAV\Plugin::NS_CALENDARSERVER . '}getctag' => 'http://sabre.io/ns/sync/' . ($this->getLastModified() ?? 0),
  132. ];
  133. }
  134. public function getOwner(): string {
  135. return $this->principalUri;
  136. }
  137. /**
  138. * @inheritDoc
  139. */
  140. public function getACL(): array {
  141. return [
  142. [
  143. 'privilege' => '{DAV:}read',
  144. 'principal' => $this->getOwner(),
  145. 'protected' => true,
  146. ],
  147. ];
  148. }
  149. private function getUid(): string {
  150. [, $uid] = \Sabre\Uri\split($this->principalUri);
  151. return $uid;
  152. }
  153. }