From 2d9235070856c0a5032ddf47f7b1dc7cc5cceb60 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Thu, 25 Jul 2024 00:19:44 -0700 Subject: refactor(Database): separate providers --- src/lib/Database/IndexedDB/activities.ts | 22 ++++ src/lib/Database/IndexedDB/chapters.ts | 22 ++++ src/lib/Database/IndexedDB/user.ts | 23 ++++ src/lib/Database/Supabase/badges.ts | 49 ++++++++ src/lib/Database/Supabase/events.ts | 46 ++++++++ src/lib/Database/Supabase/groups.ts | 32 +++++ src/lib/Database/Supabase/userBadges.ts | 106 +++++++++++++++++ src/lib/Database/Supabase/userConfiguration.ts | 50 ++++++++ src/lib/Database/Supabase/userNotifications.ts | 32 +++++ src/lib/Database/Supabase/userPreferences.ts | 156 +++++++++++++++++++++++++ src/lib/Database/activities.ts | 22 ---- src/lib/Database/badges.ts | 49 -------- src/lib/Database/chapters.ts | 22 ---- src/lib/Database/events.ts | 46 -------- src/lib/Database/groups.ts | 32 ----- src/lib/Database/user.ts | 23 ---- src/lib/Database/userBadges.ts | 106 ----------------- src/lib/Database/userConfiguration.ts | 50 -------- src/lib/Database/userNotifications.ts | 32 ----- src/lib/Database/userPreferences.ts | 156 ------------------------- 20 files changed, 538 insertions(+), 538 deletions(-) create mode 100644 src/lib/Database/IndexedDB/activities.ts create mode 100644 src/lib/Database/IndexedDB/chapters.ts create mode 100644 src/lib/Database/IndexedDB/user.ts create mode 100644 src/lib/Database/Supabase/badges.ts create mode 100644 src/lib/Database/Supabase/events.ts create mode 100644 src/lib/Database/Supabase/groups.ts create mode 100644 src/lib/Database/Supabase/userBadges.ts create mode 100644 src/lib/Database/Supabase/userConfiguration.ts create mode 100644 src/lib/Database/Supabase/userNotifications.ts create mode 100644 src/lib/Database/Supabase/userPreferences.ts delete mode 100644 src/lib/Database/activities.ts delete mode 100644 src/lib/Database/badges.ts delete mode 100644 src/lib/Database/chapters.ts delete mode 100644 src/lib/Database/events.ts delete mode 100644 src/lib/Database/groups.ts delete mode 100644 src/lib/Database/user.ts delete mode 100644 src/lib/Database/userBadges.ts delete mode 100644 src/lib/Database/userConfiguration.ts delete mode 100644 src/lib/Database/userNotifications.ts delete mode 100644 src/lib/Database/userPreferences.ts (limited to 'src/lib/Database') diff --git a/src/lib/Database/IndexedDB/activities.ts b/src/lib/Database/IndexedDB/activities.ts new file mode 100644 index 00000000..6a505ff3 --- /dev/null +++ b/src/lib/Database/IndexedDB/activities.ts @@ -0,0 +1,22 @@ +import type { ActivitiesPage } from '$lib/Data/AniList/activity'; +import Dexie, { type Table } from 'dexie'; + +export interface ActivityPage { + page: number; + data: ActivitiesPage; +} + +export class ActivityDatabase extends Dexie { + activities: Table; + + constructor() { + super('activities'); + this.version(1).stores({ + activities: 'page, data' + }); + + this.activities = this.table('activities'); + } +} + +export const database = new ActivityDatabase(); diff --git a/src/lib/Database/IndexedDB/chapters.ts b/src/lib/Database/IndexedDB/chapters.ts new file mode 100644 index 00000000..0f77f0a0 --- /dev/null +++ b/src/lib/Database/IndexedDB/chapters.ts @@ -0,0 +1,22 @@ +import Dexie, { type Table } from 'dexie'; + +export interface Chapter { + id: number; + chapters: number | null; + volumes: number | null; +} + +export class ChapterDatabase extends Dexie { + chapters: Table; + + constructor() { + super('chapters'); + this.version(1).stores({ + chapters: 'id, chapters, volumes' + }); + + this.chapters = this.table('chapters'); + } +} + +export const database = new ChapterDatabase(); diff --git a/src/lib/Database/IndexedDB/user.ts b/src/lib/Database/IndexedDB/user.ts new file mode 100644 index 00000000..e7285a07 --- /dev/null +++ b/src/lib/Database/IndexedDB/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; + + 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/Supabase/badges.ts b/src/lib/Database/Supabase/badges.ts new file mode 100644 index 00000000..022d4d14 --- /dev/null +++ b/src/lib/Database/Supabase/badges.ts @@ -0,0 +1,49 @@ +import supabase from '../supabase'; + +interface Badge { + id: number; + created_at: string; + updated_at: string; + image_url: string; + image_artist: string; + description: string; + event: number; + group: number; +} + +interface NewBadge { + updated_at?: string; + image_url: string; + image_artist: string; + description: string; +} + +interface GetBy { + event?: number; + group?: number; +} + +export const getBadges = async (getBy?: GetBy) => { + let data, error; + + if (getBy?.event) + [data, error] = await supabase.from('badges').select('*').eq('event', getBy.event); + else if (getBy?.group) + [data, error] = await supabase.from('badges').select('*').eq('group', getBy.group); + else [data, error] = await supabase.from('badges').select('*'); + + if (error) return []; + + return data as Badge[]; +}; + +export const createBadge = async (badge: NewBadge) => await supabase.from('badges').insert(badge); + +export const deleteBadge = async (id: number) => + await supabase.from('badges').delete().eq('id', id); + +export const updateBadge = async (id: number, badge: NewBadge) => { + if (!badge.updated_at) badge.updated_at = new Date().toISOString(); + + return await supabase.from('badges').update(badge).eq('id', id); +}; diff --git a/src/lib/Database/Supabase/events.ts b/src/lib/Database/Supabase/events.ts new file mode 100644 index 00000000..e87fe011 --- /dev/null +++ b/src/lib/Database/Supabase/events.ts @@ -0,0 +1,46 @@ +import type Group from '$lib/Events/Group.svelte'; +import supabase from '../supabase'; + +export interface Event { + id: number; + created_at: string; + updated_at: string; + title: string; + description: string; + group: Group; + banner: string; + anilist_url: string; +} + +interface NewEvent { + updated_at?: string; + title: string; + description: string; +} + +export const getEvents = async () => { + const { data, error } = await supabase.from('events').select('*, group:groups(*)'); + + if (error) return []; + + return data as Event[]; +}; + +export const getGroupEvents = async (group: string) => { + const { data, error } = await supabase.from('events').select('*').eq('group', group); + + if (error) return []; + + return data as Event[]; +}; + +export const createEvent = async (event: NewEvent) => await supabase.from('events').insert(event); + +export const deleteEvent = async (id: number) => + await supabase.from('events').delete().eq('id', id); + +export const updateEvent = async (id: number, event: NewEvent) => { + if (!event.updated_at) event.updated_at = new Date().toISOString(); + + return await supabase.from('events').update(event).eq('id', id); +}; diff --git a/src/lib/Database/Supabase/groups.ts b/src/lib/Database/Supabase/groups.ts new file mode 100644 index 00000000..b2770e4a --- /dev/null +++ b/src/lib/Database/Supabase/groups.ts @@ -0,0 +1,32 @@ +import supabase from '../supabase'; + +export interface Group { + id: number; + created_at: string; + updated_at?: string; + members: number[]; + avatar: string; + banner: string; + description?: string; + name: string; + anilist_id: number; + anilist_username: string; + badge?: string; + badge_description?: string; +} + +export const getGroups = async () => { + const { data, error } = await supabase.from('groups').select('*'); + + if (error) return []; + + return data as Group[]; +}; + +export const getGroup = async (slug: string) => { + const { data, error } = await supabase.from('groups').select('*').eq('anilist_username', slug); + + if (error || data.length === 0) return null; + + return data[0] as Group; +}; diff --git a/src/lib/Database/Supabase/userBadges.ts b/src/lib/Database/Supabase/userBadges.ts new file mode 100644 index 00000000..eef8490c --- /dev/null +++ b/src/lib/Database/Supabase/userBadges.ts @@ -0,0 +1,106 @@ +import { databaseTimeToDate } from '$lib/Utility/time'; +import supabase from '../supabase'; + +export interface Badge { + post?: string; + image?: string; + description?: string | null; + id?: number; + time?: string; + category?: string | null; + hidden?: boolean; + source?: string | null; + designer?: string | null; + shadow_hidden?: boolean; + click_count?: number; +} + +export const getUserBadges = async (userId: number): Promise => { + const { data, error } = await supabase.from('user_badges').select('*').eq('user_id', userId); + + if (error) return []; + + return data.sort((a, b) => + databaseTimeToDate((a as Badge).time ?? '').getTime() > + databaseTimeToDate((b as Badge).time ?? '').getTime() + ? -1 + : 1 + ) as Badge[]; +}; + +export const addUserBadge = async (userId: number, badge: Badge) => { + const { post, image, description, time, category, hidden, source, designer } = badge; + + if (post === undefined || image === undefined) return; + + if (time) { + await supabase.from('user_badges').insert({ + user_id: userId, + post, + image, + description, + time, + category, + hidden, + source, + designer + }); + } else { + await supabase + .from('user_badges') + .insert({ user_id: userId, post, image, description, category, hidden, source, designer }); + } +}; + +export const removeUserBadge = async (userId: number, id: number) => { + if (!isNaN(id)) await supabase.from('user_badges').delete().eq('id', id).eq('user_id', userId); +}; + +export const updateUserBadge = async (userId: number, id: number, badge: Badge) => { + if (badge.post === undefined || badge.image === undefined) return; + + await supabase + .from('user_badges') + .update({ + post: badge.post, + image: badge.image, + description: badge.description, + category: badge.category, + time: badge.time, + hidden: badge.hidden, + source: badge.source, + designer: badge.designer + }) + .eq('id', id) + .eq('user_id', userId); +}; + +export const renameCategory = async (userId: number, oldName: string, newName: string) => + await supabase + .from('user_badges') + .update({ category: newName }) + .eq('category', oldName) + .eq('user_id', userId); + +export const removeAllUserBadges = async (userId: number) => + await supabase.from('user_badges').delete().eq('user_id', userId); + +export const migrateCategory = async (userId: number, oldName: string, newName: string) => + await supabase + .from('user_badges') + .update({ category: newName }) + .eq('category', oldName) + .eq('user_id', userId); + +export const setShadowHidden = async (userId: number, shadowHide: boolean) => + await supabase.from('user_badges').update({ shadow_hidden: shadowHide }).eq('user_id', userId); + +export const setShadowHiddenBadge = async (userId: number, id: number, shadowHide: boolean) => + await supabase + .from('user_badges') + .update({ shadow_hidden: shadowHide }) + .eq('id', id) + .eq('user_id', userId); + +export const incrementClickCount = async (id: number) => + await supabase.rpc('user_badges_increment_click_count', { user_badge_id: id }); diff --git a/src/lib/Database/Supabase/userConfiguration.ts b/src/lib/Database/Supabase/userConfiguration.ts new file mode 100644 index 00000000..cb2e4060 --- /dev/null +++ b/src/lib/Database/Supabase/userConfiguration.ts @@ -0,0 +1,50 @@ +import supabase from '../supabase'; + +interface UserConfiguration { + user_id: number; + configuration: object; + created_at: string; + updated_at: string; +} + +interface NewUserConfiguration { + configuration: object; + updated_at?: string; +} + +export const getUserConfiguration = async (userId: number) => { + const { data, error } = await supabase + .from('user_configuration') + .select('*') + .eq('user_id', userId); + + if (error || data.length === 0 || data[0].user_id !== userId) return null; + + return data[0] as UserConfiguration; +}; + +export const setUserConfiguration = async (userId: number, configuration: NewUserConfiguration) => { + const { data, error } = await supabase + .from('user_configuration') + .upsert( + { + user_id: userId, + configuration: configuration.configuration, + updated_at: configuration.updated_at || new Date().toISOString() + }, + { onConflict: 'user_id' } + ) + .select(); + + if (error || !data || (data as []).length === 0) return null; + + return data[0].configuration as UserConfiguration; +}; + +export const deleteUserConfiguration = async (userId: number) => { + const { data, error } = await supabase.from('user_configuration').delete().eq('user_id', userId); + + if (error || !data) return null; + + return data; +}; diff --git a/src/lib/Database/Supabase/userNotifications.ts b/src/lib/Database/Supabase/userNotifications.ts new file mode 100644 index 00000000..48cd73c9 --- /dev/null +++ b/src/lib/Database/Supabase/userNotifications.ts @@ -0,0 +1,32 @@ +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 deleteUserSubscription = async (userId: number) => + await supabase.from('user_notifications').delete().eq('user_id', userId); + +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' } + ); diff --git a/src/lib/Database/Supabase/userPreferences.ts b/src/lib/Database/Supabase/userPreferences.ts new file mode 100644 index 00000000..a717953f --- /dev/null +++ b/src/lib/Database/Supabase/userPreferences.ts @@ -0,0 +1,156 @@ +import supabase from '../supabase'; + +export interface UserPreferences { + created_at: string; + updated_at: string; + user_id: number; + pinned_hololive_streams: string[]; + hide_missing_badges: boolean; + biography: string | null; + badge_wall_css: string; + hide_awc_badges: boolean; + pinned_badge_wall_categories: string[]; +} + +interface NewUserPreferences { + updated_at?: string; + pinned_hololive_streams?: string[]; + hide_missing_badges?: boolean; + badge_wall_css?: string; + biography?: string; + hide_awc_badges?: boolean; + pinned_badge_wall_categories?: string[]; +} + +export const getUserPreferences = async (userId: number) => { + const { data, error } = await supabase.from('user_preferences').select('*').eq('user_id', userId); + + if (error || data.length === 0 || data[0].user_id !== userId) return null; + + return data[0] as UserPreferences; +}; + +export const setUserPreferences = async (userId: number, preferences: NewUserPreferences) => { + const userPreferences = await getUserPreferences(userId); + const { data, error } = await supabase + .from('user_preferences') + .upsert( + { + user_id: userId, + updated_at: preferences.updated_at || new Date().toISOString(), + pinned_hololive_streams: + preferences.pinned_hololive_streams || + (userPreferences ? userPreferences.pinned_hololive_streams : []), + hide_missing_badges: preferences.hide_missing_badges || false, + biography: preferences.biography || (userPreferences ? userPreferences.biography : null), + badge_wall_css: + preferences.badge_wall_css || (userPreferences ? userPreferences.badge_wall_css : ''), + hide_awc_badges: preferences.hide_awc_badges || false, + pinned_badge_wall_categories: + preferences.pinned_badge_wall_categories || + (userPreferences ? userPreferences.pinned_badge_wall_categories : '') + }, + { onConflict: 'user_id' } + ) + .select(); + + if (error || !data || (data as []).length === 0) return null; + + return data[0] as UserPreferences; +}; + +export const toggleHololiveStreamPinning = async (userId: number, streamId: string) => { + const userPreferences = await getUserPreferences(userId); + + if (!userPreferences) return null; + + const pinnedStreams = userPreferences.pinned_hololive_streams; + const index = pinnedStreams.indexOf(streamId); + + if (index === -1) pinnedStreams.push(streamId); + else pinnedStreams.splice(index, 1); + + return await setUserPreferences(userId, { + updated_at: new Date().toISOString(), + pinned_hololive_streams: pinnedStreams, + hide_missing_badges: userPreferences.hide_missing_badges + }); +}; + +export const toggleHideMissingBadges = async (userId: number) => { + const userPreferences = await getUserPreferences(userId); + + return await setUserPreferences(userId, { + updated_at: new Date().toISOString(), + pinned_hololive_streams: userPreferences ? userPreferences.pinned_hololive_streams : [], + hide_missing_badges: userPreferences ? !userPreferences.hide_missing_badges : false + }); +}; + +export const toggleHideAWCBadges = async (userId: number) => { + const userPreferences = await getUserPreferences(userId); + + return await setUserPreferences(userId, { + updated_at: new Date().toISOString(), + hide_awc_badges: userPreferences ? !userPreferences.hide_awc_badges : false + }); +}; + +export const setCSS = async (userId: number, css: string) => { + const userPreferences = await getUserPreferences(userId); + + return await setUserPreferences(userId, { + updated_at: new Date().toISOString(), + pinned_hololive_streams: userPreferences ? userPreferences.pinned_hololive_streams : [], + hide_missing_badges: userPreferences ? userPreferences.hide_missing_badges : false, + badge_wall_css: + css || + "/* Use classes and IDs such as .badges, #badges, .badge, or standard elements like body and details, or anything, as long as it's valid CSS! */" + }); +}; + +export const setBiography = async (userId: number, biography: string) => { + const userPreferences = await getUserPreferences(userId); + + return await setUserPreferences(userId, { + updated_at: new Date().toISOString(), + pinned_hololive_streams: userPreferences ? userPreferences.pinned_hololive_streams : [], + hide_missing_badges: userPreferences ? userPreferences.hide_missing_badges : false, + badge_wall_css: userPreferences ? userPreferences.badge_wall_css : '', + biography: biography || '\n' + }); +}; + +export const togglePinnedBadgeWallCategory = async (userId: number, category: string) => { + const userPreferences = await getUserPreferences(userId); + + if (!userPreferences) return null; + + const pinnedCategories = userPreferences.pinned_badge_wall_categories; + const index = pinnedCategories.indexOf(category); + + if (index === -1) pinnedCategories.push(category); + else pinnedCategories.splice(index, 1); + + return await setUserPreferences(userId, { + updated_at: new Date().toISOString(), + pinned_hololive_streams: userPreferences.pinned_hololive_streams, + hide_missing_badges: userPreferences.hide_missing_badges, + badge_wall_css: userPreferences.badge_wall_css, + pinned_badge_wall_categories: pinnedCategories + }); +}; + +export const setPinnedBadgeWallCategories = async (userId: number, categories: string[]) => { + const userPreferences = await getUserPreferences(userId); + + if (!userPreferences) return null; + + return await setUserPreferences(userId, { + updated_at: new Date().toISOString(), + pinned_hololive_streams: userPreferences.pinned_hololive_streams, + hide_missing_badges: userPreferences.hide_missing_badges, + badge_wall_css: userPreferences.badge_wall_css, + pinned_badge_wall_categories: categories + }); +}; diff --git a/src/lib/Database/activities.ts b/src/lib/Database/activities.ts deleted file mode 100644 index 6a505ff3..00000000 --- a/src/lib/Database/activities.ts +++ /dev/null @@ -1,22 +0,0 @@ -import type { ActivitiesPage } from '$lib/Data/AniList/activity'; -import Dexie, { type Table } from 'dexie'; - -export interface ActivityPage { - page: number; - data: ActivitiesPage; -} - -export class ActivityDatabase extends Dexie { - activities: Table; - - constructor() { - super('activities'); - this.version(1).stores({ - activities: 'page, data' - }); - - this.activities = this.table('activities'); - } -} - -export const database = new ActivityDatabase(); diff --git a/src/lib/Database/badges.ts b/src/lib/Database/badges.ts deleted file mode 100644 index 145d0bc1..00000000 --- a/src/lib/Database/badges.ts +++ /dev/null @@ -1,49 +0,0 @@ -import supabase from './supabase'; - -interface Badge { - id: number; - created_at: string; - updated_at: string; - image_url: string; - image_artist: string; - description: string; - event: number; - group: number; -} - -interface NewBadge { - updated_at?: string; - image_url: string; - image_artist: string; - description: string; -} - -interface GetBy { - event?: number; - group?: number; -} - -export const getBadges = async (getBy?: GetBy) => { - let data, error; - - if (getBy?.event) - [data, error] = await supabase.from('badges').select('*').eq('event', getBy.event); - else if (getBy?.group) - [data, error] = await supabase.from('badges').select('*').eq('group', getBy.group); - else [data, error] = await supabase.from('badges').select('*'); - - if (error) return []; - - return data as Badge[]; -}; - -export const createBadge = async (badge: NewBadge) => await supabase.from('badges').insert(badge); - -export const deleteBadge = async (id: number) => - await supabase.from('badges').delete().eq('id', id); - -export const updateBadge = async (id: number, badge: NewBadge) => { - if (!badge.updated_at) badge.updated_at = new Date().toISOString(); - - return await supabase.from('badges').update(badge).eq('id', id); -}; diff --git a/src/lib/Database/chapters.ts b/src/lib/Database/chapters.ts deleted file mode 100644 index 0f77f0a0..00000000 --- a/src/lib/Database/chapters.ts +++ /dev/null @@ -1,22 +0,0 @@ -import Dexie, { type Table } from 'dexie'; - -export interface Chapter { - id: number; - chapters: number | null; - volumes: number | null; -} - -export class ChapterDatabase extends Dexie { - chapters: Table; - - constructor() { - super('chapters'); - this.version(1).stores({ - chapters: 'id, chapters, volumes' - }); - - this.chapters = this.table('chapters'); - } -} - -export const database = new ChapterDatabase(); diff --git a/src/lib/Database/events.ts b/src/lib/Database/events.ts deleted file mode 100644 index 8f507a83..00000000 --- a/src/lib/Database/events.ts +++ /dev/null @@ -1,46 +0,0 @@ -import type Group from '$lib/Events/Group.svelte'; -import supabase from './supabase'; - -export interface Event { - id: number; - created_at: string; - updated_at: string; - title: string; - description: string; - group: Group; - banner: string; - anilist_url: string; -} - -interface NewEvent { - updated_at?: string; - title: string; - description: string; -} - -export const getEvents = async () => { - const { data, error } = await supabase.from('events').select('*, group:groups(*)'); - - if (error) return []; - - return data as Event[]; -}; - -export const getGroupEvents = async (group: string) => { - const { data, error } = await supabase.from('events').select('*').eq('group', group); - - if (error) return []; - - return data as Event[]; -}; - -export const createEvent = async (event: NewEvent) => await supabase.from('events').insert(event); - -export const deleteEvent = async (id: number) => - await supabase.from('events').delete().eq('id', id); - -export const updateEvent = async (id: number, event: NewEvent) => { - if (!event.updated_at) event.updated_at = new Date().toISOString(); - - return await supabase.from('events').update(event).eq('id', id); -}; diff --git a/src/lib/Database/groups.ts b/src/lib/Database/groups.ts deleted file mode 100644 index 097d6f89..00000000 --- a/src/lib/Database/groups.ts +++ /dev/null @@ -1,32 +0,0 @@ -import supabase from './supabase'; - -export interface Group { - id: number; - created_at: string; - updated_at?: string; - members: number[]; - avatar: string; - banner: string; - description?: string; - name: string; - anilist_id: number; - anilist_username: string; - badge?: string; - badge_description?: string; -} - -export const getGroups = async () => { - const { data, error } = await supabase.from('groups').select('*'); - - if (error) return []; - - return data as Group[]; -}; - -export const getGroup = async (slug: string) => { - const { data, error } = await supabase.from('groups').select('*').eq('anilist_username', slug); - - if (error || data.length === 0) return null; - - return data[0] as Group; -}; diff --git a/src/lib/Database/user.ts b/src/lib/Database/user.ts deleted file mode 100644 index df68bc91..00000000 --- a/src/lib/Database/user.ts +++ /dev/null @@ -1,23 +0,0 @@ -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; - - 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/userBadges.ts b/src/lib/Database/userBadges.ts deleted file mode 100644 index d21c3527..00000000 --- a/src/lib/Database/userBadges.ts +++ /dev/null @@ -1,106 +0,0 @@ -import { databaseTimeToDate } from '$lib/Utility/time'; -import supabase from './supabase'; - -export interface Badge { - post?: string; - image?: string; - description?: string | null; - id?: number; - time?: string; - category?: string | null; - hidden?: boolean; - source?: string | null; - designer?: string | null; - shadow_hidden?: boolean; - click_count?: number; -} - -export const getUserBadges = async (userId: number): Promise => { - const { data, error } = await supabase.from('user_badges').select('*').eq('user_id', userId); - - if (error) return []; - - return data.sort((a, b) => - databaseTimeToDate((a as Badge).time ?? '').getTime() > - databaseTimeToDate((b as Badge).time ?? '').getTime() - ? -1 - : 1 - ) as Badge[]; -}; - -export const addUserBadge = async (userId: number, badge: Badge) => { - const { post, image, description, time, category, hidden, source, designer } = badge; - - if (post === undefined || image === undefined) return; - - if (time) { - await supabase.from('user_badges').insert({ - user_id: userId, - post, - image, - description, - time, - category, - hidden, - source, - designer - }); - } else { - await supabase - .from('user_badges') - .insert({ user_id: userId, post, image, description, category, hidden, source, designer }); - } -}; - -export const removeUserBadge = async (userId: number, id: number) => { - if (!isNaN(id)) await supabase.from('user_badges').delete().eq('id', id).eq('user_id', userId); -}; - -export const updateUserBadge = async (userId: number, id: number, badge: Badge) => { - if (badge.post === undefined || badge.image === undefined) return; - - await supabase - .from('user_badges') - .update({ - post: badge.post, - image: badge.image, - description: badge.description, - category: badge.category, - time: badge.time, - hidden: badge.hidden, - source: badge.source, - designer: badge.designer - }) - .eq('id', id) - .eq('user_id', userId); -}; - -export const renameCategory = async (userId: number, oldName: string, newName: string) => - await supabase - .from('user_badges') - .update({ category: newName }) - .eq('category', oldName) - .eq('user_id', userId); - -export const removeAllUserBadges = async (userId: number) => - await supabase.from('user_badges').delete().eq('user_id', userId); - -export const migrateCategory = async (userId: number, oldName: string, newName: string) => - await supabase - .from('user_badges') - .update({ category: newName }) - .eq('category', oldName) - .eq('user_id', userId); - -export const setShadowHidden = async (userId: number, shadowHide: boolean) => - await supabase.from('user_badges').update({ shadow_hidden: shadowHide }).eq('user_id', userId); - -export const setShadowHiddenBadge = async (userId: number, id: number, shadowHide: boolean) => - await supabase - .from('user_badges') - .update({ shadow_hidden: shadowHide }) - .eq('id', id) - .eq('user_id', userId); - -export const incrementClickCount = async (id: number) => - await supabase.rpc('user_badges_increment_click_count', { user_badge_id: id }); diff --git a/src/lib/Database/userConfiguration.ts b/src/lib/Database/userConfiguration.ts deleted file mode 100644 index 6a22105a..00000000 --- a/src/lib/Database/userConfiguration.ts +++ /dev/null @@ -1,50 +0,0 @@ -import supabase from './supabase'; - -interface UserConfiguration { - user_id: number; - configuration: object; - created_at: string; - updated_at: string; -} - -interface NewUserConfiguration { - configuration: object; - updated_at?: string; -} - -export const getUserConfiguration = async (userId: number) => { - const { data, error } = await supabase - .from('user_configuration') - .select('*') - .eq('user_id', userId); - - if (error || data.length === 0 || data[0].user_id !== userId) return null; - - return data[0] as UserConfiguration; -}; - -export const setUserConfiguration = async (userId: number, configuration: NewUserConfiguration) => { - const { data, error } = await supabase - .from('user_configuration') - .upsert( - { - user_id: userId, - configuration: configuration.configuration, - updated_at: configuration.updated_at || new Date().toISOString() - }, - { onConflict: 'user_id' } - ) - .select(); - - if (error || !data || (data as []).length === 0) return null; - - return data[0].configuration as UserConfiguration; -}; - -export const deleteUserConfiguration = async (userId: number) => { - const { data, error } = await supabase.from('user_configuration').delete().eq('user_id', userId); - - if (error || !data) return null; - - return data; -}; diff --git a/src/lib/Database/userNotifications.ts b/src/lib/Database/userNotifications.ts deleted file mode 100644 index 6516b813..00000000 --- a/src/lib/Database/userNotifications.ts +++ /dev/null @@ -1,32 +0,0 @@ -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 deleteUserSubscription = async (userId: number) => - await supabase.from('user_notifications').delete().eq('user_id', userId); - -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' } - ); diff --git a/src/lib/Database/userPreferences.ts b/src/lib/Database/userPreferences.ts deleted file mode 100644 index ca2d62be..00000000 --- a/src/lib/Database/userPreferences.ts +++ /dev/null @@ -1,156 +0,0 @@ -import supabase from './supabase'; - -export interface UserPreferences { - created_at: string; - updated_at: string; - user_id: number; - pinned_hololive_streams: string[]; - hide_missing_badges: boolean; - biography: string | null; - badge_wall_css: string; - hide_awc_badges: boolean; - pinned_badge_wall_categories: string[]; -} - -interface NewUserPreferences { - updated_at?: string; - pinned_hololive_streams?: string[]; - hide_missing_badges?: boolean; - badge_wall_css?: string; - biography?: string; - hide_awc_badges?: boolean; - pinned_badge_wall_categories?: string[]; -} - -export const getUserPreferences = async (userId: number) => { - const { data, error } = await supabase.from('user_preferences').select('*').eq('user_id', userId); - - if (error || data.length === 0 || data[0].user_id !== userId) return null; - - return data[0] as UserPreferences; -}; - -export const setUserPreferences = async (userId: number, preferences: NewUserPreferences) => { - const userPreferences = await getUserPreferences(userId); - const { data, error } = await supabase - .from('user_preferences') - .upsert( - { - user_id: userId, - updated_at: preferences.updated_at || new Date().toISOString(), - pinned_hololive_streams: - preferences.pinned_hololive_streams || - (userPreferences ? userPreferences.pinned_hololive_streams : []), - hide_missing_badges: preferences.hide_missing_badges || false, - biography: preferences.biography || (userPreferences ? userPreferences.biography : null), - badge_wall_css: - preferences.badge_wall_css || (userPreferences ? userPreferences.badge_wall_css : ''), - hide_awc_badges: preferences.hide_awc_badges || false, - pinned_badge_wall_categories: - preferences.pinned_badge_wall_categories || - (userPreferences ? userPreferences.pinned_badge_wall_categories : '') - }, - { onConflict: 'user_id' } - ) - .select(); - - if (error || !data || (data as []).length === 0) return null; - - return data[0] as UserPreferences; -}; - -export const toggleHololiveStreamPinning = async (userId: number, streamId: string) => { - const userPreferences = await getUserPreferences(userId); - - if (!userPreferences) return null; - - const pinnedStreams = userPreferences.pinned_hololive_streams; - const index = pinnedStreams.indexOf(streamId); - - if (index === -1) pinnedStreams.push(streamId); - else pinnedStreams.splice(index, 1); - - return await setUserPreferences(userId, { - updated_at: new Date().toISOString(), - pinned_hololive_streams: pinnedStreams, - hide_missing_badges: userPreferences.hide_missing_badges - }); -}; - -export const toggleHideMissingBadges = async (userId: number) => { - const userPreferences = await getUserPreferences(userId); - - return await setUserPreferences(userId, { - updated_at: new Date().toISOString(), - pinned_hololive_streams: userPreferences ? userPreferences.pinned_hololive_streams : [], - hide_missing_badges: userPreferences ? !userPreferences.hide_missing_badges : false - }); -}; - -export const toggleHideAWCBadges = async (userId: number) => { - const userPreferences = await getUserPreferences(userId); - - return await setUserPreferences(userId, { - updated_at: new Date().toISOString(), - hide_awc_badges: userPreferences ? !userPreferences.hide_awc_badges : false - }); -}; - -export const setCSS = async (userId: number, css: string) => { - const userPreferences = await getUserPreferences(userId); - - return await setUserPreferences(userId, { - updated_at: new Date().toISOString(), - pinned_hololive_streams: userPreferences ? userPreferences.pinned_hololive_streams : [], - hide_missing_badges: userPreferences ? userPreferences.hide_missing_badges : false, - badge_wall_css: - css || - "/* Use classes and IDs such as .badges, #badges, .badge, or standard elements like body and details, or anything, as long as it's valid CSS! */" - }); -}; - -export const setBiography = async (userId: number, biography: string) => { - const userPreferences = await getUserPreferences(userId); - - return await setUserPreferences(userId, { - updated_at: new Date().toISOString(), - pinned_hololive_streams: userPreferences ? userPreferences.pinned_hololive_streams : [], - hide_missing_badges: userPreferences ? userPreferences.hide_missing_badges : false, - badge_wall_css: userPreferences ? userPreferences.badge_wall_css : '', - biography: biography || '\n' - }); -}; - -export const togglePinnedBadgeWallCategory = async (userId: number, category: string) => { - const userPreferences = await getUserPreferences(userId); - - if (!userPreferences) return null; - - const pinnedCategories = userPreferences.pinned_badge_wall_categories; - const index = pinnedCategories.indexOf(category); - - if (index === -1) pinnedCategories.push(category); - else pinnedCategories.splice(index, 1); - - return await setUserPreferences(userId, { - updated_at: new Date().toISOString(), - pinned_hololive_streams: userPreferences.pinned_hololive_streams, - hide_missing_badges: userPreferences.hide_missing_badges, - badge_wall_css: userPreferences.badge_wall_css, - pinned_badge_wall_categories: pinnedCategories - }); -}; - -export const setPinnedBadgeWallCategories = async (userId: number, categories: string[]) => { - const userPreferences = await getUserPreferences(userId); - - if (!userPreferences) return null; - - return await setUserPreferences(userId, { - updated_at: new Date().toISOString(), - pinned_hololive_streams: userPreferences.pinned_hololive_streams, - hide_missing_badges: userPreferences.hide_missing_badges, - badge_wall_css: userPreferences.badge_wall_css, - pinned_badge_wall_categories: categories - }); -}; -- cgit v1.2.3