Skip to content

Commit

Permalink
Merge pull request #60 from humhub/enh-add-app-multi-instance-switcher
Browse files Browse the repository at this point in the history
For Multi-Instance apps, add a new item in the account menu to displa…
  • Loading branch information
luke- authored Dec 16, 2024
2 parents 45202c6 + ba00673 commit 940f5fc
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 63 deletions.
65 changes: 51 additions & 14 deletions Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,9 +22,6 @@

class Events
{
private const SESSION_VAR_LOGOUT = 'mobileAppHandleLogout';
private const SESSION_VAR_LOGIN = 'mobileAppHandleLogin';

public static function onBeforeRequest()
{
/** @var Module $module */
Expand Down Expand Up @@ -88,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::registerLogoutScript();
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) {
Expand All @@ -115,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)
Expand All @@ -137,4 +152,26 @@ 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,
'htmlOptions' => [
'data-pjax' => '0', // Force full page refresh to trigger the onLayoutAddonInit event
],
]));
}
}
1 change: 1 addition & 0 deletions components/MailerMessage.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
Expand Down
1 change: 1 addition & 0 deletions components/UrlRule.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
Expand Down
13 changes: 8 additions & 5 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -20,10 +22,11 @@
['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']]
//[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',
Expand Down
34 changes: 33 additions & 1 deletion controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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');
}

}
41 changes: 7 additions & 34 deletions controllers/MobileAppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,18 @@

namespace humhub\modules\fcmPush\controllers;

use humhub\modules\admin\components\Controller;
use humhub\modules\admin\notifications\NewVersionAvailable;
use humhub\modules\fcmPush\models\FcmUser;
use humhub\modules\user\models\User;
use humhub\components\Controller;
use humhub\modules\fcmPush\helpers\MobileAppHelper;
use Yii;

class MobileAppController extends Controller
{
/**
* Renders the index view for the module
*
* @return string
*/
public function actionIndex()
public function actionInstanceOpener()
{
// Send to the mobile app to display the instance opener
Yii::$app->session->set(MobileAppHelper::SESSION_VAR_SHOW_OPENER, 1);

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');
// Stay on the same page, because when we come back from the mobile app to this instance
return $this->htmlRedirect(Yii::$app->request->referrer);
}

}
1 change: 1 addition & 0 deletions controllers/WellKnownController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
Expand Down
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
2.1.1 (unreleased)
------------------------
- Fix #58: iOS mobile app detection for iPad devices
- 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)
------------------------
Expand Down
37 changes: 34 additions & 3 deletions helpers/MobileAppHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -21,7 +28,7 @@ public static function registerLoginScript()
self::sendFlutterMessage($message);
}

public static function registerLogoutScript()
public static function registerShowOpenerScript()
{
if (!static::isAppRequest()) {
return;
Expand Down Expand Up @@ -90,4 +97,28 @@ public static function isIosApp(): bool
static::isAppRequest()
&& 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');
}
}
1 change: 1 addition & 0 deletions migrations/uninstall.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2015 HumHub GmbH & Co. KG
Expand Down
1 change: 1 addition & 0 deletions services/GoService.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
Expand Down
1 change: 1 addition & 0 deletions services/WellKnownService.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
Expand Down
4 changes: 2 additions & 2 deletions views/admin/index.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

use humhub\modules\fcmPush\models\ConfigureForm;
use humhub\modules\ui\form\widgets\ActiveForm;
use humhub\modules\ui\icon\widgets\Icon;
use humhub\widgets\Button;
use humhub\modules\ui\form\widgets\ActiveForm;
use yii\helpers\Html;

/* @var $model ConfigureForm */
Expand Down Expand Up @@ -70,6 +70,6 @@

<?php ActiveForm::end(); ?>

<?= Html::a('Mobile App Debug', ['/fcm-push/mobile-app'], ['class' => 'pull-right']); ?>
<?= Html::a('Mobile App Debug', ['/fcm-push/admin/mobile-app'], ['class' => 'pull-right']) ?>
</div>
</div>
8 changes: 4 additions & 4 deletions views/mobile-app/index.php → views/admin/mobile-app.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

<?= Html::a('Show Opener', '#', ['class' => 'btn btn-default postFlutterMsgLink', 'data-message' => Json::encode(['type' => 'showOpener'])]); ?>
<?= Html::a('Hide Opener', '#', ['class' => 'btn btn-default postFlutterMsgLink', 'data-message' => Json::encode(['type' => 'hideOpener'])]); ?>
<?= Html::a('Open this page as POST Request', ['index'], ['data-method' => 'POST', 'class' => 'btn btn-default']); ?>
<?= Html::a('Open this page as POST Request', ['mobile-app'], ['data-method' => 'POST', 'class' => 'btn btn-default']); ?>

</div>
</div>
Expand All @@ -56,7 +56,7 @@
Administrative Notifications!</a>. It may take a few minutes.
</p>

<?= Html::a('Trigger "HumHub Update" notification', ['index', 'triggerNotification' => 1], ['class' => 'btn btn-primary pull-right']) ?>
<?= Html::a('Trigger "HumHub Update" notification', ['mobile-app', 'triggerNotification' => 1], ['class' => 'btn btn-primary pull-right']) ?>

</div>
</div>
Expand Down Expand Up @@ -107,7 +107,7 @@
<?= $fcm->sender_id ?>
&middot;

<?= Html::a('Delete', ['index', 'deleteToken' => $fcm->id, 'confirm' => 'PWA: You may need to delete token from localStorage to trigger resave!']) ?>
<?= Html::a('Delete', ['mobile-app', 'deleteToken' => $fcm->id, 'confirm' => 'PWA: You may need to delete token from localStorage to trigger resave!']) ?>
</li>
<?php endforeach; ?>
</ul>
Expand Down Expand Up @@ -215,4 +215,4 @@
}
});

</script>
</script>

0 comments on commit 940f5fc

Please sign in to comment.