activitymanager.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. class ActivityManager implements IManager {
  30. /**
  31. * @var \Closure[]
  32. */
  33. private $consumers = array();
  34. /**
  35. * @var \Closure[]
  36. */
  37. private $extensions = array();
  38. /** @var array list of filters "name" => "is valid" */
  39. protected $validFilters = array(
  40. 'all' => true,
  41. 'by' => true,
  42. 'self' => true,
  43. );
  44. /** @var array list of type icons "type" => "css class" */
  45. protected $typeIcons = array();
  46. /** @var array list of special parameters "app" => ["text" => ["parameter" => "type"]] */
  47. protected $specialParameters = array();
  48. /**
  49. * @param $app
  50. * @param $subject
  51. * @param $subjectParams
  52. * @param $message
  53. * @param $messageParams
  54. * @param $file
  55. * @param $link
  56. * @param $affectedUser
  57. * @param $type
  58. * @param $priority
  59. * @return mixed
  60. */
  61. function publishActivity($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority) {
  62. foreach($this->consumers as $consumer) {
  63. $c = $consumer();
  64. if ($c instanceof IConsumer) {
  65. try {
  66. $c->receive(
  67. $app,
  68. $subject,
  69. $subjectParams,
  70. $message,
  71. $messageParams,
  72. $file,
  73. $link,
  74. $affectedUser,
  75. $type,
  76. $priority);
  77. } catch (\Exception $ex) {
  78. // TODO: log the exception
  79. }
  80. }
  81. }
  82. }
  83. /**
  84. * In order to improve lazy loading a closure can be registered which will be called in case
  85. * activity consumers are actually requested
  86. *
  87. * $callable has to return an instance of OCA\Activity\IConsumer
  88. *
  89. * @param \Closure $callable
  90. */
  91. function registerConsumer(\Closure $callable) {
  92. array_push($this->consumers, $callable);
  93. }
  94. /**
  95. * In order to improve lazy loading a closure can be registered which will be called in case
  96. * activity consumers are actually requested
  97. *
  98. * $callable has to return an instance of OCA\Activity\IConsumer
  99. *
  100. * @param \Closure $callable
  101. * @return void
  102. */
  103. function registerExtension(\Closure $callable) {
  104. array_push($this->extensions, $callable);
  105. }
  106. /**
  107. * Will return additional notification types as specified by other apps
  108. *
  109. * @param string $languageCode
  110. * @return array
  111. */
  112. function getNotificationTypes($languageCode) {
  113. $notificationTypes = array();
  114. foreach($this->extensions as $extension) {
  115. $c = $extension();
  116. if ($c instanceof IExtension) {
  117. $result = $c->getNotificationTypes($languageCode);
  118. if (is_array($result)) {
  119. $notificationTypes = array_merge($notificationTypes, $result);
  120. }
  121. }
  122. }
  123. return $notificationTypes;
  124. }
  125. /**
  126. * @param string $method
  127. * @return array
  128. */
  129. function getDefaultTypes($method) {
  130. $defaultTypes = array();
  131. foreach($this->extensions as $extension) {
  132. $c = $extension();
  133. if ($c instanceof IExtension) {
  134. $types = $c->getDefaultTypes($method);
  135. if (is_array($types)) {
  136. $defaultTypes = array_merge($types, $defaultTypes);
  137. }
  138. }
  139. }
  140. return $defaultTypes;
  141. }
  142. /**
  143. * @param string $type
  144. * @return string
  145. */
  146. function getTypeIcon($type) {
  147. if (isset($this->typeIcons[$type])) {
  148. return $this->typeIcons[$type];
  149. }
  150. foreach($this->extensions as $extension) {
  151. $c = $extension();
  152. if ($c instanceof IExtension) {
  153. $icon = $c->getTypeIcon($type);
  154. if (is_string($icon)) {
  155. $this->typeIcons[$type] = $icon;
  156. return $icon;
  157. }
  158. }
  159. }
  160. $this->typeIcons[$type] = '';
  161. return '';
  162. }
  163. /**
  164. * @param string $app
  165. * @param string $text
  166. * @param array $params
  167. * @param boolean $stripPath
  168. * @param boolean $highlightParams
  169. * @param string $languageCode
  170. * @return string|false
  171. */
  172. function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) {
  173. foreach($this->extensions as $extension) {
  174. $c = $extension();
  175. if ($c instanceof IExtension) {
  176. $translation = $c->translate($app, $text, $params, $stripPath, $highlightParams, $languageCode);
  177. if (is_string($translation)) {
  178. return $translation;
  179. }
  180. }
  181. }
  182. return false;
  183. }
  184. /**
  185. * @param string $app
  186. * @param string $text
  187. * @return array|false
  188. */
  189. function getSpecialParameterList($app, $text) {
  190. if (isset($this->specialParameters[$app][$text])) {
  191. return $this->specialParameters[$app][$text];
  192. }
  193. if (!isset($this->specialParameters[$app])) {
  194. $this->specialParameters[$app] = array();
  195. }
  196. foreach($this->extensions as $extension) {
  197. $c = $extension();
  198. if ($c instanceof IExtension) {
  199. $specialParameter = $c->getSpecialParameterList($app, $text);
  200. if (is_array($specialParameter)) {
  201. $this->specialParameters[$app][$text] = $specialParameter;
  202. return $specialParameter;
  203. }
  204. }
  205. }
  206. $this->specialParameters[$app][$text] = false;
  207. return false;
  208. }
  209. /**
  210. * @param array $activity
  211. * @return integer|false
  212. */
  213. function getGroupParameter($activity) {
  214. foreach($this->extensions as $extension) {
  215. $c = $extension();
  216. if ($c instanceof IExtension) {
  217. $parameter = $c->getGroupParameter($activity);
  218. if ($parameter !== false) {
  219. return $parameter;
  220. }
  221. }
  222. }
  223. return false;
  224. }
  225. /**
  226. * @return array
  227. */
  228. function getNavigation() {
  229. $entries = array(
  230. 'apps' => array(),
  231. 'top' => array(),
  232. );
  233. foreach($this->extensions as $extension) {
  234. $c = $extension();
  235. if ($c instanceof IExtension) {
  236. $additionalEntries = $c->getNavigation();
  237. if (is_array($additionalEntries)) {
  238. $entries['apps'] = array_merge($entries['apps'], $additionalEntries['apps']);
  239. $entries['top'] = array_merge($entries['top'], $additionalEntries['top']);
  240. }
  241. }
  242. }
  243. return $entries;
  244. }
  245. /**
  246. * @param string $filterValue
  247. * @return boolean
  248. */
  249. function isFilterValid($filterValue) {
  250. if (isset($this->validFilters[$filterValue])) {
  251. return $this->validFilters[$filterValue];
  252. }
  253. foreach($this->extensions as $extension) {
  254. $c = $extension();
  255. if ($c instanceof IExtension) {
  256. if ($c->isFilterValid($filterValue) === true) {
  257. $this->validFilters[$filterValue] = true;
  258. return true;
  259. }
  260. }
  261. }
  262. $this->validFilters[$filterValue] = false;
  263. return false;
  264. }
  265. /**
  266. * @param array $types
  267. * @param string $filter
  268. * @return array
  269. */
  270. function filterNotificationTypes($types, $filter) {
  271. if (!$this->isFilterValid($filter)) {
  272. return $types;
  273. }
  274. foreach($this->extensions as $extension) {
  275. $c = $extension();
  276. if ($c instanceof IExtension) {
  277. $result = $c->filterNotificationTypes($types, $filter);
  278. if (is_array($result)) {
  279. $types = $result;
  280. }
  281. }
  282. }
  283. return $types;
  284. }
  285. /**
  286. * @param string $filter
  287. * @return array
  288. */
  289. function getQueryForFilter($filter) {
  290. if (!$this->isFilterValid($filter)) {
  291. return [null, null];
  292. }
  293. $conditions = array();
  294. $parameters = array();
  295. foreach($this->extensions as $extension) {
  296. $c = $extension();
  297. if ($c instanceof IExtension) {
  298. $result = $c->getQueryForFilter($filter);
  299. if (is_array($result)) {
  300. list($condition, $parameter) = $result;
  301. if ($condition && is_array($parameter)) {
  302. $conditions[] = $condition;
  303. $parameters = array_merge($parameters, $parameter);
  304. }
  305. }
  306. }
  307. }
  308. if (empty($conditions)) {
  309. return array(null, null);
  310. }
  311. return array(' and ((' . implode(') or (', $conditions) . '))', $parameters);
  312. }
  313. }