From c3e793ccd61e0c5ed1f46003f5b7f13c1677890d Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 9 Apr 2020 13:44:27 +0200 Subject: [PATCH] Create the initialState in a single location Signed-off-by: Joas Schilling --- lib/Controller/PageController.php | 32 +++-------- lib/Files/TemplateLoader.php | 42 +++++++++++++-- lib/PublicShare/TemplateLoader.php | 28 +++++++--- lib/PublicShareAuth/TemplateLoader.php | 26 +++++++-- lib/TInitialState.php | 73 ++++++++++++++++++++++++++ 5 files changed, 158 insertions(+), 43 deletions(-) create mode 100644 lib/TInitialState.php diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php index d34c72d9464..58de981c9bf 100644 --- a/lib/Controller/PageController.php +++ b/lib/Controller/PageController.php @@ -32,6 +32,7 @@ use OCA\Talk\Participant; use OCA\Talk\Room; use OCA\Talk\TalkSession; +use OCA\Talk\TInitialState; use OCP\App\IAppManager; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; @@ -48,9 +49,11 @@ use OCP\IUser; use OCP\IUserSession; use OCP\Notification\IManager as INotificationManager; -use OCP\Util; class PageController extends Controller { + + use TInitialState; + /** @var string|null */ private $userId; /** @var RoomController */ @@ -69,12 +72,6 @@ class PageController extends Controller { private $notificationManager; /** @var IAppManager */ private $appManager; - /** @var IInitialStateService */ - private $initialStateService; - /** @var Config */ - private $talkConfig; - /** @var IConfig */ - private $serverConfig; public function __construct(string $appName, IRequest $request, @@ -222,18 +219,7 @@ public function index(string $token = '', string $callUser = '', string $passwor } } - // Needed to enable the screensharing extension in Chromium < 72. - Util::addHeader('meta', ['id' => "app", 'class' => 'nc-enable-screensharing-extension']); - - $this->initialStateService->provideInitialState( - 'talk', 'prefer_h264', - $this->serverConfig->getAppValue('spreed', 'prefer_h264', 'no') === 'yes' - ); - - $this->initialStateService->provideInitialState( - 'talk', 'circles_enabled', - $this->appManager->isEnabledForUser('circles', $user) - ); + $this->publishInitialStateForUser($user, $this->appManager); $response = new TemplateResponse($this->appName, 'index'); $csp = new ContentSecurityPolicy(); @@ -283,13 +269,7 @@ protected function guestEnterRoom(string $token, string $password): Response { } } - // Needed to enable the screensharing extension in Chromium < 72. - Util::addHeader('meta', ['id' => "app", 'class' => 'nc-enable-screensharing-extension']); - - $this->initialStateService->provideInitialState( - 'talk', 'prefer_h264', - $this->serverConfig->getAppValue('spreed', 'prefer_h264', 'no') === 'yes' - ); + $this->publishInitialStateForGuest(); $response = new PublicTemplateResponse($this->appName, 'index'); $response->setFooterVisible(false); diff --git a/lib/Files/TemplateLoader.php b/lib/Files/TemplateLoader.php index 23fbc93af70..192f1dc09b5 100644 --- a/lib/Files/TemplateLoader.php +++ b/lib/Files/TemplateLoader.php @@ -25,9 +25,16 @@ use OCA\Files\Event\LoadSidebar; use OCA\Talk\AppInfo\Application; +use OCA\Talk\Config; +use OCA\Talk\TInitialState; +use OCP\App\IAppManager; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventDispatcher; use OCP\EventDispatcher\IEventListener; +use OCP\IConfig; +use OCP\IInitialStateService; +use OCP\IUser; +use OCP\IUserSession; use OCP\Util; /** @@ -35,8 +42,28 @@ */ class TemplateLoader implements IEventListener { + use TInitialState; + + /** @var IAppManager */ + private $appManager; + /** @var IUserSession */ + private $userSession; + + public function __construct(IInitialStateService $initialStateService, + Config $talkConfig, + IConfig $serverConfig, + IAppManager $appManager, + IUserSession $userSession) { + $this->initialStateService = $initialStateService; + $this->talkConfig = $talkConfig; + $this->serverConfig = $serverConfig; + $this->appManager = $appManager; + $this->userSession = $userSession; + } + + public static function register(IEventDispatcher $dispatcher): void { - $dispatcher->addServiceListener(LoadSidebar::class, TemplateLoader::class); + $dispatcher->addServiceListener(LoadSidebar::class, self::class); } /** @@ -44,14 +71,15 @@ public static function register(IEventDispatcher $dispatcher): void { * * This method should be called when handling the LoadSidebar event of the * Files app. + * + * @param Event $event */ public function handle(Event $event): void { if (!($event instanceof LoadSidebar)) { return; } - $config = \OC::$server->getConfig(); - if ($config->getAppValue('spreed', 'conversations_files', '1') !== '1') { + if ($this->serverConfig->getAppValue('spreed', 'conversations_files', '1') !== '1') { return; } @@ -59,8 +87,12 @@ public function handle(Event $event): void { Util::addScript(Application::APP_ID, 'talk-files-sidebar'); Util::addScript(Application::APP_ID, 'talk-files-sidebar-loader'); - // Needed to enable the screensharing extension in Chromium < 72. - Util::addHeader('meta', ['id' => "app", 'class' => 'nc-enable-screensharing-extension']); + $user = $this->userSession->getUser(); + if ($user instanceof IUser) { + $this->publishInitialStateForUser($user, $this->appManager); + } else { + $this->publishInitialStateForGuest(); + } } } diff --git a/lib/PublicShare/TemplateLoader.php b/lib/PublicShare/TemplateLoader.php index 80a3d699145..e03bb332781 100644 --- a/lib/PublicShare/TemplateLoader.php +++ b/lib/PublicShare/TemplateLoader.php @@ -24,8 +24,12 @@ namespace OCA\Talk\PublicShare; +use OCA\Talk\Config; +use OCA\Talk\TInitialState; use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\FileInfo; +use OCP\IConfig; +use OCP\IInitialStateService; use OCP\Share\IShare; use OCP\Util; use Symfony\Component\EventDispatcher\GenericEvent; @@ -39,11 +43,23 @@ */ class TemplateLoader { + use TInitialState; + + public function __construct(IInitialStateService $initialStateService, + Config $talkConfig, + IConfig $serverConfig) { + $this->initialStateService = $initialStateService; + $this->talkConfig = $talkConfig; + $this->serverConfig = $serverConfig; + } + public static function register(IEventDispatcher $dispatcher): void { $dispatcher->addListener('OCA\Files_Sharing::loadAdditionalScripts', static function(GenericEvent $event) { /** @var IShare $share */ $share = $event->getArgument('share'); - self::loadTalkSidebarUi($share); + /** @var self $templateLoader */ + $templateLoader = \OC::$server->query(self::class); + $templateLoader->loadTalkSidebarUi($share); }); } @@ -55,10 +71,9 @@ public static function register(IEventDispatcher $dispatcher): void { * * @param IShare $share */ - public static function loadTalkSidebarUi(IShare $share): void { - $config = \OC::$server->getConfig(); - if ($config->getAppValue('spreed', 'conversations_files', '1') !== '1' || - $config->getAppValue('spreed', 'conversations_files_public_shares', '1') !== '1') { + public function loadTalkSidebarUi(IShare $share): void { + if ($this->serverConfig->getAppValue('spreed', 'conversations_files', '1') !== '1' || + $this->serverConfig->getAppValue('spreed', 'conversations_files_public_shares', '1') !== '1') { return; } @@ -69,8 +84,7 @@ public static function loadTalkSidebarUi(IShare $share): void { Util::addStyle('spreed', 'merged-public-share'); Util::addScript('spreed', 'talk-public-share-sidebar'); - // Needed to enable the screensharing extension in Chromium < 72. - Util::addHeader('meta', ['id' => "app", 'class' => 'nc-enable-screensharing-extension']); + $this->publishInitialStateForGuest(); } } diff --git a/lib/PublicShareAuth/TemplateLoader.php b/lib/PublicShareAuth/TemplateLoader.php index 89bfc765417..9aeccf565c6 100644 --- a/lib/PublicShareAuth/TemplateLoader.php +++ b/lib/PublicShareAuth/TemplateLoader.php @@ -23,7 +23,11 @@ namespace OCA\Talk\PublicShareAuth; +use OCA\Talk\Config; +use OCA\Talk\TInitialState; use OCP\EventDispatcher\IEventDispatcher; +use OCP\IConfig; +use OCP\IInitialStateService; use OCP\Share\IShare; use OCP\Util; use Symfony\Component\EventDispatcher\GenericEvent; @@ -37,11 +41,23 @@ */ class TemplateLoader { + use TInitialState; + + public function __construct(IInitialStateService $initialStateService, + Config $talkConfig, + IConfig $serverConfig) { + $this->initialStateService = $initialStateService; + $this->talkConfig = $talkConfig; + $this->serverConfig = $serverConfig; + } + public static function register(IEventDispatcher $dispatcher): void { - $listener = function(GenericEvent $event) { + $listener = static function(GenericEvent $event) { /** @var IShare $share */ $share = $event->getArgument('share'); - self::loadRequestPasswordByTalkUi($share); + /** @var self $templateLoader */ + $templateLoader = \OC::$server->query(self::class); + $templateLoader->loadRequestPasswordByTalkUi($share); }; $dispatcher->addListener('OCA\Files_Sharing::loadAdditionalScripts::publicShareAuth', $listener); } @@ -58,7 +74,7 @@ public static function register(IEventDispatcher $dispatcher): void { * * @param IShare $share */ - public static function loadRequestPasswordByTalkUi(IShare $share): void { + public function loadRequestPasswordByTalkUi(IShare $share): void { if (!$share->getSendPasswordByTalk()) { return; } @@ -66,8 +82,8 @@ public static function loadRequestPasswordByTalkUi(IShare $share): void { Util::addStyle('spreed', 'merged-share-auth'); Util::addScript('spreed', 'talk-public-share-auth-sidebar'); - // Needed to enable the screensharing extension in Chromium < 72. - Util::addHeader('meta', ['id' => "app", 'class' => 'nc-enable-screensharing-extension']); + + $this->publishInitialStateForGuest(); } } diff --git a/lib/TInitialState.php b/lib/TInitialState.php new file mode 100644 index 00000000000..867e33b5159 --- /dev/null +++ b/lib/TInitialState.php @@ -0,0 +1,73 @@ + + * + * @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 . + * + */ + +namespace OCA\Talk; + + +use OC\User\NoUserException; +use OCP\App\IAppManager; +use OCP\Files\IRootFolder; +use OCP\Files\NotPermittedException; +use OCP\IConfig; +use OCP\IInitialStateService; +use OCP\IUser; +use OCP\Util; + +trait TInitialState { + + /** @var Config */ + protected $talkConfig; + /** @var IConfig */ + protected $serverConfig; + /** @var IInitialStateService */ + protected $initialStateService; + + protected function publishInitialStateShared(): void { + // Needed to enable the screensharing extension in Chromium < 72. + Util::addHeader('meta', ['id' => 'app', 'class' => 'nc-enable-screensharing-extension']); + + $this->initialStateService->provideInitialState( + 'talk', 'prefer_h264', + $this->serverConfig->getAppValue('spreed', 'prefer_h264', 'no') === 'yes' + ); + } + + protected function publishInitialStateForUser(IUser $user, IAppManager $appManager): void { + + $this->publishInitialStateShared(); + + $this->initialStateService->provideInitialState( + 'talk', 'circles_enabled', + $appManager->isEnabledForUser('circles', $user) + ); + } + + protected function publishInitialStateForGuest(): void { + + $this->publishInitialStateShared(); + + $this->initialStateService->provideInitialState( + 'talk', 'circles_enabled', + false + ); + } +}