diff options
Diffstat (limited to 'lib/private/Activity/Manager.php')
-rw-r--r-- | lib/private/Activity/Manager.php | 140 |
1 files changed, 61 insertions, 79 deletions
diff --git a/lib/private/Activity/Manager.php b/lib/private/Activity/Manager.php index 14069260c6c..7471b7d708a 100644 --- a/lib/private/Activity/Manager.php +++ b/lib/private/Activity/Manager.php @@ -1,59 +1,33 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com> - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Daniel Kesselberg <mail@danielkesselberg.de> - * @author Joas Schilling <coding@schilljs.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OC\Activity; use OCP\Activity\ActivitySettings; +use OCP\Activity\Exceptions\FilterNotFoundException; +use OCP\Activity\Exceptions\IncompleteActivityException; +use OCP\Activity\Exceptions\SettingNotFoundException; +use OCP\Activity\IBulkConsumer; use OCP\Activity\IConsumer; use OCP\Activity\IEvent; use OCP\Activity\IFilter; use OCP\Activity\IManager; use OCP\Activity\IProvider; use OCP\Activity\ISetting; +use OCP\AppFramework\Utility\ITimeFactory; use OCP\IConfig; use OCP\IL10N; use OCP\IRequest; use OCP\IUser; use OCP\IUserSession; +use OCP\RichObjectStrings\IRichTextFormatter; use OCP\RichObjectStrings\IValidator; class Manager implements IManager { - /** @var IRequest */ - protected $request; - - /** @var IUserSession */ - protected $session; - - /** @var IConfig */ - protected $config; - - /** @var IValidator */ - protected $validator; /** @var string */ protected $formattingObjectType; @@ -67,20 +41,15 @@ class Manager implements IManager { /** @var string */ protected $currentUserId; - protected $l10n; - public function __construct( - IRequest $request, - IUserSession $session, - IConfig $config, - IValidator $validator, - IL10N $l10n + protected IRequest $request, + protected IUserSession $session, + protected IConfig $config, + protected IValidator $validator, + protected IRichTextFormatter $richTextFormatter, + protected IL10N $l10n, + protected ITimeFactory $timeFactory, ) { - $this->request = $request; - $this->session = $session; - $this->config = $config; - $this->validator = $validator; - $this->l10n = $l10n; } /** @var \Closure[] */ @@ -123,22 +92,38 @@ class Manager implements IManager { * @return IEvent */ public function generateEvent(): IEvent { - return new Event($this->validator); + return new Event($this->validator, $this->richTextFormatter); } /** - * Publish an event to the activity consumers - * - * Make sure to call at least the following methods before sending an Event: - * - setApp() - * - setType() - * - setAffectedUser() - * - setSubject() - * - * @param IEvent $event - * @throws \BadMethodCallException if required values have not been set + * {@inheritDoc} */ public function publish(IEvent $event): void { + if ($event->getAuthor() === '' && $this->session->getUser() instanceof IUser) { + $event->setAuthor($this->session->getUser()->getUID()); + } + + if (!$event->getTimestamp()) { + $event->setTimestamp($this->timeFactory->getTime()); + } + + if ($event->getAffectedUser() === '' || !$event->isValid()) { + throw new IncompleteActivityException('The given event is invalid'); + } + + foreach ($this->getConsumers() as $c) { + $c->receive($event); + } + } + + /** + * {@inheritDoc} + */ + public function bulkPublish(IEvent $event, array $affectedUserIds, ISetting $setting): void { + if (empty($affectedUserIds)) { + throw new IncompleteActivityException('The given event is invalid'); + } + if ($event->getAuthor() === '') { if ($this->session->getUser() instanceof IUser) { $event->setAuthor($this->session->getUser()->getUID()); @@ -146,18 +131,25 @@ class Manager implements IManager { } if (!$event->getTimestamp()) { - $event->setTimestamp(time()); + $event->setTimestamp($this->timeFactory->getTime()); } if (!$event->isValid()) { - throw new \BadMethodCallException('The given event is invalid'); + throw new IncompleteActivityException('The given event is invalid'); } foreach ($this->getConsumers() as $c) { - $c->receive($event); + if ($c instanceof IBulkConsumer) { + $c->bulkReceive($event, $affectedUserIds, $setting); + } + foreach ($affectedUserIds as $affectedUserId) { + $event->setAffectedUser($affectedUserId); + $c->receive($event); + } } } + /** * In order to improve lazy loading a closure can be registered which will be called in case * activity consumers are actually requested @@ -206,10 +198,7 @@ class Manager implements IManager { } /** - * @param string $id - * @return IFilter - * @throws \InvalidArgumentException when the filter was not found - * @since 11.0.0 + * {@inheritDoc} */ public function getFilterById(string $id): IFilter { $filters = $this->getFilters(); @@ -218,7 +207,7 @@ class Manager implements IManager { return $filters[$id]; } - throw new \InvalidArgumentException('Requested filter does not exist'); + throw new FilterNotFoundException($id); } /** @var string[] */ @@ -294,10 +283,7 @@ class Manager implements IManager { } /** - * @param string $id - * @return ActivitySettings - * @throws \InvalidArgumentException when the setting was not found - * @since 11.0.0 + * {@inheritDoc} */ public function getSettingById(string $id): ActivitySettings { $settings = $this->getSettings(); @@ -306,7 +292,7 @@ class Manager implements IManager { return $settings[$id]; } - throw new \InvalidArgumentException('Requested setting does not exist'); + throw new SettingNotFoundException($id); } @@ -325,7 +311,7 @@ class Manager implements IManager { public function isFormattingFilteredObject(): bool { return $this->formattingObjectType !== null && $this->formattingObjectId !== null && $this->formattingObjectType === $this->request->getParam('object_type') - && $this->formattingObjectId === (int) $this->request->getParam('object_id'); + && $this->formattingObjectId === (int)$this->request->getParam('object_id'); } /** @@ -346,12 +332,8 @@ class Manager implements IManager { * Set the user we need to use * * @param string|null $currentUserId - * @throws \UnexpectedValueException If the user is invalid */ - public function setCurrentUserId(string $currentUserId = null): void { - if (!is_string($currentUserId) && $currentUserId !== null) { - throw new \UnexpectedValueException('The given current user is invalid'); - } + public function setCurrentUserId(?string $currentUserId = null): void { $this->currentUserId = $currentUserId; } @@ -382,7 +364,7 @@ class Manager implements IManager { * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique */ protected function getUserFromToken(): string { - $token = (string) $this->request->getParam('token', ''); + $token = (string)$this->request->getParam('token', ''); if (strlen($token) !== 30) { throw new \UnexpectedValueException('The token is invalid'); } |