Manager.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Activity;
  27. use OCP\Activity\IConsumer;
  28. use OCP\Activity\IEvent;
  29. use OCP\Activity\IFilter;
  30. use OCP\Activity\IManager;
  31. use OCP\Activity\IProvider;
  32. use OCP\Activity\ISetting;
  33. use OCP\IConfig;
  34. use OCP\IRequest;
  35. use OCP\IUser;
  36. use OCP\IUserSession;
  37. use OCP\RichObjectStrings\IValidator;
  38. class Manager implements IManager {
  39. /** @var IRequest */
  40. protected $request;
  41. /** @var IUserSession */
  42. protected $session;
  43. /** @var IConfig */
  44. protected $config;
  45. /** @var IValidator */
  46. protected $validator;
  47. /** @var string */
  48. protected $formattingObjectType;
  49. /** @var int */
  50. protected $formattingObjectId;
  51. /** @var bool */
  52. protected $requirePNG;
  53. /** @var string */
  54. protected $currentUserId;
  55. public function __construct(IRequest $request,
  56. IUserSession $session,
  57. IConfig $config,
  58. IValidator $validator) {
  59. $this->request = $request;
  60. $this->session = $session;
  61. $this->config = $config;
  62. $this->validator = $validator;
  63. }
  64. /** @var \Closure[] */
  65. private $consumersClosures = array();
  66. /** @var IConsumer[] */
  67. private $consumers = array();
  68. /**
  69. * @return \OCP\Activity\IConsumer[]
  70. */
  71. protected function getConsumers(): array {
  72. if (!empty($this->consumers)) {
  73. return $this->consumers;
  74. }
  75. $this->consumers = [];
  76. foreach($this->consumersClosures as $consumer) {
  77. $c = $consumer();
  78. if ($c instanceof IConsumer) {
  79. $this->consumers[] = $c;
  80. } else {
  81. throw new \InvalidArgumentException('The given consumer does not implement the \OCP\Activity\IConsumer interface');
  82. }
  83. }
  84. return $this->consumers;
  85. }
  86. /**
  87. * Generates a new IEvent object
  88. *
  89. * Make sure to call at least the following methods before sending it to the
  90. * app with via the publish() method:
  91. * - setApp()
  92. * - setType()
  93. * - setAffectedUser()
  94. * - setSubject()
  95. *
  96. * @return IEvent
  97. */
  98. public function generateEvent(): IEvent {
  99. return new Event($this->validator);
  100. }
  101. /**
  102. * Publish an event to the activity consumers
  103. *
  104. * Make sure to call at least the following methods before sending an Event:
  105. * - setApp()
  106. * - setType()
  107. * - setAffectedUser()
  108. * - setSubject()
  109. *
  110. * @param IEvent $event
  111. * @throws \BadMethodCallException if required values have not been set
  112. */
  113. public function publish(IEvent $event): void {
  114. if ($event->getAuthor() === '') {
  115. if ($this->session->getUser() instanceof IUser) {
  116. $event->setAuthor($this->session->getUser()->getUID());
  117. }
  118. }
  119. if (!$event->getTimestamp()) {
  120. $event->setTimestamp(time());
  121. }
  122. if (!$event->isValid()) {
  123. throw new \BadMethodCallException('The given event is invalid');
  124. }
  125. foreach ($this->getConsumers() as $c) {
  126. $c->receive($event);
  127. }
  128. }
  129. /**
  130. * In order to improve lazy loading a closure can be registered which will be called in case
  131. * activity consumers are actually requested
  132. *
  133. * $callable has to return an instance of OCA\Activity\IConsumer
  134. *
  135. * @param \Closure $callable
  136. */
  137. public function registerConsumer(\Closure $callable): void {
  138. $this->consumersClosures[] = $callable;
  139. $this->consumers = [];
  140. }
  141. /** @var string[] */
  142. protected $filterClasses = [];
  143. /** @var IFilter[] */
  144. protected $filters = [];
  145. /**
  146. * @param string $filter Class must implement OCA\Activity\IFilter
  147. * @return void
  148. */
  149. public function registerFilter(string $filter): void {
  150. $this->filterClasses[$filter] = false;
  151. }
  152. /**
  153. * @return IFilter[]
  154. * @throws \InvalidArgumentException
  155. */
  156. public function getFilters(): array {
  157. foreach ($this->filterClasses as $class => $false) {
  158. /** @var IFilter $filter */
  159. $filter = \OC::$server->query($class);
  160. if (!$filter instanceof IFilter) {
  161. throw new \InvalidArgumentException('Invalid activity filter registered');
  162. }
  163. $this->filters[$filter->getIdentifier()] = $filter;
  164. unset($this->filterClasses[$class]);
  165. }
  166. return $this->filters;
  167. }
  168. /**
  169. * @param string $id
  170. * @return IFilter
  171. * @throws \InvalidArgumentException when the filter was not found
  172. * @since 11.0.0
  173. */
  174. public function getFilterById(string $id): IFilter {
  175. $filters = $this->getFilters();
  176. if (isset($filters[$id])) {
  177. return $filters[$id];
  178. }
  179. throw new \InvalidArgumentException('Requested filter does not exist');
  180. }
  181. /** @var string[] */
  182. protected $providerClasses = [];
  183. /** @var IProvider[] */
  184. protected $providers = [];
  185. /**
  186. * @param string $provider Class must implement OCA\Activity\IProvider
  187. * @return void
  188. */
  189. public function registerProvider(string $provider): void {
  190. $this->providerClasses[$provider] = false;
  191. }
  192. /**
  193. * @return IProvider[]
  194. * @throws \InvalidArgumentException
  195. */
  196. public function getProviders(): array {
  197. foreach ($this->providerClasses as $class => $false) {
  198. /** @var IProvider $provider */
  199. $provider = \OC::$server->query($class);
  200. if (!$provider instanceof IProvider) {
  201. throw new \InvalidArgumentException('Invalid activity provider registered');
  202. }
  203. $this->providers[] = $provider;
  204. unset($this->providerClasses[$class]);
  205. }
  206. return $this->providers;
  207. }
  208. /** @var string[] */
  209. protected $settingsClasses = [];
  210. /** @var ISetting[] */
  211. protected $settings = [];
  212. /**
  213. * @param string $setting Class must implement OCA\Activity\ISetting
  214. * @return void
  215. */
  216. public function registerSetting(string $setting): void {
  217. $this->settingsClasses[$setting] = false;
  218. }
  219. /**
  220. * @return ISetting[]
  221. * @throws \InvalidArgumentException
  222. */
  223. public function getSettings(): array {
  224. foreach ($this->settingsClasses as $class => $false) {
  225. /** @var ISetting $setting */
  226. $setting = \OC::$server->query($class);
  227. if (!$setting instanceof ISetting) {
  228. throw new \InvalidArgumentException('Invalid activity filter registered');
  229. }
  230. $this->settings[$setting->getIdentifier()] = $setting;
  231. unset($this->settingsClasses[$class]);
  232. }
  233. return $this->settings;
  234. }
  235. /**
  236. * @param string $id
  237. * @return ISetting
  238. * @throws \InvalidArgumentException when the setting was not found
  239. * @since 11.0.0
  240. */
  241. public function getSettingById(string $id): ISetting {
  242. $settings = $this->getSettings();
  243. if (isset($settings[$id])) {
  244. return $settings[$id];
  245. }
  246. throw new \InvalidArgumentException('Requested setting does not exist');
  247. }
  248. /**
  249. * @param string $type
  250. * @param int $id
  251. */
  252. public function setFormattingObject(string $type, int $id): void {
  253. $this->formattingObjectType = $type;
  254. $this->formattingObjectId = $id;
  255. }
  256. /**
  257. * @return bool
  258. */
  259. public function isFormattingFilteredObject(): bool {
  260. return $this->formattingObjectType !== null && $this->formattingObjectId !== null
  261. && $this->formattingObjectType === $this->request->getParam('object_type')
  262. && $this->formattingObjectId === (int) $this->request->getParam('object_id');
  263. }
  264. /**
  265. * @param bool $status Set to true, when parsing events should not use SVG icons
  266. */
  267. public function setRequirePNG(bool $status): void {
  268. $this->requirePNG = $status;
  269. }
  270. /**
  271. * @return bool
  272. */
  273. public function getRequirePNG(): bool {
  274. return $this->requirePNG;
  275. }
  276. /**
  277. * Set the user we need to use
  278. *
  279. * @param string|null $currentUserId
  280. * @throws \UnexpectedValueException If the user is invalid
  281. */
  282. public function setCurrentUserId(string $currentUserId = null): void {
  283. if (!is_string($currentUserId) && $currentUserId !== null) {
  284. throw new \UnexpectedValueException('The given current user is invalid');
  285. }
  286. $this->currentUserId = $currentUserId;
  287. }
  288. /**
  289. * Get the user we need to use
  290. *
  291. * Either the user is logged in, or we try to get it from the token
  292. *
  293. * @return string
  294. * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique
  295. */
  296. public function getCurrentUserId(): string {
  297. if ($this->currentUserId !== null) {
  298. return $this->currentUserId;
  299. }
  300. if (!$this->session->isLoggedIn()) {
  301. return $this->getUserFromToken();
  302. }
  303. return $this->session->getUser()->getUID();
  304. }
  305. /**
  306. * Get the user for the token
  307. *
  308. * @return string
  309. * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique
  310. */
  311. protected function getUserFromToken(): string {
  312. $token = (string) $this->request->getParam('token', '');
  313. if (strlen($token) !== 30) {
  314. throw new \UnexpectedValueException('The token is invalid');
  315. }
  316. $users = $this->config->getUsersForUserValue('activity', 'rsstoken', $token);
  317. if (count($users) !== 1) {
  318. // No unique user found
  319. throw new \UnexpectedValueException('The token is invalid');
  320. }
  321. // Token found login as that user
  322. return array_shift($users);
  323. }
  324. }