From 260e151e878f64dd49b2bbb8598a4b871fda66fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Farr=C3=A9?= Date: Thu, 12 Dec 2024 14:41:34 +0000 Subject: [PATCH 1/6] For Multi-Instance apps, add a new item in the account menu to display the Opener app page, allowing to switch to the other HumHub instance --- Events.php | 23 ++++++++++++++++++++++- config.php | 11 +++++++---- controllers/MobileAppController.php | 10 ++++++++++ docs/CHANGELOG.md | 1 + helpers/MobileAppHelper.php | 9 ++++++++- 5 files changed, 48 insertions(+), 6 deletions(-) diff --git a/Events.php b/Events.php index 7a3c08e..718987c 100644 --- a/Events.php +++ b/Events.php @@ -11,6 +11,8 @@ use humhub\modules\fcmPush\services\DriverService; use humhub\modules\fcmPush\widgets\PushNotificationInfoWidget; use humhub\modules\notification\targets\MobileTargetProvider; +use humhub\modules\ui\menu\MenuLink; +use humhub\modules\user\widgets\AccountTopMenu; use humhub\modules\user\widgets\AuthChoice; use humhub\modules\web\pwa\controllers\ManifestController; use humhub\modules\web\pwa\controllers\ServiceWorkerController; @@ -90,7 +92,7 @@ public static function onLayoutAddonInit($event) { if (Yii::$app->session->has(self::SESSION_VAR_LOGOUT)) { MobileAppHelper::unregisterNotificationScript(); // Before Logout - MobileAppHelper::registerLogoutScript(); + MobileAppHelper::registerShowOpenerScript(); Yii::$app->session->remove(self::SESSION_VAR_LOGOUT); } @@ -137,4 +139,23 @@ public static function onAuthChoiceBeforeRun(Event $event) $sender->setClients([]); } } + + public static function onAccountTopMenuInit(Event $event) + { + if (!MobileAppHelper::isMultiInstanceApp()) { + return; + } + + /** @var AccountTopMenu $menu */ + $menu = $event->sender; + + $menu->addEntry(new MenuLink([ + 'label' => Yii::t('FcmPushModule.base', 'Switch network'), + 'url' => ['/fcm-push/mobile-app/instance-opener'], + 'icon' => 'arrows-h', + 'sortOrder' => 699, // Just before "Logout" + 'isActive' => true, + 'isVisible' => true, + ])); + } } diff --git a/config.php b/config.php index 4216cf3..1088e16 100644 --- a/config.php +++ b/config.php @@ -3,13 +3,15 @@ /** @noinspection MissedFieldInspection */ -use humhub\modules\fcmPush\Events; use humhub\components\Controller; -//use humhub\modules\notification\widgets\NotificationInfoWidget; +use humhub\modules\fcmPush\Events; +use humhub\modules\user\components\User; +use humhub\modules\user\widgets\AccountTopMenu; use humhub\modules\user\widgets\AuthChoice; use humhub\widgets\LayoutAddons; use yii\base\Application; -use humhub\modules\user\components\User; + +//use humhub\modules\notification\widgets\NotificationInfoWidget; return [ 'id' => 'fcm-push', @@ -23,7 +25,8 @@ [User::class, User::EVENT_AFTER_LOGOUT, [Events::class, 'onAfterLogout']], [User::class, User::EVENT_AFTER_LOGIN, [Events::class, 'onAfterLogin']], [AuthChoice::class, AuthChoice::EVENT_BEFORE_RUN, [Events::class, 'onAuthChoiceBeforeRun']], - //[NotificationInfoWidget::class, \humhub\widgets\BaseStack::EVENT_RUN, [Events::class, 'onNotificationInfoWidget']] + //[NotificationInfoWidget::class, \humhub\widgets\BaseStack::EVENT_RUN, [Events::class, 'onNotificationInfoWidget']], + [AccountTopMenu::class, AccountTopMenu::EVENT_INIT, [Events::class, 'onAccountTopMenuInit']], ], 'consoleControllerMap' => [ 'firebase' => 'humhub\modules\fcmPush\commands\SendController', diff --git a/controllers/MobileAppController.php b/controllers/MobileAppController.php index 068d498..00efbe4 100644 --- a/controllers/MobileAppController.php +++ b/controllers/MobileAppController.php @@ -4,6 +4,7 @@ use humhub\modules\admin\components\Controller; use humhub\modules\admin\notifications\NewVersionAvailable; +use humhub\modules\fcmPush\helpers\MobileAppHelper; use humhub\modules\fcmPush\models\FcmUser; use humhub\modules\user\models\User; use Yii; @@ -43,4 +44,13 @@ public function actionIndex() return $this->render('index'); } + public function actionInstanceOpener() + { + // Send to the mobile app to display the instance opener + MobileAppHelper::registerShowOpenerScript(); + + // Stay in the same page, for when we come back from the mobile app to this instance + return $this->redirect(Yii::$app->request->referrer); + } + } diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 4ba8552..8c306b8 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -4,6 +4,7 @@ Changelog 2.1.1 (unreleased) ------------------------ - Fix #58: iOS mobile app detection for iPad devices +- Enh: For Multi-Instance apps, add a new item in the account menu to display the Opener app page, allowing to switch to the other HumHub instance 2.1.0 (October 21, 2024) ------------------------ diff --git a/helpers/MobileAppHelper.php b/helpers/MobileAppHelper.php index 9d655cb..a4078f0 100644 --- a/helpers/MobileAppHelper.php +++ b/helpers/MobileAppHelper.php @@ -21,7 +21,7 @@ public static function registerLoginScript() self::sendFlutterMessage($message); } - public static function registerLogoutScript() + public static function registerShowOpenerScript() { if (!static::isAppRequest()) { return; @@ -90,4 +90,11 @@ public static function isIosApp(): bool static::isAppRequest() && Yii::$app->request->headers->get('x-humhub-app-is-ios'); } + + public static function isMultiInstanceApp(): bool + { + return + static::isAppRequest() + && Yii::$app->request->headers->get('x-humhub-app-is-multi-instance'); + } } From 456bcc146a042f4b80ab4860db55d58e487bb900 Mon Sep 17 00:00:00 2001 From: marc-farre Date: Thu, 12 Dec 2024 14:42:07 +0000 Subject: [PATCH 2/6] Autocommit PHP CS Fixer --- components/MailerMessage.php | 1 + components/UrlRule.php | 1 + controllers/WellKnownController.php | 1 + migrations/uninstall.php | 1 + services/GoService.php | 1 + services/WellKnownService.php | 1 + 6 files changed, 6 insertions(+) diff --git a/components/MailerMessage.php b/components/MailerMessage.php index dcd3b58..b3c8709 100644 --- a/components/MailerMessage.php +++ b/components/MailerMessage.php @@ -1,4 +1,5 @@ Date: Thu, 12 Dec 2024 14:43:27 +0000 Subject: [PATCH 3/6] Add PR ID to CHANGELOG --- docs/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 8c306b8..1e6dcb5 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -4,7 +4,7 @@ Changelog 2.1.1 (unreleased) ------------------------ - Fix #58: iOS mobile app detection for iPad devices -- Enh: For Multi-Instance apps, add a new item in the account menu to display the Opener app page, allowing to switch to the other HumHub instance +- Enh #60: For Multi-Instance apps, add a new item in the account menu to display the Opener app page, allowing to switch to the other HumHub instance 2.1.0 (October 21, 2024) ------------------------ From 8b337cecb83f286188609f7b4090d53768574cda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Farr=C3=A9?= Date: Fri, 13 Dec 2024 11:58:23 +0000 Subject: [PATCH 4/6] Fix Show Opener and introduce the openerState --- Events.php | 41 ++++++++++++------- config.php | 2 +- controllers/AdminController.php | 34 ++++++++++++++- controllers/MobileAppController.php | 41 ++----------------- helpers/MobileAppHelper.php | 28 ++++++++++++- views/admin/index.php | 4 +- .../index.php => admin/mobile-app.php} | 8 ++-- 7 files changed, 96 insertions(+), 62 deletions(-) rename views/{mobile-app/index.php => admin/mobile-app.php} (95%) diff --git a/Events.php b/Events.php index 718987c..6ab26bc 100644 --- a/Events.php +++ b/Events.php @@ -22,9 +22,6 @@ class Events { - private const SESSION_VAR_LOGOUT = 'mobileAppHandleLogout'; - private const SESSION_VAR_LOGIN = 'mobileAppHandleLogin'; - public static function onBeforeRequest() { /** @var Module $module */ @@ -90,16 +87,30 @@ public static function onNotificationInfoWidget($event) public static function onLayoutAddonInit($event) { - if (Yii::$app->session->has(self::SESSION_VAR_LOGOUT)) { - MobileAppHelper::unregisterNotificationScript(); // Before Logout - MobileAppHelper::registerShowOpenerScript(); - Yii::$app->session->remove(self::SESSION_VAR_LOGOUT); + // If the mobile app Opener page is open (after login and switching instance) + if (Yii::$app->session->has(MobileAppHelper::SESSION_VAR_HIDE_OPENER)) { + MobileAppHelper::registerHideOpenerScript(); + Yii::$app->session->remove(MobileAppHelper::SESSION_VAR_HIDE_OPENER); + } elseif (MobileAppHelper::openerState()) { + MobileAppHelper::registerHideOpenerScript(); } - if (Yii::$app->session->has(self::SESSION_VAR_LOGIN)) { - MobileAppHelper::registerLoginScript(); + // After login + if (Yii::$app->session->has(MobileAppHelper::SESSION_VAR_REGISTER_NOTIFICATION)) { MobileAppHelper::registerNotificationScript(); - Yii::$app->session->remove(self::SESSION_VAR_LOGIN); + Yii::$app->session->remove(MobileAppHelper::SESSION_VAR_REGISTER_NOTIFICATION); + } + + // Before logout + if (Yii::$app->session->has(MobileAppHelper::SESSION_VAR_UNREGISTER_NOTIFICATION)) { + MobileAppHelper::unregisterNotificationScript(); + Yii::$app->session->remove(MobileAppHelper::SESSION_VAR_UNREGISTER_NOTIFICATION); + } + + // After logout + if (Yii::$app->session->has(MobileAppHelper::SESSION_VAR_SHOW_OPENER)) { + MobileAppHelper::registerShowOpenerScript(); + Yii::$app->session->remove(MobileAppHelper::SESSION_VAR_SHOW_OPENER); } if (Yii::$app->user->isGuest) { @@ -117,14 +128,16 @@ public static function onLayoutAddonInit($event) FirebaseAsset::register(Yii::$app->view); } - public static function onAfterLogout() + public static function onAfterLogin() { - Yii::$app->session->set(self::SESSION_VAR_LOGOUT, 1); + Yii::$app->session->set(MobileAppHelper::SESSION_VAR_HIDE_OPENER, 1); + Yii::$app->session->set(MobileAppHelper::SESSION_VAR_REGISTER_NOTIFICATION, 1); } - public static function onAfterLogin() + public static function onAfterLogout() { - Yii::$app->session->set(self::SESSION_VAR_LOGIN, 1); + Yii::$app->session->set(MobileAppHelper::SESSION_VAR_UNREGISTER_NOTIFICATION, 1); + Yii::$app->session->set(MobileAppHelper::SESSION_VAR_SHOW_OPENER, 1); } public static function onAuthChoiceBeforeRun(Event $event) diff --git a/config.php b/config.php index 1088e16..9491c93 100644 --- a/config.php +++ b/config.php @@ -22,8 +22,8 @@ ['humhub\modules\web\pwa\controllers\ServiceWorkerController', Controller::EVENT_INIT, [Events::class, 'onServiceWorkerControllerInit']], [LayoutAddons::class, LayoutAddons::EVENT_INIT, [Events::class, 'onLayoutAddonInit']], [Application::class, Application::EVENT_BEFORE_REQUEST, [Events::class, 'onBeforeRequest']], - [User::class, User::EVENT_AFTER_LOGOUT, [Events::class, 'onAfterLogout']], [User::class, User::EVENT_AFTER_LOGIN, [Events::class, 'onAfterLogin']], + [User::class, User::EVENT_AFTER_LOGOUT, [Events::class, 'onAfterLogout']], [AuthChoice::class, AuthChoice::EVENT_BEFORE_RUN, [Events::class, 'onAuthChoiceBeforeRun']], //[NotificationInfoWidget::class, \humhub\widgets\BaseStack::EVENT_RUN, [Events::class, 'onNotificationInfoWidget']], [AccountTopMenu::class, AccountTopMenu::EVENT_INIT, [Events::class, 'onAccountTopMenuInit']], diff --git a/controllers/AdminController.php b/controllers/AdminController.php index b072f02..e4c5045 100644 --- a/controllers/AdminController.php +++ b/controllers/AdminController.php @@ -3,8 +3,10 @@ namespace humhub\modules\fcmPush\controllers; use humhub\modules\admin\components\Controller; -use humhub\modules\fcmPush\models\ConfigureForm; +use humhub\modules\admin\notifications\NewVersionAvailable; +use humhub\modules\fcmPush\models\FcmUser; use humhub\modules\fcmPush\Module; +use humhub\modules\user\models\User; use Yii; /** @@ -26,4 +28,34 @@ public function actionIndex() return $this->render('index', ['model' => $model]); } + /** + * @return string + */ + public function actionMobileApp() + { + if (Yii::$app->request->get('triggerNotification') == 1) { + + /** @var User $user */ + $user = Yii::$app->user->getIdentity(); + + $updateNotification = new NewVersionAvailable(); + $updateNotification->sendBulk(User::find()->where(['user.id' => $user->id])); + $this->view->setStatusMessage('success', 'Notification queued!'); + return $this->redirect('mobile-app'); + } + + if (Yii::$app->request->get('deleteToken') != "") { + $t = FcmUser::findOne(['id' => Yii::$app->request->get('deleteToken')]); + if ($t->delete() !== false) { + $this->view->setStatusMessage('success', 'Token deleted!'); + return $this->redirect('mobile-app'); + } + + $this->view->setStatusMessage('warning', 'Token NOT deleted!'); + return $this->redirect('mobile-app'); + } + + return $this->render('mobile-app'); + } + } diff --git a/controllers/MobileAppController.php b/controllers/MobileAppController.php index 00efbe4..0a4e6f7 100644 --- a/controllers/MobileAppController.php +++ b/controllers/MobileAppController.php @@ -2,55 +2,20 @@ namespace humhub\modules\fcmPush\controllers; -use humhub\modules\admin\components\Controller; -use humhub\modules\admin\notifications\NewVersionAvailable; +use humhub\components\Controller; use humhub\modules\fcmPush\helpers\MobileAppHelper; -use humhub\modules\fcmPush\models\FcmUser; -use humhub\modules\user\models\User; use Yii; class MobileAppController extends Controller { - /** - * Renders the index view for the module - * - * @return string - */ - public function actionIndex() - { - - if (Yii::$app->request->get('triggerNotification') == 1) { - - /** @var User $user */ - $user = Yii::$app->user->getIdentity(); - - $updateNotification = new NewVersionAvailable(); - $updateNotification->sendBulk(User::find()->where(['user.id' => $user->id])); - $this->view->setStatusMessage('success', 'Notification queued!'); - return $this->redirect('index'); - } - - if (Yii::$app->request->get('deleteToken') != "") { - $t = FcmUser::findOne(['id' => Yii::$app->request->get('deleteToken')]); - if ($t->delete() !== false) { - $this->view->setStatusMessage('success', 'Token deleted!'); - return $this->redirect('index'); - } else { - $this->view->setStatusMessage('warning', 'Token NOT deleted!'); - return $this->redirect('index'); - } - } - - return $this->render('index'); - } public function actionInstanceOpener() { // Send to the mobile app to display the instance opener - MobileAppHelper::registerShowOpenerScript(); + Yii::$app->session->set(MobileAppHelper::SESSION_VAR_SHOW_OPENER, 1); // Stay in the same page, for when we come back from the mobile app to this instance - return $this->redirect(Yii::$app->request->referrer); + return $this->refresh(); } } diff --git a/helpers/MobileAppHelper.php b/helpers/MobileAppHelper.php index a4078f0..cc24527 100644 --- a/helpers/MobileAppHelper.php +++ b/helpers/MobileAppHelper.php @@ -8,9 +8,16 @@ class MobileAppHelper { - public static function registerLoginScript() - { + public const SESSION_VAR_SHOW_OPENER = 'mobileAppShowOpener'; + /** + * @deprecated Remove when minimal HumHub mobile app support is v1.0.124 and later + */ + public const SESSION_VAR_HIDE_OPENER = 'mobileAppHideOpener'; + public const SESSION_VAR_REGISTER_NOTIFICATION = 'mobileAppRegisterNotification'; + public const SESSION_VAR_UNREGISTER_NOTIFICATION = 'mobileAppUnregisterNotification'; + public static function registerHideOpenerScript() + { if (!static::isAppRequest()) { return; } @@ -91,10 +98,27 @@ public static function isIosApp(): bool && Yii::$app->request->headers->get('x-humhub-app-is-ios'); } + /** + * True if the mobile app supports multi instance to display the Opener landing page without logout for switching instance + * + * @since HumHub mobile app v1.0.124 + */ public static function isMultiInstanceApp(): bool { return static::isAppRequest() && Yii::$app->request->headers->get('x-humhub-app-is-multi-instance'); } + + /** + * True if the mobile app Opener landing page is visible and should be hidden. + * + * @since HumHub mobile app v1.0.124 + */ + public static function openerState(): bool + { + return + static::isAppRequest() + && Yii::$app->request->headers->get('x-humhub-app-opener-state'); + } } diff --git a/views/admin/index.php b/views/admin/index.php index bc55409..f53b994 100644 --- a/views/admin/index.php +++ b/views/admin/index.php @@ -1,9 +1,9 @@ - 'pull-right']); ?> + 'pull-right']) ?> diff --git a/views/mobile-app/index.php b/views/admin/mobile-app.php similarity index 95% rename from views/mobile-app/index.php rename to views/admin/mobile-app.php index 508dff7..e043837 100644 --- a/views/mobile-app/index.php +++ b/views/admin/mobile-app.php @@ -43,7 +43,7 @@ 'btn btn-default postFlutterMsgLink', 'data-message' => Json::encode(['type' => 'showOpener'])]); ?> 'btn btn-default postFlutterMsgLink', 'data-message' => Json::encode(['type' => 'hideOpener'])]); ?> - 'POST', 'class' => 'btn btn-default']); ?> + 'POST', 'class' => 'btn btn-default']); ?> @@ -56,7 +56,7 @@ Administrative Notifications!. It may take a few minutes.

- 1], ['class' => 'btn btn-primary pull-right']) ?> + 1], ['class' => 'btn btn-primary pull-right']) ?> @@ -107,7 +107,7 @@ sender_id ?> · - $fcm->id, 'confirm' => 'PWA: You may need to delete token from localStorage to trigger resave!']) ?> + $fcm->id, 'confirm' => 'PWA: You may need to delete token from localStorage to trigger resave!']) ?> @@ -215,4 +215,4 @@ } }); - \ No newline at end of file + From a10b0921518003e989c6d9866df165f956d335d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Farr=C3=A9?= Date: Fri, 13 Dec 2024 12:13:14 +0000 Subject: [PATCH 5/6] Fix redirection after switching instance --- controllers/MobileAppController.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/controllers/MobileAppController.php b/controllers/MobileAppController.php index 0a4e6f7..0fae695 100644 --- a/controllers/MobileAppController.php +++ b/controllers/MobileAppController.php @@ -3,19 +3,22 @@ namespace humhub\modules\fcmPush\controllers; use humhub\components\Controller; +use humhub\libs\Html; use humhub\modules\fcmPush\helpers\MobileAppHelper; use Yii; class MobileAppController extends Controller { - public function actionInstanceOpener() { // Send to the mobile app to display the instance opener Yii::$app->session->set(MobileAppHelper::SESSION_VAR_SHOW_OPENER, 1); - // Stay in the same page, for when we come back from the mobile app to this instance - return $this->refresh(); + // Stay on the same page, because when we come back from the mobile app to this instance + // Force full page refresh to trigger the onLayoutAddonInit event + return $this->renderContent(sprintf( + '', + Yii::$app->request->referrer, + )); } - } From ba00673d96591a7cad34817ffda24505664320be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Farr=C3=A9?= Date: Fri, 13 Dec 2024 16:38:52 +0000 Subject: [PATCH 6/6] https://github.com/humhub/fcm-push/pull/60#discussion_r1883967828 --- Events.php | 3 +++ controllers/MobileAppController.php | 7 +------ 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/Events.php b/Events.php index 6ab26bc..b97c702 100644 --- a/Events.php +++ b/Events.php @@ -169,6 +169,9 @@ public static function onAccountTopMenuInit(Event $event) 'sortOrder' => 699, // Just before "Logout" 'isActive' => true, 'isVisible' => true, + 'htmlOptions' => [ + 'data-pjax' => '0', // Force full page refresh to trigger the onLayoutAddonInit event + ], ])); } } diff --git a/controllers/MobileAppController.php b/controllers/MobileAppController.php index 0fae695..2c8a16b 100644 --- a/controllers/MobileAppController.php +++ b/controllers/MobileAppController.php @@ -3,7 +3,6 @@ namespace humhub\modules\fcmPush\controllers; use humhub\components\Controller; -use humhub\libs\Html; use humhub\modules\fcmPush\helpers\MobileAppHelper; use Yii; @@ -15,10 +14,6 @@ public function actionInstanceOpener() Yii::$app->session->set(MobileAppHelper::SESSION_VAR_SHOW_OPENER, 1); // Stay on the same page, because when we come back from the mobile app to this instance - // Force full page refresh to trigger the onLayoutAddonInit event - return $this->renderContent(sprintf( - '', - Yii::$app->request->referrer, - )); + return $this->htmlRedirect(Yii::$app->request->referrer); } }