AddressBook.php 3.9 KB

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