activitymanager.php 14 KB

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