ソースを参照

Fix push token for php8.2

Daniel Supernault 9 ヶ月 前
コミット
78d2783db8

+ 3 - 3
app/Http/Controllers/Api/ApiV1Dot1Controller.php

@@ -19,6 +19,7 @@ use App\Media;
 use App\Place;
 use App\Profile;
 use App\Report;
+use App\Rules\ExpoPushTokenRule;
 use App\Services\AccountService;
 use App\Services\BouncerService;
 use App\Services\EmailService;
@@ -48,7 +49,6 @@ use Jenssegers\Agent\Agent;
 use League\Fractal;
 use League\Fractal\Serializer\ArraySerializer;
 use Mail;
-use NotificationChannels\Expo\ExpoPushToken;
 
 class ApiV1Dot1Controller extends Controller
 {
@@ -1087,7 +1087,7 @@ class ApiV1Dot1Controller extends Controller
         abort_if($request->user()->status, 422, 'Cannot access this resource at this time');
 
         $this->validate($request, [
-            'expo_token' => ['required', ExpoPushToken::rule()],
+            'expo_token' => ['required', 'string', new ExpoPushTokenRule],
         ]);
 
         $user = $request->user();
@@ -1127,7 +1127,7 @@ class ApiV1Dot1Controller extends Controller
 
         $this->validate($request, [
             'notify_enabled' => 'required',
-            'token' => ['required', ExpoPushToken::rule()],
+            'token' => ['required', 'string', new ExpoPushTokenRule],
             'notify_like' => 'sometimes',
             'notify_follow' => 'sometimes',
             'notify_mention' => 'sometimes',

+ 33 - 0
app/Rules/ExpoPushTokenRule.php

@@ -0,0 +1,33 @@
+<?php
+
+namespace App\Rules;
+
+use Closure;
+use Illuminate\Contracts\Validation\ValidationRule;
+
+class ExpoPushTokenRule implements ValidationRule
+{
+    /**
+     * Run the validation rule.
+     *
+     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
+     */
+    public function validate(string $attribute, mixed $value, Closure $fail): void
+    {
+        if (! $value || empty($value)) {
+            $fail('The :attribute must not be empty.');
+        }
+
+        if (str_starts_with($value, 'ExponentPushToken[') && mb_strlen($value) < 26) {
+            $fail('The :attribute is not a valid push token.');
+        }
+
+        if (! str_starts_with($value, 'ExponentPushToken[') && ! str_starts_with($value, 'ExpoPushToken[')) {
+            $fail('The :attribute is not a valid push token.');
+        }
+
+        if (! str_ends_with($value, ']')) {
+            $fail('The :attribute is not a valid push token.');
+        }
+    }
+}