1
0

Activity.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\ShareByMail;
  7. use OCP\Activity\IEvent;
  8. use OCP\Activity\IManager;
  9. use OCP\Activity\IProvider;
  10. use OCP\Contacts\IManager as IContactsManager;
  11. use OCP\IL10N;
  12. use OCP\IURLGenerator;
  13. use OCP\IUserManager;
  14. use OCP\L10N\IFactory;
  15. class Activity implements IProvider {
  16. /** @var IFactory */
  17. protected $languageFactory;
  18. /** @var IL10N */
  19. protected $l;
  20. /** @var IURLGenerator */
  21. protected $url;
  22. /** @var IManager */
  23. protected $activityManager;
  24. /** @var IUserManager */
  25. protected $userManager;
  26. /** @var IContactsManager */
  27. protected $contactsManager;
  28. /** @var array */
  29. protected $contactNames = [];
  30. public const SUBJECT_SHARED_EMAIL_SELF = 'shared_with_email_self';
  31. public const SUBJECT_SHARED_EMAIL_BY = 'shared_with_email_by';
  32. public const SUBJECT_SHARED_EMAIL_PASSWORD_SEND = 'shared_with_email_password_send';
  33. public const SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF = 'shared_with_email_password_send_self';
  34. public const SUBJECT_UNSHARED_EMAIL_SELF = 'unshared_with_email_self';
  35. public const SUBJECT_UNSHARED_EMAIL_BY = 'unshared_with_email_by';
  36. /**
  37. * @param IFactory $languageFactory
  38. * @param IURLGenerator $url
  39. * @param IManager $activityManager
  40. * @param IUserManager $userManager
  41. * @param IContactsManager $contactsManager
  42. */
  43. public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IContactsManager $contactsManager) {
  44. $this->languageFactory = $languageFactory;
  45. $this->url = $url;
  46. $this->activityManager = $activityManager;
  47. $this->userManager = $userManager;
  48. $this->contactsManager = $contactsManager;
  49. }
  50. /**
  51. * @param string $language
  52. * @param IEvent $event
  53. * @param IEvent|null $previousEvent
  54. * @return IEvent
  55. * @throws \InvalidArgumentException
  56. * @since 11.0.0
  57. */
  58. public function parse($language, IEvent $event, ?IEvent $previousEvent = null) {
  59. if ($event->getApp() !== 'sharebymail') {
  60. throw new \InvalidArgumentException();
  61. }
  62. $this->l = $this->languageFactory->get('sharebymail', $language);
  63. if ($this->activityManager->isFormattingFilteredObject()) {
  64. try {
  65. return $this->parseShortVersion($event);
  66. } catch (\InvalidArgumentException $e) {
  67. // Ignore and simply use the long version...
  68. }
  69. }
  70. return $this->parseLongVersion($event);
  71. }
  72. /**
  73. * @param IEvent $event
  74. * @return IEvent
  75. * @throws \InvalidArgumentException
  76. * @since 11.0.0
  77. */
  78. public function parseShortVersion(IEvent $event) {
  79. $parsedParameters = $this->getParsedParameters($event);
  80. if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_SELF) {
  81. $event->setRichSubject($this->l->t('Shared with {email}'), [
  82. 'email' => $parsedParameters['email'],
  83. ]);
  84. if ($this->activityManager->getRequirePNG()) {
  85. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  86. } else {
  87. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  88. }
  89. } elseif ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_BY) {
  90. $event->setRichSubject($this->l->t('Shared with {email} by {actor}'), [
  91. 'email' => $parsedParameters['email'],
  92. 'actor' => $parsedParameters['actor'],
  93. ]);
  94. if ($this->activityManager->getRequirePNG()) {
  95. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  96. } else {
  97. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  98. }
  99. } elseif ($event->getSubject() === self::SUBJECT_UNSHARED_EMAIL_SELF) {
  100. $event->setRichSubject($this->l->t('Unshared from {email}'), [
  101. 'email' => $parsedParameters['email'],
  102. ]);
  103. if ($this->activityManager->getRequirePNG()) {
  104. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  105. } else {
  106. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  107. }
  108. } elseif ($event->getSubject() === self::SUBJECT_UNSHARED_EMAIL_BY) {
  109. $event->setRichSubject($this->l->t('Unshared from {email} by {actor}'), [
  110. 'email' => $parsedParameters['email'],
  111. 'actor' => $parsedParameters['actor'],
  112. ]);
  113. if ($this->activityManager->getRequirePNG()) {
  114. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  115. } else {
  116. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  117. }
  118. } elseif ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND) {
  119. $event->setRichSubject($this->l->t('Password for mail share sent to {email}'), [
  120. 'email' => $parsedParameters['email']
  121. ]);
  122. if ($this->activityManager->getRequirePNG()) {
  123. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  124. } else {
  125. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  126. }
  127. } elseif ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF) {
  128. $event->setRichSubject($this->l->t('Password for mail share sent to you'));
  129. if ($this->activityManager->getRequirePNG()) {
  130. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  131. } else {
  132. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  133. }
  134. } else {
  135. throw new \InvalidArgumentException();
  136. }
  137. return $event;
  138. }
  139. /**
  140. * @param IEvent $event
  141. * @return IEvent
  142. * @throws \InvalidArgumentException
  143. * @since 11.0.0
  144. */
  145. public function parseLongVersion(IEvent $event) {
  146. $parsedParameters = $this->getParsedParameters($event);
  147. if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_SELF) {
  148. $event->setRichSubject($this->l->t('You shared {file} with {email} by mail'), $parsedParameters);
  149. if ($this->activityManager->getRequirePNG()) {
  150. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  151. } else {
  152. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  153. }
  154. } elseif ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_BY) {
  155. $event->setRichSubject($this->l->t('{actor} shared {file} with {email} by mail'), $parsedParameters);
  156. if ($this->activityManager->getRequirePNG()) {
  157. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  158. } else {
  159. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  160. }
  161. } elseif ($event->getSubject() === self::SUBJECT_UNSHARED_EMAIL_SELF) {
  162. $event->setRichSubject($this->l->t('You unshared {file} from {email} by mail'), $parsedParameters);
  163. if ($this->activityManager->getRequirePNG()) {
  164. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  165. } else {
  166. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  167. }
  168. } elseif ($event->getSubject() === self::SUBJECT_UNSHARED_EMAIL_BY) {
  169. $event->setRichSubject($this->l->t('{actor} unshared {file} from {email} by mail'), $parsedParameters);
  170. if ($this->activityManager->getRequirePNG()) {
  171. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  172. } else {
  173. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  174. }
  175. } elseif ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND) {
  176. $event->setRichSubject($this->l->t('Password to access {file} was sent to {email}'), $parsedParameters);
  177. if ($this->activityManager->getRequirePNG()) {
  178. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  179. } else {
  180. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  181. }
  182. } elseif ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF) {
  183. $event->setRichSubject($this->l->t('Password to access {file} was sent to you'), $parsedParameters);
  184. if ($this->activityManager->getRequirePNG()) {
  185. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  186. } else {
  187. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  188. }
  189. } else {
  190. throw new \InvalidArgumentException();
  191. }
  192. return $event;
  193. }
  194. protected function getParsedParameters(IEvent $event) {
  195. $subject = $event->getSubject();
  196. $parameters = $event->getSubjectParameters();
  197. switch ($subject) {
  198. case self::SUBJECT_SHARED_EMAIL_SELF:
  199. case self::SUBJECT_UNSHARED_EMAIL_SELF:
  200. return [
  201. 'file' => $this->generateFileParameter($event->getObjectId(), $parameters[0]),
  202. 'email' => $this->generateEmailParameter($parameters[1]),
  203. ];
  204. case self::SUBJECT_SHARED_EMAIL_BY:
  205. case self::SUBJECT_UNSHARED_EMAIL_BY:
  206. return [
  207. 'file' => $this->generateFileParameter($event->getObjectId(), $parameters[0]),
  208. 'email' => $this->generateEmailParameter($parameters[1]),
  209. 'actor' => $this->generateUserParameter($parameters[2]),
  210. ];
  211. case self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND:
  212. return [
  213. 'file' => $this->generateFileParameter($event->getObjectId(), $parameters[0]),
  214. 'email' => $this->generateEmailParameter($parameters[1]),
  215. ];
  216. case self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF:
  217. return [
  218. 'file' => $this->generateFileParameter($event->getObjectId(), $parameters[0]),
  219. ];
  220. }
  221. throw new \InvalidArgumentException();
  222. }
  223. /**
  224. * @param int $id
  225. * @param string $path
  226. * @return array
  227. */
  228. protected function generateFileParameter($id, $path) {
  229. return [
  230. 'type' => 'file',
  231. 'id' => $id,
  232. 'name' => basename($path),
  233. 'path' => trim($path, '/'),
  234. 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
  235. ];
  236. }
  237. /**
  238. * @param string $email
  239. * @return array
  240. */
  241. protected function generateEmailParameter($email) {
  242. if (!isset($this->contactNames[$email])) {
  243. $this->contactNames[$email] = $this->getContactName($email);
  244. }
  245. return [
  246. 'type' => 'email',
  247. 'id' => $email,
  248. 'name' => $this->contactNames[$email],
  249. ];
  250. }
  251. /**
  252. * @param string $uid
  253. * @return array
  254. */
  255. protected function generateUserParameter($uid) {
  256. return [
  257. 'type' => 'user',
  258. 'id' => $uid,
  259. 'name' => $this->userManager->getDisplayName($uid) ?? $uid,
  260. ];
  261. }
  262. /**
  263. * @param string $email
  264. * @return string
  265. */
  266. protected function getContactName($email) {
  267. $addressBookContacts = $this->contactsManager->search($email, ['EMAIL'], [
  268. 'limit' => 1,
  269. 'enumeration' => false,
  270. 'fullmatch' => false,
  271. 'strict_search' => true,
  272. ]);
  273. foreach ($addressBookContacts as $contact) {
  274. if (isset($contact['isLocalSystemBook'])) {
  275. continue;
  276. }
  277. if (in_array($email, $contact['EMAIL'])) {
  278. return $contact['FN'];
  279. }
  280. }
  281. return $email;
  282. }
  283. }