Activity.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\ShareByMail;
  27. use OCP\Activity\IEvent;
  28. use OCP\Activity\IManager;
  29. use OCP\Activity\IProvider;
  30. use OCP\Contacts\IManager as IContactsManager;
  31. use OCP\IL10N;
  32. use OCP\IURLGenerator;
  33. use OCP\IUser;
  34. use OCP\IUserManager;
  35. use OCP\L10N\IFactory;
  36. class Activity implements IProvider {
  37. /** @var IFactory */
  38. protected $languageFactory;
  39. /** @var IL10N */
  40. protected $l;
  41. /** @var IURLGenerator */
  42. protected $url;
  43. /** @var IManager */
  44. protected $activityManager;
  45. /** @var IUserManager */
  46. protected $userManager;
  47. /** @var IContactsManager */
  48. protected $contactsManager;
  49. /** @var array */
  50. protected $contactNames = [];
  51. public const SUBJECT_SHARED_EMAIL_SELF = 'shared_with_email_self';
  52. public const SUBJECT_SHARED_EMAIL_BY = 'shared_with_email_by';
  53. public const SUBJECT_SHARED_EMAIL_PASSWORD_SEND = 'shared_with_email_password_send';
  54. public const SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF = 'shared_with_email_password_send_self';
  55. public const SUBJECT_UNSHARED_EMAIL_SELF = 'unshared_with_email_self';
  56. public const SUBJECT_UNSHARED_EMAIL_BY = 'unshared_with_email_by';
  57. /**
  58. * @param IFactory $languageFactory
  59. * @param IURLGenerator $url
  60. * @param IManager $activityManager
  61. * @param IUserManager $userManager
  62. * @param IContactsManager $contactsManager
  63. */
  64. public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IContactsManager $contactsManager) {
  65. $this->languageFactory = $languageFactory;
  66. $this->url = $url;
  67. $this->activityManager = $activityManager;
  68. $this->userManager = $userManager;
  69. $this->contactsManager = $contactsManager;
  70. }
  71. /**
  72. * @param string $language
  73. * @param IEvent $event
  74. * @param IEvent|null $previousEvent
  75. * @return IEvent
  76. * @throws \InvalidArgumentException
  77. * @since 11.0.0
  78. */
  79. public function parse($language, IEvent $event, IEvent $previousEvent = null) {
  80. if ($event->getApp() !== 'sharebymail') {
  81. throw new \InvalidArgumentException();
  82. }
  83. $this->l = $this->languageFactory->get('sharebymail', $language);
  84. if ($this->activityManager->isFormattingFilteredObject()) {
  85. try {
  86. return $this->parseShortVersion($event);
  87. } catch (\InvalidArgumentException $e) {
  88. // Ignore and simply use the long version...
  89. }
  90. }
  91. return $this->parseLongVersion($event);
  92. }
  93. /**
  94. * @param IEvent $event
  95. * @return IEvent
  96. * @throws \InvalidArgumentException
  97. * @since 11.0.0
  98. */
  99. public function parseShortVersion(IEvent $event) {
  100. $parsedParameters = $this->getParsedParameters($event);
  101. if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_SELF) {
  102. $event->setParsedSubject($this->l->t('Shared with %1$s', [
  103. $parsedParameters['email']['name'],
  104. ]))
  105. ->setRichSubject($this->l->t('Shared with {email}'), [
  106. 'email' => $parsedParameters['email'],
  107. ]);
  108. if ($this->activityManager->getRequirePNG()) {
  109. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  110. } else {
  111. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  112. }
  113. } elseif ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_BY) {
  114. $event->setParsedSubject($this->l->t('Shared with %1$s by %2$s', [
  115. $parsedParameters['email']['name'],
  116. $parsedParameters['actor']['name'],
  117. ]))
  118. ->setRichSubject($this->l->t('Shared with {email} by {actor}'), [
  119. 'email' => $parsedParameters['email'],
  120. 'actor' => $parsedParameters['actor'],
  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_UNSHARED_EMAIL_SELF) {
  128. $event->setParsedSubject($this->l->t('Unshared from %1$s', [
  129. $parsedParameters['email']['name'],
  130. ]))
  131. ->setRichSubject($this->l->t('Unshared from {email}'), [
  132. 'email' => $parsedParameters['email'],
  133. ]);
  134. if ($this->activityManager->getRequirePNG()) {
  135. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  136. } else {
  137. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  138. }
  139. } elseif ($event->getSubject() === self::SUBJECT_UNSHARED_EMAIL_BY) {
  140. $event->setParsedSubject($this->l->t('Unshared from %1$s by %2$s', [
  141. $parsedParameters['email']['name'],
  142. $parsedParameters['actor']['name'],
  143. ]))
  144. ->setRichSubject($this->l->t('Unshared from {email} by {actor}'), [
  145. 'email' => $parsedParameters['email'],
  146. 'actor' => $parsedParameters['actor'],
  147. ]);
  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_SHARED_EMAIL_PASSWORD_SEND) {
  154. $event->setParsedSubject($this->l->t('Password for mail share sent to %1$s', [
  155. $parsedParameters['email']['name']
  156. ]))
  157. ->setRichSubject($this->l->t('Password for mail share sent to {email}'), [
  158. 'email' => $parsedParameters['email']
  159. ]);
  160. if ($this->activityManager->getRequirePNG()) {
  161. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  162. } else {
  163. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  164. }
  165. } elseif ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF) {
  166. $event->setParsedSubject($this->l->t('Password for mail share sent to you'))
  167. ->setRichSubject($this->l->t('Password for mail share sent to you'));
  168. if ($this->activityManager->getRequirePNG()) {
  169. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  170. } else {
  171. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  172. }
  173. } else {
  174. throw new \InvalidArgumentException();
  175. }
  176. return $event;
  177. }
  178. /**
  179. * @param IEvent $event
  180. * @return IEvent
  181. * @throws \InvalidArgumentException
  182. * @since 11.0.0
  183. */
  184. public function parseLongVersion(IEvent $event) {
  185. $parsedParameters = $this->getParsedParameters($event);
  186. if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_SELF) {
  187. $event->setParsedSubject($this->l->t('You shared %1$s with %2$s by mail', [
  188. $parsedParameters['file']['path'],
  189. $parsedParameters['email']['name'],
  190. ]))
  191. ->setRichSubject($this->l->t('You shared {file} with {email} by mail'), $parsedParameters);
  192. if ($this->activityManager->getRequirePNG()) {
  193. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  194. } else {
  195. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  196. }
  197. } elseif ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_BY) {
  198. $event->setParsedSubject($this->l->t('%3$s shared %1$s with %2$s by mail', [
  199. $parsedParameters['file']['path'],
  200. $parsedParameters['email']['name'],
  201. $parsedParameters['actor']['name'],
  202. ]))
  203. ->setRichSubject($this->l->t('{actor} shared {file} with {email} by mail'), $parsedParameters);
  204. if ($this->activityManager->getRequirePNG()) {
  205. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  206. } else {
  207. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  208. }
  209. } elseif ($event->getSubject() === self::SUBJECT_UNSHARED_EMAIL_SELF) {
  210. $event->setParsedSubject($this->l->t('You unshared %1$s from %2$s by mail', [
  211. $parsedParameters['file']['path'],
  212. $parsedParameters['email']['name'],
  213. ]))
  214. ->setRichSubject($this->l->t('You unshared {file} from {email} by mail'), $parsedParameters);
  215. if ($this->activityManager->getRequirePNG()) {
  216. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  217. } else {
  218. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  219. }
  220. } elseif ($event->getSubject() === self::SUBJECT_UNSHARED_EMAIL_BY) {
  221. $event->setParsedSubject($this->l->t('%3$s unshared %1$s from %2$s by mail', [
  222. $parsedParameters['file']['path'],
  223. $parsedParameters['email']['name'],
  224. $parsedParameters['actor']['name'],
  225. ]))
  226. ->setRichSubject($this->l->t('{actor} unshared {file} from {email} by mail'), $parsedParameters);
  227. if ($this->activityManager->getRequirePNG()) {
  228. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  229. } else {
  230. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  231. }
  232. } elseif ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND) {
  233. $event->setParsedSubject($this->l->t('Password to access %1$s was sent to %2s', [
  234. $parsedParameters['file']['path'],
  235. $parsedParameters['email']['name']
  236. ]))
  237. ->setRichSubject($this->l->t('Password to access {file} was sent to {email}'), $parsedParameters);
  238. if ($this->activityManager->getRequirePNG()) {
  239. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  240. } else {
  241. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  242. }
  243. } elseif ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF) {
  244. $event->setParsedSubject(
  245. $this->l->t('Password to access %1$s was sent to you',
  246. [$parsedParameters['file']['path']]))
  247. ->setRichSubject($this->l->t('Password to access {file} was sent to you'), $parsedParameters);
  248. if ($this->activityManager->getRequirePNG()) {
  249. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  250. } else {
  251. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  252. }
  253. } else {
  254. throw new \InvalidArgumentException();
  255. }
  256. return $event;
  257. }
  258. protected function getParsedParameters(IEvent $event) {
  259. $subject = $event->getSubject();
  260. $parameters = $event->getSubjectParameters();
  261. switch ($subject) {
  262. case self::SUBJECT_SHARED_EMAIL_SELF:
  263. case self::SUBJECT_UNSHARED_EMAIL_SELF:
  264. return [
  265. 'file' => $this->generateFileParameter($event->getObjectId(), $parameters[0]),
  266. 'email' => $this->generateEmailParameter($parameters[1]),
  267. ];
  268. case self::SUBJECT_SHARED_EMAIL_BY:
  269. case self::SUBJECT_UNSHARED_EMAIL_BY:
  270. return [
  271. 'file' => $this->generateFileParameter($event->getObjectId(), $parameters[0]),
  272. 'email' => $this->generateEmailParameter($parameters[1]),
  273. 'actor' => $this->generateUserParameter($parameters[2]),
  274. ];
  275. case self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND:
  276. return [
  277. 'file' => $this->generateFileParameter($event->getObjectId(), $parameters[0]),
  278. 'email' => $this->generateEmailParameter($parameters[1]),
  279. ];
  280. case self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF:
  281. return [
  282. 'file' => $this->generateFileParameter($event->getObjectId(), $parameters[0]),
  283. ];
  284. }
  285. throw new \InvalidArgumentException();
  286. }
  287. /**
  288. * @param int $id
  289. * @param string $path
  290. * @return array
  291. */
  292. protected function generateFileParameter($id, $path) {
  293. return [
  294. 'type' => 'file',
  295. 'id' => $id,
  296. 'name' => basename($path),
  297. 'path' => trim($path, '/'),
  298. 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
  299. ];
  300. }
  301. /**
  302. * @param string $email
  303. * @return array
  304. */
  305. protected function generateEmailParameter($email) {
  306. if (!isset($this->contactNames[$email])) {
  307. $this->contactNames[$email] = $this->getContactName($email);
  308. }
  309. return [
  310. 'type' => 'email',
  311. 'id' => $email,
  312. 'name' => $this->contactNames[$email],
  313. ];
  314. }
  315. /**
  316. * @param string $uid
  317. * @return array
  318. */
  319. protected function generateUserParameter($uid) {
  320. return [
  321. 'type' => 'user',
  322. 'id' => $uid,
  323. 'name' => $this->userManager->getDisplayName($uid) ?? $uid,
  324. ];
  325. }
  326. /**
  327. * @param string $email
  328. * @return string
  329. */
  330. protected function getContactName($email) {
  331. $addressBookContacts = $this->contactsManager->search($email, ['EMAIL'], [
  332. 'limit' => 1,
  333. 'enumeration' => false,
  334. 'fullmatch' => false,
  335. 'strict_search' => true,
  336. ]);
  337. foreach ($addressBookContacts as $contact) {
  338. if (isset($contact['isLocalSystemBook'])) {
  339. continue;
  340. }
  341. if (in_array($email, $contact['EMAIL'])) {
  342. return $contact['FN'];
  343. }
  344. }
  345. return $email;
  346. }
  347. }