Activity.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. /**
  53. * @param IFactory $languageFactory
  54. * @param IURLGenerator $url
  55. * @param IManager $activityManager
  56. * @param IUserManager $userManager
  57. * @param IContactsManager $contactsManager
  58. */
  59. public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IContactsManager $contactsManager) {
  60. $this->languageFactory = $languageFactory;
  61. $this->url = $url;
  62. $this->activityManager = $activityManager;
  63. $this->userManager = $userManager;
  64. $this->contactsManager = $contactsManager;
  65. }
  66. /**
  67. * @param string $language
  68. * @param IEvent $event
  69. * @param IEvent|null $previousEvent
  70. * @return IEvent
  71. * @throws \InvalidArgumentException
  72. * @since 11.0.0
  73. */
  74. public function parse($language, IEvent $event, IEvent $previousEvent = null) {
  75. if ($event->getApp() !== 'sharebymail') {
  76. throw new \InvalidArgumentException();
  77. }
  78. $this->l = $this->languageFactory->get('sharebymail', $language);
  79. if ($this->activityManager->isFormattingFilteredObject()) {
  80. try {
  81. return $this->parseShortVersion($event);
  82. } catch (\InvalidArgumentException $e) {
  83. // Ignore and simply use the long version...
  84. }
  85. }
  86. return $this->parseLongVersion($event);
  87. }
  88. /**
  89. * @param IEvent $event
  90. * @return IEvent
  91. * @throws \InvalidArgumentException
  92. * @since 11.0.0
  93. */
  94. public function parseShortVersion(IEvent $event) {
  95. $parsedParameters = $this->getParsedParameters($event);
  96. if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_SELF) {
  97. $event->setParsedSubject($this->l->t('Shared with %1$s', [
  98. $parsedParameters['email']['name'],
  99. ]))
  100. ->setRichSubject($this->l->t('Shared with {email}'), [
  101. 'email' => $parsedParameters['email'],
  102. ]);
  103. if ($this->activityManager->getRequirePNG()) {
  104. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  105. } else {
  106. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  107. }
  108. } else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_BY) {
  109. $event->setParsedSubject($this->l->t('Shared with %1$s by %2$s', [
  110. $parsedParameters['email']['name'],
  111. $parsedParameters['actor']['name'],
  112. ]))
  113. ->setRichSubject($this->l->t('Shared with {email} by {actor}'), [
  114. 'email' => $parsedParameters['email'],
  115. 'actor' => $parsedParameters['actor'],
  116. ]);
  117. if ($this->activityManager->getRequirePNG()) {
  118. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  119. } else {
  120. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  121. }
  122. } else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND) {
  123. $event->setParsedSubject($this->l->t('Password for mail share sent to %1$s', [
  124. $parsedParameters['email']['name']
  125. ]))
  126. ->setRichSubject($this->l->t('Password for mail share sent to {email}'), [
  127. 'email' => $parsedParameters['email']
  128. ]);
  129. if ($this->activityManager->getRequirePNG()) {
  130. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  131. } else {
  132. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  133. }
  134. } else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF) {
  135. $event->setParsedSubject($this->l->t('Password for mail share sent to you'))
  136. ->setRichSubject($this->l->t('Password for mail share sent to you'));
  137. if ($this->activityManager->getRequirePNG()) {
  138. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  139. } else {
  140. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  141. }
  142. } else {
  143. throw new \InvalidArgumentException();
  144. }
  145. return $event;
  146. }
  147. /**
  148. * @param IEvent $event
  149. * @return IEvent
  150. * @throws \InvalidArgumentException
  151. * @since 11.0.0
  152. */
  153. public function parseLongVersion(IEvent $event) {
  154. $parsedParameters = $this->getParsedParameters($event);
  155. if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_SELF) {
  156. $event->setParsedSubject($this->l->t('You shared %1$s with %2$s by mail', [
  157. $parsedParameters['file']['path'],
  158. $parsedParameters['email']['name'],
  159. ]))
  160. ->setRichSubject($this->l->t('You shared {file} with {email} by mail'), $parsedParameters);
  161. if ($this->activityManager->getRequirePNG()) {
  162. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  163. } else {
  164. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  165. }
  166. } else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_BY) {
  167. $event->setParsedSubject($this->l->t('%3$s shared %1$s with %2$s by mail', [
  168. $parsedParameters['file']['path'],
  169. $parsedParameters['email']['name'],
  170. $parsedParameters['actor']['name'],
  171. ]))
  172. ->setRichSubject($this->l->t('{actor} shared {file} with {email} by mail'), $parsedParameters);
  173. if ($this->activityManager->getRequirePNG()) {
  174. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  175. } else {
  176. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  177. }
  178. } else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND) {
  179. $event->setParsedSubject($this->l->t('Password to access %1$s was sent to %2s', [
  180. $parsedParameters['file']['path'],
  181. $parsedParameters['email']['name']
  182. ]))
  183. ->setRichSubject($this->l->t('Password to access {file} was sent to {email}'), $parsedParameters);
  184. if ($this->activityManager->getRequirePNG()) {
  185. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png')));
  186. } else {
  187. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  188. }
  189. } else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF) {
  190. $event->setParsedSubject(
  191. $this->l->t('Password to access %1$s was sent to you',
  192. [$parsedParameters['file']['path']]))
  193. ->setRichSubject($this->l->t('Password to access {file} was sent to you'), $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. } else {
  200. throw new \InvalidArgumentException();
  201. }
  202. return $event;
  203. }
  204. protected function getParsedParameters(IEvent $event) {
  205. $subject = $event->getSubject();
  206. $parameters = $event->getSubjectParameters();
  207. switch ($subject) {
  208. case self::SUBJECT_SHARED_EMAIL_SELF:
  209. return [
  210. 'file' => $this->generateFileParameter((int) $event->getObjectId(), $parameters[0]),
  211. 'email' => $this->generateEmailParameter($parameters[1]),
  212. ];
  213. case self::SUBJECT_SHARED_EMAIL_BY:
  214. return [
  215. 'file' => $this->generateFileParameter((int) $event->getObjectId(), $parameters[0]),
  216. 'email' => $this->generateEmailParameter($parameters[1]),
  217. 'actor' => $this->generateUserParameter($parameters[2]),
  218. ];
  219. case self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND:
  220. return [
  221. 'file' => $this->generateFileParameter((int) $event->getObjectId(), $parameters[0]),
  222. 'email' => $this->generateEmailParameter($parameters[1]),
  223. ];
  224. case self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF:
  225. return [
  226. 'file' => $this->generateFileParameter((int) $event->getObjectId(), $parameters[0]),
  227. ];
  228. }
  229. throw new \InvalidArgumentException();
  230. }
  231. /**
  232. * @param int $id
  233. * @param string $path
  234. * @return array
  235. */
  236. protected function generateFileParameter($id, $path) {
  237. return [
  238. 'type' => 'file',
  239. 'id' => $id,
  240. 'name' => basename($path),
  241. 'path' => trim($path, '/'),
  242. 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
  243. ];
  244. }
  245. /**
  246. * @param string $email
  247. * @return array
  248. */
  249. protected function generateEmailParameter($email) {
  250. if (!isset($this->contactNames[$email])) {
  251. $this->contactNames[$email] = $this->getContactName($email);
  252. }
  253. return [
  254. 'type' => 'email',
  255. 'id' => $email,
  256. 'name' => $this->contactNames[$email],
  257. ];
  258. }
  259. /**
  260. * @param string $uid
  261. * @return array
  262. */
  263. protected function generateUserParameter($uid) {
  264. if (!isset($this->displayNames[$uid])) {
  265. $this->displayNames[$uid] = $this->getDisplayName($uid);
  266. }
  267. return [
  268. 'type' => 'user',
  269. 'id' => $uid,
  270. 'name' => $this->displayNames[$uid],
  271. ];
  272. }
  273. /**
  274. * @param string $email
  275. * @return string
  276. */
  277. protected function getContactName($email) {
  278. $addressBookContacts = $this->contactsManager->search($email, ['EMAIL']);
  279. foreach ($addressBookContacts as $contact) {
  280. if (isset($contact['isLocalSystemBook'])) {
  281. continue;
  282. }
  283. if (in_array($email, $contact['EMAIL'])) {
  284. return $contact['FN'];
  285. }
  286. }
  287. return $email;
  288. }
  289. /**
  290. * @param string $uid
  291. * @return string
  292. */
  293. protected function getDisplayName($uid) {
  294. $user = $this->userManager->get($uid);
  295. if ($user instanceof IUser) {
  296. return $user->getDisplayName();
  297. } else {
  298. return $uid;
  299. }
  300. }
  301. }