Notifier.php 7.7 KB

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