Activity.php 11 KB

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