Manager.php 8.0 KB

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