diff options
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/Activity/Event.php | 449 | ||||
-rw-r--r-- | lib/private/Activity/LegacyFilter.php | 108 | ||||
-rw-r--r-- | lib/private/Activity/LegacySetting.php | 123 | ||||
-rw-r--r-- | lib/private/Activity/Manager.php | 363 | ||||
-rw-r--r-- | lib/private/App/InfoParser.php | 21 | ||||
-rw-r--r-- | lib/private/Server.php | 7 | ||||
-rw-r--r-- | lib/private/legacy/app.php | 17 | ||||
-rw-r--r-- | lib/private/legacy/defaults.php | 2 | ||||
-rw-r--r-- | lib/private/legacy/util.php | 61 |
9 files changed, 949 insertions, 202 deletions
diff --git a/lib/private/Activity/Event.php b/lib/private/Activity/Event.php index af0605d82c8..df6756940a0 100644 --- a/lib/private/Activity/Event.php +++ b/lib/private/Activity/Event.php @@ -24,229 +24,528 @@ namespace OC\Activity; use OCP\Activity\IEvent; +use OCP\RichObjectStrings\InvalidObjectExeption; +use OCP\RichObjectStrings\IValidator; class Event implements IEvent { + + /** @var string */ + protected $app = ''; + /** @var string */ + protected $type = ''; + /** @var string */ + protected $affectedUser = ''; + /** @var string */ + protected $author = ''; + /** @var int */ + protected $timestamp = 0; + /** @var string */ + protected $subject = ''; + /** @var array */ + protected $subjectParameters = []; + /** @var string */ + protected $subjectParsed; + /** @var string */ + protected $subjectRich; + /** @var array */ + protected $subjectRichParameters; + /** @var string */ + protected $message = ''; /** @var array */ - protected $data = [ - 'app' => null, - 'type' => null, - 'affected_user' => null, - 'author' => null, - 'timestamp' => null, - 'subject' => null, - 'subject_parameters' => null, - 'message' => '', - 'message_parameters' => [], - 'object_type' => '', - 'object_id' => 0, - 'object_name' => '', - 'link' => '', - ]; + protected $messageParameters = []; + /** @var string */ + protected $messageParsed; + /** @var string */ + protected $messageRich; + /** @var array */ + protected $messageRichParameters; + /** @var string */ + protected $objectType = ''; + /** @var int */ + protected $objectId = 0; + /** @var string */ + protected $objectName = ''; + /** @var string */ + protected $link = ''; + /** @var string */ + protected $icon = ''; + + /** @var IEvent */ + protected $child = null; + /** @var IValidator */ + protected $richValidator; + + /** + * @param IValidator $richValidator + */ + public function __construct(IValidator $richValidator) { + $this->richValidator = $richValidator; + } /** * Set the app of the activity * * @param string $app * @return IEvent + * @throws \InvalidArgumentException if the app id is invalid * @since 8.2.0 */ public function setApp($app) { - $this->data['app'] = (string) $app; + if (!is_string($app) || $app === '' || isset($app[32])) { + throw new \InvalidArgumentException('The given app is invalid'); + } + $this->app = (string) $app; return $this; } /** + * @return string + */ + public function getApp() { + return $this->app; + } + + /** * Set the type of the activity * * @param string $type * @return IEvent + * @throws \InvalidArgumentException if the type is invalid * @since 8.2.0 */ public function setType($type) { - $this->data['type'] = (string) $type; + if (!is_string($type) || $type === '' || isset($type[255])) { + throw new \InvalidArgumentException('The given type is invalid'); + } + $this->type = (string) $type; return $this; } /** + * @return string + */ + public function getType() { + return $this->type; + } + + /** * Set the affected user of the activity * * @param string $affectedUser * @return IEvent + * @throws \InvalidArgumentException if the affected user is invalid * @since 8.2.0 */ public function setAffectedUser($affectedUser) { - $this->data['affected_user'] = (string) $affectedUser; + if (!is_string($affectedUser) || $affectedUser === '' || isset($affectedUser[64])) { + throw new \InvalidArgumentException('The given affected user is invalid'); + } + $this->affectedUser = (string) $affectedUser; return $this; } /** + * @return string + */ + public function getAffectedUser() { + return $this->affectedUser; + } + + /** * Set the author of the activity * * @param string $author * @return IEvent + * @throws \InvalidArgumentException if the author is invalid * @since 8.2.0 */ public function setAuthor($author) { - $this->data['author'] = (string) $author; + if (!is_string($author) || isset($author[64])) { + throw new \InvalidArgumentException('The given author user is invalid'. serialize($author)); + } + $this->author = (string) $author; return $this; } /** + * @return string + */ + public function getAuthor() { + return $this->author; + } + + /** * Set the timestamp of the activity * * @param int $timestamp * @return IEvent + * @throws \InvalidArgumentException if the timestamp is invalid * @since 8.2.0 */ public function setTimestamp($timestamp) { - $this->data['timestamp'] = (int) $timestamp; + if (!is_int($timestamp)) { + throw new \InvalidArgumentException('The given timestamp is invalid'); + } + $this->timestamp = (int) $timestamp; return $this; } /** + * @return int + */ + public function getTimestamp() { + return $this->timestamp; + } + + /** * 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 */ public function setSubject($subject, array $parameters = []) { - $this->data['subject'] = (string) $subject; - $this->data['subject_parameters'] = $parameters; + if (!is_string($subject) || isset($subject[255])) { + throw new \InvalidArgumentException('The given subject is invalid'); + } + $this->subject = (string) $subject; + $this->subjectParameters = $parameters; return $this; } /** + * @return string + */ + public function getSubject() { + return $this->subject; + } + + /** + * @return array + */ + public function getSubjectParameters() { + return $this->subjectParameters; + } + + /** + * @param string $subject + * @return $this + * @throws \InvalidArgumentException if the subject is invalid + * @since 11.0.0 + */ + public function setParsedSubject($subject) { + if (!is_string($subject) || $subject === '') { + throw new \InvalidArgumentException('The given parsed subject is invalid'); + } + $this->subjectParsed = $subject; + return $this; + } + + /** + * @return string + * @since 11.0.0 + */ + public function getParsedSubject() { + return $this->subjectParsed; + } + + /** + * @param string $subject + * @param array $parameters + * @return $this + * @throws \InvalidArgumentException if the subject or parameters are invalid + * @since 11.0.0 + */ + public function setRichSubject($subject, array $parameters = []) { + if (!is_string($subject) || $subject === '') { + throw new \InvalidArgumentException('The given parsed subject is invalid'); + } + $this->subjectRich = $subject; + + if (!is_array($parameters)) { + throw new \InvalidArgumentException('The given subject parameters are invalid'); + } + $this->subjectRichParameters = $parameters; + + return $this; + } + + /** + * @return string + * @since 11.0.0 + */ + public function getRichSubject() { + return $this->subjectRich; + } + + /** + * @return array[] + * @since 11.0.0 + */ + public function getRichSubjectParameters() { + return $this->subjectRichParameters; + } + + /** * 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 */ public function setMessage($message, array $parameters = []) { - $this->data['message'] = (string) $message; - $this->data['message_parameters'] = $parameters; + if (!is_string($message) || isset($message[255])) { + throw new \InvalidArgumentException('The given message is invalid'); + } + $this->message = (string) $message; + $this->messageParameters = $parameters; return $this; } /** - * Set the object of the activity - * - * @param string $objectType - * @param int $objectId - * @param string $objectName - * @return IEvent - * @since 8.2.0 + * @return string */ - public function setObject($objectType, $objectId, $objectName = '') { - $this->data['object_type'] = (string) $objectType; - $this->data['object_id'] = (int) $objectId; - $this->data['object_name'] = (string) $objectName; + public function getMessage() { + return $this->message; + } + + /** + * @return array + */ + public function getMessageParameters() { + return $this->messageParameters; + } + + /** + * @param string $message + * @return $this + * @throws \InvalidArgumentException if the message is invalid + * @since 11.0.0 + */ + public function setParsedMessage($message) { + if (!is_string($message)) { + throw new \InvalidArgumentException('The given parsed message is invalid'); + } + $this->messageParsed = $message; return $this; } /** - * Set the link of the activity - * - * @param string $link - * @return IEvent - * @since 8.2.0 + * @return string + * @since 11.0.0 */ - public function setLink($link) { - $this->data['link'] = (string) $link; + public function getParsedMessage() { + return $this->messageParsed; + } + + /** + * @param string $message + * @param array $parameters + * @return $this + * @throws \InvalidArgumentException if the subject or parameters are invalid + * @since 11.0.0 + */ + public function setRichMessage($message, array $parameters = []) { + if (!is_string($message)) { + throw new \InvalidArgumentException('The given parsed message is invalid'); + } + $this->messageRich = $message; + + if (!is_array($parameters)) { + throw new \InvalidArgumentException('The given message parameters are invalid'); + } + $this->messageRichParameters = $parameters; + return $this; } /** * @return string + * @since 11.0.0 */ - public function getApp() { - return $this->data['app']; + public function getRichMessage() { + return $this->messageRich; + } + + /** + * @return array[] + * @since 11.0.0 + */ + public function getRichMessageParameters() { + return $this->messageRichParameters; + } + + /** + * 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 + */ + public function setObject($objectType, $objectId, $objectName = '') { + if (!is_string($objectType) || isset($objectType[255])) { + throw new \InvalidArgumentException('The given object type is invalid'); + } + if (!is_int($objectId)) { + throw new \InvalidArgumentException('The given object id is invalid'); + } + if (!is_string($objectName) || isset($objectName[4000])) { + throw new \InvalidArgumentException('The given object name is invalid'); + } + $this->objectType = (string) $objectType; + $this->objectId = (int) $objectId; + $this->objectName = (string) $objectName; + return $this; } /** * @return string */ - public function getType() { - return $this->data['type']; + public function getObjectType() { + return $this->objectType; } /** * @return string */ - public function getAffectedUser() { - return $this->data['affected_user']; + public function getObjectId() { + return $this->objectId; } /** * @return string */ - public function getAuthor() { - return $this->data['author']; + public function getObjectName() { + return $this->objectName; } /** - * @return int + * Set the link of the activity + * + * @param string $link + * @return IEvent + * @throws \InvalidArgumentException if the link is invalid + * @since 8.2.0 */ - public function getTimestamp() { - return $this->data['timestamp']; + public function setLink($link) { + if (!is_string($link) || isset($link[4000])) { + throw new \InvalidArgumentException('The given link is invalid'); + } + $this->link = (string) $link; + return $this; } /** * @return string */ - public function getSubject() { - return $this->data['subject']; + public function getLink() { + return $this->link; } /** - * @return array + * @param string $icon + * @return $this + * @throws \InvalidArgumentException if the icon is invalid + * @since 11.0.0 */ - public function getSubjectParameters() { - return $this->data['subject_parameters']; + public function setIcon($icon) { + if (!is_string($icon) || isset($icon[4000])) { + throw new \InvalidArgumentException('The given icon is invalid'); + } + $this->icon = $icon; + return $this; } /** * @return string + * @since 11.0.0 */ - public function getMessage() { - return $this->data['message']; + public function getIcon() { + return $this->icon; } /** - * @return array + * @param IEvent $child + * @since 11.0.0 */ - public function getMessageParameters() { - return $this->data['message_parameters']; + public function setChildEvent(IEvent $child) { + $this->child = $child; } /** - * @return string + * @return IEvent|null + * @since 11.0.0 */ - public function getObjectType() { - return $this->data['object_type']; + public function getChildEvent() { + return $this->child; } /** - * @return string + * @return bool + * @since 8.2.0 */ - public function getObjectId() { - return $this->data['object_id']; + public function isValid() { + return + $this->isValidCommon() + && + $this->getSubject() !== '' + ; } /** - * @return string + * @return bool + * @since 8.2.0 */ - public function getObjectName() { - return $this->data['object_name']; + public function isValidParsed() { + if ($this->getRichSubject() !== '' || !empty($this->getRichSubjectParameters())) { + try { + $this->richValidator->validate($this->getRichSubject(), $this->getRichSubjectParameters()); + } catch (InvalidObjectExeption $e) { + return false; + } + } + + if ($this->getRichMessage() !== '' || !empty($this->getRichMessageParameters())) { + try { + $this->richValidator->validate($this->getRichMessage(), $this->getRichMessageParameters()); + } catch (InvalidObjectExeption $e) { + return false; + } + } + + return + $this->isValidCommon() + && + $this->getParsedSubject() !== '' + ; } /** - * @return string + * @return bool */ - public function getLink() { - return $this->data['link']; + protected function isValidCommon() { + return + $this->getApp() !== '' + && + $this->getType() !== '' + && + $this->getAffectedUser() !== '' + && + $this->getTimestamp() !== 0 + /** + * Disabled for BC with old activities + && + $this->getObjectType() !== '' + && + $this->getObjectId() !== 0 + */ + ; } } diff --git a/lib/private/Activity/LegacyFilter.php b/lib/private/Activity/LegacyFilter.php new file mode 100644 index 00000000000..eadb5b1558f --- /dev/null +++ b/lib/private/Activity/LegacyFilter.php @@ -0,0 +1,108 @@ +<?php +/** + * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com> + * + * @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/>. + * + */ + +namespace OC\Activity; + +use OCP\Activity\IFilter; +use OCP\Activity\IManager; + +class LegacyFilter implements IFilter { + + /** @var IManager */ + protected $manager; + /** @var string */ + protected $identifier; + /** @var string */ + protected $name; + /** @var bool */ + protected $isTopFilter; + + /** + * LegacySetting constructor. + * + * @param IManager $manager + * @param string $identifier + * @param string $name + * @param bool $isTopFilter + */ + public function __construct(IManager $manager, + $identifier, + $name, + $isTopFilter) { + $this->manager = $manager; + $this->identifier = $identifier; + $this->name = $name; + $this->isTopFilter = $isTopFilter; + } + + /** + * @return string Lowercase a-z and underscore only identifier + * @since 11.0.0 + */ + public function getIdentifier() { + return $this->identifier; + } + + /** + * @return string A translated string + * @since 11.0.0 + */ + public function getName() { + return $this->name; + } + + /** + * @return int whether the filter should be rather on the top or bottom of + * the admin section. The filters are arranged in ascending order of the + * priority values. It is required to return a value between 0 and 100. + * @since 11.0.0 + */ + public function getPriority() { + return $this->isTopFilter ? 40 : 50; + } + + /** + * @return string Full URL to an icon, empty string when none is given + * @since 11.0.0 + */ + public function getIcon() { + // Old API was CSS class, so we can not use this... + return ''; + } + + /** + * @param string[] $types + * @return string[] An array of allowed apps from which activities should be displayed + * @since 11.0.0 + */ + public function filterTypes(array $types) { + return $this->manager->filterNotificationTypes($types, $this->getIdentifier()); + } + + /** + * @return string[] An array of allowed apps from which activities should be displayed + * @since 11.0.0 + */ + public function allowedApps() { + return []; + } +} + diff --git a/lib/private/Activity/LegacySetting.php b/lib/private/Activity/LegacySetting.php new file mode 100644 index 00000000000..27495afddb0 --- /dev/null +++ b/lib/private/Activity/LegacySetting.php @@ -0,0 +1,123 @@ +<?php +/** + * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com> + * + * @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/>. + * + */ + +namespace OC\Activity; + +use OCP\Activity\ISetting; + +class LegacySetting implements ISetting { + + /** @var string */ + protected $identifier; + /** @var string */ + protected $name; + /** @var bool */ + protected $canChangeStream; + /** @var bool */ + protected $isDefaultEnabledStream; + /** @var bool */ + protected $canChangeMail; + /** @var bool */ + protected $isDefaultEnabledMail; + + /** + * LegacySetting constructor. + * + * @param string $identifier + * @param string $name + * @param bool $canChangeStream + * @param bool $isDefaultEnabledStream + * @param bool $canChangeMail + * @param bool $isDefaultEnabledMail + */ + public function __construct($identifier, + $name, + $canChangeStream, + $isDefaultEnabledStream, + $canChangeMail, + $isDefaultEnabledMail) { + $this->identifier = $identifier; + $this->name = $name; + $this->canChangeStream = $canChangeStream; + $this->isDefaultEnabledStream = $isDefaultEnabledStream; + $this->canChangeMail = $canChangeMail; + $this->isDefaultEnabledMail = $isDefaultEnabledMail; + } + + /** + * @return string Lowercase a-z and underscore only identifier + * @since 11.0.0 + */ + public function getIdentifier() { + return $this->identifier; + } + + /** + * @return string A translated string + * @since 11.0.0 + */ + public function getName() { + return $this->name; + } + + /** + * @return int whether the filter should be rather on the top or bottom of + * the admin section. The filters are arranged in ascending order of the + * priority values. It is required to return a value between 0 and 100. + * @since 11.0.0 + */ + public function getPriority() { + return 70; + } + + /** + * @return bool True when the option can be changed for the stream + * @since 11.0.0 + */ + public function canChangeStream() { + return $this->canChangeStream; + } + + /** + * @return bool True when the option can be changed for the stream + * @since 11.0.0 + */ + public function isDefaultEnabledStream() { + return $this->isDefaultEnabledStream; + } + + /** + * @return bool True when the option can be changed for the mail + * @since 11.0.0 + */ + public function canChangeMail() { + return $this->canChangeMail; + } + + /** + * @return bool True when the option can be changed for the stream + * @since 11.0.0 + */ + public function isDefaultEnabledMail() { + return $this->isDefaultEnabledMail; + } +} + diff --git a/lib/private/Activity/Manager.php b/lib/private/Activity/Manager.php index 455bb3b8ee8..805124dc602 100644 --- a/lib/private/Activity/Manager.php +++ b/lib/private/Activity/Manager.php @@ -27,11 +27,15 @@ namespace OC\Activity; use OCP\Activity\IConsumer; use OCP\Activity\IEvent; use OCP\Activity\IExtension; +use OCP\Activity\IFilter; use OCP\Activity\IManager; +use OCP\Activity\IProvider; +use OCP\Activity\ISetting; use OCP\IConfig; use OCP\IRequest; use OCP\IUser; use OCP\IUserSession; +use OCP\RichObjectStrings\IValidator; class Manager implements IManager { /** @var IRequest */ @@ -43,6 +47,9 @@ class Manager implements IManager { /** @var IConfig */ protected $config; + /** @var IValidator */ + protected $validator; + /** @var string */ protected $formattingObjectType; @@ -58,13 +65,16 @@ class Manager implements IManager { * @param IRequest $request * @param IUserSession $session * @param IConfig $config + * @param IValidator $validator */ public function __construct(IRequest $request, IUserSession $session, - IConfig $config) { + IConfig $config, + IValidator $validator) { $this->request = $request; $this->session = $session; $this->config = $config; + $this->validator = $validator; } /** @var \Closure[] */ @@ -147,7 +157,7 @@ class Manager implements IManager { * @return IEvent */ public function generateEvent() { - return new Event(); + return new Event($this->validator); } /** @@ -160,24 +170,10 @@ class Manager implements IManager { * - setSubject() * * @param IEvent $event - * @return null * @throws \BadMethodCallException if required values have not been set */ public function publish(IEvent $event) { - if (!$event->getApp()) { - throw new \BadMethodCallException('App not set', 10); - } - if (!$event->getType()) { - throw new \BadMethodCallException('Type not set', 11); - } - if ($event->getAffectedUser() === null) { - throw new \BadMethodCallException('Affected user not set', 12); - } - if ($event->getSubject() === null || $event->getSubjectParameters() === null) { - throw new \BadMethodCallException('Subject not set', 13); - } - - if ($event->getAuthor() === null) { + if ($event->getAuthor() === '') { if ($this->session->getUser() instanceof IUser) { $event->setAuthor($this->session->getUser()->getUID()); } @@ -187,6 +183,10 @@ class Manager implements IManager { $event->setTimestamp(time()); } + if (!$event->isValid()) { + throw new \BadMethodCallException('The given event is invalid'); + } + foreach ($this->getConsumers() as $c) { $c->receive($event); } @@ -203,7 +203,6 @@ class Manager implements IManager { * @param string $affectedUser Recipient of the activity * @param string $type Type of the notification * @param int $priority Priority of the notification - * @return null */ public function publishActivity($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority) { $event = $this->generateEvent(); @@ -235,59 +234,195 @@ class Manager implements IManager { * In order to improve lazy loading a closure can be registered which will be called in case * activity consumers are actually requested * - * $callable has to return an instance of OCA\Activity\IConsumer + * $callable has to return an instance of OCA\Activity\IExtension * * @param \Closure $callable - * @return void */ public function registerExtension(\Closure $callable) { array_push($this->extensionsClosures, $callable); $this->extensions = []; } + /** @var string[] */ + protected $filterClasses = []; + + /** @var IFilter[] */ + protected $filters = []; + + /** @var bool */ + protected $loadedLegacyFilters = false; + /** - * Will return additional notification types as specified by other apps - * - * @param string $languageCode - * @return array + * @param string $filter Class must implement OCA\Activity\IFilter + * @return void */ - public function getNotificationTypes($languageCode) { - $filesNotificationTypes = []; - $sharingNotificationTypes = []; + public function registerFilter($filter) { + $this->filterClasses[$filter] = false; + } - $notificationTypes = array(); - foreach ($this->getExtensions() as $c) { - $result = $c->getNotificationTypes($languageCode); - if (is_array($result)) { - if (class_exists('\OCA\Files\Activity', false) && $c instanceof \OCA\Files\Activity) { - $filesNotificationTypes = $result; - continue; - } - if (class_exists('\OCA\Files_Sharing\Activity', false) && $c instanceof \OCA\Files_Sharing\Activity) { - $sharingNotificationTypes = $result; - continue; - } + /** + * @return IFilter[] + * @throws \InvalidArgumentException + */ + public function getFilters() { + if (!$this->loadedLegacyFilters) { + $legacyFilters = $this->getNavigation(); + + foreach ($legacyFilters['top'] as $filter => $data) { + $this->filters[$filter] = new LegacyFilter( + $this, $filter, $data['name'], true + ); + } - $notificationTypes = array_merge($notificationTypes, $result); + foreach ($legacyFilters['apps'] as $filter => $data) { + $this->filters[$filter] = new LegacyFilter( + $this, $filter, $data['name'], false + ); } + $this->loadedLegacyFilters = true; } - return array_merge($filesNotificationTypes, $sharingNotificationTypes, $notificationTypes); + foreach ($this->filterClasses as $class => $false) { + /** @var IFilter $filter */ + $filter = \OC::$server->query($class); + + if (!$filter instanceof IFilter) { + throw new \InvalidArgumentException('Invalid activity filter registered'); + } + + $this->filters[$filter->getIdentifier()] = $filter; + + unset($this->filterClasses[$class]); + } + return $this->filters; } /** - * @param string $method - * @return array + * @param string $id + * @return IFilter + * @throws \InvalidArgumentException when the filter was not found + * @since 11.0.0 */ - public function getDefaultTypes($method) { - $defaultTypes = array(); - foreach ($this->getExtensions() as $c) { - $types = $c->getDefaultTypes($method); - if (is_array($types)) { - $defaultTypes = array_merge($types, $defaultTypes); + public function getFilterById($id) { + $filters = $this->getFilters(); + + if (isset($filters[$id])) { + return $filters[$id]; + } + + throw new \InvalidArgumentException('Requested filter does not exist'); + } + + /** @var string[] */ + protected $providerClasses = []; + + /** @var IProvider[] */ + protected $providers = []; + + /** + * @param string $provider Class must implement OCA\Activity\IProvider + * @return void + */ + public function registerProvider($provider) { + $this->providerClasses[$provider] = false; + } + + /** + * @return IProvider[] + * @throws \InvalidArgumentException + */ + public function getProviders() { + foreach ($this->providerClasses as $class => $false) { + /** @var IProvider $provider */ + $provider = \OC::$server->query($class); + + if (!$provider instanceof IProvider) { + throw new \InvalidArgumentException('Invalid activity provider registered'); } + + $this->providers[] = $provider; + + unset($this->providerClasses[$class]); } - return $defaultTypes; + return $this->providers; + } + + /** @var string[] */ + protected $settingsClasses = []; + + /** @var ISetting[] */ + protected $settings = []; + + /** @var bool */ + protected $loadedLegacyTypes = false; + + /** + * @param string $setting Class must implement OCA\Activity\ISetting + * @return void + */ + public function registerSetting($setting) { + $this->settingsClasses[$setting] = false; + } + + /** + * @return ISetting[] + * @throws \InvalidArgumentException + */ + public function getSettings() { + if (!$this->loadedLegacyTypes) { + $l = \OC::$server->getL10N('core'); + $legacyTypes = $this->getNotificationTypes($l->getLanguageCode()); + $streamTypes = $this->getDefaultTypes(IExtension::METHOD_STREAM); + $mailTypes = $this->getDefaultTypes(IExtension::METHOD_MAIL); + foreach ($legacyTypes as $type => $data) { + if (is_string($data)) { + $desc = $data; + $canChangeStream = true; + $canChangeMail = true; + } else { + $desc = $data['desc']; + $canChangeStream = in_array(IExtension::METHOD_STREAM, $data['methods']); + $canChangeMail = in_array(IExtension::METHOD_MAIL, $data['methods']); + } + + $this->settings[$type] = new LegacySetting( + $type, $desc, + $canChangeStream, in_array($type, $streamTypes), + $canChangeMail, in_array($type, $mailTypes) + ); + } + $this->loadedLegacyTypes = true; + } + + foreach ($this->settingsClasses as $class => $false) { + /** @var ISetting $setting */ + $setting = \OC::$server->query($class); + + if (!$setting instanceof ISetting) { + throw new \InvalidArgumentException('Invalid activity filter registered'); + } + + $this->settings[$setting->getIdentifier()] = $setting; + + unset($this->settingsClasses[$class]); + } + return $this->settings; + } + + /** + * @param string $id + * @return ISetting + * @throws \InvalidArgumentException when the setting was not found + * @since 11.0.0 + */ + public function getSettingById($id) { + $settings = $this->getSettings(); + + if (isset($settings[$id])) { + return $settings[$id]; + } + + throw new \InvalidArgumentException('Requested setting does not exist'); } /** @@ -391,7 +526,62 @@ 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($currentUserId) { + if (!is_string($currentUserId) && $currentUserId !== null) { + throw new \UnexpectedValueException('The given current user is invalid'); + } + $this->currentUserId = $currentUserId; + } + + /** + * Get the user we need to use + * + * Either the user is logged in, or we try to get it from the token + * + * @return string + * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique + */ + public function getCurrentUserId() { + if ($this->currentUserId !== null) { + return $this->currentUserId; + } else if (!$this->session->isLoggedIn()) { + return $this->getUserFromToken(); + } else { + return $this->session->getUser()->getUID(); + } + } + + /** + * Get the user for the token + * + * @return string + * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique + */ + protected function getUserFromToken() { + $token = (string) $this->request->getParam('token', ''); + if (strlen($token) !== 30) { + throw new \UnexpectedValueException('The token is invalid'); + } + + $users = $this->config->getUsersForUserValue('activity', 'rsstoken', $token); + + if (sizeof($users) !== 1) { + // No unique user found + throw new \UnexpectedValueException('The token is invalid'); + } + + // Token found login as that user + return array_shift($users); + } + + /** * @return array + * @deprecated 11.0.0 - Use getFilters() instead */ public function getNavigation() { $entries = array( @@ -412,6 +602,7 @@ class Manager implements IManager { /** * @param string $filterValue * @return boolean + * @deprecated 11.0.0 - Use getFilterById() instead */ public function isFilterValid($filterValue) { if (isset($this->validFilters[$filterValue])) { @@ -433,6 +624,7 @@ class Manager implements IManager { * @param array $types * @param string $filter * @return array + * @deprecated 11.0.0 - Use getFilterById()->filterTypes() instead */ public function filterNotificationTypes($types, $filter) { if (!$this->isFilterValid($filter)) { @@ -451,6 +643,7 @@ class Manager implements IManager { /** * @param string $filter * @return array + * @deprecated 11.0.0 - Use getFilterById() instead */ public function getQueryForFilter($filter) { if (!$this->isFilterValid($filter)) { @@ -479,56 +672,42 @@ class Manager implements IManager { } /** - * Set the user we need to use + * Will return additional notification types as specified by other apps * - * @param string|null $currentUserId - * @throws \UnexpectedValueException If the user is invalid + * @param string $languageCode + * @return array + * @deprecated 11.0.0 - Use getSettings() instead */ - public function setCurrentUserId($currentUserId) { - if (!is_string($currentUserId) && $currentUserId !== null) { - throw new \UnexpectedValueException('The given current user is invalid'); - } - $this->currentUserId = $currentUserId; - } + public function getNotificationTypes($languageCode) { + $notificationTypes = $sharingNotificationTypes = []; + foreach ($this->getExtensions() as $c) { + $result = $c->getNotificationTypes($languageCode); + if (is_array($result)) { + if (class_exists('\OCA\Files_Sharing\Activity', false) && $c instanceof \OCA\Files_Sharing\Activity) { + $sharingNotificationTypes = $result; + continue; + } - /** - * Get the user we need to use - * - * Either the user is logged in, or we try to get it from the token - * - * @return string - * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique - */ - public function getCurrentUserId() { - if ($this->currentUserId !== null) { - return $this->currentUserId; - } else if (!$this->session->isLoggedIn()) { - return $this->getUserFromToken(); - } else { - return $this->session->getUser()->getUID(); + $notificationTypes = array_merge($notificationTypes, $result); + } } + + return array_merge($sharingNotificationTypes, $notificationTypes); } /** - * Get the user for the token - * - * @return string - * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique + * @param string $method + * @return array + * @deprecated 11.0.0 - Use getSettings()->isDefaulEnabled<method>() instead */ - protected function getUserFromToken() { - $token = (string) $this->request->getParam('token', ''); - if (strlen($token) !== 30) { - throw new \UnexpectedValueException('The token is invalid'); - } - - $users = $this->config->getUsersForUserValue('activity', 'rsstoken', $token); - - if (sizeof($users) !== 1) { - // No unique user found - throw new \UnexpectedValueException('The token is invalid'); + public function getDefaultTypes($method) { + $defaultTypes = array(); + foreach ($this->getExtensions() as $c) { + $types = $c->getDefaultTypes($method); + if (is_array($types)) { + $defaultTypes = array_merge($types, $defaultTypes); + } } - - // Token found login as that user - return array_shift($users); + return $defaultTypes; } } diff --git a/lib/private/App/InfoParser.php b/lib/private/App/InfoParser.php index 44f495534c9..47ce28e6e98 100644 --- a/lib/private/App/InfoParser.php +++ b/lib/private/App/InfoParser.php @@ -110,6 +110,18 @@ class InfoParser { if (!array_key_exists('commands', $array)) { $array['commands'] = []; } + if (!array_key_exists('activity', $array)) { + $array['activity'] = []; + } + if (!array_key_exists('filters', $array['activity'])) { + $array['activity']['filters'] = []; + } + if (!array_key_exists('settings', $array['activity'])) { + $array['activity']['settings'] = []; + } + if (!array_key_exists('providers', $array['activity'])) { + $array['activity']['providers'] = []; + } if (array_key_exists('types', $array)) { if (is_array($array['types'])) { @@ -144,6 +156,15 @@ class InfoParser { if (isset($array['commands']['command']) && is_array($array['commands']['command'])) { $array['commands'] = $array['commands']['command']; } + if (isset($array['activity']['filters']['filter']) && is_array($array['activity']['filters']['filter'])) { + $array['activity']['filters'] = $array['activity']['filters']['filter']; + } + if (isset($array['activity']['settings']['setting']) && is_array($array['activity']['settings']['setting'])) { + $array['activity']['settings'] = $array['activity']['settings']['setting']; + } + if (isset($array['activity']['providers']['provider']) && is_array($array['activity']['providers']['provider'])) { + $array['activity']['providers'] = $array['activity']['providers']['provider']; + } if(!is_null($this->cache)) { $this->cache->set($fileCacheKey, json_encode($array)); diff --git a/lib/private/Server.php b/lib/private/Server.php index 8e41ba212ad..abedf8230ed 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -90,6 +90,7 @@ use OC\Tagging\TagMapper; use OCA\Theming\ThemingDefaults; use OCP\IL10N; use OCP\IServerContainer; +use OCP\RichObjectStrings\IValidator; use OCP\Security\IContentSecurityPolicyManager; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -394,9 +395,11 @@ class Server extends ServerContainer implements IServerContainer { return new \OC\Activity\Manager( $c->getRequest(), $c->getUserSession(), - $c->getConfig() + $c->getConfig(), + $c->query(IValidator::class) ); }); + $this->registerAlias(IValidator::class, Validator::class); $this->registerService('AvatarManager', function (Server $c) { return new AvatarManager( $c->getUserManager(), @@ -662,7 +665,7 @@ class Server extends ServerContainer implements IServerContainer { }); $this->registerService('NotificationManager', function (Server $c) { return new Manager( - $c->query(Validator::class) + $c->query(IValidator::class) ); }); $this->registerService('CapabilitiesManager', function (Server $c) { diff --git a/lib/private/legacy/app.php b/lib/private/legacy/app.php index 43dd3f54d84..c2ff9a5be3c 100644 --- a/lib/private/legacy/app.php +++ b/lib/private/legacy/app.php @@ -162,6 +162,23 @@ class OC_App { } \OC::$server->getEventLogger()->end('load_app_' . $app); } + + $info = self::getAppInfo($app); + if (!empty($info['activity']['filters'])) { + foreach ($info['activity']['filters'] as $filter) { + \OC::$server->getActivityManager()->registerFilter($filter); + } + } + if (!empty($info['activity']['settings'])) { + foreach ($info['activity']['settings'] as $setting) { + \OC::$server->getActivityManager()->registerSetting($setting); + } + } + if (!empty($info['activity']['providers'])) { + foreach ($info['activity']['providers'] as $provider) { + \OC::$server->getActivityManager()->registerProvider($provider); + } + } } /** diff --git a/lib/private/legacy/defaults.php b/lib/private/legacy/defaults.php index 38bbaa8daa1..23590cd9235 100644 --- a/lib/private/legacy/defaults.php +++ b/lib/private/legacy/defaults.php @@ -59,7 +59,7 @@ class OC_Defaults { $this->defaultiOSClientUrl = 'https://itunes.apple.com/us/app/nextcloud/id1125420102?mt=8'; $this->defaultiTunesAppId = '1125420102'; $this->defaultAndroidClientUrl = 'https://play.google.com/store/apps/details?id=com.nextcloud.client'; - $this->defaultDocBaseUrl = 'https://docs.nextcloud.org'; + $this->defaultDocBaseUrl = 'https://docs.nextcloud.com'; $this->defaultDocVersion = '10'; // used to generate doc links $this->defaultSlogan = $this->l->t('a safe home for all your data'); $this->defaultLogoClaim = ''; diff --git a/lib/private/legacy/util.php b/lib/private/legacy/util.php index ecc8f053704..3bd5b5586ab 100644 --- a/lib/private/legacy/util.php +++ b/lib/private/legacy/util.php @@ -66,6 +66,9 @@ class OC_Util { private static $rootMounted = false; private static $fsSetup = false; + /** @var array Local cache of version.php */ + private static $versionCache = null; + protected static function getAppManager() { return \OC::$server->getAppManager(); } @@ -397,7 +400,7 @@ class OC_Util { */ public static function getVersion() { OC_Util::loadVersion(); - return \OC::$server->getSession()->get('OC_Version'); + return self::$versionCache['OC_Version']; } /** @@ -407,7 +410,7 @@ class OC_Util { */ public static function getVersionString() { OC_Util::loadVersion(); - return \OC::$server->getSession()->get('OC_VersionString'); + return self::$versionCache['OC_VersionString']; } /** @@ -424,7 +427,13 @@ class OC_Util { */ public static function getChannel() { OC_Util::loadVersion(); - return \OC::$server->getSession()->get('OC_Channel'); + + // Allow overriding update channel + if (\OC::$server->getSystemConfig()->getValue('installed', false)) { + self::$versionCache['OC_Channel'] = \OC::$server->getAppConfig()->getValue('core', 'OC_Channel'); + } + + return self::$versionCache['OC_Channel']; } /** @@ -433,42 +442,30 @@ class OC_Util { */ public static function getBuild() { OC_Util::loadVersion(); - return \OC::$server->getSession()->get('OC_Build'); + return self::$versionCache['OC_Build']; } /** * @description load the version.php into the session as cache */ private static function loadVersion() { - $timestamp = filemtime(OC::$SERVERROOT . '/version.php'); - if (!\OC::$server->getSession()->exists('OC_Version') or OC::$server->getSession()->get('OC_Version_Timestamp') != $timestamp) { - require OC::$SERVERROOT . '/version.php'; - $session = \OC::$server->getSession(); - /** @var $timestamp int */ - $session->set('OC_Version_Timestamp', $timestamp); - /** @var $OC_Version string */ - $session->set('OC_Version', $OC_Version); - /** @var $OC_VersionString string */ - $session->set('OC_VersionString', $OC_VersionString); - /** @var $OC_Build string */ - $session->set('OC_Build', $OC_Build); - - // Allow overriding update channel - - if (\OC::$server->getSystemConfig()->getValue('installed', false)) { - $channel = \OC::$server->getAppConfig()->getValue('core', 'OC_Channel'); - } else { - /** @var $OC_Channel string */ - $channel = $OC_Channel; - } - - if (!is_null($channel)) { - $session->set('OC_Channel', $channel); - } else { - /** @var $OC_Channel string */ - $session->set('OC_Channel', $OC_Channel); - } + if (self::$versionCache !== null) { + return; } + + $timestamp = filemtime(OC::$SERVERROOT . '/version.php'); + require OC::$SERVERROOT . '/version.php'; + /** @var $timestamp int */ + self::$versionCache['OC_Version_Timestamp'] = $timestamp; + /** @var $OC_Version string */ + self::$versionCache['OC_Version'] = $OC_Version; + /** @var $OC_VersionString string */ + self::$versionCache['OC_VersionString'] = $OC_VersionString; + /** @var $OC_Build string */ + self::$versionCache['OC_Build'] = $OC_Build; + + /** @var $OC_Channel string */ + self::$versionCache['OC_Channel'] = $OC_Channel; } /** |