You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Activity.php 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. /**
  51. * @param IFactory $languageFactory
  52. * @param IURLGenerator $url
  53. * @param IManager $activityManager
  54. * @param IUserManager $userManager
  55. * @param IContactsManager $contactsManager
  56. */
  57. public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IContactsManager $contactsManager) {
  58. $this->languageFactory = $languageFactory;
  59. $this->url = $url;
  60. $this->activityManager = $activityManager;
  61. $this->userManager = $userManager;
  62. $this->contactsManager = $contactsManager;
  63. }
  64. /**
  65. * @param string $language
  66. * @param IEvent $event
  67. * @param IEvent|null $previousEvent
  68. * @return IEvent
  69. * @throws \InvalidArgumentException
  70. * @since 11.0.0
  71. */
  72. public function parse($language, IEvent $event, IEvent $previousEvent = null) {
  73. if ($event->getApp() !== 'sharebymail') {
  74. throw new \InvalidArgumentException();
  75. }
  76. $this->l = $this->languageFactory->get('sharebymail', $language);
  77. if ($this->activityManager->isFormattingFilteredObject()) {
  78. try {
  79. return $this->parseShortVersion($event);
  80. } catch (\InvalidArgumentException $e) {
  81. // Ignore and simply use the long version...
  82. }
  83. }
  84. return $this->parseLongVersion($event);
  85. }
  86. /**
  87. * @param IEvent $event
  88. * @return IEvent
  89. * @throws \InvalidArgumentException
  90. * @since 11.0.0
  91. */
  92. public function parseShortVersion(IEvent $event) {
  93. $parsedParameters = $this->getParsedParameters($event);
  94. if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_SELF) {
  95. $event->setParsedSubject($this->l->t('Shared with %1$s', [
  96. $parsedParameters['email']['name'],
  97. ]))
  98. ->setRichSubject($this->l->t('Shared with {email}'), [
  99. 'email' => $parsedParameters['email'],
  100. ])
  101. ->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  102. } else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_BY) {
  103. $event->setParsedSubject($this->l->t('Shared with %1$s by %2$s', [
  104. $parsedParameters['email']['name'],
  105. $parsedParameters['actor']['name'],
  106. ]))
  107. ->setRichSubject($this->l->t('Shared with {email} by {actor}'), [
  108. 'email' => $parsedParameters['email'],
  109. 'actor' => $parsedParameters['actor'],
  110. ])
  111. ->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  112. } else {
  113. throw new \InvalidArgumentException();
  114. }
  115. return $event;
  116. }
  117. /**
  118. * @param IEvent $event
  119. * @return IEvent
  120. * @throws \InvalidArgumentException
  121. * @since 11.0.0
  122. */
  123. public function parseLongVersion(IEvent $event) {
  124. $parsedParameters = $this->getParsedParameters($event);
  125. if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_SELF) {
  126. $event->setParsedSubject($this->l->t('You shared %1$s with %2$s by mail', [
  127. $parsedParameters['file']['path'],
  128. $parsedParameters['email']['name'],
  129. ]))
  130. ->setRichSubject($this->l->t('You shared {file} with {email} by mail'), $parsedParameters)
  131. ->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  132. } else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_BY) {
  133. $event->setParsedSubject($this->l->t('%3$s shared %1$s with %2$s by mail', [
  134. $parsedParameters['file']['path'],
  135. $parsedParameters['email']['name'],
  136. $parsedParameters['actor']['name'],
  137. ]))
  138. ->setRichSubject($this->l->t('{actor} shared {file} with {email} by mail'), $parsedParameters)
  139. ->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  140. } else {
  141. throw new \InvalidArgumentException();
  142. }
  143. return $event;
  144. }
  145. protected function getParsedParameters(IEvent $event) {
  146. $subject = $event->getSubject();
  147. $parameters = $event->getSubjectParameters();
  148. switch ($subject) {
  149. case self::SUBJECT_SHARED_EMAIL_SELF:
  150. return [
  151. 'file' => $this->generateFileParameter((int) $event->getObjectId(), $parameters[0]),
  152. 'email' => $this->generateEmailParameter($parameters[1]),
  153. ];
  154. case self::SUBJECT_SHARED_EMAIL_BY:
  155. return [
  156. 'file' => $this->generateFileParameter((int) $event->getObjectId(), $parameters[0]),
  157. 'email' => $this->generateEmailParameter($parameters[1]),
  158. 'actor' => $this->generateUserParameter($parameters[2]),
  159. ];
  160. }
  161. throw new \InvalidArgumentException();
  162. }
  163. /**
  164. * @param int $id
  165. * @param string $path
  166. * @return array
  167. */
  168. protected function generateFileParameter($id, $path) {
  169. return [
  170. 'type' => 'file',
  171. 'id' => $id,
  172. 'name' => basename($path),
  173. 'path' => trim($path, '/'),
  174. 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
  175. ];
  176. }
  177. /**
  178. * @param string $email
  179. * @return array
  180. */
  181. protected function generateEmailParameter($email) {
  182. if (!isset($this->contactNames[$email])) {
  183. $this->contactNames[$email] = $this->getContactName($email);
  184. }
  185. return [
  186. 'type' => 'email',
  187. 'id' => $email,
  188. 'name' => $this->contactNames[$email],
  189. ];
  190. }
  191. /**
  192. * @param string $uid
  193. * @return array
  194. */
  195. protected function generateUserParameter($uid) {
  196. if (!isset($this->displayNames[$uid])) {
  197. $this->displayNames[$uid] = $this->getDisplayName($uid);
  198. }
  199. return [
  200. 'type' => 'user',
  201. 'id' => $uid,
  202. 'name' => $this->displayNames[$uid],
  203. ];
  204. }
  205. /**
  206. * @param string $email
  207. * @return string
  208. */
  209. protected function getContactName($email) {
  210. $addressBookContacts = $this->contactsManager->search($email, ['EMAIL']);
  211. foreach ($addressBookContacts as $contact) {
  212. if (isset($contact['isLocalSystemBook'])) {
  213. continue;
  214. }
  215. if (in_array($email, $contact['EMAIL'])) {
  216. return $contact['FN'];
  217. }
  218. }
  219. return $email;
  220. }
  221. /**
  222. * @param string $uid
  223. * @return string
  224. */
  225. protected function getDisplayName($uid) {
  226. $user = $this->userManager->get($uid);
  227. if ($user instanceof IUser) {
  228. return $user->getDisplayName();
  229. } else {
  230. return $uid;
  231. }
  232. }
  233. }