diff options
Diffstat (limited to 'lib/private/Activity')
-rw-r--r-- | lib/private/Activity/ActivitySettingsAdapter.php | 31 | ||||
-rw-r--r-- | lib/private/Activity/Event.php | 198 | ||||
-rw-r--r-- | lib/private/Activity/EventMerger.php | 27 | ||||
-rw-r--r-- | lib/private/Activity/Manager.php | 146 |
4 files changed, 136 insertions, 266 deletions
diff --git a/lib/private/Activity/ActivitySettingsAdapter.php b/lib/private/Activity/ActivitySettingsAdapter.php index eba13ad0350..27c85ee5554 100644 --- a/lib/private/Activity/ActivitySettingsAdapter.php +++ b/lib/private/Activity/ActivitySettingsAdapter.php @@ -3,25 +3,8 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2020 Robin Appelman <robin@icewind.nl> - * - * @author Robin Appelman <robin@icewind.nl> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OC\Activity; @@ -34,12 +17,10 @@ use OCP\IL10N; * class based one */ class ActivitySettingsAdapter extends ActivitySettings { - private $oldSettings; - private $l10n; - - public function __construct(ISetting $oldSettings, IL10N $l10n) { - $this->oldSettings = $oldSettings; - $this->l10n = $l10n; + public function __construct( + private ISetting $oldSettings, + private IL10N $l10n, + ) { } public function getIdentifier() { diff --git a/lib/private/Activity/Event.php b/lib/private/Activity/Event.php index fd0c0afd9d8..0ccad1d0a4e 100644 --- a/lib/private/Activity/Event.php +++ b/lib/private/Activity/Event.php @@ -3,37 +3,19 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com> - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * @author Phil Davis <phil.davis@inf.org> - * @author Robin Appelman <robin@icewind.nl> - * - * @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\Exceptions\InvalidValueException; use OCP\Activity\IEvent; use OCP\RichObjectStrings\InvalidObjectExeption; +use OCP\RichObjectStrings\IRichTextFormatter; use OCP\RichObjectStrings\IValidator; class Event implements IEvent { - /** @var string */ protected $app = ''; /** @var string */ @@ -52,7 +34,7 @@ class Event implements IEvent { protected $subjectParsed = ''; /** @var string */ protected $subjectRich = ''; - /** @var array */ + /** @var array<string, array<string, string>> */ protected $subjectRichParameters = []; /** @var string */ protected $message = ''; @@ -62,7 +44,7 @@ class Event implements IEvent { protected $messageParsed = ''; /** @var string */ protected $messageRich = ''; - /** @var array */ + /** @var array<string, array<string, string>> */ protected $messageRichParameters = []; /** @var string */ protected $objectType = ''; @@ -79,27 +61,19 @@ class Event implements IEvent { /** @var IEvent|null */ protected $child; - /** @var IValidator */ - protected $richValidator; - /** - * @param IValidator $richValidator - */ - public function __construct(IValidator $richValidator) { - $this->richValidator = $richValidator; + public function __construct( + protected IValidator $richValidator, + protected IRichTextFormatter $richTextFormatter, + ) { } /** - * Set the app of the activity - * - * @param string $app - * @return IEvent - * @throws \InvalidArgumentException if the app id is invalid - * @since 8.2.0 + * {@inheritDoc} */ public function setApp(string $app): IEvent { if ($app === '' || isset($app[32])) { - throw new \InvalidArgumentException('The given app is invalid'); + throw new InvalidValueException('app'); } $this->app = $app; return $this; @@ -113,16 +87,11 @@ class Event implements IEvent { } /** - * Set the type of the activity - * - * @param string $type - * @return IEvent - * @throws \InvalidArgumentException if the type is invalid - * @since 8.2.0 + * {@inheritDoc} */ public function setType(string $type): IEvent { if ($type === '' || isset($type[255])) { - throw new \InvalidArgumentException('The given type is invalid'); + throw new InvalidValueException('type'); } $this->type = $type; return $this; @@ -136,16 +105,11 @@ class Event implements IEvent { } /** - * Set the affected user of the activity - * - * @param string $affectedUser - * @return IEvent - * @throws \InvalidArgumentException if the affected user is invalid - * @since 8.2.0 + * {@inheritDoc} */ public function setAffectedUser(string $affectedUser): IEvent { if ($affectedUser === '' || isset($affectedUser[64])) { - throw new \InvalidArgumentException('The given affected user is invalid'); + throw new InvalidValueException('affectedUser'); } $this->affectedUser = $affectedUser; return $this; @@ -159,16 +123,11 @@ class Event implements IEvent { } /** - * Set the author of the activity - * - * @param string $author - * @return IEvent - * @throws \InvalidArgumentException if the author is invalid - * @since 8.2.0 + * {@inheritDoc} */ public function setAuthor(string $author): IEvent { if (isset($author[64])) { - throw new \InvalidArgumentException('The given author user is invalid'); + throw new InvalidValueException('author'); } $this->author = $author; return $this; @@ -182,14 +141,12 @@ class Event implements IEvent { } /** - * Set the timestamp of the activity - * - * @param int $timestamp - * @return IEvent - * @throws \InvalidArgumentException if the timestamp is invalid - * @since 8.2.0 + * {@inheritDoc} */ public function setTimestamp(int $timestamp): IEvent { + if ($timestamp < 0) { + throw new InvalidValueException('timestamp'); + } $this->timestamp = $timestamp; return $this; } @@ -202,17 +159,11 @@ class Event implements IEvent { } /** - * Set the subject of the activity - * - * @param string $subject - * @param array $parameters - * @return IEvent - * @throws \InvalidArgumentException if the subject or parameters are invalid - * @since 8.2.0 + * {@inheritDoc} */ public function setSubject(string $subject, array $parameters = []): IEvent { if (isset($subject[255])) { - throw new \InvalidArgumentException('The given subject is invalid'); + throw new InvalidValueException('subject'); } $this->subject = $subject; $this->subjectParameters = $parameters; @@ -234,14 +185,11 @@ class Event implements IEvent { } /** - * @param string $subject - * @return $this - * @throws \InvalidArgumentException if the subject is invalid - * @since 11.0.0 + * {@inheritDoc} */ public function setParsedSubject(string $subject): IEvent { if ($subject === '') { - throw new \InvalidArgumentException('The given parsed subject is invalid'); + throw new InvalidValueException('parsedSubject'); } $this->subjectParsed = $subject; return $this; @@ -256,19 +204,23 @@ class Event implements IEvent { } /** - * @param string $subject - * @param array $parameters - * @return $this - * @throws \InvalidArgumentException if the subject or parameters are invalid - * @since 11.0.0 + * {@inheritDoc} */ public function setRichSubject(string $subject, array $parameters = []): IEvent { if ($subject === '') { - throw new \InvalidArgumentException('The given parsed subject is invalid'); + throw new InvalidValueException('richSubject'); } $this->subjectRich = $subject; $this->subjectRichParameters = $parameters; + if ($this->subjectParsed === '') { + try { + $this->subjectParsed = $this->richTextFormatter->richToParsed($subject, $parameters); + } catch (\InvalidArgumentException $e) { + throw new InvalidValueException('richSubjectParameters', $e); + } + } + return $this; } @@ -281,7 +233,7 @@ class Event implements IEvent { } /** - * @return array[] + * @return array<string, array<string, string>> * @since 11.0.0 */ public function getRichSubjectParameters(): array { @@ -289,17 +241,11 @@ class Event implements IEvent { } /** - * Set the message of the activity - * - * @param string $message - * @param array $parameters - * @return IEvent - * @throws \InvalidArgumentException if the message or parameters are invalid - * @since 8.2.0 + * {@inheritDoc} */ public function setMessage(string $message, array $parameters = []): IEvent { if (isset($message[255])) { - throw new \InvalidArgumentException('The given message is invalid'); + throw new InvalidValueException('message'); } $this->message = $message; $this->messageParameters = $parameters; @@ -321,10 +267,7 @@ class Event implements IEvent { } /** - * @param string $message - * @return $this - * @throws \InvalidArgumentException if the message is invalid - * @since 11.0.0 + * {@inheritDoc} */ public function setParsedMessage(string $message): IEvent { $this->messageParsed = $message; @@ -340,16 +283,20 @@ class Event implements IEvent { } /** - * @param string $message - * @param array $parameters - * @return $this - * @throws \InvalidArgumentException if the subject or parameters are invalid - * @since 11.0.0 + * {@inheritDoc} */ public function setRichMessage(string $message, array $parameters = []): IEvent { $this->messageRich = $message; $this->messageRichParameters = $parameters; + if ($this->messageParsed === '') { + try { + $this->messageParsed = $this->richTextFormatter->richToParsed($message, $parameters); + } catch (\InvalidArgumentException $e) { + throw new InvalidValueException('richMessageParameters', $e); + } + } + return $this; } @@ -362,7 +309,7 @@ class Event implements IEvent { } /** - * @return array[] + * @return array<string, array<string, string>> * @since 11.0.0 */ public function getRichMessageParameters(): array { @@ -370,21 +317,14 @@ class Event implements IEvent { } /** - * Set the object of the activity - * - * @param string $objectType - * @param int $objectId - * @param string $objectName - * @return IEvent - * @throws \InvalidArgumentException if the object is invalid - * @since 8.2.0 + * {@inheritDoc} */ public function setObject(string $objectType, int $objectId, string $objectName = ''): IEvent { if (isset($objectType[255])) { - throw new \InvalidArgumentException('The given object type is invalid'); + throw new InvalidValueException('objectType'); } if (isset($objectName[4000])) { - throw new \InvalidArgumentException('The given object name is invalid'); + throw new InvalidValueException('objectName'); } $this->objectType = $objectType; $this->objectId = $objectId; @@ -414,16 +354,11 @@ class Event implements IEvent { } /** - * Set the link of the activity - * - * @param string $link - * @return IEvent - * @throws \InvalidArgumentException if the link is invalid - * @since 8.2.0 + * {@inheritDoc} */ public function setLink(string $link): IEvent { if (isset($link[4000])) { - throw new \InvalidArgumentException('The given link is invalid'); + throw new InvalidValueException('link'); } $this->link = $link; return $this; @@ -437,14 +372,11 @@ class Event implements IEvent { } /** - * @param string $icon - * @return $this - * @throws \InvalidArgumentException if the icon is invalid - * @since 11.0.0 + * {@inheritDoc} */ public function setIcon(string $icon): IEvent { if (isset($icon[4000])) { - throw new \InvalidArgumentException('The given icon is invalid'); + throw new InvalidValueException('icon'); } $this->icon = $icon; return $this; @@ -483,8 +415,7 @@ class Event implements IEvent { public function isValid(): bool { return $this->isValidCommon() - && - $this->getSubject() !== '' + && $this->getSubject() !== '' ; } @@ -511,20 +442,15 @@ class Event implements IEvent { return $this->isValidCommon() - && - $this->getParsedSubject() !== '' + && $this->getParsedSubject() !== '' ; } protected function isValidCommon(): bool { return $this->getApp() !== '' - && - $this->getType() !== '' - && - $this->getAffectedUser() !== '' - && - $this->getTimestamp() !== 0 + && $this->getType() !== '' + && $this->getTimestamp() !== 0 /** * Disabled for BC with old activities * && diff --git a/lib/private/Activity/EventMerger.php b/lib/private/Activity/EventMerger.php index 5f0993532cb..0e7d318103a 100644 --- a/lib/private/Activity/EventMerger.php +++ b/lib/private/Activity/EventMerger.php @@ -1,26 +1,8 @@ <?php + /** - * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com> - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * @author Julius Härtl <jus@bitgrid.net> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * 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 - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OC\Activity; @@ -29,7 +11,6 @@ use OCP\Activity\IEventMerger; use OCP\IL10N; class EventMerger implements IEventMerger { - /** @var IL10N */ protected $l10n; @@ -68,7 +49,7 @@ class EventMerger implements IEventMerger { * @param IEvent|null $previousEvent * @return IEvent */ - public function mergeEvents($mergeParameter, IEvent $event, IEvent $previousEvent = null) { + public function mergeEvents($mergeParameter, IEvent $event, ?IEvent $previousEvent = null) { // No second event => can not combine if (!$previousEvent instanceof IEvent) { return $event; diff --git a/lib/private/Activity/Manager.php b/lib/private/Activity/Manager.php index 00b2ba434a8..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 @@ -192,7 +184,7 @@ class Manager implements IManager { public function getFilters(): array { foreach ($this->filterClasses as $class => $false) { /** @var IFilter $filter */ - $filter = \OC::$server->query($class); + $filter = \OCP\Server::get($class); if (!$filter instanceof IFilter) { throw new \InvalidArgumentException('Invalid activity filter registered'); @@ -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[] */ @@ -242,7 +231,7 @@ class Manager implements IManager { public function getProviders(): array { foreach ($this->providerClasses as $class => $false) { /** @var IProvider $provider */ - $provider = \OC::$server->query($class); + $provider = \OCP\Server::get($class); if (!$provider instanceof IProvider) { throw new \InvalidArgumentException('Invalid activity provider registered'); @@ -276,7 +265,7 @@ class Manager implements IManager { public function getSettings(): array { foreach ($this->settingsClasses as $class => $false) { /** @var ISetting $setting */ - $setting = \OC::$server->query($class); + $setting = \OCP\Server::get($class); if ($setting instanceof ISetting) { if (!$setting instanceof ActivitySettings) { @@ -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'); } |