main
Ashley Flanagan 10 months ago
parent f8cf8620b4
commit 9b514ca75e

@ -2,5 +2,5 @@ export interface Notification {
id: number,
text: string,
date: Date,
status: { username: string, status: string }[],
status: string[],
}

@ -1,8 +1,6 @@
<script lang="ts">
import { onMount } from 'svelte';
let username = 'user1';
//OnMount
onMount(() => {
console.log('Hello World!');
@ -10,12 +8,16 @@
//Request notification permission
if ('Notification' in window) {
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");
setTimeout(() => {
new Notification('Hello World!');
}, 5000);
// new Notification('Hello World!');
}, 500);
// const username = prompt("Please enter a unique username for this device");
//Service Worker
if ('serviceWorker' in navigator) {
console.log('registering service worker...');

@ -3,9 +3,9 @@ import type { RequestHandler } from './$types';
import type { Notification } from '$lib/notif';
let notifications: Notification[] = [
{ id: 1, text: "This is a notification", date: new Date(), status: [{ username: "user1", status: "new" }, { username: "user2", status: "new" }] },
{ id: 2, text: "This is another 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: [{ 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: [] },
{ id: 3, text: "This is a third notification", date: new Date(), status: [] },
]
// GET handler
@ -14,22 +14,26 @@ let notifications: Notification[] = [
export const GET: RequestHandler = ({ url }) => {
const { searchParams } = new URL(url);
const username = searchParams.get('username');
if (!username) {
return error(400, 'username is required');
const user_id = searchParams.get('id')??"1";
const date = searchParams.get('date')??"";
if (!user_id) {
//return error(400, 'username is required');
}
const user_notifications = notifications.filter(notification => {
// return true;
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;
if(!date){
//return error(400, 'date is required');
}
return false;
//Notifications that do not have a status entry for the current user and are newer than the date
const user_notifications = notifications.filter(notification => {
return notification.status.find(status => status == user_id) == undefined && notification.date > new Date(date);
});
//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' } });
};
@ -43,7 +47,7 @@ export const POST: RequestHandler = async ({ request }) => {
notification.id = notifications.length + 1;
notification.date = new Date();
notification.status = [{ username: "user1", status: "new" }, { username: "user2", status: "new" }];
notification.status = [];
notifications = [...notifications, notification];

@ -10,11 +10,18 @@
// );
// });
let user_id = 0;
let date = undefined;
self.addEventListener('activate', event => {
// alert("Service worker activated");
console.log('Service worker activating...');
event.waitUntil(self.clients.claim());
console.log('Service worker activated');
user_id = Math.random();
console.log("Setting user id to " + user_id);
date = new Date();
setTimeout(() => {
notif();
}, 5000);
@ -32,7 +39,7 @@ self.addEventListener('activate', event => {
// Send notification after 5 seconds
function notif() {
console.log('Checking notifications...');
fetch("/notif?username=user1").then((res) => {
fetch("/notif?id=" + user_id + "&date=" + date.toUTCString()).then((res) => {
res.json().then((data) => {
if (data.length) {
for (const d of data) {
@ -54,6 +61,3 @@ function notif() {
notif();
}, 5000);
}
setTimeout(() => {
notif();
}, 5000);

Loading…
Cancel
Save