activitymanager.php 9.6 KB

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