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.

Manager.php 9.1KB

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