From e1af2369964e0e5eed8fed9d86a89b8547559251 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Mon, 8 Jul 2024 19:11:47 -0700 Subject: feat(pwa): anilist notifications --- src/lib/Locale/english.ts | 3 ++- src/lib/Locale/japanese.ts | 3 ++- src/lib/Locale/layout.ts | 1 + src/lib/Settings/Categories/Display.svelte | 15 +++++++++++ src/routes/+layout.svelte | 42 ++++++++++++++++++++++++++++-- src/service-worker.ts | 17 ++++++++++++ src/stores/settings.ts | 2 ++ 7 files changed, 79 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/lib/Locale/english.ts b/src/lib/Locale/english.ts index 509ebca0..2b9d1c3e 100644 --- a/src/lib/Locale/english.ts +++ b/src/lib/Locale/english.ts @@ -75,7 +75,8 @@ const English: Locale = { title: 'Motion & Accessibility', fields: { disablePageTransitionAnimations: 'Disable page transition animations', - disableNotifications: 'Disable notifications', + disableNotifications: 'Disable verbose site notifications', + disableAniListNotifications: 'Disable AniList notifications', limitPanelAreaToScreenHeight: 'Limit panel area to screen height', interfaceLanguage: 'Interface language' } diff --git a/src/lib/Locale/japanese.ts b/src/lib/Locale/japanese.ts index 089946ef..cc094e07 100644 --- a/src/lib/Locale/japanese.ts +++ b/src/lib/Locale/japanese.ts @@ -76,7 +76,8 @@ const Japanese: Locale = { title: 'モーションとアクセシビリティ', fields: { disablePageTransitionAnimations: 'ページ遷移アニメーションを無効にする', - disableNotifications: '通知を無効にする', + disableNotifications: '詳細なサイト通知を無効にする', + disableAniListNotifications: 'AniListの通知を無効にする', limitPanelAreaToScreenHeight: 'メディアパネルの面積をスクリーンの高さに制限する', interfaceLanguage: 'インターフェース言語' } diff --git a/src/lib/Locale/layout.ts b/src/lib/Locale/layout.ts index 4fea935a..081926fd 100644 --- a/src/lib/Locale/layout.ts +++ b/src/lib/Locale/layout.ts @@ -78,6 +78,7 @@ export interface Locale { fields: { disablePageTransitionAnimations: LocaleValue; disableNotifications: LocaleValue; + disableAniListNotifications: LocaleValue; limitPanelAreaToScreenHeight: LocaleValue; interfaceLanguage: LocaleValue; }; diff --git a/src/lib/Settings/Categories/Display.svelte b/src/lib/Settings/Categories/Display.svelte index 0b23904b..76337b58 100644 --- a/src/lib/Settings/Categories/Display.svelte +++ b/src/lib/Settings/Categories/Display.svelte @@ -185,6 +185,21 @@

+ + + Periodically check and display recent AniList notifications as a native platform notification +
+ This may be useful for users who have installed due.moe as a PWA or on mobile devices, since AniList + has no official mobile app. +
+
+ +

+ {$locale().settings.display.categories.dateAndTime.title}
{ + try { + const recentNotifications = await notifications(data.user.accessToken); + + if ( + recentNotifications && + recentNotifications.length > 0 && + recentNotifications[0].id > lastNotificationId && + new Date(recentNotifications[0].createdAt * 1000).getTime() + 30000 > + new Date().getTime() + ) { + lastNotificationId = recentNotifications[0].id; + + if ('serviceWorker' in navigator && navigator.serviceWorker.controller) { + navigator.serviceWorker.ready.then((registration) => { + registration.showNotification('due.moe', { + body: `${recentNotifications[0].user.name}${recentNotifications[0].context}`, + icon: recentNotifications[0].user.avatar.large || '/favicon-196x196.png' + }); + }); + } + } + } catch (error) { + console.error(error); + } + }, 15000); + } + } }); onDestroy(() => { if (browser) window.removeEventListener('scroll', handleScroll); + + if (notificationInterval) clearInterval(notificationInterval); }); $: { @@ -229,7 +267,7 @@

- + {#if $userIdentity.id !== -1} diff --git a/src/service-worker.ts b/src/service-worker.ts index b1dcc3b3..7d2310f6 100644 --- a/src/service-worker.ts +++ b/src/service-worker.ts @@ -89,3 +89,20 @@ self.addEventListener('fetch', (event) => { event.respondWith(respond()); }); + +self.addEventListener('push', (e: Event) => { + if (!(self.Notification && self.Notification.permission === 'granted')) { + return; + } + + const event = e as PushEvent; + + if (event.data) { + event.waitUntil( + (self as unknown as ServiceWorkerGlobalScope).registration.showNotification('due.moe', { + body: event.data.json().url, + icon: '/favicon-196x196.png' + }) + ); + } +}); diff --git a/src/stores/settings.ts b/src/stores/settings.ts index 7dfd6e4a..37558cba 100644 --- a/src/stores/settings.ts +++ b/src/stores/settings.ts @@ -60,6 +60,7 @@ export interface Settings { displayBlurAdultContent: boolean; displayCopyMediaTitleNotLink: boolean; displayTotalDueEpisodes: boolean; + displayAniListNotifications: boolean; } const defaultSettings: Settings = { @@ -98,6 +99,7 @@ const defaultSettings: Settings = { displayBlurAdultContent: true, displayCopyMediaTitleNotLink: false, displayTotalDueEpisodes: false, + displayAniListNotifications: false, // Calculation calculateChaptersRoundedDown: true, -- cgit v1.2.3