1
0

Activity.php 14 KB

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