From cd68431e4563d96489f9d4d88064a8592ccb3990 Mon Sep 17 00:00:00 2001 From: Yuriy Bakhtin Date: Mon, 18 Sep 2023 19:57:05 +0200 Subject: [PATCH 1/2] Wrap URLs in email messages to "go" app --- Events.php | 10 ++++- Module.php | 15 +++++-- components/MailerMessage.php | 44 +++++++++++++++++++++ docs/CHANGELOG.md | 4 ++ models/ConfigureForm.php | 6 +++ services/GoService.php | 76 ++++++++++++++++++++++++++++++++++++ views/admin/index.php | 16 ++++---- 7 files changed, 159 insertions(+), 12 deletions(-) create mode 100644 components/MailerMessage.php create mode 100644 services/GoService.php diff --git a/Events.php b/Events.php index feb7642..198c84f 100644 --- a/Events.php +++ b/Events.php @@ -3,8 +3,10 @@ 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; @@ -21,7 +23,7 @@ class Events private const SESSION_VAR_LOGOUT = 'mobileAppHandleLogout'; private const SESSION_VAR_LOGIN = 'mobileAppHandleLogin'; - public static function onBeforeRequest($event) + public static function onBeforeRequest() { /** @var Module $module */ $module = Yii::$app->getModule('fcm-push'); @@ -29,6 +31,10 @@ public static function onBeforeRequest($event) 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) @@ -125,4 +131,4 @@ public static function onAfterLogin() Yii::$app->session->set(self::SESSION_VAR_LOGIN, 1); } -} \ No newline at end of file +} diff --git a/Module.php b/Module.php index 7a0a0b4..3692f86 100644 --- a/Module.php +++ b/Module.php @@ -4,9 +4,9 @@ use humhub\modules\fcmPush\models\ConfigureForm; use humhub\modules\fcmPush\services\DriverService; +use humhub\modules\fcmPush\services\GoService; use Yii; use yii\helpers\Url; -use Kreait\Firebase\Factory; class Module extends \humhub\components\Module { @@ -18,7 +18,8 @@ class Module extends \humhub\components\Module public string $humhubProxySenderId = '21392898126'; private ?ConfigureForm $configForm = null; - private ?DriverService $driverService= null; + private ?DriverService $driverService = null; + private ?GoService $goService = null; /** * @inheritdoc @@ -39,12 +40,20 @@ public function getConfigureForm(): ConfigureForm public function getDriverService(): DriverService { - if (!$this->driverService) { + if ($this->driverService === null) { $this->driverService = new DriverService($this->getConfigureForm()); } return $this->driverService; } + public function getGoService(): GoService + { + if ($this->goService === null) { + $this->goService = new GoService('https://go.humhub.com'); + } + return $this->goService; + } + public static function registerAutoloader() { require Yii::getAlias('@fcm-push/vendor/autoload.php'); diff --git a/components/MailerMessage.php b/components/MailerMessage.php new file mode 100644 index 0000000..dcd3b58 --- /dev/null +++ b/components/MailerMessage.php @@ -0,0 +1,44 @@ +module = Yii::$app->getModule('fcm-push'); + } + + /** + * @inheritdoc + */ + public function setHtmlBody($html): Message + { + return Message::setHtmlBody($this->module->getGoService()->processLinks($html)); + } + + /** + * @inheritdoc + */ + public function setTextBody($text): Message + { + return Message::setTextBody($this->module->getGoService()->processUrls($text)); + } +} diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 9b4980b..9953579 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,3 +1,7 @@ +2.0.0-beta.8 (Unreleased) +------------------------- +- Enh #29: Wrap URLs in email messages to "go" app + 2.0.0-beta.7 (August 21, 2023) ------------------------------ - Enh: Added Token Unregister on App Logout diff --git a/models/ConfigureForm.php b/models/ConfigureForm.php index 8cd143f..3f7dc10 100644 --- a/models/ConfigureForm.php +++ b/models/ConfigureForm.php @@ -10,6 +10,8 @@ class ConfigureForm extends Model { + public $enableEmailGoService; + public $humhubInstallId; public $senderId; @@ -78,6 +80,7 @@ private function validateJsonParams($arrayPattern, $arrayCheck) public function rules() { return [ + [['enableEmailGoService'], 'boolean'], [['senderId'], 'number'], [['serverKey', 'json', 'humhubApiKey'], 'safe'], ['json', function ($attribute, $params, $validator) { @@ -130,6 +133,7 @@ public function rules() public function attributeLabels() { return [ + 'enableEmailGoService' => Yii::t('FcmPushModule.base', 'Enable Link Redirection Service for Links in outgoing E-Mails'), 'humhubInstallId' => Yii::t('FcmPushModule.base', 'Install ID'), 'humhubApiKey' => Yii::t('FcmPushModule.base', 'API Key'), 'senderId' => Yii::t('FcmPushModule.base', 'Sender ID'), @@ -157,6 +161,7 @@ public function loadSettings() /** @var \humhub\modules\admin\Module $adminModule */ $adminModule = Yii::$app->getModule('admin'); + $this->enableEmailGoService = $settings->get('enableEmailGoService', false); $this->humhubInstallId = $adminModule->settings->get('installationId'); $this->senderId = $settings->get('senderId'); $this->json = $settings->get('json'); @@ -171,6 +176,7 @@ public function saveSettings() /** @var Module $module */ $module = Yii::$app->getModule('fcm-push'); + $module->settings->set('enableEmailGoService', $this->enableEmailGoService); $module->settings->set('senderId', $this->senderId); $module->settings->set('json', $this->json); $module->settings->set('serverKey', $this->serverKey); diff --git a/services/GoService.php b/services/GoService.php new file mode 100644 index 0000000..91f90d5 --- /dev/null +++ b/services/GoService.php @@ -0,0 +1,76 @@ +appUrl = $appUrl; + + $baseUrl = Yii::$app->settings->get('baseUrl'); + $this->sitePattern = is_string($baseUrl) ? preg_quote($baseUrl, '#') : null; + + $installationId = Yii::$app->getModule('admin')->settings->get('installationId'); + $this->hid = is_string($installationId) && strlen($installationId) > 5 + ? substr($installationId, 0, 3) . substr($installationId, -3) + : null; + + $this->isEnabled = (bool) ConfigureForm::getInstance()->enableEmailGoService; + } + + public function isConfigured(): bool + { + return $this->isEnabled && + is_string($this->appUrl) && + is_string($this->hid) && + is_string($this->sitePattern); + } + + public function processUrls(string $text): ?string + { + return $this->isConfigured() + ? preg_replace_callback('#' . $this->sitePattern . '[^\s]+#i', [$this, 'callbackReplaceUrl'], $text) + : $text; + } + + private function callbackReplaceUrl($matches) + { + return $this->buildUrl($matches[0]); + } + + public function processLinks($text): ?string + { + return $this->isConfigured() + ? preg_replace_callback('#(sitePattern . '.+?)(".*?>)#i', [$this, 'callbackReplaceLink'], $text) + : $text; + } + + private function callbackReplaceLink($matches) + { + return $matches[1] . $this->buildUrl($matches[2]) . $matches[3]; + } + + public function buildUrl(string $url): string + { + return $this->appUrl . '?url=' . urlencode($url) . '&hid=' . $this->hid; + } +} diff --git a/views/admin/index.php b/views/admin/index.php index 07d0dab..6966fed 100644 --- a/views/admin/index.php +++ b/views/admin/index.php @@ -13,11 +13,14 @@
'configure-form', 'enableClientValidation' => false, 'enableClientScript' => false]); ?> -

Push Service (required for the mobile app) (Beta)

+

+ field($model, 'enableEmailGoService')->checkbox() ?> + +
+ +

- For HumHub mobile app users, push notifications can be sent via the HumHub push service. - If you want to use this service, please enter your access key below.
- Please note, this push gateway is only available for the users of the official HumHub mobile app. + Please note, this push gateway is only available for the users of the official HumHub mobile app.') ?>

link('https://push.humhub.com')->options(['target' => '_blank'])->loader(false) ?> field($model, 'humhubInstallId')->textInput(['disabled' => 'disabled']); ?> @@ -27,9 +30,9 @@
-

Firebase Cloud Messaging (required for browser & PWA notifications)

+

- To send Firebase push notifications with your own Firebase project, enter your access details here. +

link('https://marketplace.humhub.com/module/fcm-push/installation')->options(['target' => '_blank'])->loader(false) ?> field($model, 'senderId'); ?> @@ -49,4 +52,3 @@ 'pull-right']); ?>
- From dad217683972ff76a345afe6a8fa0eed4fe7f766 Mon Sep 17 00:00:00 2001 From: Yuriy Bakhtin Date: Wed, 4 Oct 2023 16:26:02 +0200 Subject: [PATCH 2/2] Wording --- models/ConfigureForm.php | 1 - views/admin/index.php | 7 +++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/models/ConfigureForm.php b/models/ConfigureForm.php index 3f7dc10..26874e3 100644 --- a/models/ConfigureForm.php +++ b/models/ConfigureForm.php @@ -133,7 +133,6 @@ public function rules() public function attributeLabels() { return [ - 'enableEmailGoService' => Yii::t('FcmPushModule.base', 'Enable Link Redirection Service for Links in outgoing E-Mails'), 'humhubInstallId' => Yii::t('FcmPushModule.base', 'Install ID'), 'humhubApiKey' => Yii::t('FcmPushModule.base', 'API Key'), 'senderId' => Yii::t('FcmPushModule.base', 'Sender ID'), diff --git a/views/admin/index.php b/views/admin/index.php index 6966fed..454ae7e 100644 --- a/views/admin/index.php +++ b/views/admin/index.php @@ -13,8 +13,11 @@
'configure-form', 'enableClientValidation' => false, 'enableClientScript' => false]); ?> -

- field($model, 'enableEmailGoService')->checkbox() ?> +

+ field($model, 'enableEmailGoService')->checkbox() + ->label(Yii::t('FcmPushModule.base', 'Enable Link Redirection Service. In order for links to open in the app on mobile devices, rather than in the mobile browser, all links (e.g. notification emails) need to be routed through the HumHub proxy server. (Experimental Features // Privacy Policy)', [ + 'url' => 'https://www.humhub.com/en/privacy/' + ])) ?>