fix: ???
This commit is contained in:
parent
f8cf8620b4
commit
9b514ca75e
@ -2,5 +2,5 @@ export interface Notification {
|
|||||||
id: number,
|
id: number,
|
||||||
text: string,
|
text: string,
|
||||||
date: Date,
|
date: Date,
|
||||||
status: { username: string, status: string }[],
|
status: string[],
|
||||||
}
|
}
|
@ -1,8 +1,6 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
|
|
||||||
let username = 'user1';
|
|
||||||
|
|
||||||
//OnMount
|
//OnMount
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
console.log('Hello World!');
|
console.log('Hello World!');
|
||||||
@ -10,12 +8,16 @@
|
|||||||
//Request notification permission
|
//Request notification permission
|
||||||
if ('Notification' in window) {
|
if ('Notification' in window) {
|
||||||
alert("I'm about to request notification permissions");
|
alert("I'm about to request notification permissions");
|
||||||
Notification.requestPermission();
|
Notification.requestPermission().then((result) => {
|
||||||
|
alert('Notification permission: ' + result);
|
||||||
|
});
|
||||||
alert("Should've just seen a notification request popup");
|
alert("Should've just seen a notification request popup");
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
new Notification('Hello World!');
|
new Notification('Hello World!');
|
||||||
}, 5000);
|
}, 500);
|
||||||
// new Notification('Hello World!');
|
|
||||||
|
// const username = prompt("Please enter a unique username for this device");
|
||||||
|
|
||||||
//Service Worker
|
//Service Worker
|
||||||
if ('serviceWorker' in navigator) {
|
if ('serviceWorker' in navigator) {
|
||||||
console.log('registering service worker...');
|
console.log('registering service worker...');
|
||||||
|
@ -3,9 +3,9 @@ import type { RequestHandler } from './$types';
|
|||||||
import type { Notification } from '$lib/notif';
|
import type { Notification } from '$lib/notif';
|
||||||
|
|
||||||
let notifications: Notification[] = [
|
let notifications: Notification[] = [
|
||||||
{ id: 1, text: "This is a notification", date: new Date(), status: [{ username: "user1", status: "new" }, { username: "user2", status: "new" }] },
|
{ id: 1, text: "This is a notification", date: new Date(), status: [] },
|
||||||
{ id: 2, text: "This is another notification", date: new Date(), status: [{ username: "user1", status: "new" }, { username: "user2", status: "new" }] },
|
{ id: 2, text: "This is another notification", date: new Date(), status: [] },
|
||||||
{ id: 3, text: "This is a third notification", date: new Date(), status: [{ username: "user1", status: "new" }, { username: "user2", status: "new" }] },
|
{ id: 3, text: "This is a third notification", date: new Date(), status: [] },
|
||||||
]
|
]
|
||||||
|
|
||||||
// GET handler
|
// GET handler
|
||||||
@ -14,22 +14,26 @@ let notifications: Notification[] = [
|
|||||||
|
|
||||||
export const GET: RequestHandler = ({ url }) => {
|
export const GET: RequestHandler = ({ url }) => {
|
||||||
const { searchParams } = new URL(url);
|
const { searchParams } = new URL(url);
|
||||||
const username = searchParams.get('username');
|
const user_id = searchParams.get('id')??"1";
|
||||||
if (!username) {
|
const date = searchParams.get('date')??"";
|
||||||
return error(400, 'username is required');
|
if (!user_id) {
|
||||||
|
//return error(400, 'username is required');
|
||||||
}
|
}
|
||||||
|
if(!date){
|
||||||
|
//return error(400, 'date is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Notifications that do not have a status entry for the current user and are newer than the date
|
||||||
const user_notifications = notifications.filter(notification => {
|
const user_notifications = notifications.filter(notification => {
|
||||||
// return true;
|
return notification.status.find(status => status == user_id) == undefined && notification.date > new Date(date);
|
||||||
const user_status = notification.status.find(status => status.username === username);
|
|
||||||
if (user_status?.status === "new") {
|
|
||||||
console.log("found one!")
|
|
||||||
user_status.status = "old";
|
|
||||||
//@ts-ignore
|
|
||||||
// notifications.find(n => notification.id === n.id).status.find(status => status.username === username).status = "old";
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//Add the current user to the status list for each notification
|
||||||
|
user_notifications.forEach(notification => {
|
||||||
|
notification.status.push(user_id);
|
||||||
|
});
|
||||||
|
|
||||||
return new Response(JSON.stringify(user_notifications), { headers: { 'content-type': 'application/json' } });
|
return new Response(JSON.stringify(user_notifications), { headers: { 'content-type': 'application/json' } });
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -43,7 +47,7 @@ export const POST: RequestHandler = async ({ request }) => {
|
|||||||
|
|
||||||
notification.id = notifications.length + 1;
|
notification.id = notifications.length + 1;
|
||||||
notification.date = new Date();
|
notification.date = new Date();
|
||||||
notification.status = [{ username: "user1", status: "new" }, { username: "user2", status: "new" }];
|
notification.status = [];
|
||||||
|
|
||||||
notifications = [...notifications, notification];
|
notifications = [...notifications, notification];
|
||||||
|
|
||||||
|
12
src/sw.js
12
src/sw.js
@ -10,11 +10,18 @@
|
|||||||
// );
|
// );
|
||||||
// });
|
// });
|
||||||
|
|
||||||
|
let user_id = 0;
|
||||||
|
let date = undefined;
|
||||||
|
|
||||||
|
|
||||||
self.addEventListener('activate', event => {
|
self.addEventListener('activate', event => {
|
||||||
// alert("Service worker activated");
|
// alert("Service worker activated");
|
||||||
console.log('Service worker activating...');
|
console.log('Service worker activating...');
|
||||||
event.waitUntil(self.clients.claim());
|
event.waitUntil(self.clients.claim());
|
||||||
console.log('Service worker activated');
|
console.log('Service worker activated');
|
||||||
|
user_id = Math.random();
|
||||||
|
console.log("Setting user id to " + user_id);
|
||||||
|
date = new Date();
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
notif();
|
notif();
|
||||||
}, 5000);
|
}, 5000);
|
||||||
@ -32,7 +39,7 @@ self.addEventListener('activate', event => {
|
|||||||
// Send notification after 5 seconds
|
// Send notification after 5 seconds
|
||||||
function notif() {
|
function notif() {
|
||||||
console.log('Checking notifications...');
|
console.log('Checking notifications...');
|
||||||
fetch("/notif?username=user1").then((res) => {
|
fetch("/notif?id=" + user_id + "&date=" + date.toUTCString()).then((res) => {
|
||||||
res.json().then((data) => {
|
res.json().then((data) => {
|
||||||
if (data.length) {
|
if (data.length) {
|
||||||
for (const d of data) {
|
for (const d of data) {
|
||||||
@ -54,6 +61,3 @@ function notif() {
|
|||||||
notif();
|
notif();
|
||||||
}, 5000);
|
}, 5000);
|
||||||
}
|
}
|
||||||
setTimeout(() => {
|
|
||||||
notif();
|
|
||||||
}, 5000);
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user