This repository has been archived by the owner on Nov 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
notifications.blade.php
307 lines (285 loc) · 15.7 KB
/
notifications.blade.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
@extends('layouts.app')
@section('title')
Paramètres
@endsection
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-3 col-xl-2 mb-3">
@include('user.settings._navigation')
</div>
<div class="col-lg-7 col-xl-8">
<form method="POST" action="{{ route('user.settings.notifications') }}" enctype='multipart/form-data' id="settings">
@method('put')
@csrf
<div class="card mb-3">
<div class="card-header">
Gestion des notifications
</div>
<div class="card-body">
<div class="row mb-3">
<div class="col-md-3">M'abonner automatiquement..</div>
<div class="col-md-9">
<div class="form-group mb-1">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="subscribe_on_create" name="subscribe_on_create" {{ old('subscribe_on_create', $user->getSetting('notifications.subscribe_on_create', true)) ? 'checked' : '' }} >
<label class="custom-control-label" for="subscribe_on_create">
Quand je crée une discussion
</label>
</div>
</div>
<div class="form-group mb-1">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="subscribe_on_reply" name="subscribe_on_reply" {{ old('subscribe_on_reply', $user->getSetting('notifications.subscribe_on_reply', false)) ? 'checked' : '' }}>
<label class="custom-control-label" for="subscribe_on_reply">
Quand je réponds à une discussion
</label>
</div>
</div>
</div>
</div>
<div class="row mb-3">
<div class="col-md-3">Je souhaite être notifié..</div>
<div class="col-md-9">
<div class="form-group mb-1">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="notification_on_new_private_message" name="notification_on_new_private_message" {{ old('notification_on_new_private_message', $user->getSetting('notifications.on_new_private_message', true)) ? 'checked' : '' }} >
<label class="custom-control-label" for="notification_on_new_private_message">
Quand je reçois un message privé
</label>
</div>
</div>
<div class="form-group mb-1">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="notification_when_mentionned_or_quoted" name="notification_when_mentionned_or_quoted" {{ old('notification_when_mentionned_or_quoted', $user->getSetting('notifications.when_mentionned_or_quoted', true)) ? 'checked' : '' }}>
<label class="custom-control-label" for="notification_when_mentionned_or_quoted">
Quand je suis cité/mentionné
</label>
</div>
</div>
<div class="form-group mb-1">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="notification_on_subscribed_discussions" name="notification_on_subscribed_discussions" {{ old('notification_on_subscribed_discussions', $user->getSetting('notifications.on_subscribed_discussions', true)) ? 'checked' : '' }} >
<label class="custom-control-label" for="notification_on_subscribed_discussions">
Quand un membre réponds dans une discussion à laquelle je me suis abonné
</label>
</div>
</div>
</div>
</div>
<div class="form-group" style="display: none;">
<div class="alert alert-primary">
<div class="row align-items-center">
<div class="col col-md-8">
<p>
Le WebPush c'est génial, so STARTUP NATION !<br>
Ça permet de reçevoir une notification sur ton téléphone (ou PC) sans même que ton navigateur soit ouvert. Un peu comme un app, mais avec 3 lettres en moins.<br>
</p>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="optin_webpush" name="optin_webpush" {{ old('optin_webpush', $user->getSetting('webpush.enabled', 0)) ? 'checked' : '' }} value="1">
<label class="custom-control-label" for="optin_webpush">
Activer les notifications WebPush (expérimental)
</label>
</div>
</div>
<div class="d-none d-md-block col-md-4 pr-1">
<a href='{{ url('/img/settings/notifications.webpush.gif') }}' data-toggle='lightbox' data-type='image'>
<img src="{{ url('/img/settings/notifications.webpush.gif') }}" class="img-fluid rounded">
</a>
</div>
</div>
</div>
</div>
<div id="webpush_settings" style="display: none;">
<div class="alert border">
<p>
Tu dois autoriser <strong>chaque appareil</strong> sur lequel tu veux recevoir des notifications WebPush :<br>
<small>Si y'a rien quand t'appuie sur "Autoriser cet appareil", y'a un souci.</small>
</p>
<a href="javascript:void(0)" class="btn btn-primary mb-1" id="enable-webpush" onclick="enableNotifications()">Autoriser cet appareil</a>
{{-- <a href="javascript:void(0)" class="btn btn-danger mb-1" id="disable-webpush">Révoquer cet appareil</a> --}}
{{-- <a href="javascript:void(0)" class="btn btn-danger mb-1" id="">Révoquer tous les appareils</a> --}}
<a href="javascript:void(0)" class="btn btn-secondary mb-1" id="test-notification" style="display: none;" onclick="testNotification()">Envoyer une notification de test</a>
</div>
@include('components.form.select', [
'name' => 'idle_wait',
'label' => 'Lorsque je ne suis pas actif·ve sur 4sucres, envoyer les notifications WebPush :',
'options' => [
0 => 'immédiatement',
1 => 'après 1 minute d\'inactivité',
2 => 'après 2 minutes d\'inactivité',
5 => 'après 5 minutes d\'inactivité',
10 => 'après 10 minutes d\'inactivité',
15 => 'après 15 minutes d\'inactivité',
20 => 'après 20 minutes d\'inactivité',
30 => 'après 30 minutes d\'inactivité',
],
'value' => $user->getSetting('webpush.idle_wait', 1),
'help_text' => 'FYI: Garder un onglet ouvert sur 4sucres.org est considéré comme une activité.'
])
</div>
</div>
<div class="card-footer">
<div class="text-right">
<button type="submit" class="btn btn-primary"><i class="fas fa-check"></i> Enregistrer</button>
</div>
</div>
</div>
</form>
<form method="POST" action="{{ route('user.settings.notifications.pushbullet') }}" enctype='multipart/form-data' id="settings">
@method('put')
@csrf
<div class="card mb-3">
<div class="card-header">
<img src="{{ url('/img/pushbullet_logo.png') }}" height="24px">
</div>
<div class="card-body">
<div class="custom-control custom-checkbox mb-3">
<input type="checkbox" class="custom-control-input" id="optin_pushbullet" name="optin_pushbullet" {{ old('optin_pushbullet', $user->getSetting('services.pushbullet.enabled', 0)) ? 'checked' : '' }} value="1">
<label class="custom-control-label" for="optin_pushbullet">
Activer l'envoi des notifications sur Pushbullet
</label>
</div>
@include('components.form.input', [
'type' => 'text',
'name' => 'email',
'label' => 'Adresse e-mail du compte Pushbullet',
'value' => $user->getSetting('services.pushbullet.email'),
'required' => true,
])
@if ($user->getSetting('services.pushbullet.enabled', 0))
<a href="{{ route('user.settings.notifications.pushbullet.test') }}" class="btn btn-primary">Test de notification</a>
@endif
</div>
<div class="card-footer">
<div class="text-right">
<button type="submit" class="btn btn-primary"><i class="fas fa-check"></i> Enregistrer</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
@endsection
@push('js')
<script>
$('#settings').change(function(){
refreshForm();
});
$(document).ready(function(){
refreshForm();
});
function refreshForm(){
if (!$('#optin_webpush').is(':checked')) {
$("#webpush_settings").slideUp('fast')
} else {
registerServiceWorker()
$("#webpush_settings").slideDown('fast')
}
}
var _registration = null;
function registerServiceWorker() {
return navigator.serviceWorker.register('/js/service-worker.js')
.then(function (registration) {
console.log('Service worker successfully registered.');
_registration = registration;
registration.update()
return registration;
})
.catch(function (err) {
console.error('Unable to register service worker.', err);
});
}
function askPermission() {
return new Promise(function (resolve, reject) {
const permissionResult = Notification.requestPermission(function (result) {
resolve(result);
});
if (permissionResult) {
permissionResult.then(resolve, reject);
}
})
.then(function (permissionResult) {
if (permissionResult !== 'granted') {
throw new Error('We weren\'t granted permission.');
} else {
subscribeUserToPush();
}
});
}
function urlBase64ToUint8Array(base64String) {
const padding = '='.repeat((4 - base64String.length % 4) % 4);
const base64 = (base64String + padding)
.replace(/\-/g, '+')
.replace(/_/g, '/');
const rawData = window.atob(base64);
const outputArray = new Uint8Array(rawData.length);
for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray;
}
function getSWRegistration() {
var promise = new Promise(function (resolve, reject) {
if (_registration != null) {
resolve(_registration);
} else {
reject(Error("It broke"));
}
});
return promise;
}
function subscribeUserToPush() {
getSWRegistration()
.then(function (registration) {
console.log(registration);
const subscribeOptions = {
userVisibleOnly: true,
applicationServerKey: urlBase64ToUint8Array(
"{{ env('VAPID_PUBLIC_KEY') }}"
)
};
return registration.pushManager.subscribe(subscribeOptions);
})
.then(function (pushSubscription) {
console.log('Received PushSubscription: ', JSON.stringify(pushSubscription));
sendSubscriptionToBackEnd(pushSubscription);
return pushSubscription;
});
}
function sendSubscriptionToBackEnd(subscription) {
return fetch('/api/v0/webpush/subscribe', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': @json(csrf_token())
},
body: JSON.stringify(subscription)
})
.then(function (response) {
if (!response.ok) {
throw new Error('Bad status code from server.');
}
$("#enable-webpush").hide();
$("#test-notification").show();
return response.json();
})
.then(function (responseData) {
if (!(responseData && responseData.success)) {
throw new Error('Bad response from server.');
}
});
}
function enableNotifications() {
askPermission();
}
function testNotification(){
_registration.showNotification('J\'suis allé à ma voiture j\'men vais !', {
body: 'Et puis quand j\'arrive sur le truc pour rentrer sur Colmar..',
icon: @json(url('/img/webpush/icon.png')),
badge: @json(url('/img/webpush/badge.png')),
});
}
</script>
@endpush