PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/private/activitymanager.php

https://gitlab.com/wuhang2003/core
PHP | 534 lines | 288 code | 67 blank | 179 comment | 48 complexity | dae2e8aa199429bb253b7434c77e0313 MD5 | raw file
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Thomas Müller <thomas.mueller@tmit.eu>
  6. *
  7. * @copyright Copyright (c) 2016, ownCloud, Inc.
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC;
  24. use OC\Activity\Event;
  25. use OCP\Activity\IConsumer;
  26. use OCP\Activity\IEvent;
  27. use OCP\Activity\IExtension;
  28. use OCP\Activity\IManager;
  29. use OCP\IConfig;
  30. use OCP\IRequest;
  31. use OCP\IUser;
  32. use OCP\IUserSession;
  33. class ActivityManager implements IManager {
  34. /** @var IRequest */
  35. protected $request;
  36. /** @var IUserSession */
  37. protected $session;
  38. /** @var IConfig */
  39. protected $config;
  40. /** @var string */
  41. protected $formattingObjectType;
  42. /** @var int */
  43. protected $formattingObjectId;
  44. /** @var string */
  45. protected $currentUserId;
  46. /**
  47. * constructor of the controller
  48. *
  49. * @param IRequest $request
  50. * @param IUserSession $session
  51. * @param IConfig $config
  52. */
  53. public function __construct(IRequest $request,
  54. IUserSession $session,
  55. IConfig $config) {
  56. $this->request = $request;
  57. $this->session = $session;
  58. $this->config = $config;
  59. }
  60. /** @var \Closure[] */
  61. private $consumersClosures = array();
  62. /** @var IConsumer[] */
  63. private $consumers = array();
  64. /** @var \Closure[] */
  65. private $extensionsClosures = array();
  66. /** @var IExtension[] */
  67. private $extensions = array();
  68. /** @var array list of filters "name" => "is valid" */
  69. protected $validFilters = array(
  70. 'all' => true,
  71. 'by' => true,
  72. 'self' => true,
  73. );
  74. /** @var array list of type icons "type" => "css class" */
  75. protected $typeIcons = array();
  76. /** @var array list of special parameters "app" => ["text" => ["parameter" => "type"]] */
  77. protected $specialParameters = array();
  78. /**
  79. * @return \OCP\Activity\IConsumer[]
  80. */
  81. protected function getConsumers() {
  82. if (!empty($this->consumers)) {
  83. return $this->consumers;
  84. }
  85. $this->consumers = [];
  86. foreach($this->consumersClosures as $consumer) {
  87. $c = $consumer();
  88. if ($c instanceof IConsumer) {
  89. $this->consumers[] = $c;
  90. } else {
  91. throw new \InvalidArgumentException('The given consumer does not implement the \OCP\Activity\IConsumer interface');
  92. }
  93. }
  94. return $this->consumers;
  95. }
  96. /**
  97. * @return \OCP\Activity\IExtension[]
  98. */
  99. protected function getExtensions() {
  100. if (!empty($this->extensions)) {
  101. return $this->extensions;
  102. }
  103. $this->extensions = [];
  104. foreach($this->extensionsClosures as $extension) {
  105. $e = $extension();
  106. if ($e instanceof IExtension) {
  107. $this->extensions[] = $e;
  108. } else {
  109. throw new \InvalidArgumentException('The given extension does not implement the \OCP\Activity\IExtension interface');
  110. }
  111. }
  112. return $this->extensions;
  113. }
  114. /**
  115. * Generates a new IEvent object
  116. *
  117. * Make sure to call at least the following methods before sending it to the
  118. * app with via the publish() method:
  119. * - setApp()
  120. * - setType()
  121. * - setAffectedUser()
  122. * - setSubject()
  123. *
  124. * @return IEvent
  125. */
  126. public function generateEvent() {
  127. return new Event();
  128. }
  129. /**
  130. * Publish an event to the activity consumers
  131. *
  132. * Make sure to call at least the following methods before sending an Event:
  133. * - setApp()
  134. * - setType()
  135. * - setAffectedUser()
  136. * - setSubject()
  137. *
  138. * @param IEvent $event
  139. * @return null
  140. * @throws \BadMethodCallException if required values have not been set
  141. */
  142. public function publish(IEvent $event) {
  143. if (!$event->getApp()) {
  144. throw new \BadMethodCallException('App not set', 10);
  145. }
  146. if (!$event->getType()) {
  147. throw new \BadMethodCallException('Type not set', 11);
  148. }
  149. if ($event->getAffectedUser() === null) {
  150. throw new \BadMethodCallException('Affected user not set', 12);
  151. }
  152. if ($event->getSubject() === null || $event->getSubjectParameters() === null) {
  153. throw new \BadMethodCallException('Subject not set', 13);
  154. }
  155. if ($event->getAuthor() === null) {
  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. foreach ($this->getConsumers() as $c) {
  164. $c->receive($event);
  165. }
  166. }
  167. /**
  168. * @param string $app The app where this event is associated with
  169. * @param string $subject A short description of the event
  170. * @param array $subjectParams Array with parameters that are filled in the subject
  171. * @param string $message A longer description of the event
  172. * @param array $messageParams Array with parameters that are filled in the message
  173. * @param string $file The file including path where this event is associated with
  174. * @param string $link A link where this event is associated with
  175. * @param string $affectedUser Recipient of the activity
  176. * @param string $type Type of the notification
  177. * @param int $priority Priority of the notification
  178. * @return null
  179. */
  180. public function publishActivity($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority) {
  181. $event = $this->generateEvent();
  182. $event->setApp($app)
  183. ->setType($type)
  184. ->setAffectedUser($affectedUser)
  185. ->setSubject($subject, $subjectParams)
  186. ->setMessage($message, $messageParams)
  187. ->setObject('', 0, $file)
  188. ->setLink($link);
  189. $this->publish($event);
  190. }
  191. /**
  192. * In order to improve lazy loading a closure can be registered which will be called in case
  193. * activity consumers are actually requested
  194. *
  195. * $callable has to return an instance of OCA\Activity\IConsumer
  196. *
  197. * @param \Closure $callable
  198. */
  199. public function registerConsumer(\Closure $callable) {
  200. array_push($this->consumersClosures, $callable);
  201. $this->consumers = [];
  202. }
  203. /**
  204. * In order to improve lazy loading a closure can be registered which will be called in case
  205. * activity consumers are actually requested
  206. *
  207. * $callable has to return an instance of OCA\Activity\IConsumer
  208. *
  209. * @param \Closure $callable
  210. * @return void
  211. */
  212. public function registerExtension(\Closure $callable) {
  213. array_push($this->extensionsClosures, $callable);
  214. $this->extensions = [];
  215. }
  216. /**
  217. * Will return additional notification types as specified by other apps
  218. *
  219. * @param string $languageCode
  220. * @return array
  221. */
  222. public function getNotificationTypes($languageCode) {
  223. $filesNotificationTypes = [];
  224. $sharingNotificationTypes = [];
  225. $notificationTypes = array();
  226. foreach ($this->getExtensions() as $c) {
  227. $result = $c->getNotificationTypes($languageCode);
  228. if (is_array($result)) {
  229. if (class_exists('\OCA\Files\Activity', false) && $c instanceof \OCA\Files\Activity) {
  230. $filesNotificationTypes = $result;
  231. continue;
  232. }
  233. if (class_exists('\OCA\Files_Sharing\Activity', false) && $c instanceof \OCA\Files_Sharing\Activity) {
  234. $sharingNotificationTypes = $result;
  235. continue;
  236. }
  237. $notificationTypes = array_merge($notificationTypes, $result);
  238. }
  239. }
  240. return array_merge($filesNotificationTypes, $sharingNotificationTypes, $notificationTypes);
  241. }
  242. /**
  243. * @param string $method
  244. * @return array
  245. */
  246. public function getDefaultTypes($method) {
  247. $defaultTypes = array();
  248. foreach ($this->getExtensions() as $c) {
  249. $types = $c->getDefaultTypes($method);
  250. if (is_array($types)) {
  251. $defaultTypes = array_merge($types, $defaultTypes);
  252. }
  253. }
  254. return $defaultTypes;
  255. }
  256. /**
  257. * @param string $type
  258. * @return string
  259. */
  260. public function getTypeIcon($type) {
  261. if (isset($this->typeIcons[$type])) {
  262. return $this->typeIcons[$type];
  263. }
  264. foreach ($this->getExtensions() as $c) {
  265. $icon = $c->getTypeIcon($type);
  266. if (is_string($icon)) {
  267. $this->typeIcons[$type] = $icon;
  268. return $icon;
  269. }
  270. }
  271. $this->typeIcons[$type] = '';
  272. return '';
  273. }
  274. /**
  275. * @param string $type
  276. * @param string $id
  277. */
  278. public function setFormattingObject($type, $id) {
  279. $this->formattingObjectType = $type;
  280. $this->formattingObjectId = (string) $id;
  281. }
  282. /**
  283. * @return bool
  284. */
  285. public function isFormattingFilteredObject() {
  286. return $this->formattingObjectType !== null && $this->formattingObjectId !== null
  287. && $this->formattingObjectType === $this->request->getParam('object_type')
  288. && $this->formattingObjectId === $this->request->getParam('object_id');
  289. }
  290. /**
  291. * @param string $app
  292. * @param string $text
  293. * @param array $params
  294. * @param boolean $stripPath
  295. * @param boolean $highlightParams
  296. * @param string $languageCode
  297. * @return string|false
  298. */
  299. public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) {
  300. foreach ($this->getExtensions() as $c) {
  301. $translation = $c->translate($app, $text, $params, $stripPath, $highlightParams, $languageCode);
  302. if (is_string($translation)) {
  303. return $translation;
  304. }
  305. }
  306. return false;
  307. }
  308. /**
  309. * @param string $app
  310. * @param string $text
  311. * @return array|false
  312. */
  313. public function getSpecialParameterList($app, $text) {
  314. if (isset($this->specialParameters[$app][$text])) {
  315. return $this->specialParameters[$app][$text];
  316. }
  317. if (!isset($this->specialParameters[$app])) {
  318. $this->specialParameters[$app] = array();
  319. }
  320. foreach ($this->getExtensions() as $c) {
  321. $specialParameter = $c->getSpecialParameterList($app, $text);
  322. if (is_array($specialParameter)) {
  323. $this->specialParameters[$app][$text] = $specialParameter;
  324. return $specialParameter;
  325. }
  326. }
  327. $this->specialParameters[$app][$text] = false;
  328. return false;
  329. }
  330. /**
  331. * @param array $activity
  332. * @return integer|false
  333. */
  334. public function getGroupParameter($activity) {
  335. foreach ($this->getExtensions() as $c) {
  336. $parameter = $c->getGroupParameter($activity);
  337. if ($parameter !== false) {
  338. return $parameter;
  339. }
  340. }
  341. return false;
  342. }
  343. /**
  344. * @return array
  345. */
  346. public function getNavigation() {
  347. $entries = array(
  348. 'apps' => array(),
  349. 'top' => array(),
  350. );
  351. foreach ($this->getExtensions() as $c) {
  352. $additionalEntries = $c->getNavigation();
  353. if (is_array($additionalEntries)) {
  354. $entries['apps'] = array_merge($entries['apps'], $additionalEntries['apps']);
  355. $entries['top'] = array_merge($entries['top'], $additionalEntries['top']);
  356. }
  357. }
  358. return $entries;
  359. }
  360. /**
  361. * @param string $filterValue
  362. * @return boolean
  363. */
  364. public function isFilterValid($filterValue) {
  365. if (isset($this->validFilters[$filterValue])) {
  366. return $this->validFilters[$filterValue];
  367. }
  368. foreach ($this->getExtensions() as $c) {
  369. if ($c->isFilterValid($filterValue) === true) {
  370. $this->validFilters[$filterValue] = true;
  371. return true;
  372. }
  373. }
  374. $this->validFilters[$filterValue] = false;
  375. return false;
  376. }
  377. /**
  378. * @param array $types
  379. * @param string $filter
  380. * @return array
  381. */
  382. public function filterNotificationTypes($types, $filter) {
  383. if (!$this->isFilterValid($filter)) {
  384. return $types;
  385. }
  386. foreach ($this->getExtensions() as $c) {
  387. $result = $c->filterNotificationTypes($types, $filter);
  388. if (is_array($result)) {
  389. $types = $result;
  390. }
  391. }
  392. return $types;
  393. }
  394. /**
  395. * @param string $filter
  396. * @return array
  397. */
  398. public function getQueryForFilter($filter) {
  399. if (!$this->isFilterValid($filter)) {
  400. return [null, null];
  401. }
  402. $conditions = array();
  403. $parameters = array();
  404. foreach ($this->getExtensions() as $c) {
  405. $result = $c->getQueryForFilter($filter);
  406. if (is_array($result)) {
  407. list($condition, $parameter) = $result;
  408. if ($condition && is_array($parameter)) {
  409. $conditions[] = $condition;
  410. $parameters = array_merge($parameters, $parameter);
  411. }
  412. }
  413. }
  414. if (empty($conditions)) {
  415. return array(null, null);
  416. }
  417. return array(' and ((' . implode(') or (', $conditions) . '))', $parameters);
  418. }
  419. /**
  420. * Set the user we need to use
  421. *
  422. * @param string|null $currentUserId
  423. * @throws \UnexpectedValueException If the user is invalid
  424. */
  425. public function setCurrentUserId($currentUserId) {
  426. if (!is_string($currentUserId) && $currentUserId !== null) {
  427. throw new \UnexpectedValueException('The given current user is invalid');
  428. }
  429. $this->currentUserId = $currentUserId;
  430. }
  431. /**
  432. * Get the user we need to use
  433. *
  434. * Either the user is logged in, or we try to get it from the token
  435. *
  436. * @return string
  437. * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique
  438. */
  439. public function getCurrentUserId() {
  440. if ($this->currentUserId !== null) {
  441. return $this->currentUserId;
  442. } else if (!$this->session->isLoggedIn()) {
  443. return $this->getUserFromToken();
  444. } else {
  445. return $this->session->getUser()->getUID();
  446. }
  447. }
  448. /**
  449. * Get the user for the token
  450. *
  451. * @return string
  452. * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique
  453. */
  454. protected function getUserFromToken() {
  455. $token = (string) $this->request->getParam('token', '');
  456. if (strlen($token) !== 30) {
  457. throw new \UnexpectedValueException('The token is invalid');
  458. }
  459. $users = $this->config->getUsersForUserValue('activity', 'rsstoken', $token);
  460. if (sizeof($users) !== 1) {
  461. // No unique user found
  462. throw new \UnexpectedValueException('The token is invalid');
  463. }
  464. // Token found login as that user
  465. return array_shift($users);
  466. }
  467. }