1
0
Fork 1
mirror of https://github.com/pixelfed/pixelfed.git synced 2024-12-22 15:55:14 +00:00
pixelfed/app/Services/NotificationAppGatewayService.php

126 lines
3.2 KiB
PHP
Raw Normal View History

2024-09-18 08:29:38 +00:00
<?php
namespace App\Services;
2024-09-28 09:52:58 +00:00
use Cache;
use Exception;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Http;
2024-09-18 08:29:38 +00:00
class NotificationAppGatewayService
{
2024-09-28 09:52:58 +00:00
const GATEWAY_SUPPORT_CHECK = 'px:nags:gateway-support-check';
2024-09-18 08:29:38 +00:00
public static function config()
{
return config('instance.notifications.nag');
}
2024-09-28 09:52:58 +00:00
public static function enabled()
{
if ((bool) config('instance.notifications.nag.enabled') === false) {
return false;
}
$apiKey = config('instance.notifications.nag.api_key');
if (! $apiKey || empty($apiKey) || strlen($apiKey) !== 45) {
return false;
}
return Cache::remember(self::GATEWAY_SUPPORT_CHECK, 43200, function () {
return self::checkServerSupport();
});
}
public static function checkServerSupport()
{
$endpoint = 'https://'.config('instance.notifications.nag.endpoint').'/api/v1/instance-check?domain='.config('pixelfed.domain.app');
try {
$res = Http::withHeaders(['X-PIXELFED-API' => 1])
->retry(3, 500)
->throw()
->get($endpoint);
$data = $res->json();
} catch (RequestException $e) {
return false;
} catch (Exception $e) {
return false;
}
if ($res->successful() && isset($data['active']) && $data['active'] === true) {
return true;
}
return false;
}
public static function forceSupportRecheck()
{
Cache::forget(self::GATEWAY_SUPPORT_CHECK);
return self::enabled();
}
public static function isValidExpoPushToken($token)
{
if (! $token || empty($token)) {
return false;
}
2024-09-30 10:02:33 +00:00
if (str_starts_with($token, 'ExponentPushToken[') && mb_strlen($token) < 26) {
2024-09-28 09:52:58 +00:00
return false;
}
2024-09-30 10:02:33 +00:00
if (! str_starts_with($token, 'ExponentPushToken[') && ! str_starts_with($token, 'ExpoPushToken[')) {
2024-09-28 09:52:58 +00:00
return false;
}
2024-09-30 10:02:33 +00:00
if (! str_ends_with($token, ']')) {
2024-09-28 09:52:58 +00:00
return false;
}
return true;
}
public static function send($userToken, $type, $actor = '')
{
if (! self::enabled()) {
return false;
}
if (! $userToken || empty($userToken) || ! self::isValidExpoPushToken($userToken)) {
return false;
}
2024-09-30 10:02:33 +00:00
$types = PushNotificationService::NOTIFY_TYPES;
2024-09-28 09:52:58 +00:00
if (! $type || empty($type) || ! in_array($type, $types)) {
return false;
}
$apiKey = config('instance.notifications.nag.api_key');
if (! $apiKey || empty($apiKey)) {
return false;
}
$url = 'https://'.config('instance.notifications.nag.endpoint').'/api/v1/relay/deliver';
try {
$response = Http::withToken($apiKey)
->withHeaders(['X-PIXELFED-API' => 1])
->post($url, [
'token' => $userToken,
'type' => $type,
'actor' => $actor,
]);
$response->throw();
} catch (RequestException $e) {
return;
} catch (Exception $e) {
return;
}
}
2024-09-18 08:29:38 +00:00
}