Notifier.php 7.6 KB

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