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.

401 lines
9.7 KiB

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