1
0

Provider.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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\Files\Activity;
  22. use OCP\Activity\IEvent;
  23. use OCP\Activity\IEventMerger;
  24. use OCP\Activity\IManager;
  25. use OCP\Activity\IProvider;
  26. use OCP\IL10N;
  27. use OCP\IURLGenerator;
  28. use OCP\IUser;
  29. use OCP\IUserManager;
  30. use OCP\L10N\IFactory;
  31. class Provider 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 IEventMerger */
  43. protected $eventMerger;
  44. /** @var string[] cached displayNames - key is the UID and value the displayname */
  45. protected $displayNames = [];
  46. /**
  47. * @param IFactory $languageFactory
  48. * @param IURLGenerator $url
  49. * @param IManager $activityManager
  50. * @param IUserManager $userManager
  51. * @param IEventMerger $eventMerger
  52. */
  53. public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IEventMerger $eventMerger) {
  54. $this->languageFactory = $languageFactory;
  55. $this->url = $url;
  56. $this->activityManager = $activityManager;
  57. $this->userManager = $userManager;
  58. $this->eventMerger = $eventMerger;
  59. }
  60. /**
  61. * @param string $language
  62. * @param IEvent $event
  63. * @param IEvent|null $previousEvent
  64. * @return IEvent
  65. * @throws \InvalidArgumentException
  66. * @since 11.0.0
  67. */
  68. public function parse($language, IEvent $event, IEvent $previousEvent = null) {
  69. if ($event->getApp() !== 'files') {
  70. throw new \InvalidArgumentException();
  71. }
  72. $this->l = $this->languageFactory->get('files', $language);
  73. if ($this->activityManager->isFormattingFilteredObject()) {
  74. try {
  75. return $this->parseShortVersion($event, $previousEvent);
  76. } catch (\InvalidArgumentException $e) {
  77. // Ignore and simply use the long version...
  78. }
  79. }
  80. return $this->parseLongVersion($event, $previousEvent);
  81. }
  82. /**
  83. * @param IEvent $event
  84. * @param IEvent|null $previousEvent
  85. * @return IEvent
  86. * @throws \InvalidArgumentException
  87. * @since 11.0.0
  88. */
  89. public function parseShortVersion(IEvent $event, IEvent $previousEvent = null) {
  90. $parsedParameters = $this->getParameters($event);
  91. if ($event->getSubject() === 'created_by') {
  92. $subject = $this->l->t('Created by {user}');
  93. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'add-color.svg')));
  94. } else if ($event->getSubject() === 'changed_by') {
  95. $subject = $this->l->t('Changed by {user}');
  96. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg')));
  97. } else if ($event->getSubject() === 'deleted_by') {
  98. $subject = $this->l->t('Deleted by {user}');
  99. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'delete-color.svg')));
  100. } else if ($event->getSubject() === 'restored_by') {
  101. $subject = $this->l->t('Restored by {user}');
  102. } else if ($event->getSubject() === 'renamed_by') {
  103. $subject = $this->l->t('Renamed by {user}');
  104. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg')));
  105. } else if ($event->getSubject() === 'moved_by') {
  106. $subject = $this->l->t('Moved by {user}');
  107. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg')));
  108. } else {
  109. throw new \InvalidArgumentException();
  110. }
  111. $this->setSubjects($event, $subject, $parsedParameters);
  112. return $this->eventMerger->mergeEvents('user', $event, $previousEvent);
  113. }
  114. /**
  115. * @param IEvent $event
  116. * @param IEvent|null $previousEvent
  117. * @return IEvent
  118. * @throws \InvalidArgumentException
  119. * @since 11.0.0
  120. */
  121. public function parseLongVersion(IEvent $event, IEvent $previousEvent = null) {
  122. $parsedParameters = $this->getParameters($event);
  123. if ($event->getSubject() === 'created_self') {
  124. $subject = $this->l->t('You created {file}');
  125. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'add-color.svg')));
  126. } else if ($event->getSubject() === 'created_by') {
  127. $subject = $this->l->t('{user} created {file}');
  128. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'add-color.svg')));
  129. } else if ($event->getSubject() === 'created_public') {
  130. $subject = $this->l->t('{file} was created in a public folder');
  131. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'add-color.svg')));
  132. } else if ($event->getSubject() === 'changed_self') {
  133. $subject = $this->l->t('You changed {file}');
  134. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg')));
  135. } else if ($event->getSubject() === 'changed_by') {
  136. $subject = $this->l->t('{user} changed {file}');
  137. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg')));
  138. } else if ($event->getSubject() === 'deleted_self') {
  139. $subject = $this->l->t('You deleted {file}');
  140. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'delete-color.svg')));
  141. } else if ($event->getSubject() === 'deleted_by') {
  142. $subject = $this->l->t('{user} deleted {file}');
  143. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'delete-color.svg')));
  144. } else if ($event->getSubject() === 'restored_self') {
  145. $subject = $this->l->t('You restored {file}');
  146. } else if ($event->getSubject() === 'restored_by') {
  147. $subject = $this->l->t('{user} restored {file}');
  148. } else if ($event->getSubject() === 'renamed_self') {
  149. $subject = $this->l->t('You renamed {oldfile} to {newfile}');
  150. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg')));
  151. } else if ($event->getSubject() === 'renamed_by') {
  152. $subject = $this->l->t('{user} renamed {oldfile} to {newfile}');
  153. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg')));
  154. } else if ($event->getSubject() === 'moved_self') {
  155. $subject = $this->l->t('You moved {oldfile} to {newfile}');
  156. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg')));
  157. } else if ($event->getSubject() === 'moved_by') {
  158. $subject = $this->l->t('{user} moved {oldfile} to {newfile}');
  159. $event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('files', 'change.svg')));
  160. } else {
  161. throw new \InvalidArgumentException();
  162. }
  163. $this->setSubjects($event, $subject, $parsedParameters);
  164. $event = $this->eventMerger->mergeEvents('file', $event, $previousEvent);
  165. if ($event->getChildEvent() === null) {
  166. // Couldn't group by file, maybe we can group by user
  167. $event = $this->eventMerger->mergeEvents('user', $event, $previousEvent);
  168. }
  169. return $event;
  170. }
  171. protected function setSubjects(IEvent $event, $subject, array $parameters) {
  172. $placeholders = $replacements = [];
  173. foreach ($parameters as $placeholder => $parameter) {
  174. $placeholders[] = '{' . $placeholder . '}';
  175. if ($parameter['type'] === 'file') {
  176. $replacements[] = $parameter['path'];
  177. } else {
  178. $replacements[] = $parameter['name'];
  179. }
  180. }
  181. $event->setParsedSubject(str_replace($placeholders, $replacements, $subject))
  182. ->setRichSubject($subject, $parameters);
  183. }
  184. /**
  185. * @param IEvent $event
  186. * @return array
  187. * @throws \InvalidArgumentException
  188. */
  189. protected function getParameters(IEvent $event) {
  190. $parameters = $event->getSubjectParameters();
  191. switch ($event->getSubject()) {
  192. case 'created_self':
  193. case 'created_public':
  194. case 'changed_self':
  195. case 'deleted_self':
  196. case 'restored_self':
  197. return [
  198. 'file' => $this->getFile($parameters[0], $event),
  199. ];
  200. case 'created_by':
  201. case 'changed_by':
  202. case 'deleted_by':
  203. case 'restored_by':
  204. return [
  205. 'file' => $this->getFile($parameters[0], $event),
  206. 'user' => $this->getUser($parameters[1]),
  207. ];
  208. case 'renamed_self':
  209. case 'moved_self':
  210. return [
  211. 'newfile' => $this->getFile($parameters[0]),
  212. 'oldfile' => $this->getFile($parameters[1]),
  213. ];
  214. case 'renamed_by':
  215. case 'moved_by':
  216. return [
  217. 'newfile' => $this->getFile($parameters[0]),
  218. 'user' => $this->getUser($parameters[1]),
  219. 'oldfile' => $this->getFile($parameters[2]),
  220. ];
  221. }
  222. return [];
  223. }
  224. /**
  225. * @param array|string $parameter
  226. * @param IEvent|null $event
  227. * @return array
  228. * @throws \InvalidArgumentException
  229. */
  230. protected function getFile($parameter, IEvent $event = null) {
  231. if (is_array($parameter)) {
  232. $path = reset($parameter);
  233. $id = (string) key($parameter);
  234. } else if ($event !== null) {
  235. // Legacy from before ownCloud 8.2
  236. $path = $parameter;
  237. $id = $event->getObjectId();
  238. } else {
  239. throw new \InvalidArgumentException('Could not generate file parameter');
  240. }
  241. return [
  242. 'type' => 'file',
  243. 'id' => $id,
  244. 'name' => basename($path),
  245. 'path' => trim($path, '/'),
  246. 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
  247. ];
  248. }
  249. /**
  250. * @param string $uid
  251. * @return array
  252. */
  253. protected function getUser($uid) {
  254. if (!isset($this->displayNames[$uid])) {
  255. $this->displayNames[$uid] = $this->getDisplayName($uid);
  256. }
  257. return [
  258. 'type' => 'user',
  259. 'id' => $uid,
  260. 'name' => $this->displayNames[$uid],
  261. ];
  262. }
  263. /**
  264. * @param string $uid
  265. * @return string
  266. */
  267. protected function getDisplayName($uid) {
  268. $user = $this->userManager->get($uid);
  269. if ($user instanceof IUser) {
  270. return $user->getDisplayName();
  271. } else {
  272. return $uid;
  273. }
  274. }
  275. }