Notifier.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\FederatedFileSharing;
  24. use OC\HintException;
  25. use OCP\Contacts\IManager;
  26. use OCP\Federation\ICloudId;
  27. use OCP\Federation\ICloudIdManager;
  28. use OCP\IURLGenerator;
  29. use OCP\L10N\IFactory;
  30. use OCP\Notification\INotification;
  31. use OCP\Notification\INotifier;
  32. class Notifier implements INotifier {
  33. /** @var IFactory */
  34. protected $factory;
  35. /** @var IManager */
  36. protected $contactsManager;
  37. /** @var IURLGenerator */
  38. protected $url;
  39. /** @var array */
  40. protected $federatedContacts;
  41. /** @var ICloudIdManager */
  42. protected $cloudIdManager;
  43. /**
  44. * @param IFactory $factory
  45. * @param IManager $contactsManager
  46. * @param IURLGenerator $url
  47. * @param ICloudIdManager $cloudIdManager
  48. */
  49. public function __construct(IFactory $factory, IManager $contactsManager, IURLGenerator $url, ICloudIdManager $cloudIdManager) {
  50. $this->factory = $factory;
  51. $this->contactsManager = $contactsManager;
  52. $this->url = $url;
  53. $this->cloudIdManager = $cloudIdManager;
  54. }
  55. /**
  56. * @param INotification $notification
  57. * @param string $languageCode The code of the language that should be used to prepare the notification
  58. * @return INotification
  59. * @throws \InvalidArgumentException
  60. */
  61. public function prepare(INotification $notification, $languageCode) {
  62. if ($notification->getApp() !== 'files_sharing') {
  63. // Not my app => throw
  64. throw new \InvalidArgumentException();
  65. }
  66. // Read the language from the notification
  67. $l = $this->factory->get('files_sharing', $languageCode);
  68. switch ($notification->getSubject()) {
  69. // Deal with known subjects
  70. case 'remote_share':
  71. $notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  72. $params = $notification->getSubjectParameters();
  73. if ($params[0] !== $params[1] && $params[1] !== null) {
  74. $notification->setParsedSubject(
  75. $l->t('You received "%3$s" as a remote share from %1$s (on behalf of %2$s)', $params)
  76. );
  77. $notification->setRichSubject(
  78. $l->t('You received {share} as a remote share from {user} (on behalf of {behalf})'),
  79. [
  80. 'share' => [
  81. 'type' => 'pending-federated-share',
  82. 'id' => $notification->getObjectId(),
  83. 'name' => $params[2],
  84. ],
  85. 'user' => $this->createRemoteUser($params[0]),
  86. 'behalf' => $this->createRemoteUser($params[1]),
  87. ]
  88. );
  89. } else {
  90. $notification->setParsedSubject(
  91. $l->t('You received "%3$s" as a remote share from %1$s', $params)
  92. );
  93. $notification->setRichSubject(
  94. $l->t('You received {share} as a remote share from {user}'),
  95. [
  96. 'share' => [
  97. 'type' => 'pending-federated-share',
  98. 'id' => $notification->getObjectId(),
  99. 'name' => $params[2],
  100. ],
  101. 'user' => $this->createRemoteUser($params[0]),
  102. ]
  103. );
  104. }
  105. // Deal with the actions for a known subject
  106. foreach ($notification->getActions() as $action) {
  107. switch ($action->getLabel()) {
  108. case 'accept':
  109. $action->setParsedLabel(
  110. (string) $l->t('Accept')
  111. )
  112. ->setPrimary(true);
  113. break;
  114. case 'decline':
  115. $action->setParsedLabel(
  116. (string) $l->t('Decline')
  117. );
  118. break;
  119. }
  120. $notification->addParsedAction($action);
  121. }
  122. return $notification;
  123. default:
  124. // Unknown subject => Unknown notification => throw
  125. throw new \InvalidArgumentException();
  126. }
  127. }
  128. /**
  129. * @param string $cloudId
  130. * @return array
  131. */
  132. protected function createRemoteUser($cloudId) {
  133. $displayName = $cloudId;
  134. try {
  135. $resolvedId = $this->cloudIdManager->resolveCloudId($cloudId);
  136. $displayName = $this->getDisplayName($resolvedId);
  137. $user = $resolvedId->getUser();
  138. $server = $resolvedId->getRemote();
  139. } catch (HintException $e) {
  140. $user = $cloudId;
  141. $server = '';
  142. }
  143. return [
  144. 'type' => 'user',
  145. 'id' => $user,
  146. 'name' => $displayName,
  147. 'server' => $server,
  148. ];
  149. }
  150. /**
  151. * Try to find the user in the contacts
  152. *
  153. * @param ICloudId $cloudId
  154. * @return string
  155. */
  156. protected function getDisplayName(ICloudId $cloudId) {
  157. $server = $cloudId->getRemote();
  158. $user = $cloudId->getUser();
  159. if (strpos($server, 'http://') === 0) {
  160. $server = substr($server, strlen('http://'));
  161. } else if (strpos($server, 'https://') === 0) {
  162. $server = substr($server, strlen('https://'));
  163. }
  164. try {
  165. return $this->getDisplayNameFromContact($cloudId->getId());
  166. } catch (\OutOfBoundsException $e) {
  167. }
  168. try {
  169. $this->getDisplayNameFromContact($user . '@http://' . $server);
  170. } catch (\OutOfBoundsException $e) {
  171. }
  172. try {
  173. $this->getDisplayNameFromContact($user . '@https://' . $server);
  174. } catch (\OutOfBoundsException $e) {
  175. }
  176. return $cloudId->getId();
  177. }
  178. /**
  179. * Try to find the user in the contacts
  180. *
  181. * @param string $federatedCloudId
  182. * @return string
  183. * @throws \OutOfBoundsException when there is no contact for the id
  184. */
  185. protected function getDisplayNameFromContact($federatedCloudId) {
  186. if (isset($this->federatedContacts[$federatedCloudId])) {
  187. if ($this->federatedContacts[$federatedCloudId] !== '') {
  188. return $this->federatedContacts[$federatedCloudId];
  189. } else {
  190. throw new \OutOfBoundsException('No contact found for federated cloud id');
  191. }
  192. }
  193. $addressBookEntries = $this->contactsManager->search($federatedCloudId, ['CLOUD']);
  194. foreach ($addressBookEntries as $entry) {
  195. if (isset($entry['CLOUD'])) {
  196. foreach ($entry['CLOUD'] as $cloudID) {
  197. if ($cloudID === $federatedCloudId) {
  198. $this->federatedContacts[$federatedCloudId] = $entry['FN'];
  199. return $entry['FN'];
  200. }
  201. }
  202. }
  203. }
  204. $this->federatedContacts[$federatedCloudId] = '';
  205. throw new \OutOfBoundsException('No contact found for federated cloud id');
  206. }
  207. }