db.ts

1import { supabase } from '@/configs/supabase'
2
3export async function getGlobalBlacklist(): Promise<string[]> {
4	const { data, error } = await supabase.from('global_blacklist').select('tag')
5
6	if (error) {
7		console.error('Error fetching global blacklist:', error)
8		return []
9	}
10
11	return data.map((item) => `-${item.tag}`)
12}
13
14export async function getUserBlacklist(userId: string): Promise<string[]> {
15	const { data, error } = await supabase
16		.from('user_blacklists')
17		.select('blacklisted_tags')
18		.eq('user_id', userId)
19		.single()
20
21	if (error) {
22		console.error('Error fetching user blacklist:', error)
23		return []
24	}
25
26	return (
27		data?.blacklisted_tags?.map((tag: string) =>
28			tag.startsWith('-') ? tag : `-${tag}`,
29		) || []
30	)
31}
32
33export async function addToBlacklist(
34	userId: string,
35	tags: string[],
36): Promise<boolean> {
37	const { error } = await supabase.from('user_blacklists').upsert(
38		{
39			user_id: userId,
40			blacklisted_tags: tags,
41		},
42		{
43			onConflict: 'user_id',
44		},
45	)
46
47	if (error) {
48		console.error('Error updating blacklist:', error)
49		return false
50	}
51
52	return true
53}
54
55export async function addToHistory(
56	userId: string,
57	site: string,
58	tags: string[],
59	postUrl: string,
60) {
61	const { error } = await supabase.from('booru_history').insert({
62		user_id: userId,
63		site,
64		tags,
65		post_url: postUrl,
66	})
67
68	if (error) {
69		console.error('Error adding to history:', error)
70	}
71}
72