-
Notifications
You must be signed in to change notification settings - Fork 7
/
Events.php
176 lines (144 loc) · 6.09 KB
/
Events.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
namespace humhub\modules\fcmPush;
use humhub\components\mail\Message;
use humhub\modules\fcmPush\assets\FcmPushAsset;
use humhub\modules\fcmPush\assets\FirebaseAsset;
use humhub\modules\fcmPush\components\MailerMessage;
use humhub\modules\fcmPush\components\NotificationTargetProvider;
use humhub\modules\fcmPush\helpers\MobileAppHelper;
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;
use humhub\widgets\BaseStack;
use Yii;
use yii\base\Event;
class Events
{
public static function onBeforeRequest()
{
/** @var Module $module */
$module = Yii::$app->getModule('fcm-push');
if ($module->getDriverService()->hasConfiguredDriver()) {
Yii::$container->set(MobileTargetProvider::class, NotificationTargetProvider::class);
}
if ($module->getGoService()->isConfigured()) {
Yii::$container->set(Message::class, MailerMessage::class);
}
}
public static function onManifestControllerInit($event)
{
/** @var ManifestController $controller */
$controller = $event->sender;
$controller->manifest['gcm_sender_id'] = (string)103953800507;
}
public static function onServiceWorkerControllerInit($event): void
{
/** @var ServiceWorkerController $controller */
$controller = $event->sender;
/** @var Module $module */
$module = Yii::$app->getModule('fcm-push');
if (!$module->getDriverService()->hasConfiguredWebDriver()) {
return;
}
$bundle = FirebaseAsset::register(Yii::$app->view);
$pushDriver = (new DriverService($module->getConfigureForm()))->getWebDriver();
// Service Worker Addons
$controller->additionalJs .= <<<JS
// Give the service worker access to Firebase Messaging.
importScripts('{$bundle->baseUrl}/firebase-app-compat.js');
importScripts('{$bundle->baseUrl}/firebase-messaging-compat.js');
firebase.initializeApp({
messagingSenderId: "{$pushDriver->getSenderId()}",
projectId: "{$module->getConfigureForm()->getJsonParam('project_id')}",
appId: "{$module->getConfigureForm()->firebaseAppId}",
apiKey: "{$module->getConfigureForm()->firebaseApiKey}",
});
// Initialize Firebase Cloud Messaging and get a reference to the service
firebase.messaging();
JS;
}
public static function onNotificationInfoWidget($event)
{
/** @var BaseStack $baseStack */
$baseStack = $event->sender;
$baseStack->addWidget(PushNotificationInfoWidget::class);
}
public static function onLayoutAddonInit($event)
{
// 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();
}
// After login
if (Yii::$app->session->has(MobileAppHelper::SESSION_VAR_REGISTER_NOTIFICATION)) {
MobileAppHelper::registerNotificationScript();
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) {
return;
}
/** @var Module $module */
$module = Yii::$app->getModule('fcm-push');
if (!$module->getDriverService()->hasConfiguredWebDriver()) {
return;
}
FcmPushAsset::register(Yii::$app->view);
FirebaseAsset::register(Yii::$app->view);
}
public static function onAfterLogin()
{
Yii::$app->session->set(MobileAppHelper::SESSION_VAR_HIDE_OPENER, 1);
Yii::$app->session->set(MobileAppHelper::SESSION_VAR_REGISTER_NOTIFICATION, 1);
}
public static function onAfterLogout()
{
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)
{
/** @var AuthChoice $sender */
$sender = $event->sender;
/** @var Module $module */
$module = Yii::$app->getModule('fcm-push');
if (MobileAppHelper::isIosApp() && $module->getConfigureForm()->disableAuthChoicesIos) {
$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"
'isVisible' => true,
'htmlOptions' => [
'data-pjax' => '0', // Force full page refresh to trigger the onLayoutAddonInit event
],
]));
}
}