AddressBook.php 3.9 KB

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