@rocapine/community 0.21.1 → 0.21.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rocapine/community",
3
- "version": "0.21.1",
3
+ "version": "0.21.4",
4
4
  "description": "CLI to install, upgrade and adopt the Rocapine Community SDK's Supabase backend (migrations, Edge Functions) into a project.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -1,6 +1,9 @@
1
1
  // Shared Supabase admin client, caller identity helpers + JSON response helper.
2
2
 
3
3
  import { createClient } from "jsr:@supabase/supabase-js@2";
4
+ import { corsHeaders } from "jsr:@supabase/supabase-js@2/cors";
5
+
6
+ export { corsHeaders };
4
7
 
5
8
  export const SUPABASE_URL = Deno.env.get("SUPABASE_URL")!;
6
9
 
@@ -70,9 +73,15 @@ export async function callerUserId(req: Request): Promise<string | null> {
70
73
 
71
74
  // The client consumes responses via supabase.functions.invoke, which only
72
75
  // parses the body as JSON when the Content-Type says so. Without this header
73
- // invoke returns a raw string and verdict.status is undefined.
76
+ // invoke returns a raw string and verdict.status is undefined. The CORS
77
+ // headers let a browser (the app's web build) read the response.
74
78
  export const json = (body: unknown, status = 200) =>
75
79
  new Response(JSON.stringify(body), {
76
80
  status,
77
- headers: { "Content-Type": "application/json" },
81
+ headers: { ...corsHeaders, "Content-Type": "application/json" },
78
82
  });
83
+
84
+ // A browser preflights every functions.invoke call with OPTIONS before the
85
+ // POST. Answer it before reading the body, or the browser never sends the call.
86
+ export const preflight = (req: Request) =>
87
+ req.method === "OPTIONS" ? new Response("ok", { headers: corsHeaders }) : null;
@@ -4,6 +4,7 @@
4
4
  "jsr:@std/assert@1": "1.0.19",
5
5
  "jsr:@std/internal@^1.0.12": "1.0.14",
6
6
  "jsr:@supabase/supabase-js@2": "2.116.0",
7
+ "jsr:@supabase/supabase-js@2.116.0": "2.116.0",
7
8
  "npm:@supabase/auth-js@2.116.0": "2.116.0",
8
9
  "npm:@supabase/functions-js@2.116.0": "2.116.0",
9
10
  "npm:@supabase/postgrest-js@2.116.0": "2.116.0",
@@ -10,7 +10,7 @@
10
10
  // `status = 'pending'` so a row the author deleted mid-flight is never
11
11
  // resurrected.
12
12
 
13
- import { adminClient, callerUserId, isServiceCaller, json } from "../_shared/client.ts";
13
+ import { adminClient, callerUserId, isServiceCaller, json, preflight } from "../_shared/client.ts";
14
14
  import { fetchImagePath, POST_IMAGES_BUCKET, postImageUrl } from "../_shared/images.ts";
15
15
  import { assertModerationConfigured, moderateItem } from "../_shared/moderation.ts";
16
16
  import { postToSlack } from "../_shared/slack.ts";
@@ -20,6 +20,8 @@ assertModerationConfigured();
20
20
  const supabase = adminClient();
21
21
 
22
22
  Deno.serve(async (req) => {
23
+ const pre = preflight(req);
24
+ if (pre) return pre;
23
25
  const { kind, id } = (await req.json().catch(() => ({}))) as {
24
26
  kind?: "post" | "comment";
25
27
  id?: string;
@@ -9,7 +9,7 @@
9
9
  // time, so partial application is not observable in practice.
10
10
 
11
11
  import { createClient } from "jsr:@supabase/supabase-js@2";
12
- import { adminClient, json, SUPABASE_URL } from "../_shared/client.ts";
12
+ import { adminClient, json, preflight, SUPABASE_URL } from "../_shared/client.ts";
13
13
  import { assertModerationConfigured, moderateInput } from "../_shared/moderation.ts";
14
14
  import { containsAppName, isReservedHandle, suggestHandle } from "../_shared/names.ts";
15
15
 
@@ -18,6 +18,8 @@ assertModerationConfigured();
18
18
  const admin = adminClient();
19
19
 
20
20
  Deno.serve(async (req) => {
21
+ const pre = preflight(req);
22
+ if (pre) return pre;
21
23
  const caller = createClient(SUPABASE_URL, Deno.env.get("SUPABASE_ANON_KEY")!, {
22
24
  global: { headers: { Authorization: req.headers.get("Authorization") ?? "" } },
23
25
  });
@@ -0,0 +1,58 @@
1
+ -- community-sdk core/020_draft_poll_limits.sql (schema v1)
2
+ -- Drafts follow the project's poll limits (polls/002, poll_set_limits) instead of the fixed 2 to 4
3
+ -- options of up to 60 characters. check_post_draft (core/015) is redefined with that one change; a
4
+ -- backend without polls/002 keeps the old limits. Requires core/015. Fully idempotent.
5
+
6
+ create or replace function public.check_post_draft(d post_drafts, p_at timestamptz default null)
7
+ returns void
8
+ language plpgsql security definer set search_path = public
9
+ as $$
10
+ declare
11
+ author profiles;
12
+ n int;
13
+ min_n int := 2;
14
+ max_n int := 4;
15
+ max_len int := 60;
16
+ begin
17
+ select * into author from profiles where id = d.author_id;
18
+ if not found or not author.is_house then raise exception 'not_a_team_account'; end if;
19
+ if author.is_banned then raise exception 'author_banned'; end if;
20
+ if (d.pin or d.notify) and not author.is_admin then raise exception 'not_admin'; end if;
21
+
22
+ -- Like posts_content_check: text is optional when there is an image.
23
+ if char_length(d.content) > 2000 or (char_length(d.content) = 0 and d.image_path is null) then
24
+ raise exception 'invalid_content';
25
+ end if;
26
+ -- Mirrors posts_image_check and posts_image_path_format (images/001). The image lives
27
+ -- in the author's storage folder; the dashboard moves it when the author changes.
28
+ if d.image_path is not null and (coalesce(d.image_width, 0) <= 0 or coalesce(d.image_height, 0) <= 0
29
+ or d.image_path !~ '^[0-9a-f-]{36}/[A-Za-z0-9_-]+\.jpg$'
30
+ or split_part(d.image_path, '/', 1) <> d.author_id::text) then
31
+ raise exception 'invalid_image';
32
+ end if;
33
+ n := coalesce(array_length(d.poll_options, 1), 0);
34
+ if d.poll_options is not null and to_regprocedure('public.poll_limits()') is not null then
35
+ select l.min_options, l.max_options, l.option_max_length into min_n, max_n, max_len
36
+ from public.poll_limits() l;
37
+ end if;
38
+ if d.poll_options is not null and (n not between min_n and max_n
39
+ or exists (select 1 from unnest(d.poll_options) l where coalesce(char_length(l), 0) not between 1 and max_len)) then
40
+ raise exception 'invalid_poll';
41
+ end if;
42
+ -- Mirrors posts_attachment_check (attachments/001); coalesce because a null check result would pass.
43
+ if d.attachment is not null and not coalesce(jsonb_typeof(d.attachment) = 'object'
44
+ and d.attachment ? 'type' and d.attachment ? 'id'
45
+ and jsonb_typeof(d.attachment -> 'type') = 'string' and jsonb_typeof(d.attachment -> 'id') = 'string'
46
+ and octet_length(d.attachment::text) <= 512, false) then
47
+ raise exception 'invalid_attachment';
48
+ end if;
49
+ if (d.image_path is not null)::int + (d.poll_options is not null)::int + (d.attachment is not null)::int > 1 then
50
+ raise exception 'one_extra_only';
51
+ end if;
52
+ -- Topic rules live in the dashboard; here a draft only needs one, so the cron never posts without it.
53
+ if d.topic is null then raise exception 'invalid_topic'; end if;
54
+ perform check_post_draft_window(d, p_at);
55
+ end;
56
+ $$;
57
+
58
+ revoke execute on function public.check_post_draft(post_drafts, timestamptz) from public, anon, authenticated;
@@ -0,0 +1,109 @@
1
+ -- community-sdk polls/002_poll_limits.sql (schema v1)
2
+ -- Poll limits become a per-project setting (default 2 to 4 options of up to 60
3
+ -- characters, the limits of polls/001). Set them from the SQL editor (service
4
+ -- role), with the same values as the app's `modules.polls` config:
5
+ -- select public.poll_set_limits(2, 7, 100);
6
+ -- Bounds: 2 <= min_options <= max_options <= 10, option_max_length 1..200.
7
+ -- The option limits move from CHECK constraints to a trigger that an importer can
8
+ -- switch off (alter table public.poll_options disable trigger user), like
9
+ -- core/018 did for post length. Fully idempotent.
10
+
11
+ create table if not exists public.poll_settings (
12
+ id boolean primary key default true check (id),
13
+ min_options int not null default 2,
14
+ max_options int not null default 4,
15
+ option_max_length int not null default 60,
16
+ check (2 <= min_options and min_options <= max_options and max_options <= 10),
17
+ check (option_max_length between 1 and 200)
18
+ );
19
+ insert into public.poll_settings (id) values (true) on conflict do nothing;
20
+ alter table public.poll_settings enable row level security;
21
+ revoke all on public.poll_settings from public, anon, authenticated;
22
+
23
+ -- The check constraints reject values outside the bounds above.
24
+ create or replace function public.poll_set_limits(p_min int, p_max int, p_max_length int)
25
+ returns void
26
+ language sql security definer set search_path = public
27
+ as $$
28
+ insert into poll_settings (id, min_options, max_options, option_max_length)
29
+ values (true, p_min, p_max, p_max_length)
30
+ on conflict (id) do update set
31
+ min_options = excluded.min_options,
32
+ max_options = excluded.max_options,
33
+ option_max_length = excluded.option_max_length;
34
+ $$;
35
+
36
+ revoke execute on function public.poll_set_limits(int, int, int) from public, anon, authenticated;
37
+ grant execute on function public.poll_set_limits(int, int, int) to service_role;
38
+
39
+ -- The effective limits (defaults when the row is missing). Readable by members:
40
+ -- create_poll_post and the option trigger run as the caller.
41
+ create or replace function public.poll_limits(
42
+ out min_options int, out max_options int, out option_max_length int)
43
+ language sql stable security definer set search_path = public
44
+ as $$
45
+ select coalesce(s.min_options, 2), coalesce(s.max_options, 4), coalesce(s.option_max_length, 60)
46
+ from (select 1) one left join poll_settings s on true;
47
+ $$;
48
+
49
+ revoke execute on function public.poll_limits() from public, anon;
50
+ grant execute on function public.poll_limits() to authenticated, service_role;
51
+
52
+ create or replace function public.enforce_poll_option_limits()
53
+ returns trigger language plpgsql set search_path = '' as $$
54
+ declare
55
+ lim record;
56
+ begin
57
+ select * into lim from public.poll_limits();
58
+ if new.idx >= lim.max_options then
59
+ raise exception 'a poll has at most % options', lim.max_options using errcode = 'check_violation';
60
+ end if;
61
+ if char_length(new.label) > lim.option_max_length then
62
+ raise exception 'a poll option is longer than % characters', lim.option_max_length
63
+ using errcode = 'check_violation';
64
+ end if;
65
+ return new;
66
+ end $$;
67
+
68
+ drop trigger if exists poll_options_limits on public.poll_options;
69
+ create trigger poll_options_limits
70
+ before insert or update of idx, label on public.poll_options
71
+ for each row execute function public.enforce_poll_option_limits();
72
+
73
+ -- The checks keep only the minimum.
74
+ alter table public.poll_options drop constraint if exists poll_options_idx_check;
75
+ alter table public.poll_options add constraint poll_options_idx_check check (idx >= 0);
76
+ alter table public.poll_options drop constraint if exists poll_options_label_check;
77
+ alter table public.poll_options add constraint poll_options_label_check check (char_length(label) >= 1);
78
+
79
+ -- Same as polls/001, with the option count read from the settings.
80
+ create or replace function public.create_poll_post(
81
+ p_topic text,
82
+ p_content text,
83
+ p_options text[]
84
+ ) returns uuid
85
+ language plpgsql set search_path = public
86
+ as $$
87
+ declare
88
+ new_id uuid;
89
+ n int := coalesce(array_length(p_options, 1), 0);
90
+ lim record;
91
+ i int;
92
+ begin
93
+ select * into lim from public.poll_limits();
94
+ if n < lim.min_options or n > lim.max_options then
95
+ raise exception 'a poll needs % to % options', lim.min_options, lim.max_options;
96
+ end if;
97
+ insert into public.posts (author_id, topic, content)
98
+ values (auth.uid(), p_topic, p_content)
99
+ returning id into new_id;
100
+ for i in 1..n loop
101
+ insert into public.poll_options (post_id, idx, label)
102
+ values (new_id, i - 1, p_options[i]);
103
+ end loop;
104
+ return new_id;
105
+ end;
106
+ $$;
107
+
108
+ revoke execute on function public.create_poll_post(text, text, text[]) from public, anon;
109
+ grant execute on function public.create_poll_post(text, text, text[]) to authenticated;