diff --git a/sql/auth/user_preferences.sql b/sql/auth/user_preferences.sql new file mode 100644 index 0000000..b16b5ab --- /dev/null +++ b/sql/auth/user_preferences.sql @@ -0,0 +1,39 @@ +-- Create user_preferences table to store user settings +CREATE TABLE IF NOT EXISTS user_preferences ( + id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE, + hide_from_leaderboard BOOLEAN NOT NULL DEFAULT false, + created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), + updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), + UNIQUE(user_id) +); + +-- Create RLS policies for user_preferences table +ALTER TABLE user_preferences ENABLE ROW LEVEL SECURITY; + +-- Users can read their own preferences +CREATE POLICY "Users can read their own preferences" ON user_preferences FOR +SELECT USING (auth.uid() = user_id); + +-- Users can insert their own preferences +CREATE POLICY "Users can insert their own preferences" ON user_preferences FOR +INSERT WITH CHECK (auth.uid() = user_id); + +-- Users can update their own preferences +CREATE POLICY "Users can update their own preferences" ON user_preferences FOR +UPDATE USING (auth.uid() = user_id); + +-- Create function to automatically create user preferences on new user creation +CREATE OR REPLACE FUNCTION public.handle_new_user_preferences() RETURNS TRIGGER AS $$ +BEGIN + INSERT INTO public.user_preferences (user_id, hide_from_leaderboard) + VALUES (NEW.id, false); + RETURN NEW; +END; +$$ LANGUAGE plpgsql SECURITY DEFINER; + +-- Create trigger to call the function when a new user is created +DROP TRIGGER IF EXISTS on_auth_user_created_preferences ON auth.users; +CREATE TRIGGER on_auth_user_created_preferences +AFTER INSERT ON auth.users +FOR EACH ROW EXECUTE FUNCTION public.handle_new_user_preferences(); diff --git a/sql/init.sql b/sql/init.sql index e2cd86a..d1c3fdc 100644 --- a/sql/init.sql +++ b/sql/init.sql @@ -2,21 +2,26 @@ -- This file includes all the necessary SQL files to set up the database schema -- Enable UUID extension if not already enabled CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; + -- Common utility functions \ i common / utility_functions.sql -- Authentication and user management -\ i auth / user_roles.sql \ i auth / user_triggers.sql -- Problem-related tables and functions +\ i auth / user_roles.sql \ i auth / user_triggers.sql \ i auth / user_preferences.sql -- Problem-related tables and functions \ i problems / problems.sql \ i problems / user_problem_feedback.sql \ i problems / user_solved_problems.sql \ i problems / problem_functions.sql -- Contest-related tables and functions \ i contests / contests.sql \ i contests / user_contest_participation.sql \ i contests / user_contest_feedback.sql \ i contests / contest_functions.sql -- Leaderboard functions \ i leaderboard / leaderboard_functions.sql -- Grant necessary permissions + GRANT USAGE ON SCHEMA public TO anon, authenticated, service_role; + GRANT ALL ON ALL TABLES IN SCHEMA public TO anon, authenticated, service_role; + GRANT ALL ON ALL FUNCTIONS IN SCHEMA public TO anon, authenticated, service_role; + GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO anon, authenticated, service_role; \ No newline at end of file diff --git a/sql/leaderboard/leaderboard_functions.sql b/sql/leaderboard/leaderboard_functions.sql index 118607b..d0c70fe 100644 --- a/sql/leaderboard/leaderboard_functions.sql +++ b/sql/leaderboard/leaderboard_functions.sql @@ -27,6 +27,8 @@ CREATE OR REPLACE FUNCTION get_leaderboard() RETURNS TABLE ( ) AS earliest_solves_sum FROM auth.users u LEFT JOIN user_solved_problems usp ON u.id = usp.user_id + LEFT JOIN user_preferences up ON u.id = up.user_id + WHERE COALESCE(up.hide_from_leaderboard, false) = false GROUP BY u.id, u.raw_user_meta_data HAVING COUNT(usp.problem_id) > 0 @@ -45,6 +47,7 @@ FROM user_stats ORDER BY user_stats.problems_solved DESC, user_stats.earliest_solves_sum ASC; $$ LANGUAGE SQL SECURITY DEFINER; + -- Grant permissions GRANT EXECUTE ON FUNCTION get_leaderboard() TO authenticated, anon; \ No newline at end of file diff --git a/src/lib/components/Header.svelte b/src/lib/components/Header.svelte index 8c4dd99..bf7cbd1 100644 --- a/src/lib/components/Header.svelte +++ b/src/lib/components/Header.svelte @@ -129,16 +129,18 @@ $: if ($page) { gitgud Logo - gitgud.cc + + gitgud + -