Manager.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Activity;
  26. use OCP\Activity\IConsumer;
  27. use OCP\Activity\IEvent;
  28. use OCP\Activity\IExtension;
  29. use OCP\Activity\IFilter;
  30. use OCP\Activity\IManager;
  31. use OCP\Activity\IProvider;
  32. use OCP\Activity\ISetting;
  33. use OCP\IConfig;
  34. use OCP\IRequest;
  35. use OCP\IUser;
  36. use OCP\IUserSession;
  37. use OCP\RichObjectStrings\IValidator;
  38. class Manager implements IManager {
  39. /** @var IRequest */
  40. protected $request;
  41. /** @var IUserSession */
  42. protected $session;
  43. /** @var IConfig */
  44. protected $config;
  45. /** @var IValidator */
  46. protected $validator;
  47. /** @var string */
  48. protected $formattingObjectType;
  49. /** @var int */
  50. protected $formattingObjectId;
  51. /** @var bool */
  52. protected $requirePNG;
  53. /** @var string */
  54. protected $currentUserId;
  55. /**
  56. * constructor of the controller
  57. *
  58. * @param IRequest $request
  59. * @param IUserSession $session
  60. * @param IConfig $config
  61. * @param IValidator $validator
  62. */
  63. public function __construct(IRequest $request,
  64. IUserSession $session,
  65. IConfig $config,
  66. IValidator $validator) {
  67. $this->request = $request;
  68. $this->session = $session;
  69. $this->config = $config;
  70. $this->validator = $validator;
  71. }
  72. /** @var \Closure[] */
  73. private $consumersClosures = array();
  74. /** @var IConsumer[] */
  75. private $consumers = array();
  76. /** @var \Closure[] */
  77. private $extensionsClosures = array();
  78. /** @var IExtension[] */
  79. private $extensions = array();
  80. /** @var array list of filters "name" => "is valid" */
  81. protected $validFilters = array(
  82. 'all' => true,
  83. 'by' => true,
  84. 'self' => true,
  85. );
  86. /** @var array list of type icons "type" => "css class" */
  87. protected $typeIcons = array();
  88. /** @var array list of special parameters "app" => ["text" => ["parameter" => "type"]] */
  89. protected $specialParameters = array();
  90. /**
  91. * @return \OCP\Activity\IConsumer[]
  92. */
  93. protected function getConsumers() {
  94. if (!empty($this->consumers)) {
  95. return $this->consumers;
  96. }
  97. $this->consumers = [];
  98. foreach($this->consumersClosures as $consumer) {
  99. $c = $consumer();
  100. if ($c instanceof IConsumer) {
  101. $this->consumers[] = $c;
  102. } else {
  103. throw new \InvalidArgumentException('The given consumer does not implement the \OCP\Activity\IConsumer interface');
  104. }
  105. }
  106. return $this->consumers;
  107. }
  108. /**
  109. * @return \OCP\Activity\IExtension[]
  110. */
  111. protected function getExtensions() {
  112. if (!empty($this->extensions)) {
  113. return $this->extensions;
  114. }
  115. $this->extensions = [];
  116. foreach($this->extensionsClosures as $extension) {
  117. $e = $extension();
  118. if ($e instanceof IExtension) {
  119. $this->extensions[] = $e;
  120. } else {
  121. throw new \InvalidArgumentException('The given extension does not implement the \OCP\Activity\IExtension interface');
  122. }
  123. }
  124. return $this->extensions;
  125. }
  126. /**
  127. * Generates a new IEvent object
  128. *
  129. * Make sure to call at least the following methods before sending it to the
  130. * app with via the publish() method:
  131. * - setApp()
  132. * - setType()
  133. * - setAffectedUser()
  134. * - setSubject()
  135. *
  136. * @return IEvent
  137. */
  138. public function generateEvent() {
  139. return new Event($this->validator);
  140. }
  141. /**
  142. * Publish an event to the activity consumers
  143. *
  144. * Make sure to call at least the following methods before sending an Event:
  145. * - setApp()
  146. * - setType()
  147. * - setAffectedUser()
  148. * - setSubject()
  149. *
  150. * @param IEvent $event
  151. * @throws \BadMethodCallException if required values have not been set
  152. */
  153. public function publish(IEvent $event) {
  154. if ($event->getAuthor() === '') {
  155. if ($this->session->getUser() instanceof IUser) {
  156. $event->setAuthor($this->session->getUser()->getUID());
  157. }
  158. }
  159. if (!$event->getTimestamp()) {
  160. $event->setTimestamp(time());
  161. }
  162. if (!$event->isValid()) {
  163. throw new \BadMethodCallException('The given event is invalid');
  164. }
  165. foreach ($this->getConsumers() as $c) {
  166. $c->receive($event);
  167. }
  168. }
  169. /**
  170. * @param string $app The app where this event is associated with
  171. * @param string $subject A short description of the event
  172. * @param array $subjectParams Array with parameters that are filled in the subject
  173. * @param string $message A longer description of the event
  174. * @param array $messageParams Array with parameters that are filled in the message
  175. * @param string $file The file including path where this event is associated with
  176. * @param string $link A link where this event is associated with
  177. * @param string $affectedUser Recipient of the activity
  178. * @param string $type Type of the notification
  179. * @param int $priority Priority of the notification
  180. */
  181. public function publishActivity($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority) {
  182. $event = $this->generateEvent();
  183. $event->setApp($app)
  184. ->setType($type)
  185. ->setAffectedUser($affectedUser)
  186. ->setSubject($subject, $subjectParams)
  187. ->setMessage($message, $messageParams)
  188. ->setObject('', 0, $file)
  189. ->setLink($link);
  190. $this->publish($event);
  191. }
  192. /**
  193. * In order to improve lazy loading a closure can be registered which will be called in case
  194. * activity consumers are actually requested
  195. *
  196. * $callable has to return an instance of OCA\Activity\IConsumer
  197. *
  198. * @param \Closure $callable
  199. */
  200. public function registerConsumer(\Closure $callable) {
  201. array_push($this->consumersClosures, $callable);
  202. $this->consumers = [];
  203. }
  204. /**
  205. * In order to improve lazy loading a closure can be registered which will be called in case
  206. * activity consumers are actually requested
  207. *
  208. * $callable has to return an instance of OCA\Activity\IExtension
  209. *
  210. * @param \Closure $callable
  211. */
  212. public function registerExtension(\Closure $callable) {
  213. array_push($this->extensionsClosures, $callable);
  214. $this->extensions = [];
  215. }
  216. /** @var string[] */
  217. protected $filterClasses = [];
  218. /** @var IFilter[] */
  219. protected $filters = [];
  220. /** @var bool */
  221. protected $loadedLegacyFilters = false;
  222. /**
  223. * @param string $filter Class must implement OCA\Activity\IFilter
  224. * @return void
  225. */
  226. public function registerFilter($filter) {
  227. $this->filterClasses[$filter] = false;
  228. }
  229. /**
  230. * @return IFilter[]
  231. * @throws \InvalidArgumentException
  232. */
  233. public function getFilters() {
  234. if (!$this->loadedLegacyFilters) {
  235. $legacyFilters = $this->getNavigation();
  236. foreach ($legacyFilters['top'] as $filter => $data) {
  237. $this->filters[$filter] = new LegacyFilter(
  238. $this, $filter, $data['name'], true
  239. );
  240. }
  241. foreach ($legacyFilters['apps'] as $filter => $data) {
  242. $this->filters[$filter] = new LegacyFilter(
  243. $this, $filter, $data['name'], false
  244. );
  245. }
  246. $this->loadedLegacyFilters = true;
  247. }
  248. foreach ($this->filterClasses as $class => $false) {
  249. /** @var IFilter $filter */
  250. $filter = \OC::$server->query($class);
  251. if (!$filter instanceof IFilter) {
  252. throw new \InvalidArgumentException('Invalid activity filter registered');
  253. }
  254. $this->filters[$filter->getIdentifier()] = $filter;
  255. unset($this->filterClasses[$class]);
  256. }
  257. return $this->filters;
  258. }
  259. /**
  260. * @param string $id
  261. * @return IFilter
  262. * @throws \InvalidArgumentException when the filter was not found
  263. * @since 11.0.0
  264. */
  265. public function getFilterById($id) {
  266. $filters = $this->getFilters();
  267. if (isset($filters[$id])) {
  268. return $filters[$id];
  269. }
  270. throw new \InvalidArgumentException('Requested filter does not exist');
  271. }
  272. /** @var string[] */
  273. protected $providerClasses = [];
  274. /** @var IProvider[] */
  275. protected $providers = [];
  276. /**
  277. * @param string $provider Class must implement OCA\Activity\IProvider
  278. * @return void
  279. */
  280. public function registerProvider($provider) {
  281. $this->providerClasses[$provider] = false;
  282. }
  283. /**
  284. * @return IProvider[]
  285. * @throws \InvalidArgumentException
  286. */
  287. public function getProviders() {
  288. foreach ($this->providerClasses as $class => $false) {
  289. /** @var IProvider $provider */
  290. $provider = \OC::$server->query($class);
  291. if (!$provider instanceof IProvider) {
  292. throw new \InvalidArgumentException('Invalid activity provider registered');
  293. }
  294. $this->providers[] = $provider;
  295. unset($this->providerClasses[$class]);
  296. }
  297. return $this->providers;
  298. }
  299. /** @var string[] */
  300. protected $settingsClasses = [];
  301. /** @var ISetting[] */
  302. protected $settings = [];
  303. /** @var bool */
  304. protected $loadedLegacyTypes = false;
  305. /**
  306. * @param string $setting Class must implement OCA\Activity\ISetting
  307. * @return void
  308. */
  309. public function registerSetting($setting) {
  310. $this->settingsClasses[$setting] = false;
  311. }
  312. /**
  313. * @return ISetting[]
  314. * @throws \InvalidArgumentException
  315. */
  316. public function getSettings() {
  317. if (!$this->loadedLegacyTypes) {
  318. $l = \OC::$server->getL10N('core');
  319. $legacyTypes = $this->getNotificationTypes($l->getLanguageCode());
  320. $streamTypes = $this->getDefaultTypes(IExtension::METHOD_STREAM);
  321. $mailTypes = $this->getDefaultTypes(IExtension::METHOD_MAIL);
  322. foreach ($legacyTypes as $type => $data) {
  323. if (is_string($data)) {
  324. $desc = $data;
  325. $canChangeStream = true;
  326. $canChangeMail = true;
  327. } else {
  328. $desc = $data['desc'];
  329. $canChangeStream = in_array(IExtension::METHOD_STREAM, $data['methods']);
  330. $canChangeMail = in_array(IExtension::METHOD_MAIL, $data['methods']);
  331. }
  332. $this->settings[$type] = new LegacySetting(
  333. $type, $desc,
  334. $canChangeStream, in_array($type, $streamTypes),
  335. $canChangeMail, in_array($type, $mailTypes)
  336. );
  337. }
  338. $this->loadedLegacyTypes = true;
  339. }
  340. foreach ($this->settingsClasses as $class => $false) {
  341. /** @var ISetting $setting */
  342. $setting = \OC::$server->query($class);
  343. if (!$setting instanceof ISetting) {
  344. throw new \InvalidArgumentException('Invalid activity filter registered');
  345. }
  346. $this->settings[$setting->getIdentifier()] = $setting;
  347. unset($this->settingsClasses[$class]);
  348. }
  349. return $this->settings;
  350. }
  351. /**
  352. * @param string $id
  353. * @return ISetting
  354. * @throws \InvalidArgumentException when the setting was not found
  355. * @since 11.0.0
  356. */
  357. public function getSettingById($id) {
  358. $settings = $this->getSettings();
  359. if (isset($settings[$id])) {
  360. return $settings[$id];
  361. }
  362. throw new \InvalidArgumentException('Requested setting does not exist');
  363. }
  364. /**
  365. * @param string $type
  366. * @return string
  367. */
  368. public function getTypeIcon($type) {
  369. if (isset($this->typeIcons[$type])) {
  370. return $this->typeIcons[$type];
  371. }
  372. foreach ($this->getExtensions() as $c) {
  373. $icon = $c->getTypeIcon($type);
  374. if (is_string($icon)) {
  375. $this->typeIcons[$type] = $icon;
  376. return $icon;
  377. }
  378. }
  379. $this->typeIcons[$type] = '';
  380. return '';
  381. }
  382. /**
  383. * @param string $type
  384. * @param string $id
  385. */
  386. public function setFormattingObject($type, $id) {
  387. $this->formattingObjectType = $type;
  388. $this->formattingObjectId = (string) $id;
  389. }
  390. /**
  391. * @return bool
  392. */
  393. public function isFormattingFilteredObject() {
  394. return $this->formattingObjectType !== null && $this->formattingObjectId !== null
  395. && $this->formattingObjectType === $this->request->getParam('object_type')
  396. && $this->formattingObjectId === $this->request->getParam('object_id');
  397. }
  398. /**
  399. * @param bool $status Set to true, when parsing events should not use SVG icons
  400. */
  401. public function setRequirePNG($status) {
  402. $this->requirePNG = $status;
  403. }
  404. /**
  405. * @return bool
  406. */
  407. public function getRequirePNG() {
  408. return $this->requirePNG;
  409. }
  410. /**
  411. * @param string $app
  412. * @param string $text
  413. * @param array $params
  414. * @param boolean $stripPath
  415. * @param boolean $highlightParams
  416. * @param string $languageCode
  417. * @return string|false
  418. */
  419. public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) {
  420. foreach ($this->getExtensions() as $c) {
  421. $translation = $c->translate($app, $text, $params, $stripPath, $highlightParams, $languageCode);
  422. if (is_string($translation)) {
  423. return $translation;
  424. }
  425. }
  426. return false;
  427. }
  428. /**
  429. * @param string $app
  430. * @param string $text
  431. * @return array|false
  432. */
  433. public function getSpecialParameterList($app, $text) {
  434. if (isset($this->specialParameters[$app][$text])) {
  435. return $this->specialParameters[$app][$text];
  436. }
  437. if (!isset($this->specialParameters[$app])) {
  438. $this->specialParameters[$app] = array();
  439. }
  440. foreach ($this->getExtensions() as $c) {
  441. $specialParameter = $c->getSpecialParameterList($app, $text);
  442. if (is_array($specialParameter)) {
  443. $this->specialParameters[$app][$text] = $specialParameter;
  444. return $specialParameter;
  445. }
  446. }
  447. $this->specialParameters[$app][$text] = false;
  448. return false;
  449. }
  450. /**
  451. * @param array $activity
  452. * @return integer|false
  453. */
  454. public function getGroupParameter($activity) {
  455. foreach ($this->getExtensions() as $c) {
  456. $parameter = $c->getGroupParameter($activity);
  457. if ($parameter !== false) {
  458. return $parameter;
  459. }
  460. }
  461. return false;
  462. }
  463. /**
  464. * Set the user we need to use
  465. *
  466. * @param string|null $currentUserId
  467. * @throws \UnexpectedValueException If the user is invalid
  468. */
  469. public function setCurrentUserId($currentUserId) {
  470. if (!is_string($currentUserId) && $currentUserId !== null) {
  471. throw new \UnexpectedValueException('The given current user is invalid');
  472. }
  473. $this->currentUserId = $currentUserId;
  474. }
  475. /**
  476. * Get the user we need to use
  477. *
  478. * Either the user is logged in, or we try to get it from the token
  479. *
  480. * @return string
  481. * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique
  482. */
  483. public function getCurrentUserId() {
  484. if ($this->currentUserId !== null) {
  485. return $this->currentUserId;
  486. } else if (!$this->session->isLoggedIn()) {
  487. return $this->getUserFromToken();
  488. } else {
  489. return $this->session->getUser()->getUID();
  490. }
  491. }
  492. /**
  493. * Get the user for the token
  494. *
  495. * @return string
  496. * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique
  497. */
  498. protected function getUserFromToken() {
  499. $token = (string) $this->request->getParam('token', '');
  500. if (strlen($token) !== 30) {
  501. throw new \UnexpectedValueException('The token is invalid');
  502. }
  503. $users = $this->config->getUsersForUserValue('activity', 'rsstoken', $token);
  504. if (sizeof($users) !== 1) {
  505. // No unique user found
  506. throw new \UnexpectedValueException('The token is invalid');
  507. }
  508. // Token found login as that user
  509. return array_shift($users);
  510. }
  511. /**
  512. * @return array
  513. * @deprecated 11.0.0 - Use getFilters() instead
  514. */
  515. public function getNavigation() {
  516. $entries = array(
  517. 'apps' => array(),
  518. 'top' => array(),
  519. );
  520. foreach ($this->getExtensions() as $c) {
  521. $additionalEntries = $c->getNavigation();
  522. if (is_array($additionalEntries)) {
  523. $entries['apps'] = array_merge($entries['apps'], $additionalEntries['apps']);
  524. $entries['top'] = array_merge($entries['top'], $additionalEntries['top']);
  525. }
  526. }
  527. return $entries;
  528. }
  529. /**
  530. * @param string $filterValue
  531. * @return boolean
  532. * @deprecated 11.0.0 - Use getFilterById() instead
  533. */
  534. public function isFilterValid($filterValue) {
  535. if (isset($this->validFilters[$filterValue])) {
  536. return $this->validFilters[$filterValue];
  537. }
  538. foreach ($this->getExtensions() as $c) {
  539. if ($c->isFilterValid($filterValue) === true) {
  540. $this->validFilters[$filterValue] = true;
  541. return true;
  542. }
  543. }
  544. $this->validFilters[$filterValue] = false;
  545. return false;
  546. }
  547. /**
  548. * @param array $types
  549. * @param string $filter
  550. * @return array
  551. * @deprecated 11.0.0 - Use getFilterById()->filterTypes() instead
  552. */
  553. public function filterNotificationTypes($types, $filter) {
  554. if (!$this->isFilterValid($filter)) {
  555. return $types;
  556. }
  557. foreach ($this->getExtensions() as $c) {
  558. $result = $c->filterNotificationTypes($types, $filter);
  559. if (is_array($result)) {
  560. $types = $result;
  561. }
  562. }
  563. return $types;
  564. }
  565. /**
  566. * @param string $filter
  567. * @return array
  568. * @deprecated 11.0.0 - Use getFilterById() instead
  569. */
  570. public function getQueryForFilter($filter) {
  571. if (!$this->isFilterValid($filter)) {
  572. return [null, null];
  573. }
  574. $conditions = array();
  575. $parameters = array();
  576. foreach ($this->getExtensions() as $c) {
  577. $result = $c->getQueryForFilter($filter);
  578. if (is_array($result)) {
  579. list($condition, $parameter) = $result;
  580. if ($condition && is_array($parameter)) {
  581. $conditions[] = $condition;
  582. $parameters = array_merge($parameters, $parameter);
  583. }
  584. }
  585. }
  586. if (empty($conditions)) {
  587. return array(null, null);
  588. }
  589. return array(' and ((' . implode(') or (', $conditions) . '))', $parameters);
  590. }
  591. /**
  592. * Will return additional notification types as specified by other apps
  593. *
  594. * @param string $languageCode
  595. * @return array
  596. * @deprecated 11.0.0 - Use getSettings() instead
  597. */
  598. public function getNotificationTypes($languageCode) {
  599. $notificationTypes = $sharingNotificationTypes = [];
  600. foreach ($this->getExtensions() as $c) {
  601. $result = $c->getNotificationTypes($languageCode);
  602. if (is_array($result)) {
  603. $notificationTypes = array_merge($notificationTypes, $result);
  604. }
  605. }
  606. return array_merge($sharingNotificationTypes, $notificationTypes);
  607. }
  608. /**
  609. * @param string $method
  610. * @return array
  611. * @deprecated 11.0.0 - Use getSettings()->isDefaulEnabled<method>() instead
  612. */
  613. public function getDefaultTypes($method) {
  614. $defaultTypes = array();
  615. foreach ($this->getExtensions() as $c) {
  616. $types = $c->getDefaultTypes($method);
  617. if (is_array($types)) {
  618. $defaultTypes = array_merge($types, $defaultTypes);
  619. }
  620. }
  621. return $defaultTypes;
  622. }
  623. }