Notifier.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. * Identifier of the notifier, only use [a-z0-9_]
  58. *
  59. * @return string
  60. * @since 17.0.0
  61. */
  62. public function getID(): string {
  63. return 'federatedfilesharing';
  64. }
  65. /**
  66. * Human readable name describing the notifier
  67. *
  68. * @return string
  69. * @since 17.0.0
  70. */
  71. public function getName(): string {
  72. return $this->factory->get('federatedfilesharing')->t('Federated sharing');
  73. }
  74. /**
  75. * @param INotification $notification
  76. * @param string $languageCode The code of the language that should be used to prepare the notification
  77. * @return INotification
  78. * @throws \InvalidArgumentException
  79. */
  80. public function prepare(INotification $notification, string $languageCode): INotification {
  81. if ($notification->getApp() !== 'files_sharing') {
  82. // Not my app => throw
  83. throw new \InvalidArgumentException();
  84. }
  85. // Read the language from the notification
  86. $l = $this->factory->get('files_sharing', $languageCode);
  87. switch ($notification->getSubject()) {
  88. // Deal with known subjects
  89. case 'remote_share':
  90. $notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  91. $params = $notification->getSubjectParameters();
  92. if ($params[0] !== $params[1] && $params[1] !== null) {
  93. $notification->setParsedSubject(
  94. $l->t('You received "%3$s" as a remote share from %1$s (on behalf of %2$s)', $params)
  95. );
  96. $notification->setRichSubject(
  97. $l->t('You received {share} as a remote share from {user} (on behalf of {behalf})'),
  98. [
  99. 'share' => [
  100. 'type' => 'pending-federated-share',
  101. 'id' => $notification->getObjectId(),
  102. 'name' => $params[2],
  103. ],
  104. 'user' => $this->createRemoteUser($params[0]),
  105. 'behalf' => $this->createRemoteUser($params[1]),
  106. ]
  107. );
  108. } else {
  109. $notification->setParsedSubject(
  110. $l->t('You received "%3$s" as a remote share from %1$s', $params)
  111. );
  112. $notification->setRichSubject(
  113. $l->t('You received {share} as a remote share from {user}'),
  114. [
  115. 'share' => [
  116. 'type' => 'pending-federated-share',
  117. 'id' => $notification->getObjectId(),
  118. 'name' => $params[2],
  119. ],
  120. 'user' => $this->createRemoteUser($params[0]),
  121. ]
  122. );
  123. }
  124. // Deal with the actions for a known subject
  125. foreach ($notification->getActions() as $action) {
  126. switch ($action->getLabel()) {
  127. case 'accept':
  128. $action->setParsedLabel(
  129. (string) $l->t('Accept')
  130. )
  131. ->setPrimary(true);
  132. break;
  133. case 'decline':
  134. $action->setParsedLabel(
  135. (string) $l->t('Decline')
  136. );
  137. break;
  138. }
  139. $notification->addParsedAction($action);
  140. }
  141. return $notification;
  142. default:
  143. // Unknown subject => Unknown notification => throw
  144. throw new \InvalidArgumentException();
  145. }
  146. }
  147. /**
  148. * @param string $cloudId
  149. * @return array
  150. */
  151. protected function createRemoteUser($cloudId) {
  152. $displayName = $cloudId;
  153. try {
  154. $resolvedId = $this->cloudIdManager->resolveCloudId($cloudId);
  155. $displayName = $this->getDisplayName($resolvedId);
  156. $user = $resolvedId->getUser();
  157. $server = $resolvedId->getRemote();
  158. } catch (HintException $e) {
  159. $user = $cloudId;
  160. $server = '';
  161. }
  162. return [
  163. 'type' => 'user',
  164. 'id' => $user,
  165. 'name' => $displayName,
  166. 'server' => $server,
  167. ];
  168. }
  169. /**
  170. * Try to find the user in the contacts
  171. *
  172. * @param ICloudId $cloudId
  173. * @return string
  174. */
  175. protected function getDisplayName(ICloudId $cloudId) {
  176. $server = $cloudId->getRemote();
  177. $user = $cloudId->getUser();
  178. if (strpos($server, 'http://') === 0) {
  179. $server = substr($server, strlen('http://'));
  180. } else if (strpos($server, 'https://') === 0) {
  181. $server = substr($server, strlen('https://'));
  182. }
  183. try {
  184. return $this->getDisplayNameFromContact($cloudId->getId());
  185. } catch (\OutOfBoundsException $e) {
  186. }
  187. try {
  188. $this->getDisplayNameFromContact($user . '@http://' . $server);
  189. } catch (\OutOfBoundsException $e) {
  190. }
  191. try {
  192. $this->getDisplayNameFromContact($user . '@https://' . $server);
  193. } catch (\OutOfBoundsException $e) {
  194. }
  195. return $cloudId->getId();
  196. }
  197. /**
  198. * Try to find the user in the contacts
  199. *
  200. * @param string $federatedCloudId
  201. * @return string
  202. * @throws \OutOfBoundsException when there is no contact for the id
  203. */
  204. protected function getDisplayNameFromContact($federatedCloudId) {
  205. if (isset($this->federatedContacts[$federatedCloudId])) {
  206. if ($this->federatedContacts[$federatedCloudId] !== '') {
  207. return $this->federatedContacts[$federatedCloudId];
  208. } else {
  209. throw new \OutOfBoundsException('No contact found for federated cloud id');
  210. }
  211. }
  212. $addressBookEntries = $this->contactsManager->search($federatedCloudId, ['CLOUD']);
  213. foreach ($addressBookEntries as $entry) {
  214. if (isset($entry['CLOUD'])) {
  215. foreach ($entry['CLOUD'] as $cloudID) {
  216. if ($cloudID === $federatedCloudId) {
  217. $this->federatedContacts[$federatedCloudId] = $entry['FN'];
  218. return $entry['FN'];
  219. }
  220. }
  221. }
  222. }
  223. $this->federatedContacts[$federatedCloudId] = '';
  224. throw new \OutOfBoundsException('No contact found for federated cloud id');
  225. }
  226. }