1
0

Activity.php 12 KB

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