|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\ExpoPushNotifications\Http; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Illuminate\Http\JsonResponse; |
|
7
|
|
|
use Illuminate\Routing\Controller; |
|
8
|
|
|
use Illuminate\Support\Facades\Auth; |
|
9
|
|
|
use Illuminate\Support\Facades\Validator; |
|
10
|
|
|
use NotificationChannels\ExpoPushNotifications\ExpoChannel; |
|
11
|
|
|
|
|
12
|
|
|
class ExpoController extends Controller |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var ExpoChannel |
|
16
|
|
|
*/ |
|
17
|
|
|
private $expoChannel; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* ExpoController constructor. |
|
21
|
|
|
* |
|
22
|
|
|
* @param ExpoChannel $expoChannel |
|
23
|
|
|
*/ |
|
24
|
5 |
|
public function __construct(ExpoChannel $expoChannel) |
|
25
|
|
|
{ |
|
26
|
5 |
|
$this->expoChannel = $expoChannel; |
|
27
|
5 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Handles subscription endpoint for an expo token. |
|
31
|
|
|
* |
|
32
|
|
|
* @param Request $request |
|
33
|
|
|
* |
|
34
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
35
|
|
|
*/ |
|
36
|
3 |
|
public function subscribe(Request $request) |
|
37
|
|
|
{ |
|
38
|
3 |
|
$validator = Validator::make($request->all(), [ |
|
39
|
3 |
|
'expo_token' => 'required|string', |
|
40
|
|
|
]); |
|
41
|
|
|
|
|
42
|
3 |
|
if ($validator->fails()) { |
|
43
|
1 |
|
return JsonResponse::create([ |
|
44
|
1 |
|
'status' => 'failed', |
|
45
|
|
|
'error' => [ |
|
46
|
|
|
'message' => 'Expo Token is required', |
|
47
|
|
|
], |
|
48
|
1 |
|
], 422); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
2 |
|
$token = $request->get('expo_token'); |
|
52
|
|
|
|
|
53
|
2 |
|
$interest = $this->expoChannel->interestName(Auth::user()); |
|
54
|
|
|
|
|
55
|
|
|
try { |
|
56
|
2 |
|
$this->expoChannel->expo->subscribe($interest, $token); |
|
57
|
1 |
|
} catch (\Exception $e) { |
|
58
|
1 |
|
return JsonResponse::create([ |
|
59
|
1 |
|
'status' => 'failed', |
|
60
|
|
|
'error' => [ |
|
61
|
1 |
|
'message' => $e->getMessage(), |
|
62
|
|
|
], |
|
63
|
1 |
|
], 500); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
return JsonResponse::create([ |
|
67
|
1 |
|
'status' => 'succeeded', |
|
68
|
1 |
|
'expo_token' => $token, |
|
69
|
1 |
|
], 200); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Handles removing subscription endpoint for the authenticated interest. |
|
74
|
|
|
* |
|
75
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
76
|
|
|
*/ |
|
77
|
2 |
|
public function unsubscribe() |
|
78
|
|
|
{ |
|
79
|
2 |
|
$interest = $this->expoChannel->interestName(Auth::user()); |
|
80
|
|
|
|
|
81
|
|
|
try { |
|
82
|
2 |
|
$deleted = $this->expoChannel->expo->unsubscribe($interest); |
|
83
|
1 |
|
} catch (\Exception $e) { |
|
84
|
1 |
|
return JsonResponse::create([ |
|
85
|
1 |
|
'status' => 'failed', |
|
86
|
|
|
'error' => [ |
|
87
|
1 |
|
'message' => $e->getMessage(), |
|
88
|
|
|
], |
|
89
|
1 |
|
], 500); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
return JsonResponse::create(['deleted' => $deleted]); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|