From 6569ef8d89acb2ee98b7338d74db5b314bc6bc79 Mon Sep 17 00:00:00 2001 From: Jonathan Grim Date: Sat, 24 Feb 2024 15:17:16 +0100 Subject: [PATCH 1/5] Improve developing docs and bring in timeout migration --- docs/developing.md | 48 ++++++++++++++++++- .../20240223203511_remote_schema.sql | 47 ++++++++++++++++++ 2 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 supabase/migrations/20240223203511_remote_schema.sql diff --git a/docs/developing.md b/docs/developing.md index b37a15f..741f5ae 100644 --- a/docs/developing.md +++ b/docs/developing.md @@ -2,9 +2,53 @@ Local development of Playabl uses Docker to run a containerized version of the backend alongside a local copy of the UI. The Netlify service is also supported locally and can be run to test serverless functions. There is also some AWS in the codebase, but Netlify is typically preferred for serverless functions. -## Supabase setup +## Environment setup -The local workflow is based on Supabase's guide for [local development](https://supabase.com/docs/guides/cli/local-development) with supabase CLI. +_Note: these steps have only been tested on M1 Mac. If you want to develop on Linux or Windows you may need to alter these commands_ + +**Prereqs** + +- Docker +- Node v20 or greater + +The local workflow is based on Supabase's guide for [local development](https://supabase.com/docs/guides/cli/local-development) with supabase CLI. It is **highly** recommended you read this guide and at least be familiar with its contents before starting. This guide has all the info you need for how to do various tasks like starting services, stopping services, resetting the DB, creating a migration, etc. + +**Install packages** + +`npm install` + +**Start supabase** + +`npx supabase start` + +This will pull the docker images and start the containers. At the end of it, you will be given an output of URLs and keys to use for local development. Some of these will need to be set in a `.env` file. Here is a sample output: + +``` +Started supabase local development setup. + +API URL: http://127.0.0.1:54321 +GraphQL URL: http://127.0.0.1:54321/graphql/v1 +DB URL: postgresql://postgres:postgres@127.0.0.1:54322/postgres +Studio URL: http://127.0.0.1:54323 +Inbucket URL: http://127.0.0.1:54324 +JWT secret: super-secret-jwt-token-with-at-least-32-characters-long +anon key: +service_role key: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImV4cCI6MTk4MzgxMjk5Nn0.EGIM96RAZx35lJzdJsyH-qQwv8Hdp7fsn3W0YpN81IU +``` + +**Set env variables** + +Set these values in a `.env` file at the root of the project: + +``` +VITE_SUPABASE_URL= +VITE_SUPABASE_ANON_KEY= +SUPABASE_URL= +SUPABASE_SERVICE_ROLE= +VITE_PLAYABL_URL=http://localhost:8888 +``` + +You may see other environement variables in the code, but these aren't typically necessary for normal development. If you want to work on something that does require it, like AWS, I'll work with you to figure out the right values. ### Storage (optional) diff --git a/supabase/migrations/20240223203511_remote_schema.sql b/supabase/migrations/20240223203511_remote_schema.sql new file mode 100644 index 0000000..ede6930 --- /dev/null +++ b/supabase/migrations/20240223203511_remote_schema.sql @@ -0,0 +1,47 @@ +drop trigger if exists "send_notification" on "public"."notifications"; + +set check_function_bodies = off; + +CREATE OR REPLACE FUNCTION public.dispatch_unread_notifications_emails() + RETURNS void + LANGUAGE plpgsql + SECURITY DEFINER +AS $function$declare + row record; +begin +for row in select p.id, n.email, p.username, count(n.email) + from notifications n + inner join profiles p + on p.id = n.user_id + and (p.email_preferences->>'email_enabled')::boolean is true + and (p.email_preferences->>'unread_notifications_enabled')::boolean is true + and n.read = false + and n.created_at > current_timestamp - interval '1 day' + group by (p.id, n.email, p.username) + loop + perform net.http_post( + 'https://app.playabl.io/.netlify/functions/unreadNotificationsEmail', + jsonb_build_object( + 'user_id', + row.id, + 'email', + row.email, + 'username', + row.username, + 'count', + row.count + ), + '{}', + jsonb_build_object( + 'Content-Type', 'application/json', + 'Authorization', '2gCvGM1WXv2i5Y3LzDT6kZfUqzimkEzvmwn31CtiyWg2' + ), + 5000 + ); + end loop; +end;$function$ +; + +CREATE TRIGGER send_notification AFTER INSERT ON public.notifications FOR EACH ROW EXECUTE FUNCTION supabase_functions.http_request('https://app.playabl.io/.netlify/functions/notify', 'POST', '{"Content-type":"application/json","Authorization":"2gCvGM1WXv2i5Y3LzDT6kZfUqzimkEzvmwn31CtiyWg2"}', '{}', '5000'); + + From 583a8c43793ec31af812fbff610721406d040cf3 Mon Sep 17 00:00:00 2001 From: Jonathan Grim Date: Sat, 24 Feb 2024 15:18:44 +0100 Subject: [PATCH 2/5] fix anon key example --- docs/developing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/developing.md b/docs/developing.md index 741f5ae..f15cbec 100644 --- a/docs/developing.md +++ b/docs/developing.md @@ -32,7 +32,7 @@ DB URL: postgresql://postgres:postgres@127.0.0.1:54322/postgres Studio URL: http://127.0.0.1:54323 Inbucket URL: http://127.0.0.1:54324 JWT secret: super-secret-jwt-token-with-at-least-32-characters-long -anon key: +anon key: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0 service_role key: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImV4cCI6MTk4MzgxMjk5Nn0.EGIM96RAZx35lJzdJsyH-qQwv8Hdp7fsn3W0YpN81IU ``` From 31812b0ab4a75ae98fa414e88ddebe95f9ac16a6 Mon Sep 17 00:00:00 2001 From: Jonathan Grim Date: Sat, 24 Feb 2024 17:00:24 +0100 Subject: [PATCH 3/5] type-gen --- src/typings/supabase.ts | 76 ++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/src/typings/supabase.ts b/src/typings/supabase.ts index 7dbe1d4..f7ab581 100644 --- a/src/typings/supabase.ts +++ b/src/typings/supabase.ts @@ -6,7 +6,7 @@ export type Json = | { [key: string]: Json | undefined } | Json[] -export interface Database { +export type Database = { public: { Tables: { access_levels: { @@ -47,7 +47,7 @@ export interface Database { isOneToOne: false referencedRelation: "communities" referencedColumns: ["id"] - } + }, ] } communities: { @@ -148,7 +148,7 @@ export interface Database { isOneToOne: false referencedRelation: "profiles" referencedColumns: ["id"] - } + }, ] } community_access: { @@ -194,7 +194,7 @@ export interface Database { isOneToOne: false referencedRelation: "profiles" referencedColumns: ["id"] - } + }, ] } community_events: { @@ -247,7 +247,7 @@ export interface Database { isOneToOne: false referencedRelation: "communities" referencedColumns: ["id"] - } + }, ] } community_invites: { @@ -276,7 +276,7 @@ export interface Database { isOneToOne: false referencedRelation: "communities" referencedColumns: ["id"] - } + }, ] } community_membership_requests: { @@ -315,7 +315,7 @@ export interface Database { isOneToOne: false referencedRelation: "profiles" referencedColumns: ["id"] - } + }, ] } community_memberships: { @@ -361,7 +361,7 @@ export interface Database { isOneToOne: false referencedRelation: "profiles" referencedColumns: ["id"] - } + }, ] } draft_games: { @@ -402,7 +402,7 @@ export interface Database { isOneToOne: false referencedRelation: "profiles" referencedColumns: ["id"] - } + }, ] } flags: { @@ -455,7 +455,7 @@ export interface Database { isOneToOne: false referencedRelation: "games" referencedColumns: ["id"] - } + }, ] } games: { @@ -534,7 +534,7 @@ export interface Database { isOneToOne: false referencedRelation: "community_events" referencedColumns: ["id"] - } + }, ] } integrations: { @@ -584,7 +584,7 @@ export interface Database { isOneToOne: false referencedRelation: "communities" referencedColumns: ["id"] - } + }, ] } messages: { @@ -664,7 +664,7 @@ export interface Database { isOneToOne: false referencedRelation: "profiles" referencedColumns: ["id"] - } + }, ] } profiles: { @@ -794,7 +794,7 @@ export interface Database { isOneToOne: false referencedRelation: "games" referencedColumns: ["id"] - } + }, ] } } @@ -978,7 +978,7 @@ export interface Database { isOneToOne: false referencedRelation: "buckets" referencedColumns: ["id"] - } + }, ] } } @@ -1057,7 +1057,7 @@ export type Tables< TableName extends PublicTableNameOrOptions extends { schema: keyof Database } ? keyof (Database[PublicTableNameOrOptions["schema"]]["Tables"] & Database[PublicTableNameOrOptions["schema"]]["Views"]) - : never = never + : never = never, > = PublicTableNameOrOptions extends { schema: keyof Database } ? (Database[PublicTableNameOrOptions["schema"]]["Tables"] & Database[PublicTableNameOrOptions["schema"]]["Views"])[TableName] extends { @@ -1066,14 +1066,14 @@ export type Tables< ? R : never : PublicTableNameOrOptions extends keyof (Database["public"]["Tables"] & - Database["public"]["Views"]) - ? (Database["public"]["Tables"] & - Database["public"]["Views"])[PublicTableNameOrOptions] extends { - Row: infer R - } - ? R + Database["public"]["Views"]) + ? (Database["public"]["Tables"] & + Database["public"]["Views"])[PublicTableNameOrOptions] extends { + Row: infer R + } + ? R + : never : never - : never export type TablesInsert< PublicTableNameOrOptions extends @@ -1081,7 +1081,7 @@ export type TablesInsert< | { schema: keyof Database }, TableName extends PublicTableNameOrOptions extends { schema: keyof Database } ? keyof Database[PublicTableNameOrOptions["schema"]]["Tables"] - : never = never + : never = never, > = PublicTableNameOrOptions extends { schema: keyof Database } ? Database[PublicTableNameOrOptions["schema"]]["Tables"][TableName] extends { Insert: infer I @@ -1089,12 +1089,12 @@ export type TablesInsert< ? I : never : PublicTableNameOrOptions extends keyof Database["public"]["Tables"] - ? Database["public"]["Tables"][PublicTableNameOrOptions] extends { - Insert: infer I - } - ? I + ? Database["public"]["Tables"][PublicTableNameOrOptions] extends { + Insert: infer I + } + ? I + : never : never - : never export type TablesUpdate< PublicTableNameOrOptions extends @@ -1102,7 +1102,7 @@ export type TablesUpdate< | { schema: keyof Database }, TableName extends PublicTableNameOrOptions extends { schema: keyof Database } ? keyof Database[PublicTableNameOrOptions["schema"]]["Tables"] - : never = never + : never = never, > = PublicTableNameOrOptions extends { schema: keyof Database } ? Database[PublicTableNameOrOptions["schema"]]["Tables"][TableName] extends { Update: infer U @@ -1110,12 +1110,12 @@ export type TablesUpdate< ? U : never : PublicTableNameOrOptions extends keyof Database["public"]["Tables"] - ? Database["public"]["Tables"][PublicTableNameOrOptions] extends { - Update: infer U - } - ? U + ? Database["public"]["Tables"][PublicTableNameOrOptions] extends { + Update: infer U + } + ? U + : never : never - : never export type Enums< PublicEnumNameOrOptions extends @@ -1123,10 +1123,10 @@ export type Enums< | { schema: keyof Database }, EnumName extends PublicEnumNameOrOptions extends { schema: keyof Database } ? keyof Database[PublicEnumNameOrOptions["schema"]]["Enums"] - : never = never + : never = never, > = PublicEnumNameOrOptions extends { schema: keyof Database } ? Database[PublicEnumNameOrOptions["schema"]]["Enums"][EnumName] : PublicEnumNameOrOptions extends keyof Database["public"]["Enums"] - ? Database["public"]["Enums"][PublicEnumNameOrOptions] - : never + ? Database["public"]["Enums"][PublicEnumNameOrOptions] + : never From d754b045058b920b92721b1f4bb8bb77ec43f606 Mon Sep 17 00:00:00 2001 From: Jonathan Grim Date: Sat, 24 Feb 2024 17:08:14 +0100 Subject: [PATCH 4/5] add codeowners --- .github/.CODEOWNERS | 1 + 1 file changed, 1 insertion(+) create mode 100644 .github/.CODEOWNERS diff --git a/.github/.CODEOWNERS b/.github/.CODEOWNERS new file mode 100644 index 0000000..b3be1a7 --- /dev/null +++ b/.github/.CODEOWNERS @@ -0,0 +1 @@ +@main/maintainters \ No newline at end of file From 8aec269d0ab04d2f30208ea58004cc52ef68e0a0 Mon Sep 17 00:00:00 2001 From: Jonathan Grim Date: Sat, 24 Feb 2024 17:09:19 +0100 Subject: [PATCH 5/5] typo --- .github/.CODEOWNERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/.CODEOWNERS b/.github/.CODEOWNERS index b3be1a7..db3e4ff 100644 --- a/.github/.CODEOWNERS +++ b/.github/.CODEOWNERS @@ -1 +1 @@ -@main/maintainters \ No newline at end of file +@main/maintainers \ No newline at end of file