Activity.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 $displayNames = [];
  51. /** @var array */
  52. protected $contactNames = [];
  53. public const SUBJECT_SHARED_EMAIL_SELF = 'shared_with_email_self';
  54. public const SUBJECT_SHARED_EMAIL_BY = 'shared_with_email_by';
  55. public const SUBJECT_SHARED_EMAIL_PASSWORD_SEND = 'shared_with_email_password_send';
  56. public const SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF = 'shared_with_email_password_send_self';
  57. public const SUBJECT_UNSHARED_EMAIL_SELF = 'unshared_with_email_self';
  58. public const SUBJECT_UNSHARED_EMAIL_BY = 'unshared_with_email_by';
  59. /**
  60. * @param IFactory $languageFactory
  61. * @param IURLGenerator $url
  62. * @param IManager $activityManager
  63. * @param IUserManager $userManager
  64. * @param IContactsManager $contactsManager
  65. */
  66. public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IContactsManager $contactsManager) {
  67. $this->languageFactory = $languageFactory;
  68. $this->url = $url;
  69. $this->activityManager = $activityManager;
  70. $this->userManager = $userManager;
  71. $this->contactsManager = $contactsManager;
  72. }
  73. /**
  74. * @param string $language
  75. * @param IEvent $event
  76. * @param IEvent|null $previousEvent
  77. * @return IEvent
  78. * @throws \InvalidArgumentException
  79. * @since 11.0.0
  80. */
  81. public function parse($language, IEvent $event, IEvent $previousEvent = null) {
  82. if ($event->getApp() !== 'sharebymail') {
  83. throw new \InvalidArgumentException();
  84. }
  85. $this->l = $this->languageFactory->get('sharebymail', $language);
  86. if ($this->activityManager->isFormattingFilteredObject()) {
  87. try {
  88. return $this->parseShortVersion($event);
  89. } catch (\InvalidArgumentException $e) {
  90. // Ignore and simply use the long version...
  91. }
  92. }
  93. return $this->parseLongVersion($event);
  94. }
  95. /**
  96. * @param IEvent $event
  97. * @return IEvent
  98. * @throws \InvalidArgumentException
  99. * @since 11.0.0
  100. */
  101. public function parseShortVersion(IEvent $event) {
  102. $parsedParameters = $this->getParsedParameters($event);
  103. if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_SELF) {
  104. $event->setParsedSubject($this->l->t('Shared with %1$s', [
  105. $parsedParameters['email']['name'],
  106. ]))
  107. ->setRichSubject($this->l->t('Shared with {email}'), [
  108. 'email' => $parsedParameters['email'],
  109. ]);
  110. if ($this->activityManager->getRequirePNG()) {
  111. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  112. } else {
  113. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  114. }
  115. } elseif ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_BY) {
  116. $event->setParsedSubject($this->l->t('Shared with %1$s by %2$s', [
  117. $parsedParameters['email']['name'],
  118. $parsedParameters['actor']['name'],
  119. ]))
  120. ->setRichSubject($this->l->t('Shared with {email} by {actor}'), [
  121. 'email' => $parsedParameters['email'],
  122. 'actor' => $parsedParameters['actor'],
  123. ]);
  124. if ($this->activityManager->getRequirePNG()) {
  125. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  126. } else {
  127. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  128. }
  129. } elseif ($event->getSubject() === self::SUBJECT_UNSHARED_EMAIL_SELF) {
  130. $event->setParsedSubject($this->l->t('Unshared from %1$s', [
  131. $parsedParameters['email']['name'],
  132. ]))
  133. ->setRichSubject($this->l->t('Unshared from {email}'), [
  134. 'email' => $parsedParameters['email'],
  135. ]);
  136. if ($this->activityManager->getRequirePNG()) {
  137. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  138. } else {
  139. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  140. }
  141. } elseif ($event->getSubject() === self::SUBJECT_UNSHARED_EMAIL_BY) {
  142. $event->setParsedSubject($this->l->t('Unshared from %1$s by %2$s', [
  143. $parsedParameters['email']['name'],
  144. $parsedParameters['actor']['name'],
  145. ]))
  146. ->setRichSubject($this->l->t('Unshared from {email} by {actor}'), [
  147. 'email' => $parsedParameters['email'],
  148. 'actor' => $parsedParameters['actor'],
  149. ]);
  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_PASSWORD_SEND) {
  156. $event->setParsedSubject($this->l->t('Password for mail share sent to %1$s', [
  157. $parsedParameters['email']['name']
  158. ]))
  159. ->setRichSubject($this->l->t('Password for mail share sent to {email}'), [
  160. 'email' => $parsedParameters['email']
  161. ]);
  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_SELF) {
  168. $event->setParsedSubject($this->l->t('Password for mail share sent to you'))
  169. ->setRichSubject($this->l->t('Password for mail share sent to you'));
  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. } else {
  176. throw new \InvalidArgumentException();
  177. }
  178. return $event;
  179. }
  180. /**
  181. * @param IEvent $event
  182. * @return IEvent
  183. * @throws \InvalidArgumentException
  184. * @since 11.0.0
  185. */
  186. public function parseLongVersion(IEvent $event) {
  187. $parsedParameters = $this->getParsedParameters($event);
  188. if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_SELF) {
  189. $event->setParsedSubject($this->l->t('You shared %1$s with %2$s by mail', [
  190. $parsedParameters['file']['path'],
  191. $parsedParameters['email']['name'],
  192. ]))
  193. ->setRichSubject($this->l->t('You shared {file} with {email} by mail'), $parsedParameters);
  194. if ($this->activityManager->getRequirePNG()) {
  195. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  196. } else {
  197. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  198. }
  199. } elseif ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_BY) {
  200. $event->setParsedSubject($this->l->t('%3$s shared %1$s with %2$s by mail', [
  201. $parsedParameters['file']['path'],
  202. $parsedParameters['email']['name'],
  203. $parsedParameters['actor']['name'],
  204. ]))
  205. ->setRichSubject($this->l->t('{actor} shared {file} with {email} by mail'), $parsedParameters);
  206. if ($this->activityManager->getRequirePNG()) {
  207. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  208. } else {
  209. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  210. }
  211. } elseif ($event->getSubject() === self::SUBJECT_UNSHARED_EMAIL_SELF) {
  212. $event->setParsedSubject($this->l->t('You unshared %1$s from %2$s by mail', [
  213. $parsedParameters['file']['path'],
  214. $parsedParameters['email']['name'],
  215. ]))
  216. ->setRichSubject($this->l->t('You unshared {file} from {email} by mail'), $parsedParameters);
  217. if ($this->activityManager->getRequirePNG()) {
  218. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  219. } else {
  220. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  221. }
  222. } elseif ($event->getSubject() === self::SUBJECT_UNSHARED_EMAIL_BY) {
  223. $event->setParsedSubject($this->l->t('%3$s unshared %1$s from %2$s by mail', [
  224. $parsedParameters['file']['path'],
  225. $parsedParameters['email']['name'],
  226. $parsedParameters['actor']['name'],
  227. ]))
  228. ->setRichSubject($this->l->t('{actor} unshared {file} from {email} by mail'), $parsedParameters);
  229. if ($this->activityManager->getRequirePNG()) {
  230. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  231. } else {
  232. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  233. }
  234. } elseif ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND) {
  235. $event->setParsedSubject($this->l->t('Password to access %1$s was sent to %2s', [
  236. $parsedParameters['file']['path'],
  237. $parsedParameters['email']['name']
  238. ]))
  239. ->setRichSubject($this->l->t('Password to access {file} was sent to {email}'), $parsedParameters);
  240. if ($this->activityManager->getRequirePNG()) {
  241. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  242. } else {
  243. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  244. }
  245. } elseif ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF) {
  246. $event->setParsedSubject(
  247. $this->l->t('Password to access %1$s was sent to you',
  248. [$parsedParameters['file']['path']]))
  249. ->setRichSubject($this->l->t('Password to access {file} was sent to you'), $parsedParameters);
  250. if ($this->activityManager->getRequirePNG()) {
  251. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  252. } else {
  253. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  254. }
  255. } else {
  256. throw new \InvalidArgumentException();
  257. }
  258. return $event;
  259. }
  260. protected function getParsedParameters(IEvent $event) {
  261. $subject = $event->getSubject();
  262. $parameters = $event->getSubjectParameters();
  263. switch ($subject) {
  264. case self::SUBJECT_SHARED_EMAIL_SELF:
  265. case self::SUBJECT_UNSHARED_EMAIL_SELF:
  266. return [
  267. 'file' => $this->generateFileParameter($event->getObjectId(), $parameters[0]),
  268. 'email' => $this->generateEmailParameter($parameters[1]),
  269. ];
  270. case self::SUBJECT_SHARED_EMAIL_BY:
  271. case self::SUBJECT_UNSHARED_EMAIL_BY:
  272. return [
  273. 'file' => $this->generateFileParameter($event->getObjectId(), $parameters[0]),
  274. 'email' => $this->generateEmailParameter($parameters[1]),
  275. 'actor' => $this->generateUserParameter($parameters[2]),
  276. ];
  277. case self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND:
  278. return [
  279. 'file' => $this->generateFileParameter($event->getObjectId(), $parameters[0]),
  280. 'email' => $this->generateEmailParameter($parameters[1]),
  281. ];
  282. case self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF:
  283. return [
  284. 'file' => $this->generateFileParameter($event->getObjectId(), $parameters[0]),
  285. ];
  286. }
  287. throw new \InvalidArgumentException();
  288. }
  289. /**
  290. * @param int $id
  291. * @param string $path
  292. * @return array
  293. */
  294. protected function generateFileParameter($id, $path) {
  295. return [
  296. 'type' => 'file',
  297. 'id' => $id,
  298. 'name' => basename($path),
  299. 'path' => trim($path, '/'),
  300. 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
  301. ];
  302. }
  303. /**
  304. * @param string $email
  305. * @return array
  306. */
  307. protected function generateEmailParameter($email) {
  308. if (!isset($this->contactNames[$email])) {
  309. $this->contactNames[$email] = $this->getContactName($email);
  310. }
  311. return [
  312. 'type' => 'email',
  313. 'id' => $email,
  314. 'name' => $this->contactNames[$email],
  315. ];
  316. }
  317. /**
  318. * @param string $uid
  319. * @return array
  320. */
  321. protected function generateUserParameter($uid) {
  322. if (!isset($this->displayNames[$uid])) {
  323. $this->displayNames[$uid] = $this->getDisplayName($uid);
  324. }
  325. return [
  326. 'type' => 'user',
  327. 'id' => $uid,
  328. 'name' => $this->displayNames[$uid],
  329. ];
  330. }
  331. /**
  332. * @param string $email
  333. * @return string
  334. */
  335. protected function getContactName($email) {
  336. $addressBookContacts = $this->contactsManager->search($email, ['EMAIL'], [
  337. 'limit' => 1,
  338. 'enumeration' => false,
  339. 'fullmatch' => false,
  340. 'strict_search' => true,
  341. ]);
  342. foreach ($addressBookContacts as $contact) {
  343. if (isset($contact['isLocalSystemBook'])) {
  344. continue;
  345. }
  346. if (in_array($email, $contact['EMAIL'])) {
  347. return $contact['FN'];
  348. }
  349. }
  350. return $email;
  351. }
  352. /**
  353. * @param string $uid
  354. * @return string
  355. */
  356. protected function getDisplayName($uid) {
  357. $user = $this->userManager->get($uid);
  358. if ($user instanceof IUser) {
  359. return $user->getDisplayName();
  360. } else {
  361. return $uid;
  362. }
  363. }
  364. }