You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

activitymanager.php 12KB

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