diff options
| author | Fuwn <[email protected]> | 2024-07-24 21:24:09 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-07-24 21:24:09 -0700 |
| commit | 8a94bbfba322f8011017980e4362d46c4d51bb55 (patch) | |
| tree | 4d861cd7f1d9304e6297e1b52fe759674fd5df1a /src/lib/Database | |
| parent | feat(layout): browser notifications (diff) | |
| download | due.moe-8a94bbfba322f8011017980e4362d46c4d51bb55.tar.xz due.moe-8a94bbfba322f8011017980e4362d46c4d51bb55.zip | |
feat: background notifications
Diffstat (limited to 'src/lib/Database')
| -rw-r--r-- | src/lib/Database/user.ts | 23 | ||||
| -rw-r--r-- | src/lib/Database/userNotifications.ts | 29 |
2 files changed, 52 insertions, 0 deletions
diff --git a/src/lib/Database/user.ts b/src/lib/Database/user.ts new file mode 100644 index 00000000..df68bc91 --- /dev/null +++ b/src/lib/Database/user.ts @@ -0,0 +1,23 @@ +import { type AniListAuthorisation } from '$lib/Data/AniList/identity'; +import Dexie, { type Table } from 'dexie'; + +export interface User { + id: number; + user: AniListAuthorisation; + lastNotificationID: number | null; +} + +export class UserDatabase extends Dexie { + users: Table<User>; + + constructor() { + super('users'); + this.version(1).stores({ + users: 'id, user, lastNotificationID' + }); + + this.users = this.table('users'); + } +} + +export const database = new UserDatabase(); diff --git a/src/lib/Database/userNotifications.ts b/src/lib/Database/userNotifications.ts new file mode 100644 index 00000000..c61c2aa7 --- /dev/null +++ b/src/lib/Database/userNotifications.ts @@ -0,0 +1,29 @@ +import supabase from './supabase'; + +export interface UserNotifications { + created_at: string; + updated_at: string; + user_id: number; + subscription: JSON; +} + +export const getUserSubscription = async (userId: number) => + await supabase.from('user_notifications').select('*').eq('user_id', userId); + +export const getUserSubscriptions = async () => { + const { data, error } = await supabase.from('user_notifications').select('*'); + + if (error) return []; + + return data as UserNotifications[]; +}; + +export const setUserSubscription = async (userId: number, subscription: JSON) => + await supabase.from('user_notifications').upsert( + { + user_id: userId, + updated_at: new Date().toISOString(), + subscription: subscription + }, + { onConflict: 'user_id' } + ); |