Notifier.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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('federatedfilesharing', $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. $displayName = (count($params) > 3) ? $params[3] : '';
  97. if ($params[0] !== $params[1] && $params[1] !== null) {
  98. $remoteInitiator = $this->createRemoteUser($params[0], $displayName);
  99. $remoteOwner = $this->createRemoteUser($params[1]);
  100. $params[3] = $remoteInitiator['name'] . '@' . $remoteInitiator['server'];
  101. $params[4] = $remoteOwner['name'] . '@' . $remoteOwner['server'];
  102. $notification->setRichSubject(
  103. $l->t('You received {share} as a remote share from {user} (on behalf of {behalf})'),
  104. [
  105. 'share' => [
  106. 'type' => 'pending-federated-share',
  107. 'id' => $notification->getObjectId(),
  108. 'name' => $params[2],
  109. ],
  110. 'user' => $remoteInitiator,
  111. 'behalf' => $remoteOwner,
  112. ]
  113. );
  114. } else {
  115. $remoteOwner = $this->createRemoteUser($params[0], $displayName);
  116. $params[3] = $remoteOwner['name'] . '@' . $remoteOwner['server'];
  117. $notification->setRichSubject(
  118. $l->t('You received {share} as a remote share from {user}'),
  119. [
  120. 'share' => [
  121. 'type' => 'pending-federated-share',
  122. 'id' => $notification->getObjectId(),
  123. 'name' => $params[2],
  124. ],
  125. 'user' => $remoteOwner,
  126. ]
  127. );
  128. }
  129. // Deal with the actions for a known subject
  130. foreach ($notification->getActions() as $action) {
  131. switch ($action->getLabel()) {
  132. case 'accept':
  133. $action->setParsedLabel(
  134. $l->t('Accept')
  135. )
  136. ->setPrimary(true);
  137. break;
  138. case 'decline':
  139. $action->setParsedLabel(
  140. $l->t('Decline')
  141. );
  142. break;
  143. }
  144. $notification->addParsedAction($action);
  145. }
  146. return $notification;
  147. default:
  148. // Unknown subject => Unknown notification => throw
  149. throw new \InvalidArgumentException();
  150. }
  151. }
  152. /**
  153. * @param string $cloudId
  154. * @param string $displayName - overwrite display name
  155. *
  156. * @return array
  157. */
  158. protected function createRemoteUser(string $cloudId, string $displayName = '') {
  159. try {
  160. $resolvedId = $this->cloudIdManager->resolveCloudId($cloudId);
  161. if ($displayName === '') {
  162. $displayName = $this->getDisplayName($resolvedId);
  163. }
  164. $user = $resolvedId->getUser();
  165. $server = $resolvedId->getRemote();
  166. } catch (HintException $e) {
  167. $user = $cloudId;
  168. $displayName = ($displayName !== '') ? $displayName : $cloudId;
  169. $server = '';
  170. }
  171. return [
  172. 'type' => 'user',
  173. 'id' => $user,
  174. 'name' => $displayName,
  175. 'server' => $server,
  176. ];
  177. }
  178. /**
  179. * Try to find the user in the contacts
  180. *
  181. * @param ICloudId $cloudId
  182. * @return string
  183. */
  184. protected function getDisplayName(ICloudId $cloudId): string {
  185. $server = $cloudId->getRemote();
  186. $user = $cloudId->getUser();
  187. if (str_starts_with($server, 'http://')) {
  188. $server = substr($server, strlen('http://'));
  189. } elseif (str_starts_with($server, 'https://')) {
  190. $server = substr($server, strlen('https://'));
  191. }
  192. try {
  193. // contains protocol in the ID
  194. return $this->getDisplayNameFromContact($cloudId->getId());
  195. } catch (\OutOfBoundsException $e) {
  196. }
  197. try {
  198. // does not include protocol, as stored in addressbooks
  199. return $this->getDisplayNameFromContact($cloudId->getDisplayId());
  200. } catch (\OutOfBoundsException $e) {
  201. }
  202. try {
  203. return $this->getDisplayNameFromContact($user . '@http://' . $server);
  204. } catch (\OutOfBoundsException $e) {
  205. }
  206. try {
  207. return $this->getDisplayNameFromContact($user . '@https://' . $server);
  208. } catch (\OutOfBoundsException $e) {
  209. }
  210. return $cloudId->getId();
  211. }
  212. /**
  213. * Try to find the user in the contacts
  214. *
  215. * @param string $federatedCloudId
  216. * @return string
  217. * @throws \OutOfBoundsException when there is no contact for the id
  218. */
  219. protected function getDisplayNameFromContact($federatedCloudId) {
  220. if (isset($this->federatedContacts[$federatedCloudId])) {
  221. if ($this->federatedContacts[$federatedCloudId] !== '') {
  222. return $this->federatedContacts[$federatedCloudId];
  223. } else {
  224. throw new \OutOfBoundsException('No contact found for federated cloud id');
  225. }
  226. }
  227. $addressBookEntries = $this->contactsManager->search($federatedCloudId, ['CLOUD'], [
  228. 'limit' => 1,
  229. 'enumeration' => false,
  230. 'fullmatch' => false,
  231. 'strict_search' => true,
  232. ]);
  233. foreach ($addressBookEntries as $entry) {
  234. if (isset($entry['CLOUD'])) {
  235. foreach ($entry['CLOUD'] as $cloudID) {
  236. if ($cloudID === $federatedCloudId) {
  237. $this->federatedContacts[$federatedCloudId] = $entry['FN'];
  238. return $entry['FN'];
  239. }
  240. }
  241. }
  242. }
  243. $this->federatedContacts[$federatedCloudId] = '';
  244. throw new \OutOfBoundsException('No contact found for federated cloud id');
  245. }
  246. }