82 lines
2.3 KiB
TypeScript
82 lines
2.3 KiB
TypeScript
import { getDb } from './db'
|
|
import type { UserSettings } from '@/types'
|
|
|
|
const DEFAULTS: UserSettings = {
|
|
mixedAutoplay: true,
|
|
mixedLoop: true,
|
|
mixedMuted: true,
|
|
moviesAutoplay: true,
|
|
moviesLoop: false,
|
|
moviesMuted: false,
|
|
tvAutoplay: true,
|
|
tvLoop: false,
|
|
tvMuted: false,
|
|
}
|
|
|
|
interface SettingsRow {
|
|
mixed_autoplay: number
|
|
mixed_loop: number
|
|
mixed_muted: number
|
|
movies_autoplay: number
|
|
movies_loop: number
|
|
movies_muted: number
|
|
tv_autoplay: number
|
|
tv_loop: number
|
|
tv_muted: number
|
|
}
|
|
|
|
function rowToSettings(row: SettingsRow): UserSettings {
|
|
return {
|
|
mixedAutoplay: row.mixed_autoplay === 1,
|
|
mixedLoop: row.mixed_loop === 1,
|
|
mixedMuted: row.mixed_muted === 1,
|
|
moviesAutoplay: row.movies_autoplay === 1,
|
|
moviesLoop: row.movies_loop === 1,
|
|
moviesMuted: row.movies_muted === 1,
|
|
tvAutoplay: row.tv_autoplay === 1,
|
|
tvLoop: row.tv_loop === 1,
|
|
tvMuted: row.tv_muted === 1,
|
|
}
|
|
}
|
|
|
|
export function getUserSettings(userId: string): UserSettings {
|
|
const db = getDb()
|
|
const row = db
|
|
.prepare('SELECT * FROM user_settings WHERE user_id = ?')
|
|
.get(userId) as SettingsRow | undefined
|
|
return row ? rowToSettings(row) : { ...DEFAULTS }
|
|
}
|
|
|
|
export function updateUserSettings(userId: string, settings: UserSettings): void {
|
|
const db = getDb()
|
|
db.prepare(`
|
|
INSERT INTO user_settings (
|
|
user_id,
|
|
mixed_autoplay, mixed_loop, mixed_muted,
|
|
movies_autoplay, movies_loop, movies_muted,
|
|
tv_autoplay, tv_loop, tv_muted
|
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
ON CONFLICT(user_id) DO UPDATE SET
|
|
mixed_autoplay = excluded.mixed_autoplay,
|
|
mixed_loop = excluded.mixed_loop,
|
|
mixed_muted = excluded.mixed_muted,
|
|
movies_autoplay = excluded.movies_autoplay,
|
|
movies_loop = excluded.movies_loop,
|
|
movies_muted = excluded.movies_muted,
|
|
tv_autoplay = excluded.tv_autoplay,
|
|
tv_loop = excluded.tv_loop,
|
|
tv_muted = excluded.tv_muted
|
|
`).run(
|
|
userId,
|
|
settings.mixedAutoplay ? 1 : 0,
|
|
settings.mixedLoop ? 1 : 0,
|
|
settings.mixedMuted ? 1 : 0,
|
|
settings.moviesAutoplay ? 1 : 0,
|
|
settings.moviesLoop ? 1 : 0,
|
|
settings.moviesMuted ? 1 : 0,
|
|
settings.tvAutoplay ? 1 : 0,
|
|
settings.tvLoop ? 1 : 0,
|
|
settings.tvMuted ? 1 : 0,
|
|
)
|
|
}
|