Skip to content

Commit

Permalink
Merge branch 'askvortsov1:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
DellZHackintosh authored Jun 28, 2024
2 parents cd2d080 + d1e14b4 commit 38d424c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 23 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"flarum/core": "^1.7",
"minishlink/web-push": "^7.0",
"spomky-labs/base64url" : "^2.0",
"kreait/firebase-php": "^7.9"
"kreait/firebase-php": "^5.0"
},
"suggest": {
"ext-gmp": "This can improve push notification performance."
Expand Down
2 changes: 1 addition & 1 deletion js/dist/admin.js.map

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions js/src/admin/components/PWAPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,13 @@ export default class PWAPage extends ExtensionPage {
setting: 'askvortsov-pwa.windowControlsOverlay',
label: app.translator.trans('askvortsov-pwa.admin.pwa.other.window_controls_overlay_label'),
help: app.translator.trans('askvortsov-pwa.admin.pwa.other.window_controls_overlay_text', {
compatibilitylink: <a href="https://caniuse.com/mdn-api_windowcontrolsoverlay" tabindex="-1"/>,
learnlink: <a href="https://learn.microsoft.com/en-us/microsoft-edge/progressive-web-apps-chromium/how-to/window-controls-overlay" tabindex="-1"/>
compatibilitylink: <a href="https://caniuse.com/mdn-api_windowcontrolsoverlay" tabindex="-1" />,
learnlink: (
<a
href="https://learn.microsoft.com/en-us/microsoft-edge/progressive-web-apps-chromium/how-to/window-controls-overlay"
tabindex="-1"
/>
),
}),
type: 'bool',
})}
Expand Down
36 changes: 33 additions & 3 deletions src/FirebasePushSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
namespace Askvortsov\FlarumPWA;

use Flarum\Notification\Blueprint\BlueprintInterface;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Container\Container;
use Kreait\Firebase\Contract\Messaging;
use Kreait\Firebase\Exception\Messaging\AuthenticationError;
use Kreait\Firebase\Exception\Messaging\NotFound;
use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Firebase\Messaging\Notification;
use Psr\Log\LoggerInterface;
Expand All @@ -26,23 +29,33 @@ class FirebasePushSender

protected LoggerInterface $logger;

protected SettingsRepositoryInterface $settings;

public function __construct(
Container $container,
NotificationBuilder $notifications,
LoggerInterface $logger,
SettingsRepositoryInterface $settings,
) {
$this->container = $container;
$this->notifications = $notifications;
$this->logger = $logger;
$this->settings = $settings;
}

public function notify(BlueprintInterface $blueprint, array $userIds = []): void
{
if (! $this->hasValidFirebaseSettings()) {
return;
}

try {
// We're using the container to resolve the FirebaseMessagingContract here so we have more
// control on when and where to log the error. Having it passed on the constructor will mean
// we'll have to throw an exception and log the error for the user in the exception handler
// rather than directly in the class that consumes the contract.

/** @throws FirebaseConfigInvalid */
$messaging = $this->container->make(Messaging::class);
} catch (FirebaseConfigInvalid) {
$this->logger->error('Firebase config invalid');
Expand All @@ -51,9 +64,13 @@ public function notify(BlueprintInterface $blueprint, array $userIds = []): void
}

FirebasePushSubscription::whereIn('user_id', $userIds)->each(function (FirebasePushSubscription $subscription) use ($messaging, $blueprint) {
$messaging->send(
$this->newFirebaseCloudMessage($subscription, $blueprint)
);
try {
$messaging->send($this->newFirebaseCloudMessage($subscription, $blueprint));
} catch (AuthenticationError $e) {
$this->logger->error($e->getMessage());
} catch (NotFound) {
$subscription->delete();
}
});
}

Expand All @@ -70,4 +87,17 @@ private function newFirebaseCloudMessage(FirebasePushSubscription $subscription,
])
);
}

private function hasValidFirebaseSettings(): bool
{
$config = $this->settings->get('askvortsov-pwa.firebaseConfig');

if (! $config) {
return false;
}

json_decode($config);

return json_last_error() === JSON_ERROR_NONE;
}
}
18 changes: 2 additions & 16 deletions src/Job/SendPushNotificationsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@

namespace Askvortsov\FlarumPWA\Job;

use Askvortsov\FlarumPWA\FirebaseConfigInvalid;
use Askvortsov\FlarumPWA\FirebasePushSender;
use Askvortsov\FlarumPWA\PushSender;
use ErrorException;
use Flarum\Notification\Blueprint\BlueprintInterface;
use Flarum\Queue\AbstractJob;
use Flarum\Settings\SettingsRepositoryInterface;

class SendPushNotificationsJob extends AbstractJob
{
Expand All @@ -37,22 +35,10 @@ public function __construct(BlueprintInterface $blueprint, array $recipientIds =
/**
* @throws ErrorException
*/
public function handle(PushSender $native, FirebasePushSender $firebase, SettingsRepositoryInterface $settings): void
public function handle(PushSender $native, FirebasePushSender $firebase): void
{
$native->notify($this->blueprint, $this->recipientIds);

try {
$firebase->notify($this->blueprint, $this->recipientIds);
} catch (FirebaseConfigInvalid) {
}
}

private function hasValidFirebaseSettings(SettingsRepositoryInterface $settings): bool
{
try {
return (bool) json_encode($settings->get('askvortsov-pwa.firebaseConfig'));
} catch (\Throwable) {
return false;
}
$firebase->notify($this->blueprint, $this->recipientIds);
}
}

0 comments on commit 38d424c

Please sign in to comment.