Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

wip: Supabase local setup + contributor guide

Richie McIlroyef0b31576c5f
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 6c0cccd73..419bb3538 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -20,6 +20,14 @@ There are many ways to contribute to Cap. You can:
- [Suggest a feature (via Discord)](https://discord.com/invite/y8gdQ3WRN3)
- Submit a PR
+### How do I set up a local Supabase database?
+
+1. Follow the instructions here to install the Supabase CLI: https://supabase.com/docs/guides/cli/getting-started
+2. Run `cd supabase && supabase start` from the route of the project. This will take you to the supabase folder, and start the local Supabase server.
+3. On first run, it'll take a few mins to set up the local Supabase server.
+4. Once running, your local Supabase server env vars will be printed to the console. Copy these into your .env file.
+5. To stop the local Supabase server, run `supabase stop` from the supabase folder, and `supabase start` to start it again.
+
### How do I get started with development on my local machine?
This is a very top level guide right now, but the basics are:
diff --git a/apps/desktop/src-tauri/Cargo.lock b/apps/desktop/src-tauri/Cargo.lock
index 4ff4a6d16..e7bd1c674 100644
--- a/apps/desktop/src-tauri/Cargo.lock
+++ b/apps/desktop/src-tauri/Cargo.lock
@@ -451,7 +451,6 @@ name = "cap"
version = "0.0.0"
dependencies = [
"ffmpeg-sidecar",
- "regex",
"reqwest",
"serde",
"serde_json",
diff --git a/packages/utils/schema/schema.sql b/packages/utils/schema/schema.sql
index 91fa97feb..960f5796c 100644
--- a/packages/utils/schema/schema.sql
+++ b/packages/utils/schema/schema.sql
@@ -20,7 +20,6 @@ CREATE TABLE users (
stripe_subscription_id VARCHAR(255),
stripe_subscription_status VARCHAR(255),
stripe_subscription_price_id VARCHAR(255),
- active_space_id UUID REFERENCES spaces(id) ON DELETE SET NULL;
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);
@@ -35,6 +34,10 @@ CREATE TABLE spaces (
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);
+-- Now that 'spaces' has been created, we can add the foreign key to 'users'
+ALTER TABLE users
+ADD COLUMN active_space_id UUID REFERENCES spaces(id) ON DELETE SET NULL;
+
-- Space members table definition (junction table for space to users many-to-many relationship)
CREATE TABLE space_members (
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
diff --git a/supabase/.gitignore b/supabase/.gitignore
new file mode 100644
index 000000000..773c7c3e0
--- /dev/null
+++ b/supabase/.gitignore
@@ -0,0 +1,3 @@
+# Supabase
+.branches
+.temp
diff --git a/supabase/config.toml b/supabase/config.toml
new file mode 100644
index 000000000..ca2c9a453
--- /dev/null
+++ b/supabase/config.toml
@@ -0,0 +1,72 @@
+# A string used to distinguish different Supabase projects on the same host. Defaults to the working
+# directory name when running `supabase init`.
+project_id = "utils"
+
+[api]
+# Port to use for the API URL.
+port = 54321
+# Schemas to expose in your API. Tables, views and stored procedures in this schema will get API
+# endpoints. public and storage are always included.
+schemas = ["public", "storage", "graphql_public"]
+# Extra schemas to add to the search_path of every request. public is always included.
+extra_search_path = ["public", "extensions"]
+# The maximum number of rows returns from a view, table, or stored procedure. Limits payload size
+# for accidental or malicious requests.
+max_rows = 1000
+
+[db]
+# Port to use for the local database URL.
+port = 54322
+# The database major version to use. This has to be the same as your remote database's. Run `SHOW
+# server_version;` on the remote database to check.
+major_version = 15
+
+[studio]
+# Port to use for Supabase Studio.
+port = 54323
+
+# Email testing server. Emails sent with the local dev setup are not actually sent - rather, they
+# are monitored, and you can view the emails that would have been sent from the web interface.
+[inbucket]
+# Port to use for the email testing server web interface.
+port = 54324
+smtp_port = 54325
+pop3_port = 54326
+
+[storage]
+# The maximum file size allowed (e.g. "5MB", "500KB").
+file_size_limit = "50MiB"
+
+[auth]
+# The base URL of your website. Used as an allow-list for redirects and for constructing URLs used
+# in emails.
+site_url = "http://localhost:3000"
+# A list of *exact* URLs that auth providers are permitted to redirect to post authentication.
+additional_redirect_urls = ["https://localhost:3000"]
+# How long tokens are valid for, in seconds. Defaults to 3600 (1 hour), maximum 604,800 seconds (one
+# week).
+jwt_expiry = 3600
+# Allow/disallow new user signups to your project.
+enable_signup = true
+
+[auth.email]
+# Allow/disallow new user signups via email to your project.
+enable_signup = true
+# If enabled, a user will be required to confirm any email change on both the old, and new email
+# addresses. If disabled, only the new email is required to confirm.
+double_confirm_changes = true
+# If enabled, users need to confirm their email address before signing in.
+enable_confirmations = false
+
+# Use an external OAuth provider. The full list of providers are: `apple`, `azure`, `bitbucket`,
+# `discord`, `facebook`, `github`, `gitlab`, `google`, `keycloak`, `linkedin`, `notion`, `twitch`,
+# `twitter`, `slack`, `spotify`, `workos`, `zoom`.
+[auth.external.apple]
+enabled = false
+client_id = ""
+secret = ""
+# Overrides the default auth redirectUrl.
+redirect_uri = ""
+# Overrides the default auth provider URL. Used to support self-hosted gitlab, single-tenant Azure,
+# or any other third-party OIDC providers.
+url = ""
diff --git a/supabase/seed.sql b/supabase/seed.sql
new file mode 100644
index 000000000..960f5796c
--- /dev/null
+++ b/supabase/seed.sql
@@ -0,0 +1,374 @@
+CREATE EXTENSION IF NOT EXISTS "pgcrypto";
+CREATE EXTENSION IF NOT EXISTS "citext";
+
+CREATE TYPE user_role AS ENUM ('owner', 'admin', 'member');
+
+---------- ### TABLES ### ----------
+---------- #################### ----------
+---------- #################### ----------
+---------- #################### ----------
+
+CREATE TABLE users (
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
+ full_name VARCHAR(255),
+ email VARCHAR(255) UNIQUE NOT NULL,
+ onboarding_questions JSONB,
+ avatar_url TEXT,
+ video_quota INT NOT NULL DEFAULT 10,
+ videos_created INT NOT NULL DEFAULT 0,
+ stripe_customer_id VARCHAR(255),
+ stripe_subscription_id VARCHAR(255),
+ stripe_subscription_status VARCHAR(255),
+ stripe_subscription_price_id VARCHAR(255),
+ created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
+ updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
+);
+
+-- Spaces table definition
+CREATE TABLE spaces (
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
+ name VARCHAR(255) NOT NULL,
+ owner_id UUID REFERENCES users(id) ON DELETE SET NULL,
+ metadata JSONB,
+ created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
+ updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
+);
+
+-- Now that 'spaces' has been created, we can add the foreign key to 'users'
+ALTER TABLE users
+ADD COLUMN active_space_id UUID REFERENCES spaces(id) ON DELETE SET NULL;
+
+-- Space members table definition (junction table for space to users many-to-many relationship)
+CREATE TABLE space_members (
+ user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
+ space_id UUID NOT NULL REFERENCES spaces(id) ON DELETE CASCADE,
+ role user_role NOT NULL,
+ PRIMARY KEY (user_id, space_id)
+);
+
+-- Videos table definition
+CREATE TABLE videos (
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
+ owner_id UUID REFERENCES users(id) ON DELETE CASCADE,
+ name VARCHAR(255) NOT NULL default 'My Video: ' || to_char(now(), 'YYYY-MM-DD HH24:MI:SS'),
+ s3_url TEXT default null, -- AWS S3 URL to the video
+ aws_region VARCHAR(255) default null, -- AWS region where the video is stored
+ aws_bucket VARCHAR(255) default null, -- AWS bucket where the video is stored
+ thumbnail_url TEXT, -- URL to the video's thumbnail
+ duration NUMERIC, -- The length of the video
+ metadata JSONB, -- Any additional metadata about the video
+ complete BOOLEAN NOT NULL DEFAULT true, -- Whether video is fully processed
+ is_public BOOLEAN NOT NULL DEFAULT false, -- Can be shared to spaces
+ created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
+ updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
+);
+
+-- Shared videos table definition (junction table for video to spaces many-to-many relationship)
+CREATE TABLE shared_videos (
+ video_id UUID REFERENCES videos(id) ON DELETE CASCADE,
+ space_id UUID REFERENCES spaces(id) ON DELETE CASCADE,
+ shared_by_user_id UUID REFERENCES users(id) ON DELETE CASCADE, -- Who shared the video
+ shared_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
+ PRIMARY KEY (video_id, space_id) -- Ensure a video can't be shared to the same space more than once
+);
+
+---------- ### RLS POLICIES ### ----------
+---------- #################### ----------
+---------- #################### ----------
+---------- #################### ----------
+
+---------- ### USERS ###
+ALTER TABLE users ENABLE ROW LEVEL SECURITY;
+
+CREATE POLICY user_view_own_data ON users
+FOR SELECT USING (id = auth.uid());
+
+CREATE POLICY user_modify_own_data ON users
+FOR UPDATE USING (id = auth.uid());
+
+CREATE POLICY user_delete_own_data ON users
+FOR DELETE USING (id = auth.uid());
+
+---------- ### SPACES ###
+ALTER TABLE spaces ENABLE ROW LEVEL SECURITY;
+
+CREATE POLICY space_view ON spaces
+FOR SELECT USING (
+ EXISTS (SELECT 1 FROM space_members WHERE space_members.space_id = spaces.id AND space_members.user_id = auth.uid())
+ OR owner_id = auth.uid()
+);
+
+CREATE POLICY space_modify ON spaces
+FOR UPDATE USING (
+ (owner_id = auth.uid()) OR
+ (EXISTS (SELECT 1 FROM space_members WHERE space_members.space_id = spaces.id AND space_members.user_id = auth.uid() AND space_members.role = 'admin'))
+);
+
+CREATE POLICY space_delete ON spaces
+FOR DELETE USING (
+ (owner_id = auth.uid()) OR
+ (EXISTS (SELECT 1 FROM space_members WHERE space_members.space_id = spaces.id AND space_members.user_id = auth.uid() AND space_members.role = 'admin'))
+);
+
+CREATE POLICY space_create ON spaces
+FOR INSERT WITH CHECK (
+ (owner_id = auth.uid())
+);
+
+---------- ### SPACE_MEMBERS ###
+
+ALTER TABLE space_members ENABLE ROW LEVEL SECURITY;
+-- Allow insertion into space members if you are the owner or an admin of that space
+CREATE POLICY space_members_insert ON space_members
+FOR INSERT WITH CHECK (
+ EXISTS (
+ SELECT 1 FROM spaces
+ WHERE spaces.id = space_members.space_id
+ AND spaces.owner_id = auth.uid()
+ ) OR
+ EXISTS (
+ SELECT 1 FROM space_members adm
+ WHERE
+ adm.space_id = space_members.space_id
+ AND adm.user_id = auth.uid()
+ AND adm.role = 'admin'
+ )
+);
+
+-- Allow deletion of space members if you are the owner or an admin of that space
+CREATE POLICY space_members_delete ON space_members
+FOR DELETE USING (
+ EXISTS (
+ SELECT 1 FROM spaces
+ WHERE spaces.id = space_members.space_id
+ AND spaces.owner_id = auth.uid()
+ ) OR
+ EXISTS (
+ SELECT 1 FROM space_members adm
+ WHERE
+ adm.space_id = space_members.space_id
+ AND adm.user_id = auth.uid()
+ AND adm.role = 'admin'
+ )
+);
+
+---------- ### VIDEOS ###
+ALTER TABLE videos ENABLE ROW LEVEL SECURITY;
+
+CREATE POLICY insert_video ON videos
+FOR INSERT WITH CHECK (
+ auth.uid() = owner_id
+);
+
+-- Policy to allow users to view their own videos or view videos shared with spaces they belong to
+CREATE POLICY select_video ON videos
+FOR SELECT USING (
+ auth.uid() = owner_id OR
+ EXISTS (
+ SELECT 1
+ FROM shared_videos
+ JOIN space_members ON shared_videos.space_id = space_members.space_id
+ WHERE shared_videos.video_id = videos.id AND space_members.user_id = auth.uid()
+ )
+);
+
+-- Policy to allow users to update their own videos
+CREATE POLICY update_video ON videos
+FOR UPDATE USING (
+ auth.uid() = owner_id
+);
+
+-- Policy to allow users to delete their own videos and to adjust the remaining quota if needed
+CREATE POLICY delete_video ON videos
+FOR DELETE USING (
+ auth.uid() = owner_id
+);
+
+---------- ### SHARED VIDEOS ###
+ALTER TABLE shared_videos ENABLE ROW LEVEL SECURITY;
+
+-- Policy to view shared videos within the spaces a user belongs to
+CREATE POLICY view_shared_videos ON shared_videos
+FOR SELECT USING (
+ EXISTS (
+ SELECT 1
+ FROM space_members
+ WHERE
+ space_members.space_id = shared_videos.space_id AND
+ space_members.user_id = auth.uid()
+ )
+);
+
+-- Policy to share a video with a space
+CREATE POLICY share_video ON shared_videos
+FOR INSERT WITH CHECK (
+ EXISTS (
+ SELECT 1
+ FROM videos
+ WHERE videos.id = shared_videos.video_id
+ AND videos.owner_id = auth.uid()
+ ) AND
+ EXISTS (
+ SELECT 1
+ FROM space_members
+ WHERE space_members.space_id = shared_videos.space_id
+ AND space_members.user_id = auth.uid()
+ )
+);
+
+-- Policy to update shared video records
+CREATE POLICY update_shared_videos ON shared_videos
+FOR UPDATE USING (
+ auth.uid() = shared_videos.shared_by_user_id
+);
+
+-- Adding a policy for deletion if it's allowed to unshare videos,
+-- it grants permission to the user who shared the video
+CREATE POLICY delete_shared_videos ON shared_videos
+FOR DELETE USING (
+ auth.uid() = shared_videos.shared_by_user_id
+);
+
+---------- ### FUNCTIONS ### ----------
+---------- #################### ----------
+---------- #################### ----------
+---------- #################### ----------
+
+/**
+* This trigger automatically creates a user entry when a new user signs up via Supabase Auth.
+*/
+create or replace function public.handle_new_user()
+returns trigger as $$
+begin
+ insert into public.users (id, email, full_name, avatar_url)
+ values (new.id, new.email, new.raw_user_meta_data->>'full_name', new.raw_user_meta_data->>'avatar_url');
+
+ return new;
+end;
+$$ language plpgsql security definer;
+create trigger on_auth_user_created
+ after insert on auth.users
+ for each row execute procedure public.handle_new_user();
+
+-- Function to increment 'videos_created' counter
+CREATE OR REPLACE FUNCTION increment_videos_created()
+RETURNS TRIGGER AS $$
+BEGIN
+ UPDATE users
+ SET videos_created = videos_created + 1
+ WHERE id = NEW.owner_id;
+ RETURN NEW;
+END;
+$$ LANGUAGE plpgsql;
+
+-- Trigger to fire the above function after a video is successfully inserted
+CREATE TRIGGER increment_videos_created_trigger
+AFTER INSERT ON videos
+FOR EACH ROW
+EXECUTE FUNCTION increment_videos_created();
+
+-- Function to decrement 'videos_created' counter
+CREATE OR REPLACE FUNCTION decrement_videos_created()
+RETURNS TRIGGER AS $$
+BEGIN
+ UPDATE users
+ SET videos_created = videos_created - 1
+ WHERE id = OLD.owner_id;
+ RETURN OLD;
+END;
+$$ LANGUAGE plpgsql;
+
+-- Trigger to fire the above function before a video is successfully deleted
+CREATE TRIGGER decrement_videos_created_trigger
+BEFORE DELETE ON videos
+FOR EACH ROW
+EXECUTE FUNCTION decrement_videos_created();
+
+-- Automatically update the 'updated_at' column when a row is updated
+CREATE OR REPLACE FUNCTION update_timestamp()
+RETURNS TRIGGER AS $$
+BEGIN
+ NEW.updated_at = NOW();
+ RETURN NEW;
+END;
+$$ LANGUAGE plpgsql;
+
+CREATE TRIGGER update_videos_timestamp
+BEFORE UPDATE ON videos
+FOR EACH ROW
+EXECUTE FUNCTION update_timestamp();
+
+CREATE TRIGGER update_spaces_timestamp
+BEFORE UPDATE ON spaces
+FOR EACH ROW
+EXECUTE FUNCTION update_timestamp();
+
+CREATE TRIGGER update_users_timestamp
+BEFORE UPDATE ON users
+FOR EACH ROW
+EXECUTE FUNCTION update_timestamp();
+
+-- Validate space membership
+CREATE OR REPLACE FUNCTION validate_space_membership()
+RETURNS TRIGGER AS $$
+BEGIN
+ IF NOT EXISTS (
+ SELECT 1 FROM space_members
+ WHERE user_id = NEW.shared_by_user_id AND space_id = NEW.space_id
+ )
+ THEN
+ RAISE EXCEPTION 'User is not a member of the space.';
+ END IF;
+ RETURN NEW;
+END;
+$$ LANGUAGE plpgsql;
+
+CREATE TRIGGER validate_space_membership_trigger
+BEFORE INSERT ON shared_videos
+FOR EACH ROW
+EXECUTE FUNCTION validate_space_membership();
+
+-- Enforce video quota
+CREATE OR REPLACE FUNCTION enforce_video_quota()
+RETURNS TRIGGER AS $$
+BEGIN
+ IF (SELECT videos_created FROM users WHERE id = NEW.owner_id) >= (SELECT video_quota FROM users WHERE id = NEW.owner_id) THEN
+ RAISE EXCEPTION 'You have reached your video quota.';
+ END IF;
+ RETURN NEW;
+END;
+$$ LANGUAGE plpgsql;
+
+CREATE TRIGGER enforce_video_quota_trigger
+BEFORE INSERT ON videos
+FOR EACH ROW
+EXECUTE FUNCTION enforce_video_quota();
+
+-- Function to set the active space if it's the user's first space or if active_space_id is null
+CREATE OR REPLACE FUNCTION set_active_space()
+RETURNS TRIGGER AS $$
+BEGIN
+ IF (SELECT active_space_id FROM users WHERE id = NEW.owner_id) IS NULL THEN
+ UPDATE users SET active_space_id = NEW.id WHERE id = NEW.owner_id;
+ END IF;
+ RETURN NEW;
+END;
+$$ LANGUAGE plpgsql;
+
+-- Trigger to automatically set active space when a new space is created
+CREATE TRIGGER set_active_space_trigger
+AFTER INSERT ON spaces
+FOR EACH ROW
+EXECUTE FUNCTION set_active_space();
+
+---------- ### INDEXES ### ----------
+---------- #################### ----------
+---------- #################### ----------
+---------- #################### ----------
+
+CREATE INDEX idx_users_active_space ON users(active_space_id);
+CREATE INDEX idx_videos_owner ON videos(owner_id);
+CREATE INDEX idx_shared_videos_video_id ON shared_videos(video_id);
+CREATE INDEX idx_shared_videos_space_id ON shared_videos(space_id);
+CREATE INDEX idx_space_members_user_id ON space_members(user_id);
+CREATE INDEX idx_space_members_space_id ON space_members(space_id);
\ No newline at end of file