You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Notifier.php 7.8KB

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