You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AddressBook.php 3.8KB

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\Exception\NotImplemented;
  34. use Sabre\DAV\PropPatch;
  35. use Sabre\DAVACL\ACLTrait;
  36. use Sabre\DAVACL\IACL;
  37. class AddressBook extends ExternalAddressBook implements IACL {
  38. public const URI = 'recent';
  39. use ACLTrait;
  40. /** @var RecentContactMapper */
  41. private $mapper;
  42. /** @var IL10N */
  43. private $l10n;
  44. /** @var string */
  45. private $principalUri;
  46. public function __construct(RecentContactMapper $mapper,
  47. IL10N $l10n,
  48. string $principalUri) {
  49. parent::__construct(Application::APP_ID, self::URI);
  50. $this->mapper = $mapper;
  51. $this->l10n = $l10n;
  52. $this->principalUri = $principalUri;
  53. }
  54. /**
  55. * @inheritDoc
  56. */
  57. public function delete(): void {
  58. throw new Exception("This addressbook is immutable");
  59. }
  60. /**
  61. * @inheritDoc
  62. */
  63. function createFile($name, $data = null) {
  64. throw new Exception("This addressbook is immutable");
  65. }
  66. /**
  67. * @inheritDoc
  68. * @throws NotFound
  69. */
  70. public function getChild($name) {
  71. try {
  72. return new Card(
  73. $this->mapper->find(
  74. $this->getUid(),
  75. (int)$name
  76. ),
  77. $this->principalUri,
  78. $this->getACL()
  79. );
  80. } catch (DoesNotExistException $ex) {
  81. throw new NotFound("Contact does not exist: " . $ex->getMessage(), 0, $ex);
  82. }
  83. }
  84. /**
  85. * @inheritDoc
  86. */
  87. public function getChildren(): array {
  88. return array_map(
  89. function (RecentContact $contact) {
  90. return new Card(
  91. $contact,
  92. $this->principalUri,
  93. $this->getACL()
  94. );
  95. },
  96. $this->mapper->findAll($this->getUid())
  97. );
  98. }
  99. /**
  100. * @inheritDoc
  101. */
  102. public function childExists($name) {
  103. try {
  104. $this->mapper->find(
  105. $this->getUid(),
  106. (int)$name
  107. );
  108. return true;
  109. } catch (DoesNotExistException $e) {
  110. return false;
  111. }
  112. }
  113. /**
  114. * @inheritDoc
  115. */
  116. public function getLastModified() {
  117. throw new NotImplemented();
  118. }
  119. /**
  120. * @inheritDoc
  121. */
  122. public function propPatch(PropPatch $propPatch) {
  123. throw new Exception("This addressbook is immutable");
  124. }
  125. /**
  126. * @inheritDoc
  127. */
  128. public function getProperties($properties) {
  129. return [
  130. 'principaluri' => $this->principalUri,
  131. '{DAV:}displayname' => $this->l10n->t('Recently contacted'),
  132. '{' . Plugin::NS_OWNCLOUD . '}read-only' => true,
  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. }