Manager.php 18 KB

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