Manager.php 8.2 KB

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