Notifier.php 6.5 KB

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