mirror of
https://github.com/jjbrunton/Sauci.git
synced 2026-11-01 18:19:43 +00:00
Add question types, nudge feature, and share modal
- Add new question card components (photo, audio, text, who-likely) - Add share preview modal for matches - Add nudge notification system with rate limiting - Add session-based notification improvements - Add match confetti celebration animation - Update onboarding flow with paywall tracking - Remove deprecated swipe card and tutorial components - Update GlassCard/GlassInput to use solid backgrounds - Fix routing issues after packs screen removal
This commit is contained in:
1 parent
8612b16b93
commit
1daf386765
114 files changed
+14877
-7911
No files matched your search
@@ -0,0 +1,111 @@
|
||||
-- Migration: Add Question Types Support
|
||||
-- This migration adds support for different question types beyond the original swipe mechanic.
|
||||
-- New types include: text_answer, audio, photo, and who_likely
|
||||
|
||||
-- 1. Create question_type enum
|
||||
CREATE TYPE question_type AS ENUM ('swipe', 'text_answer', 'audio', 'photo', 'who_likely');
|
||||
|
||||
-- 2. Add columns to questions table
|
||||
-- question_type: The type of question (defaults to 'swipe' for backwards compatibility)
|
||||
-- config: JSONB field for type-specific configuration (e.g., max_length for text, duration for audio)
|
||||
ALTER TABLE questions
|
||||
ADD COLUMN question_type question_type NOT NULL DEFAULT 'swipe',
|
||||
ADD COLUMN config JSONB DEFAULT '{}';
|
||||
|
||||
-- 3. Add response_data to responses table
|
||||
-- This stores type-specific response data (e.g., text answer, audio URL, photo URL, selection)
|
||||
-- For swipe questions, this remains NULL (the answer column is used)
|
||||
ALTER TABLE responses
|
||||
ADD COLUMN response_data JSONB DEFAULT NULL;
|
||||
|
||||
-- 4. Extend match_type enum with 'both_answered' for non-swipe question types
|
||||
-- Using DO block because ADD VALUE IF NOT EXISTS doesn't work in transaction blocks
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM pg_enum
|
||||
WHERE enumlabel = 'both_answered'
|
||||
AND enumtypid = (SELECT oid FROM pg_type WHERE typname = 'match_type')
|
||||
) THEN
|
||||
ALTER TYPE match_type ADD VALUE 'both_answered';
|
||||
END IF;
|
||||
END
|
||||
$$;
|
||||
|
||||
-- 5. Add response_summary to matches table
|
||||
-- This stores a summary of both partners' responses for display purposes
|
||||
-- For swipe matches, this can remain NULL
|
||||
-- For other types, it contains both responses for easy retrieval
|
||||
ALTER TABLE matches
|
||||
ADD COLUMN response_summary JSONB DEFAULT NULL;
|
||||
|
||||
-- 6. Create storage bucket for response media (audio, photos)
|
||||
INSERT INTO storage.buckets (id, name, public)
|
||||
VALUES ('response-media', 'response-media', false)
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
-- 7. RLS policies for response-media bucket
|
||||
|
||||
-- Policy: Users can upload to their own folder (user_id/*)
|
||||
CREATE POLICY "Users can upload to own folder"
|
||||
ON storage.objects
|
||||
FOR INSERT
|
||||
TO authenticated
|
||||
WITH CHECK (
|
||||
bucket_id = 'response-media'
|
||||
AND (storage.foldername(name))[1] = auth.uid()::text
|
||||
);
|
||||
|
||||
-- Policy: Users can update their own uploads
|
||||
CREATE POLICY "Users can update own uploads"
|
||||
ON storage.objects
|
||||
FOR UPDATE
|
||||
TO authenticated
|
||||
USING (
|
||||
bucket_id = 'response-media'
|
||||
AND (storage.foldername(name))[1] = auth.uid()::text
|
||||
)
|
||||
WITH CHECK (
|
||||
bucket_id = 'response-media'
|
||||
AND (storage.foldername(name))[1] = auth.uid()::text
|
||||
);
|
||||
|
||||
-- Policy: Users can delete their own uploads
|
||||
CREATE POLICY "Users can delete own uploads"
|
||||
ON storage.objects
|
||||
FOR DELETE
|
||||
TO authenticated
|
||||
USING (
|
||||
bucket_id = 'response-media'
|
||||
AND (storage.foldername(name))[1] = auth.uid()::text
|
||||
);
|
||||
|
||||
-- Policy: Users can read from their couple's folder (their own + partner's uploads)
|
||||
-- This allows partners to see each other's media in matches
|
||||
CREATE POLICY "Users can read couple media"
|
||||
ON storage.objects
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (
|
||||
bucket_id = 'response-media'
|
||||
AND (
|
||||
-- User can read their own folder
|
||||
(storage.foldername(name))[1] = auth.uid()::text
|
||||
OR
|
||||
-- User can read their partner's folder (same couple)
|
||||
(storage.foldername(name))[1] IN (
|
||||
SELECT p.id::text
|
||||
FROM profiles p
|
||||
WHERE p.couple_id = (
|
||||
SELECT couple_id FROM profiles WHERE id = auth.uid()
|
||||
)
|
||||
AND p.couple_id IS NOT NULL
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
-- Add comments for documentation
|
||||
COMMENT ON COLUMN questions.question_type IS 'The type of question: swipe (yes/no/maybe), text_answer, audio, photo, or who_likely';
|
||||
COMMENT ON COLUMN questions.config IS 'Type-specific configuration as JSONB. Examples: {"max_length": 500} for text, {"max_duration_seconds": 60} for audio';
|
||||
COMMENT ON COLUMN responses.response_data IS 'Type-specific response data as JSONB. Examples: {"text": "..."} for text_answer, {"url": "..."} for audio/photo, {"selection": "partner1"} for who_likely';
|
||||
COMMENT ON COLUMN matches.response_summary IS 'Summary of both partners responses for display. Contains both response_data values keyed by user_id';
|
||||
+227
@@ -0,0 +1,227 @@
|
||||
-- Update get_recommended_questions to return question_type and config for new question types
|
||||
-- Must DROP first because we're changing the return type signature
|
||||
DROP FUNCTION IF EXISTS public.get_recommended_questions(uuid);
|
||||
|
||||
CREATE OR REPLACE FUNCTION public.get_recommended_questions(target_pack_id uuid DEFAULT NULL::uuid)
|
||||
RETURNS TABLE(id uuid, text text, partner_text text, is_two_part boolean, pack_id uuid, intensity integer, partner_answered boolean, allowed_couple_genders text[], target_user_genders text[], question_type question_type, config jsonb)
|
||||
LANGUAGE plpgsql
|
||||
SECURITY DEFINER
|
||||
SET search_path TO 'public'
|
||||
AS $function$
|
||||
DECLARE
|
||||
v_user_id UUID;
|
||||
v_couple_id UUID;
|
||||
v_partner_id UUID;
|
||||
v_user_gender TEXT;
|
||||
v_partner_gender TEXT;
|
||||
v_couple_type TEXT;
|
||||
v_has_premium BOOLEAN;
|
||||
v_gap_threshold INTEGER;
|
||||
v_my_unanswered INTEGER;
|
||||
v_partner_unanswered INTEGER;
|
||||
v_net_gap INTEGER;
|
||||
v_user_max_intensity INTEGER;
|
||||
v_partner_max_intensity INTEGER;
|
||||
v_effective_max_intensity INTEGER;
|
||||
v_use_couple_intensity_gate BOOLEAN;
|
||||
v_daily_limit INTEGER;
|
||||
v_daily_count INTEGER;
|
||||
v_today_start TIMESTAMPTZ;
|
||||
v_tomorrow_start TIMESTAMPTZ;
|
||||
BEGIN
|
||||
v_user_id := auth.uid();
|
||||
|
||||
SELECT p.couple_id, p.gender, COALESCE(p.max_intensity, 5)
|
||||
INTO v_couple_id, v_user_gender, v_user_max_intensity
|
||||
FROM public.profiles p
|
||||
WHERE p.id = v_user_id;
|
||||
|
||||
IF v_couple_id IS NULL THEN
|
||||
RETURN;
|
||||
END IF;
|
||||
|
||||
SELECT p.id, p.gender, COALESCE(p.max_intensity, 5)
|
||||
INTO v_partner_id, v_partner_gender, v_partner_max_intensity
|
||||
FROM public.profiles p
|
||||
WHERE p.couple_id = v_couple_id AND p.id != v_user_id
|
||||
LIMIT 1;
|
||||
|
||||
-- Toggle: gate questions by couple-effective intensity
|
||||
SELECT COALESCE(ac.couple_intensity_gate_enabled, FALSE)
|
||||
INTO v_use_couple_intensity_gate
|
||||
FROM app_config ac
|
||||
LIMIT 1;
|
||||
|
||||
v_use_couple_intensity_gate := COALESCE(v_use_couple_intensity_gate, FALSE);
|
||||
|
||||
v_effective_max_intensity := CASE
|
||||
WHEN v_use_couple_intensity_gate AND v_partner_id IS NOT NULL THEN LEAST(v_user_max_intensity, v_partner_max_intensity)
|
||||
ELSE v_user_max_intensity
|
||||
END;
|
||||
|
||||
-- Check premium access (used for both answer gap and daily limit)
|
||||
v_has_premium := public.has_premium_access(v_user_id);
|
||||
|
||||
-- Check daily response limit (only when not browsing a specific pack and user is non-premium)
|
||||
IF v_partner_id IS NOT NULL AND target_pack_id IS NULL AND NOT v_has_premium THEN
|
||||
SELECT COALESCE(ac.daily_response_limit, 0) INTO v_daily_limit
|
||||
FROM app_config ac
|
||||
LIMIT 1;
|
||||
|
||||
IF v_daily_limit > 0 THEN
|
||||
v_today_start := date_trunc('day', NOW() AT TIME ZONE 'UTC') AT TIME ZONE 'UTC';
|
||||
v_tomorrow_start := v_today_start + INTERVAL '1 day';
|
||||
|
||||
SELECT COUNT(*)::INTEGER INTO v_daily_count
|
||||
FROM responses r
|
||||
WHERE r.user_id = v_user_id
|
||||
AND r.created_at >= v_today_start
|
||||
AND r.created_at < v_tomorrow_start;
|
||||
|
||||
IF v_daily_count >= v_daily_limit THEN
|
||||
RETURN;
|
||||
END IF;
|
||||
END IF;
|
||||
END IF;
|
||||
|
||||
-- Check answer gap threshold using NET gap (only when not browsing a specific pack)
|
||||
IF v_partner_id IS NOT NULL AND target_pack_id IS NULL THEN
|
||||
SELECT COALESCE(ac.answer_gap_threshold, 10) INTO v_gap_threshold
|
||||
FROM app_config ac
|
||||
LIMIT 1;
|
||||
|
||||
IF v_gap_threshold > 0 THEN
|
||||
WITH enabled_packs AS (
|
||||
SELECT cp.pack_id
|
||||
FROM couple_packs cp
|
||||
WHERE cp.couple_id = v_couple_id AND cp.enabled = true
|
||||
)
|
||||
SELECT
|
||||
COALESCE(SUM(CASE WHEN r.user_id = v_user_id AND NOT EXISTS (
|
||||
SELECT 1 FROM responses pr
|
||||
WHERE pr.question_id = r.question_id
|
||||
AND pr.user_id = v_partner_id
|
||||
) THEN 1 ELSE 0 END), 0)::integer,
|
||||
COALESCE(SUM(CASE WHEN r.user_id = v_partner_id AND NOT EXISTS (
|
||||
SELECT 1 FROM responses pr
|
||||
WHERE pr.question_id = r.question_id
|
||||
AND pr.user_id = v_user_id
|
||||
) THEN 1 ELSE 0 END), 0)::integer
|
||||
INTO v_my_unanswered, v_partner_unanswered
|
||||
FROM responses r
|
||||
INNER JOIN questions q ON q.id = r.question_id
|
||||
WHERE r.couple_id = v_couple_id
|
||||
AND r.user_id IN (v_user_id, v_partner_id)
|
||||
AND EXISTS (SELECT 1 FROM enabled_packs)
|
||||
AND q.pack_id IN (SELECT ep.pack_id FROM enabled_packs ep)
|
||||
AND (
|
||||
NOT v_use_couple_intensity_gate
|
||||
OR q.intensity IS NULL
|
||||
OR q.intensity <= v_effective_max_intensity
|
||||
);
|
||||
|
||||
v_net_gap := GREATEST(0, v_my_unanswered - v_partner_unanswered);
|
||||
|
||||
IF v_net_gap >= v_gap_threshold THEN
|
||||
RETURN;
|
||||
END IF;
|
||||
END IF;
|
||||
END IF;
|
||||
|
||||
IF v_user_gender IS NOT NULL AND v_partner_gender IS NOT NULL THEN
|
||||
IF v_user_gender < v_partner_gender THEN
|
||||
v_couple_type := v_user_gender || '+' || v_partner_gender;
|
||||
ELSE
|
||||
v_couple_type := v_partner_gender || '+' || v_user_gender;
|
||||
END IF;
|
||||
END IF;
|
||||
|
||||
RETURN QUERY
|
||||
WITH user_config_exists AS (
|
||||
SELECT 1 FROM public.couple_packs WHERE couple_id = v_couple_id LIMIT 1
|
||||
),
|
||||
active_packs AS (
|
||||
SELECT target_pack_id AS pack_id
|
||||
WHERE target_pack_id IS NOT NULL
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM public.question_packs qp
|
||||
WHERE qp.id = target_pack_id
|
||||
AND (qp.is_premium = FALSE OR v_has_premium = TRUE)
|
||||
)
|
||||
UNION ALL
|
||||
SELECT cp.pack_id FROM public.couple_packs cp
|
||||
INNER JOIN public.question_packs qp ON qp.id = cp.pack_id
|
||||
WHERE target_pack_id IS NULL
|
||||
AND cp.couple_id = v_couple_id
|
||||
AND cp.enabled = TRUE
|
||||
AND (qp.is_premium = FALSE OR v_has_premium = TRUE)
|
||||
AND EXISTS (SELECT 1 FROM user_config_exists)
|
||||
UNION ALL
|
||||
SELECT p.id AS pack_id FROM public.question_packs p
|
||||
WHERE target_pack_id IS NULL
|
||||
AND p.is_public = TRUE
|
||||
AND (p.is_premium = FALSE OR v_has_premium = TRUE)
|
||||
AND NOT EXISTS (SELECT 1 FROM user_config_exists)
|
||||
)
|
||||
SELECT
|
||||
q.id,
|
||||
CASE
|
||||
WHEN q.partner_text IS NOT NULL AND EXISTS (
|
||||
SELECT 1 FROM public.responses r
|
||||
WHERE r.question_id = q.id
|
||||
AND r.couple_id = v_couple_id
|
||||
AND r.user_id != v_user_id
|
||||
) THEN q.partner_text
|
||||
ELSE q.text
|
||||
END as text,
|
||||
q.partner_text,
|
||||
q.partner_text IS NOT NULL as is_two_part,
|
||||
q.pack_id,
|
||||
q.intensity,
|
||||
EXISTS (
|
||||
SELECT 1 FROM public.responses r
|
||||
WHERE r.question_id = q.id
|
||||
AND r.couple_id = v_couple_id
|
||||
AND r.user_id != v_user_id
|
||||
) AS partner_answered,
|
||||
q.allowed_couple_genders,
|
||||
q.target_user_genders,
|
||||
q.question_type, -- NEW: Return question type
|
||||
q.config -- NEW: Return question config
|
||||
FROM public.questions q
|
||||
WHERE q.pack_id IN (SELECT ap.pack_id FROM active_packs ap)
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM public.responses r
|
||||
WHERE r.question_id = q.id
|
||||
AND r.user_id = v_user_id
|
||||
)
|
||||
AND (
|
||||
q.allowed_couple_genders IS NULL
|
||||
OR v_couple_type IS NULL
|
||||
OR v_couple_type = ANY(q.allowed_couple_genders)
|
||||
)
|
||||
AND (
|
||||
q.target_user_genders IS NULL
|
||||
OR v_user_gender = ANY(q.target_user_genders)
|
||||
OR EXISTS (
|
||||
SELECT 1 FROM public.responses r
|
||||
WHERE r.question_id = q.id
|
||||
AND r.couple_id = v_couple_id
|
||||
AND r.user_id != v_user_id
|
||||
)
|
||||
)
|
||||
AND (
|
||||
q.intensity IS NULL
|
||||
OR q.intensity <= v_effective_max_intensity
|
||||
)
|
||||
ORDER BY
|
||||
EXISTS (
|
||||
SELECT 1 FROM public.responses r
|
||||
WHERE r.question_id = q.id
|
||||
AND r.couple_id = v_couple_id
|
||||
AND r.user_id != v_user_id
|
||||
) DESC,
|
||||
q.intensity ASC,
|
||||
q.id ASC;
|
||||
END;
|
||||
$function$;
|
||||
@@ -0,0 +1,109 @@
|
||||
-- Session-based notification system
|
||||
--
|
||||
-- Changes from the previous match-based notification system:
|
||||
-- 1. Notifications are triggered by ANY response, not just matches
|
||||
-- 2. Tracks which user is actively answering (active_user_id)
|
||||
-- 3. Only notifies the partner of the active user (not both)
|
||||
-- 4. Uses rolling 5-minute inactivity window instead of fixed 2-minute delay
|
||||
-- 5. Notification copy changes to "[Partner] has been answering questions!"
|
||||
|
||||
-- Add new columns to pending_match_notifications
|
||||
ALTER TABLE pending_match_notifications
|
||||
ADD COLUMN IF NOT EXISTS active_user_id UUID REFERENCES profiles(id) ON DELETE CASCADE,
|
||||
ADD COLUMN IF NOT EXISTS response_count INTEGER NOT NULL DEFAULT 0;
|
||||
|
||||
-- Drop the old trigger on matches (we'll trigger on responses instead)
|
||||
DROP TRIGGER IF EXISTS on_match_created ON matches;
|
||||
|
||||
-- Create or replace the notification queue function
|
||||
-- Now triggers on responses instead of matches
|
||||
CREATE OR REPLACE FUNCTION queue_response_notification()
|
||||
RETURNS TRIGGER
|
||||
LANGUAGE plpgsql
|
||||
SECURITY DEFINER
|
||||
SET search_path = 'public'
|
||||
AS $$
|
||||
DECLARE
|
||||
v_couple_id UUID;
|
||||
BEGIN
|
||||
-- Get couple_id from the response
|
||||
v_couple_id := NEW.couple_id;
|
||||
|
||||
-- Skip if no couple_id (shouldn't happen, but safety check)
|
||||
IF v_couple_id IS NULL THEN
|
||||
RETURN NEW;
|
||||
END IF;
|
||||
|
||||
-- Insert or update pending notification with rolling 5-minute window
|
||||
-- The notification is for the PARTNER of the user who is answering
|
||||
INSERT INTO pending_match_notifications (
|
||||
couple_id,
|
||||
active_user_id,
|
||||
match_count,
|
||||
response_count,
|
||||
latest_match_id,
|
||||
notify_at
|
||||
)
|
||||
VALUES (
|
||||
v_couple_id,
|
||||
NEW.user_id,
|
||||
0, -- match_count starts at 0, will be incremented by match trigger if needed
|
||||
1,
|
||||
NULL, -- No match yet
|
||||
now() + interval '5 minutes' -- Rolling 5-minute inactivity window
|
||||
)
|
||||
ON CONFLICT (couple_id) DO UPDATE SET
|
||||
active_user_id = NEW.user_id, -- Update who is active
|
||||
response_count = pending_match_notifications.response_count + 1,
|
||||
notify_at = now() + interval '5 minutes', -- Reset the 5-minute timer
|
||||
updated_at = now();
|
||||
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$;
|
||||
|
||||
-- Create or replace the match count incrementer
|
||||
-- This runs when a match is created to track match_count separately
|
||||
CREATE OR REPLACE FUNCTION increment_pending_match_count()
|
||||
RETURNS TRIGGER
|
||||
LANGUAGE plpgsql
|
||||
SECURITY DEFINER
|
||||
SET search_path = 'public'
|
||||
AS $$
|
||||
BEGIN
|
||||
-- Update the pending notification to increment match count and store latest match
|
||||
UPDATE pending_match_notifications
|
||||
SET
|
||||
match_count = match_count + 1,
|
||||
latest_match_id = NEW.id,
|
||||
updated_at = now()
|
||||
WHERE couple_id = NEW.couple_id;
|
||||
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$;
|
||||
|
||||
-- Trigger on responses table for activity tracking
|
||||
CREATE TRIGGER on_response_created
|
||||
AFTER INSERT ON responses
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION queue_response_notification();
|
||||
|
||||
-- Trigger on matches table just to track match count
|
||||
CREATE TRIGGER on_match_created_count
|
||||
AFTER INSERT ON matches
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION increment_pending_match_count();
|
||||
|
||||
-- Add index on active_user_id for efficient queries
|
||||
CREATE INDEX IF NOT EXISTS idx_pending_match_notifications_active_user
|
||||
ON pending_match_notifications(active_user_id);
|
||||
|
||||
-- Comment explaining the new behavior
|
||||
COMMENT ON TABLE pending_match_notifications IS
|
||||
'Session-based notification queue. Tracks when a user is actively answering questions
|
||||
and sends a single digest notification to their partner after 5 minutes of inactivity.
|
||||
- active_user_id: The user who was answering (their partner receives the notification)
|
||||
- response_count: Total responses during this session
|
||||
- match_count: Number of matches created during this session
|
||||
- notify_at: When to send the notification (rolling 5-min from last activity)';
|
||||
@@ -0,0 +1,14 @@
|
||||
-- Add onboarding_version column to track which version of onboarding users completed
|
||||
-- This allows forcing users through re-onboarding when new data is required
|
||||
|
||||
ALTER TABLE profiles
|
||||
ADD COLUMN onboarding_version INTEGER DEFAULT 0;
|
||||
|
||||
-- Set existing users who completed onboarding to version 1
|
||||
-- New users will default to 0 and need to complete the current version
|
||||
UPDATE profiles
|
||||
SET onboarding_version = 1
|
||||
WHERE onboarding_completed = true;
|
||||
|
||||
-- Add comment for documentation
|
||||
COMMENT ON COLUMN profiles.onboarding_version IS 'Version of onboarding flow completed. 0 = never completed, 1+ = completed that version. Used to force re-onboarding when new data is required.';
|
||||
@@ -0,0 +1,14 @@
|
||||
-- Add nudge feature support
|
||||
-- Allows partners to send reminder notifications with rate limiting
|
||||
|
||||
-- Add nudge tracking to profiles (for rate limiting)
|
||||
ALTER TABLE profiles
|
||||
ADD COLUMN IF NOT EXISTS last_nudge_sent_at TIMESTAMPTZ;
|
||||
|
||||
-- Add nudge preference to notification_preferences
|
||||
ALTER TABLE notification_preferences
|
||||
ADD COLUMN IF NOT EXISTS nudges_enabled BOOLEAN DEFAULT true;
|
||||
|
||||
-- Comment for documentation
|
||||
COMMENT ON COLUMN profiles.last_nudge_sent_at IS 'Timestamp of last nudge sent to partner (rate limited to once per 12 hours)';
|
||||
COMMENT ON COLUMN notification_preferences.nudges_enabled IS 'Whether to receive nudge notifications from partner';
|
||||
Reference in new issue
Block a user