@rocapine/community 0.17.0 → 0.18.0
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/README.md +3 -3
- package/lib/index.js +2 -2
- package/lib/install-shared.d.ts +1 -1
- package/lib/install-shared.js +1 -0
- package/package.json +1 -1
- package/templates/migrations/presence/001_presence.sql +139 -0
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ version. The binary is named `community`.
|
|
|
13
13
|
## `init`
|
|
14
14
|
|
|
15
15
|
```bash
|
|
16
|
-
npx @rocapine/community init [--modules core,push,polls,reaction,inbox,translation,images,attachments,badges,streaks] [--project-url <url>] [--anon-key <key>] [--dir supabase]
|
|
16
|
+
npx @rocapine/community init [--modules core,push,polls,reaction,inbox,translation,images,attachments,badges,presence,streaks] [--project-url <url>] [--anon-key <key>] [--dir supabase]
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
Copies the migrations and Edge Functions for the requested modules into
|
|
@@ -21,10 +21,10 @@ Copies the migrations and Edge Functions for the requested modules into
|
|
|
21
21
|
relative to the current directory), and writes a `community-sdk.json`
|
|
22
22
|
manifest at the repo root recording what was installed.
|
|
23
23
|
|
|
24
|
-
- `--modules` — comma-separated (`core,push,polls,reaction,inbox,translation,images,attachments,badges,streaks`). `core`
|
|
24
|
+
- `--modules` — comma-separated (`core,push,polls,reaction,inbox,translation,images,attachments,badges,presence,streaks`). `core`
|
|
25
25
|
is always implied even if omitted. Omit the flag entirely to install every
|
|
26
26
|
module. Regardless of the order you type them, modules are installed
|
|
27
|
-
`core → push → polls → reaction → inbox → translation → images → attachments → badges → streaks` — this order is load-bearing:
|
|
27
|
+
`core → push → polls → reaction → inbox → translation → images → attachments → badges → presence → streaks` — this order is load-bearing:
|
|
28
28
|
the inbox module's reaction trigger is guarded at install time and must
|
|
29
29
|
find the reaction module's table already present, so reaction has to land
|
|
30
30
|
first. `streaks` (deprecated: it moves an existing streak install onto badges) always brings `badges`. Requesting `inbox` without `reaction`, or `translation` without `polls`, prints a (non-fatal) warning (reactions missing from the inbox; poll labels left untranslated).
|
package/lib/index.js
CHANGED
|
@@ -60,7 +60,7 @@ program
|
|
|
60
60
|
program
|
|
61
61
|
.command("init")
|
|
62
62
|
.description("Copy the community backend into a Supabase project: migrations, Edge Functions, and a community-sdk.json manifest")
|
|
63
|
-
.option("--modules <modules>", "comma-separated module list (core,push,polls,reaction,inbox,translation,images,attachments,badges,streaks) — core is always implied; defaults to all modules")
|
|
63
|
+
.option("--modules <modules>", "comma-separated module list (core,push,polls,reaction,inbox,translation,images,attachments,badges,presence,streaks) — core is always implied; defaults to all modules")
|
|
64
64
|
.option("--project-url <url>", "Supabase project URL, e.g. https://<ref>.supabase.co — optional, only fills in the printed settings seed statement")
|
|
65
65
|
.option("--anon-key <key>", "Supabase anon key — optional, only fills in the printed settings seed statement")
|
|
66
66
|
.option("--dir <dir>", "target Supabase directory, relative to the current directory", "supabase")
|
|
@@ -103,7 +103,7 @@ program
|
|
|
103
103
|
.command("adopt")
|
|
104
104
|
.description("Register an already-live community backend (e.g. an app that hand-rolled its own before this SDK existed) with community-sdk.json, without copying any files")
|
|
105
105
|
.requiredOption("--schema-version <n>", "the community-sdk schema version this app's live backend corresponds to")
|
|
106
|
-
.requiredOption("--modules <modules>", "comma-separated module list (core,push,polls,reaction,inbox,translation,images,attachments,badges,streaks) — core is always implied")
|
|
106
|
+
.requiredOption("--modules <modules>", "comma-separated module list (core,push,polls,reaction,inbox,translation,images,attachments,badges,presence,streaks) — core is always implied")
|
|
107
107
|
.option("--dir <dir>", "Supabase directory this app would use, relative to the current directory", "supabase")
|
|
108
108
|
.action(async (opts) => {
|
|
109
109
|
try {
|
package/lib/install-shared.d.ts
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* single source of truth for module names, order, and the function sets
|
|
11
11
|
* each module pulls in.
|
|
12
12
|
*/
|
|
13
|
-
export declare const MODULE_ORDER: readonly ["core", "push", "polls", "reaction", "inbox", "translation", "images", "attachments", "badges", "streaks"];
|
|
13
|
+
export declare const MODULE_ORDER: readonly ["core", "push", "polls", "reaction", "inbox", "translation", "images", "attachments", "badges", "presence", "streaks"];
|
|
14
14
|
export type Module = (typeof MODULE_ORDER)[number];
|
|
15
15
|
export declare function functionsForModules(modules: readonly Module[]): string[];
|
|
16
16
|
/**
|
package/lib/install-shared.js
CHANGED
package/package.json
CHANGED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
-- community-sdk presence/001_presence.sql (schema v1)
|
|
2
|
+
-- Presence module: a green dot on online members, a live online count, and a
|
|
3
|
+
-- per-member switch to hide. The app calls touch_presence() while it is open; a
|
|
4
|
+
-- member is online when their last touch is within the project's grace window
|
|
5
|
+
-- (presence_settings, default 5 minutes).
|
|
6
|
+
--
|
|
7
|
+
-- The timestamp is never readable by clients (only user_id and visible are, and
|
|
8
|
+
-- only for the member's own row): readers get a boolean through online_member_ids()
|
|
9
|
+
-- or a number through online_member_count(). Both skip banned and (for the ids)
|
|
10
|
+
-- hidden and mutually blocked members. The caller counts as online (whoever calls the
|
|
11
|
+
-- readers has the app open) even before their first touch lands, unless banned;
|
|
12
|
+
-- a hidden caller is counted but never listed.
|
|
13
|
+
-- Fully idempotent.
|
|
14
|
+
|
|
15
|
+
create table if not exists public.member_presence (
|
|
16
|
+
user_id uuid primary key references public.profiles(id) on delete cascade,
|
|
17
|
+
last_active_at timestamptz not null default now(),
|
|
18
|
+
visible boolean not null default true
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
create index if not exists member_presence_last_active_idx
|
|
22
|
+
on public.member_presence (last_active_at);
|
|
23
|
+
|
|
24
|
+
alter table public.member_presence enable row level security;
|
|
25
|
+
|
|
26
|
+
-- A member reads only their own row, and only these columns (user_id because the
|
|
27
|
+
-- owner filters on it: column privileges apply to WHERE columns too).
|
|
28
|
+
drop policy if exists "read own presence" on public.member_presence;
|
|
29
|
+
create policy "read own presence" on public.member_presence
|
|
30
|
+
for select to authenticated using (user_id = auth.uid());
|
|
31
|
+
|
|
32
|
+
revoke all on public.member_presence from public, anon, authenticated;
|
|
33
|
+
grant select (user_id, visible) on public.member_presence to authenticated;
|
|
34
|
+
|
|
35
|
+
create or replace function public.touch_presence()
|
|
36
|
+
returns void
|
|
37
|
+
language sql security definer set search_path = public
|
|
38
|
+
as $$
|
|
39
|
+
insert into member_presence (user_id, last_active_at)
|
|
40
|
+
select auth.uid(), now()
|
|
41
|
+
where auth.uid() is not null
|
|
42
|
+
on conflict (user_id) do update set last_active_at = excluded.last_active_at;
|
|
43
|
+
$$;
|
|
44
|
+
|
|
45
|
+
create or replace function public.set_presence_visible(p_visible boolean)
|
|
46
|
+
returns void
|
|
47
|
+
language sql security definer set search_path = public
|
|
48
|
+
as $$
|
|
49
|
+
-- A first row starts long idle: only touch_presence makes someone online.
|
|
50
|
+
insert into member_presence (user_id, last_active_at, visible)
|
|
51
|
+
select auth.uid(), 'epoch', p_visible
|
|
52
|
+
where auth.uid() is not null
|
|
53
|
+
on conflict (user_id) do update set visible = excluded.visible;
|
|
54
|
+
$$;
|
|
55
|
+
|
|
56
|
+
-- The grace window is a per-project setting, never a client parameter: if callers
|
|
57
|
+
-- chose it, sweeping 1..60 would tell them when each member was last active.
|
|
58
|
+
-- One row, readable only by the security definer RPCs below. Change it from the
|
|
59
|
+
-- SQL editor (service role): select public.presence_set_grace_minutes(10);
|
|
60
|
+
create table if not exists public.presence_settings (
|
|
61
|
+
id boolean primary key default true check (id),
|
|
62
|
+
grace_minutes int not null default 5 check (grace_minutes between 1 and 60)
|
|
63
|
+
);
|
|
64
|
+
insert into public.presence_settings (id) values (true) on conflict do nothing;
|
|
65
|
+
alter table public.presence_settings enable row level security;
|
|
66
|
+
revoke all on public.presence_settings from public, anon, authenticated;
|
|
67
|
+
|
|
68
|
+
-- The check constraint rejects values outside 1..60.
|
|
69
|
+
create or replace function public.presence_set_grace_minutes(p_minutes int)
|
|
70
|
+
returns void
|
|
71
|
+
language sql security definer set search_path = public
|
|
72
|
+
as $$
|
|
73
|
+
insert into presence_settings (id, grace_minutes) values (true, p_minutes)
|
|
74
|
+
on conflict (id) do update set grace_minutes = excluded.grace_minutes;
|
|
75
|
+
$$;
|
|
76
|
+
|
|
77
|
+
-- Earlier drafts took the grace as a parameter.
|
|
78
|
+
drop function if exists public.online_member_count(int);
|
|
79
|
+
drop function if exists public.online_member_ids(uuid[], int);
|
|
80
|
+
|
|
81
|
+
-- Members online within the grace window, hidden members included: a count
|
|
82
|
+
-- reveals no one. The caller counts even without a fresh row (union dedups). A banned
|
|
83
|
+
-- caller gets 0.
|
|
84
|
+
create or replace function public.online_member_count()
|
|
85
|
+
returns int
|
|
86
|
+
language sql stable security definer set search_path = public
|
|
87
|
+
as $$
|
|
88
|
+
select count(*)::int from (
|
|
89
|
+
select mp.user_id
|
|
90
|
+
from member_presence mp
|
|
91
|
+
join profiles p on p.id = mp.user_id
|
|
92
|
+
where not p.is_banned
|
|
93
|
+
and mp.last_active_at > now() - make_interval(
|
|
94
|
+
mins => coalesce((select grace_minutes from presence_settings), 5))
|
|
95
|
+
union
|
|
96
|
+
select p.id from profiles p where p.id = auth.uid() and not p.is_banned
|
|
97
|
+
) online
|
|
98
|
+
where not exists (select 1 from profiles b where b.id = auth.uid() and b.is_banned);
|
|
99
|
+
$$;
|
|
100
|
+
|
|
101
|
+
-- Which of the first 200 of p_ids are online, visible, not banned and not
|
|
102
|
+
-- blocked either way with the caller. The caller is online when asking, unless
|
|
103
|
+
-- they hid themselves (a missing row means visible). A banned caller gets '{}'.
|
|
104
|
+
create or replace function public.online_member_ids(p_ids uuid[])
|
|
105
|
+
returns uuid[]
|
|
106
|
+
language sql stable security definer set search_path = public
|
|
107
|
+
as $$
|
|
108
|
+
select coalesce(array_agg(id), '{}') from (
|
|
109
|
+
select mp.user_id as id
|
|
110
|
+
from member_presence mp
|
|
111
|
+
join profiles p on p.id = mp.user_id
|
|
112
|
+
where mp.user_id = any (p_ids[1:200])
|
|
113
|
+
and mp.visible
|
|
114
|
+
and not p.is_banned
|
|
115
|
+
and mp.last_active_at > now() - make_interval(
|
|
116
|
+
mins => coalesce((select grace_minutes from presence_settings), 5))
|
|
117
|
+
and not (mp.user_id = any ((select public.blocked_member_ids())::uuid[]))
|
|
118
|
+
union
|
|
119
|
+
select p.id
|
|
120
|
+
from profiles p
|
|
121
|
+
left join member_presence mp on mp.user_id = p.id
|
|
122
|
+
where p.id = auth.uid()
|
|
123
|
+
and p.id = any (p_ids[1:200])
|
|
124
|
+
and not p.is_banned
|
|
125
|
+
and coalesce(mp.visible, true)
|
|
126
|
+
) online
|
|
127
|
+
where not exists (select 1 from profiles b where b.id = auth.uid() and b.is_banned);
|
|
128
|
+
$$;
|
|
129
|
+
|
|
130
|
+
revoke execute on function public.touch_presence() from public, anon;
|
|
131
|
+
revoke execute on function public.set_presence_visible(boolean) from public, anon;
|
|
132
|
+
revoke execute on function public.online_member_count() from public, anon;
|
|
133
|
+
revoke execute on function public.online_member_ids(uuid[]) from public, anon;
|
|
134
|
+
grant execute on function public.touch_presence() to authenticated;
|
|
135
|
+
grant execute on function public.set_presence_visible(boolean) to authenticated;
|
|
136
|
+
grant execute on function public.online_member_count() to authenticated;
|
|
137
|
+
grant execute on function public.online_member_ids(uuid[]) to authenticated;
|
|
138
|
+
revoke execute on function public.presence_set_grace_minutes(int) from public, anon, authenticated;
|
|
139
|
+
grant execute on function public.presence_set_grace_minutes(int) to service_role;
|