Skip to content

Commit

Permalink
Merge pull request #29 from humhub/enh/go-app-service
Browse files Browse the repository at this point in the history
Wrap URLs in email messages to "go" app
  • Loading branch information
luke- authored Oct 4, 2023
2 parents 2bf1cc0 + dad2176 commit 9ddf14f
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 12 deletions.
10 changes: 8 additions & 2 deletions Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,14 +23,18 @@ 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');

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)
Expand Down Expand Up @@ -125,4 +131,4 @@ public static function onAfterLogin()
Yii::$app->session->set(self::SESSION_VAR_LOGIN, 1);
}

}
}
15 changes: 12 additions & 3 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand All @@ -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');
Expand Down
44 changes: 44 additions & 0 deletions components/MailerMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/

namespace humhub\modules\fcmPush\components;

use humhub\components\mail\Message;
use humhub\modules\fcmPush\Module;
use Yii;

/**
* Message
*
* @author Luke
*/
class MailerMessage extends Message
{
protected ?Module $module = null;

public function init()
{
parent::init();
$this->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));
}
}
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 5 additions & 0 deletions models/ConfigureForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

class ConfigureForm extends Model
{
public $enableEmailGoService;

public $humhubInstallId;

public $senderId;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -157,6 +160,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');
Expand All @@ -171,6 +175,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);
Expand Down
76 changes: 76 additions & 0 deletions services/GoService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/

namespace humhub\modules\fcmPush\services;

use humhub\modules\fcmPush\models\ConfigureForm;
use Yii;

/**
* Service to handle URLs to "go" app in order to open then in mobile app
*
* @author Luke
*/
class GoService
{
private ?string $appUrl;
private ?string $sitePattern;
private ?string $hid = null;
private bool $isEnabled = false;

public function __construct(string $appUrl)
{
$this->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('#(<a.+?href=")(' . $this->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;
}
}
19 changes: 12 additions & 7 deletions views/admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@
<div class="panel-body">
<?php $form = ActiveForm::begin(['id' => 'configure-form', 'enableClientValidation' => false, 'enableClientScript' => false]); ?>

<h3>Push Service (required for the mobile app) (Beta)</h3>
<h3><?= Yii::t('FcmPushModule.base', 'Link Redirection Service') ?></h3>
<?= $form->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 // <a href="{url}">Privacy Policy</a>)', [
'url' => 'https://www.humhub.com/en/privacy/'
])) ?>

<hr>

<h3><?= Yii::t('FcmPushModule.base', 'Push Service (required for the mobile app) (Beta)') ?></h3>
<p>
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.<br/>
Please note, this push gateway is only available for the users of the official HumHub mobile app.
<?= Yii::t('FcmPushModule.base', '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.<br/>Please note, this push gateway is only available for the users of the official HumHub mobile app.') ?>
</p>
<?= Button::info('Push Service Registration')->link('https://push.humhub.com')->options(['target' => '_blank'])->loader(false) ?>
<?= $form->field($model, 'humhubInstallId')->textInput(['disabled' => 'disabled']); ?>
Expand All @@ -27,9 +33,9 @@

<hr>

<h3>Firebase Cloud Messaging (required for browser & PWA notifications)</h3>
<h3><?= Yii::t('FcmPushModule.base', 'Firebase Cloud Messaging (required for browser & PWA notifications)') ?></h3>
<p>
To send Firebase push notifications with your own Firebase project, enter your access details here.
<?= Yii::t('FcmPushModule.base', 'To send Firebase push notifications with your own Firebase project, enter your access details here.') ?>
</p>
<?= Button::info('Installation documentation')->link('https://marketplace.humhub.com/module/fcm-push/installation')->options(['target' => '_blank'])->loader(false) ?>
<?= $form->field($model, 'senderId'); ?>
Expand All @@ -49,4 +55,3 @@
<?= Html::a('Mobile App Debug', ['/fcm-push/mobile-app'], ['class' => 'pull-right']); ?>
</div>
</div>

0 comments on commit 9ddf14f

Please sign in to comment.