activitymanager.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 Morris Jobke <hey@morrisjobke.de>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @copyright Copyright (c) 2015, ownCloud, Inc.
  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;
  26. use OCP\Activity\IConsumer;
  27. use OCP\Activity\IExtension;
  28. use OCP\Activity\IManager;
  29. use OCP\IConfig;
  30. use OCP\IRequest;
  31. use OCP\IUserSession;
  32. class ActivityManager implements IManager {
  33. /** @var IRequest */
  34. protected $request;
  35. /** @var IUserSession */
  36. protected $session;
  37. /** @var IConfig */
  38. protected $config;
  39. /**
  40. * constructor of the controller
  41. *
  42. * @param IRequest $request
  43. * @param IUserSession $session
  44. * @param IConfig $config
  45. */
  46. public function __construct(IRequest $request,
  47. IUserSession $session,
  48. IConfig $config) {
  49. $this->request = $request;
  50. $this->session = $session;
  51. $this->config = $config;
  52. }
  53. /**
  54. * @var \Closure[]
  55. */
  56. private $consumers = array();
  57. /**
  58. * @var \Closure[]
  59. */
  60. private $extensions = array();
  61. /** @var array list of filters "name" => "is valid" */
  62. protected $validFilters = array(
  63. 'all' => true,
  64. 'by' => true,
  65. 'self' => true,
  66. );
  67. /** @var array list of type icons "type" => "css class" */
  68. protected $typeIcons = array();
  69. /** @var array list of special parameters "app" => ["text" => ["parameter" => "type"]] */
  70. protected $specialParameters = array();
  71. /**
  72. * @param $app
  73. * @param $subject
  74. * @param $subjectParams
  75. * @param $message
  76. * @param $messageParams
  77. * @param $file
  78. * @param $link
  79. * @param $affectedUser
  80. * @param $type
  81. * @param $priority
  82. * @return mixed
  83. */
  84. function publishActivity($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority) {
  85. foreach($this->consumers as $consumer) {
  86. $c = $consumer();
  87. if ($c instanceof IConsumer) {
  88. try {
  89. $c->receive(
  90. $app,
  91. $subject,
  92. $subjectParams,
  93. $message,
  94. $messageParams,
  95. $file,
  96. $link,
  97. $affectedUser,
  98. $type,
  99. $priority);
  100. } catch (\Exception $ex) {
  101. // TODO: log the exception
  102. }
  103. }
  104. }
  105. }
  106. /**
  107. * In order to improve lazy loading a closure can be registered which will be called in case
  108. * activity consumers are actually requested
  109. *
  110. * $callable has to return an instance of OCA\Activity\IConsumer
  111. *
  112. * @param \Closure $callable
  113. */
  114. function registerConsumer(\Closure $callable) {
  115. array_push($this->consumers, $callable);
  116. }
  117. /**
  118. * In order to improve lazy loading a closure can be registered which will be called in case
  119. * activity consumers are actually requested
  120. *
  121. * $callable has to return an instance of OCA\Activity\IConsumer
  122. *
  123. * @param \Closure $callable
  124. * @return void
  125. */
  126. function registerExtension(\Closure $callable) {
  127. array_push($this->extensions, $callable);
  128. }
  129. /**
  130. * Will return additional notification types as specified by other apps
  131. *
  132. * @param string $languageCode
  133. * @return array
  134. */
  135. function getNotificationTypes($languageCode) {
  136. $notificationTypes = array();
  137. foreach($this->extensions as $extension) {
  138. $c = $extension();
  139. if ($c instanceof IExtension) {
  140. $result = $c->getNotificationTypes($languageCode);
  141. if (is_array($result)) {
  142. $notificationTypes = array_merge($notificationTypes, $result);
  143. }
  144. }
  145. }
  146. return $notificationTypes;
  147. }
  148. /**
  149. * @param string $method
  150. * @return array
  151. */
  152. function getDefaultTypes($method) {
  153. $defaultTypes = array();
  154. foreach($this->extensions as $extension) {
  155. $c = $extension();
  156. if ($c instanceof IExtension) {
  157. $types = $c->getDefaultTypes($method);
  158. if (is_array($types)) {
  159. $defaultTypes = array_merge($types, $defaultTypes);
  160. }
  161. }
  162. }
  163. return $defaultTypes;
  164. }
  165. /**
  166. * @param string $type
  167. * @return string
  168. */
  169. function getTypeIcon($type) {
  170. if (isset($this->typeIcons[$type])) {
  171. return $this->typeIcons[$type];
  172. }
  173. foreach($this->extensions as $extension) {
  174. $c = $extension();
  175. if ($c instanceof IExtension) {
  176. $icon = $c->getTypeIcon($type);
  177. if (is_string($icon)) {
  178. $this->typeIcons[$type] = $icon;
  179. return $icon;
  180. }
  181. }
  182. }
  183. $this->typeIcons[$type] = '';
  184. return '';
  185. }
  186. /**
  187. * @param string $app
  188. * @param string $text
  189. * @param array $params
  190. * @param boolean $stripPath
  191. * @param boolean $highlightParams
  192. * @param string $languageCode
  193. * @return string|false
  194. */
  195. function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) {
  196. foreach($this->extensions as $extension) {
  197. $c = $extension();
  198. if ($c instanceof IExtension) {
  199. $translation = $c->translate($app, $text, $params, $stripPath, $highlightParams, $languageCode);
  200. if (is_string($translation)) {
  201. return $translation;
  202. }
  203. }
  204. }
  205. return false;
  206. }
  207. /**
  208. * @param string $app
  209. * @param string $text
  210. * @return array|false
  211. */
  212. function getSpecialParameterList($app, $text) {
  213. if (isset($this->specialParameters[$app][$text])) {
  214. return $this->specialParameters[$app][$text];
  215. }
  216. if (!isset($this->specialParameters[$app])) {
  217. $this->specialParameters[$app] = array();
  218. }
  219. foreach($this->extensions as $extension) {
  220. $c = $extension();
  221. if ($c instanceof IExtension) {
  222. $specialParameter = $c->getSpecialParameterList($app, $text);
  223. if (is_array($specialParameter)) {
  224. $this->specialParameters[$app][$text] = $specialParameter;
  225. return $specialParameter;
  226. }
  227. }
  228. }
  229. $this->specialParameters[$app][$text] = false;
  230. return false;
  231. }
  232. /**
  233. * @param array $activity
  234. * @return integer|false
  235. */
  236. function getGroupParameter($activity) {
  237. foreach($this->extensions as $extension) {
  238. $c = $extension();
  239. if ($c instanceof IExtension) {
  240. $parameter = $c->getGroupParameter($activity);
  241. if ($parameter !== false) {
  242. return $parameter;
  243. }
  244. }
  245. }
  246. return false;
  247. }
  248. /**
  249. * @return array
  250. */
  251. function getNavigation() {
  252. $entries = array(
  253. 'apps' => array(),
  254. 'top' => array(),
  255. );
  256. foreach($this->extensions as $extension) {
  257. $c = $extension();
  258. if ($c instanceof IExtension) {
  259. $additionalEntries = $c->getNavigation();
  260. if (is_array($additionalEntries)) {
  261. $entries['apps'] = array_merge($entries['apps'], $additionalEntries['apps']);
  262. $entries['top'] = array_merge($entries['top'], $additionalEntries['top']);
  263. }
  264. }
  265. }
  266. return $entries;
  267. }
  268. /**
  269. * @param string $filterValue
  270. * @return boolean
  271. */
  272. function isFilterValid($filterValue) {
  273. if (isset($this->validFilters[$filterValue])) {
  274. return $this->validFilters[$filterValue];
  275. }
  276. foreach($this->extensions as $extension) {
  277. $c = $extension();
  278. if ($c instanceof IExtension) {
  279. if ($c->isFilterValid($filterValue) === true) {
  280. $this->validFilters[$filterValue] = true;
  281. return true;
  282. }
  283. }
  284. }
  285. $this->validFilters[$filterValue] = false;
  286. return false;
  287. }
  288. /**
  289. * @param array $types
  290. * @param string $filter
  291. * @return array
  292. */
  293. function filterNotificationTypes($types, $filter) {
  294. if (!$this->isFilterValid($filter)) {
  295. return $types;
  296. }
  297. foreach($this->extensions as $extension) {
  298. $c = $extension();
  299. if ($c instanceof IExtension) {
  300. $result = $c->filterNotificationTypes($types, $filter);
  301. if (is_array($result)) {
  302. $types = $result;
  303. }
  304. }
  305. }
  306. return $types;
  307. }
  308. /**
  309. * @param string $filter
  310. * @return array
  311. */
  312. function getQueryForFilter($filter) {
  313. if (!$this->isFilterValid($filter)) {
  314. return [null, null];
  315. }
  316. $conditions = array();
  317. $parameters = array();
  318. foreach($this->extensions as $extension) {
  319. $c = $extension();
  320. if ($c instanceof IExtension) {
  321. $result = $c->getQueryForFilter($filter);
  322. if (is_array($result)) {
  323. list($condition, $parameter) = $result;
  324. if ($condition && is_array($parameter)) {
  325. $conditions[] = $condition;
  326. $parameters = array_merge($parameters, $parameter);
  327. }
  328. }
  329. }
  330. }
  331. if (empty($conditions)) {
  332. return array(null, null);
  333. }
  334. return array(' and ((' . implode(') or (', $conditions) . '))', $parameters);
  335. }
  336. /**
  337. * Get the user we need to use
  338. *
  339. * Either the user is logged in, or we try to get it from the token
  340. *
  341. * @return string
  342. * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique
  343. */
  344. public function getCurrentUserId() {
  345. if (!$this->session->isLoggedIn()) {
  346. return $this->getUserFromToken();
  347. } else {
  348. return $this->session->getUser()->getUID();
  349. }
  350. }
  351. /**
  352. * Get the user for the token
  353. *
  354. * @return string
  355. * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique
  356. */
  357. protected function getUserFromToken() {
  358. $token = (string) $this->request->getParam('token', '');
  359. if (strlen($token) !== 30) {
  360. throw new \UnexpectedValueException('The token is invalid');
  361. }
  362. $users = $this->config->getUsersForUserValue('activity', 'rsstoken', $token);
  363. if (sizeof($users) !== 1) {
  364. // No unique user found
  365. throw new \UnexpectedValueException('The token is invalid');
  366. }
  367. // Token found login as that user
  368. return array_shift($users);
  369. }
  370. }