1
0

Activity.php 11 KB

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