@marktiderman/genesis-cli 1.19.0 → 1.21.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 +98 -1
- package/dist/cli.js +682 -74
- package/dist/cli.js.map +1 -1
- package/dist/doctor.d.ts +1 -1
- package/dist/doctor.js +270 -8
- package/dist/doctor.js.map +1 -1
- package/dist/eslint.cjs +99 -0
- package/dist/eslint.cjs.map +1 -0
- package/dist/eslint.d.cts +68 -0
- package/dist/eslint.d.ts +68 -0
- package/dist/eslint.js +72 -0
- package/dist/eslint.js.map +1 -0
- package/dist/genesis-catalog.json +86 -14
- package/dist/index.js +278 -16
- package/dist/index.js.map +1 -1
- package/dist/starter/README.md +17 -5
- package/dist/starter/__tests__/components/setup.ts +11 -0
- package/dist/starter/__tests__/starter.test.ts +19 -0
- package/dist/starter/eslint.config.js +8 -1
- package/dist/starter/nativewind-env.d.ts +1 -0
- package/dist/starter/package.json +5 -3
- package/dist/starter/src/lib/app.ts +13 -5
- package/dist/starter/src/lib/define-starter.ts +1 -1
- package/dist/starter/src/lib/devtools-overlay.ts +3 -4
- package/dist/starter/src/lib/native-modules.ts +5 -1
- package/dist/starter/src/lib/offline.ts +14 -0
- package/dist/starter/supabase/migrations/20261024000750_support_default_tenant.sql +18 -0
- package/dist/starter/supabase/migrations/20261024000800_genesis_support_tickets.sql +800 -0
- package/dist/starter/supabase/migrations/20261025000070_starter_tenancy.sql +26 -0
- package/dist/starter/supabase/migrations/20261025000080_genesis_chat_tenants.sql +716 -0
- package/dist/starter/supabase/tests/database/chat_people.test.sql +1 -1
- package/dist/starter/supabase/tests/database/genesis_chat.test.sql +10 -2
- package/dist/starter/supabase/tests/database/genesis_chat_coaching.test.sql +8 -0
- package/dist/starter/supabase/tests/database/genesis_chat_search_threads.test.sql +8 -0
- package/dist/starter/supabase/tests/database/genesis_chat_tenants.test.sql +259 -0
- package/dist/starter/supabase/tests/database/genesis_support.test.sql +15 -3
- package/dist/starter/supabase/tests/database/genesis_support_findings.test.sql +10 -0
- package/dist/starter/supabase/tests/database/genesis_support_tickets.test.sql +327 -0
- package/dist/starter/supabase/tests/database/starter_tenancy.test.sql +19 -0
- package/dist/vite.js +270 -8
- package/dist/vite.js.map +1 -1
- package/package.json +18 -4
|
@@ -0,0 +1,716 @@
|
|
|
1
|
+
-- Genesis Chat 0004: every conversation belongs to a tenant; subjects are typed per kind
|
|
2
|
+
--
|
|
3
|
+
-- Builds on 0001–0003 (never edited once released).
|
|
4
|
+
--
|
|
5
|
+
-- 1. Tenants. genesis_chat.conversations.tenant_id (uuid, not null, no foreign key: add your own
|
|
6
|
+
-- to your tenants table in a later migration if you want deletes to cascade). Who belongs to
|
|
7
|
+
-- which tenant is your app's answer, through the same hook genesis-supabase's viewer people
|
|
8
|
+
-- use (0016):
|
|
9
|
+
--
|
|
10
|
+
-- private.genesis_viewer_people(viewer uuid) returns table (tenant_id uuid, person_id uuid)
|
|
11
|
+
--
|
|
12
|
+
-- A single-tenant app returns its one tenant id for everyone. Without the hook nobody belongs
|
|
13
|
+
-- to any tenant: people can't start conversations, and no one observes one.
|
|
14
|
+
--
|
|
15
|
+
-- 2. Subjects. A kind's conversations about your records (a ticket, a cohort) register their
|
|
16
|
+
-- subject type with a resolver that names the record's tenant:
|
|
17
|
+
--
|
|
18
|
+
-- select genesis_chat.register_subject('support', 'ticket', 'private.ticket_tenant(text)',
|
|
19
|
+
-- p_aliases => array['support_report']);
|
|
20
|
+
-- -- private.ticket_tenant(subject_id text) returns uuid: the record's tenant, null if none.
|
|
21
|
+
--
|
|
22
|
+
-- ensure_conversation and start_conversation take the tenant from it (and refuse a subject
|
|
23
|
+
-- it doesn't know). An alias is an older name for the same type: calls using it reach the
|
|
24
|
+
-- same conversation, and new ones are stored under the type's own name. An unregistered
|
|
25
|
+
-- subject needs `p_tenant_id`, unless a resolver named for its type exists
|
|
26
|
+
-- (`private.genesis_chat_resolve_<subject_type>(subject_id text) returns uuid`), which
|
|
27
|
+
-- ensure_conversation and the backfill below use.
|
|
28
|
+
--
|
|
29
|
+
-- 3. Starting one. start_conversation's members must all share the tenant, which is the
|
|
30
|
+
-- subject's, or the one they share (`p_tenant_id` chooses when they share several). People
|
|
31
|
+
-- with no tenant in common can't start one. add_members adds only people of the
|
|
32
|
+
-- conversation's tenant. A direct conversation is one per pair per tenant.
|
|
33
|
+
--
|
|
34
|
+
-- A kind registered with `p_cross_tenant => true` lets people with no tenant in common talk:
|
|
35
|
+
-- such a conversation takes its starter's tenant (`p_tenant_id` chooses among the starter's),
|
|
36
|
+
-- and its members may come from anywhere. The trade-off: it lives in that one tenant, so that
|
|
37
|
+
-- tenant's erase or export includes it, the other people's messages too. Such a kind has no
|
|
38
|
+
-- observer check (register_kind refuses one), so no tenant's staff read it.
|
|
39
|
+
--
|
|
40
|
+
-- 4. Observers. A kind's observer check is asked only about people of the conversation's tenant,
|
|
41
|
+
-- and never about anyone who is or was a member of it: the person who opened a ticket never
|
|
42
|
+
-- reads its internal notes, even if they are also staff. A kind whose check already decides by
|
|
43
|
+
-- the record's own tenant registers with `p_observer_tenant_gate => false`, or, when it was
|
|
44
|
+
-- registered before this migration, defines `private.genesis_chat_ungated_<kind>()` returning
|
|
45
|
+
-- true (genesis-support does): chat then leaves the tenant to that check, so app-wide staff
|
|
46
|
+
-- your tenancy hook doesn't list keep reading, and still never a member. Registering a kind
|
|
47
|
+
-- again without the argument keeps its gate.
|
|
48
|
+
--
|
|
49
|
+
-- Existing conversations get their tenant in this order: your optional hook
|
|
50
|
+
-- `private.genesis_chat_conversation_tenant(conversation_id uuid) returns uuid`; for one about a
|
|
51
|
+
-- record, a resolver named for its subject type, `private.genesis_chat_resolve_<subject_type>
|
|
52
|
+
-- (subject_id text) returns uuid`, when one exists (a block that owns the type ships it before
|
|
53
|
+
-- this, since register_subject is new here); then the one tenant all its members share, then its
|
|
54
|
+
-- creator's only tenant, then the only tenant of any one of its members. If one is still left without, this migration stops and says how many: add the
|
|
55
|
+
-- hook (it runs only here), then apply it again.
|
|
56
|
+
--
|
|
57
|
+
-- Those last two fall back to one person's tenant, which not every member may share: like a
|
|
58
|
+
-- cross-tenant conversation, that tenant's erase or export then includes the others' messages.
|
|
59
|
+
-- Place such conversations yourself with the hook when that matters.
|
|
60
|
+
--
|
|
61
|
+
-- Apply this after the migration of any block that ships a subject resolver (genesis-support's
|
|
62
|
+
-- 0006): `genesis migrate` and `upgrade` order them so, from the block's `runsBefore`.
|
|
63
|
+
--
|
|
64
|
+
-- Ship it together with genesis-support's 0004 if you use genesis-support's chat: support's 0001
|
|
65
|
+
-- opens its conversations without a tenant, which this refuses.
|
|
66
|
+
--
|
|
67
|
+
-- Use: `genesis add chat` copies this after 0003. Verify: tests/0004_genesis_chat_tenants.test.sql.
|
|
68
|
+
|
|
69
|
+
-- ── Tenants ───────────────────────────────────────────────────────────────────
|
|
70
|
+
|
|
71
|
+
-- The tenants a person belongs to, from the app's hook. Empty without the hook, when it fails or
|
|
72
|
+
-- returns the wrong shape, or for nobody.
|
|
73
|
+
create or replace function genesis_chat.tenants_of(p_user_id uuid)
|
|
74
|
+
returns setof uuid
|
|
75
|
+
language plpgsql
|
|
76
|
+
stable
|
|
77
|
+
security definer
|
|
78
|
+
set search_path = ''
|
|
79
|
+
as $$
|
|
80
|
+
begin
|
|
81
|
+
if p_user_id is null or to_regprocedure('private.genesis_viewer_people(uuid)') is null then
|
|
82
|
+
return;
|
|
83
|
+
end if;
|
|
84
|
+
return query execute
|
|
85
|
+
'select distinct r.tenant_id::uuid from private.genesis_viewer_people($1) r where r.tenant_id is not null'
|
|
86
|
+
using p_user_id;
|
|
87
|
+
exception when others then
|
|
88
|
+
return;
|
|
89
|
+
end;
|
|
90
|
+
$$;
|
|
91
|
+
|
|
92
|
+
-- The tenants every one of these people belongs to.
|
|
93
|
+
create or replace function genesis_chat.shared_tenants(p_user_ids uuid[])
|
|
94
|
+
returns setof uuid
|
|
95
|
+
language sql
|
|
96
|
+
stable
|
|
97
|
+
security definer
|
|
98
|
+
set search_path = ''
|
|
99
|
+
as $$
|
|
100
|
+
select t
|
|
101
|
+
from (select distinct u from unnest(p_user_ids) u where u is not null) people
|
|
102
|
+
cross join lateral genesis_chat.tenants_of(people.u) t
|
|
103
|
+
group by t
|
|
104
|
+
having count(*) = (select count(distinct v) from unnest(p_user_ids) v where v is not null)
|
|
105
|
+
$$;
|
|
106
|
+
|
|
107
|
+
revoke execute on function genesis_chat.tenants_of(uuid), genesis_chat.shared_tenants(uuid[]) from public, anon, authenticated;
|
|
108
|
+
grant execute on function genesis_chat.tenants_of(uuid), genesis_chat.shared_tenants(uuid[]) to service_role;
|
|
109
|
+
|
|
110
|
+
comment on function genesis_chat.tenants_of(uuid) is
|
|
111
|
+
'The tenants a person belongs to, from the app''s private.genesis_viewer_people(viewer) hook; empty without it. Genesis Chat.';
|
|
112
|
+
|
|
113
|
+
-- ── Kinds that cross tenants ──────────────────────────────────────────────────
|
|
114
|
+
|
|
115
|
+
alter table genesis_chat.kinds add column cross_tenant boolean not null default false;
|
|
116
|
+
alter table genesis_chat.kinds add column observer_tenant_gate boolean not null default true;
|
|
117
|
+
comment on column genesis_chat.kinds.observer_tenant_gate is
|
|
118
|
+
'Ask the observer check only about people of the conversation''s tenant (on by default). Off for a kind whose own check already decides by the record''s tenant (support''s, by the ticket''s), so app-wide staff the tenancy hook doesn''t list are not shut out.';
|
|
119
|
+
|
|
120
|
+
-- A kind registered before this migration (a block's earlier migration, which can't pass the new
|
|
121
|
+
-- argument yet) turns the gate off by convention: `private.genesis_chat_ungated_<kind>() returns
|
|
122
|
+
-- boolean`, true. The name fits Postgres's 63-character limit for any kind name.
|
|
123
|
+
create or replace function genesis_chat.conventional_observer_gate(p_kind text)
|
|
124
|
+
returns boolean
|
|
125
|
+
language plpgsql
|
|
126
|
+
stable
|
|
127
|
+
security definer
|
|
128
|
+
set search_path = ''
|
|
129
|
+
as $$
|
|
130
|
+
declare
|
|
131
|
+
fn regprocedure;
|
|
132
|
+
ungated boolean;
|
|
133
|
+
begin
|
|
134
|
+
if p_kind is null or p_kind !~ '^[a-z][a-z0-9_]{0,39}$' then return null; end if;
|
|
135
|
+
fn := to_regprocedure(format('private.%I()', 'genesis_chat_ungated_' || p_kind));
|
|
136
|
+
if fn is null or (select prorettype from pg_proc where oid = fn) <> 'boolean'::regtype then return null; end if;
|
|
137
|
+
execute format('select %s()', fn::regproc) into ungated;
|
|
138
|
+
return case when ungated then false end;
|
|
139
|
+
end;
|
|
140
|
+
$$;
|
|
141
|
+
|
|
142
|
+
revoke execute on function genesis_chat.conventional_observer_gate(text) from public, anon, authenticated;
|
|
143
|
+
grant execute on function genesis_chat.conventional_observer_gate(text) to service_role;
|
|
144
|
+
|
|
145
|
+
update genesis_chat.kinds set observer_tenant_gate = false
|
|
146
|
+
where genesis_chat.conventional_observer_gate(kind) is false;
|
|
147
|
+
|
|
148
|
+
comment on column genesis_chat.kinds.cross_tenant is
|
|
149
|
+
'People with no tenant in common may start one; it takes the starter''s tenant. Never with an observer check.';
|
|
150
|
+
|
|
151
|
+
drop function genesis_chat.register_kind(text, text, integer, text, text, boolean, regprocedure, boolean, regprocedure, interval);
|
|
152
|
+
|
|
153
|
+
create function genesis_chat.register_kind(
|
|
154
|
+
p_kind text,
|
|
155
|
+
p_label text,
|
|
156
|
+
p_max_members integer default null,
|
|
157
|
+
p_who_posts text default 'members',
|
|
158
|
+
p_membership text default 'app',
|
|
159
|
+
p_threads boolean default true,
|
|
160
|
+
p_observer_check regprocedure default null,
|
|
161
|
+
p_observers_post boolean default false,
|
|
162
|
+
p_start_check regprocedure default null,
|
|
163
|
+
p_edit_window interval default null,
|
|
164
|
+
p_cross_tenant boolean default false,
|
|
165
|
+
p_observer_tenant_gate boolean default null
|
|
166
|
+
)
|
|
167
|
+
returns void
|
|
168
|
+
language plpgsql
|
|
169
|
+
security definer
|
|
170
|
+
set search_path = ''
|
|
171
|
+
as $$
|
|
172
|
+
begin
|
|
173
|
+
if p_observer_check is not null and (
|
|
174
|
+
pg_get_function_identity_arguments(p_observer_check) !~ '^[a-z_]* ?uuid, [a-z_]* ?uuid$'
|
|
175
|
+
or (select prorettype from pg_proc where oid = p_observer_check) <> 'boolean'::regtype) then
|
|
176
|
+
raise exception 'An observer check takes (conversation_id uuid, user_id uuid) and returns boolean' using errcode = '22023';
|
|
177
|
+
end if;
|
|
178
|
+
if p_start_check is not null and (
|
|
179
|
+
pg_get_function_identity_arguments(p_start_check) !~ '^[a-z_]* ?uuid\[\], [a-z_]* ?text, [a-z_]* ?text, [a-z_]* ?uuid$'
|
|
180
|
+
or (select prorettype from pg_proc where oid = p_start_check) <> 'boolean'::regtype) then
|
|
181
|
+
raise exception 'A start check takes (member_ids uuid[], subject_type text, subject_id text, user_id uuid) and returns boolean' using errcode = '22023';
|
|
182
|
+
end if;
|
|
183
|
+
if coalesce(p_cross_tenant, false) and p_observer_check is not null then
|
|
184
|
+
raise exception 'A kind that crosses tenants has no observers: no tenant''s staff may read it' using errcode = '22023';
|
|
185
|
+
end if;
|
|
186
|
+
insert into genesis_chat.kinds as k (kind, label, max_members, who_posts, membership, threads,
|
|
187
|
+
observer_check, observers_post, start_check, edit_window, cross_tenant, observer_tenant_gate)
|
|
188
|
+
values (p_kind, p_label, p_max_members, p_who_posts, p_membership, p_threads,
|
|
189
|
+
p_observer_check, p_observers_post, p_start_check, p_edit_window, coalesce(p_cross_tenant, false),
|
|
190
|
+
coalesce(p_observer_tenant_gate, genesis_chat.conventional_observer_gate(p_kind), true))
|
|
191
|
+
on conflict (kind) do update set
|
|
192
|
+
label = excluded.label, max_members = excluded.max_members, who_posts = excluded.who_posts,
|
|
193
|
+
membership = excluded.membership, threads = excluded.threads, observer_check = excluded.observer_check,
|
|
194
|
+
observers_post = excluded.observers_post, start_check = excluded.start_check,
|
|
195
|
+
edit_window = excluded.edit_window, cross_tenant = excluded.cross_tenant,
|
|
196
|
+
observer_tenant_gate = coalesce(p_observer_tenant_gate, k.observer_tenant_gate), updated_at = now();
|
|
197
|
+
end;
|
|
198
|
+
$$;
|
|
199
|
+
|
|
200
|
+
revoke execute on function genesis_chat.register_kind(text, text, integer, text, text, boolean, regprocedure, boolean, regprocedure, interval, boolean, boolean) from public, anon, authenticated;
|
|
201
|
+
grant execute on function genesis_chat.register_kind(text, text, integer, text, text, boolean, regprocedure, boolean, regprocedure, interval, boolean, boolean) to service_role;
|
|
202
|
+
|
|
203
|
+
comment on function genesis_chat.register_kind(text, text, integer, text, text, boolean, regprocedure, boolean, regprocedure, interval, boolean, boolean) is
|
|
204
|
+
'Adds or replaces a kind of conversation and its rules. Run from a migration or the service role. Genesis Chat.';
|
|
205
|
+
|
|
206
|
+
-- ── Subjects ──────────────────────────────────────────────────────────────────
|
|
207
|
+
|
|
208
|
+
create table genesis_chat.subjects (
|
|
209
|
+
kind text not null references genesis_chat.kinds (kind) on update cascade on delete cascade,
|
|
210
|
+
subject_type text not null check (subject_type ~ '^[a-z][a-z0-9_]{0,39}$'),
|
|
211
|
+
resolver regprocedure not null,
|
|
212
|
+
aliases text[] not null default '{}',
|
|
213
|
+
created_at timestamptz not null default now(),
|
|
214
|
+
updated_at timestamptz not null default now(),
|
|
215
|
+
primary key (kind, subject_type)
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
comment on table genesis_chat.subjects is
|
|
219
|
+
'The record types a kind''s conversations are about, each with a resolver naming the record''s tenant. Written by register_subject. Genesis Chat.';
|
|
220
|
+
comment on column genesis_chat.subjects.resolver is 'f(subject_id text) returns uuid: the record''s tenant, null when there is no such record.';
|
|
221
|
+
comment on column genesis_chat.subjects.aliases is 'Older names for this type: calls using one reach the same conversations.';
|
|
222
|
+
|
|
223
|
+
alter table genesis_chat.subjects enable row level security;
|
|
224
|
+
create policy subjects_read on genesis_chat.subjects for select to authenticated using (true);
|
|
225
|
+
grant select on genesis_chat.subjects to authenticated;
|
|
226
|
+
grant select, insert, update, delete on genesis_chat.subjects to service_role;
|
|
227
|
+
|
|
228
|
+
create or replace function genesis_chat.register_subject(
|
|
229
|
+
p_kind text,
|
|
230
|
+
p_subject_type text,
|
|
231
|
+
p_resolver regprocedure,
|
|
232
|
+
p_aliases text[] default '{}'
|
|
233
|
+
)
|
|
234
|
+
returns void
|
|
235
|
+
language plpgsql
|
|
236
|
+
security definer
|
|
237
|
+
set search_path = ''
|
|
238
|
+
as $$
|
|
239
|
+
declare
|
|
240
|
+
aliases text[];
|
|
241
|
+
taken text;
|
|
242
|
+
begin
|
|
243
|
+
if p_resolver is null
|
|
244
|
+
or pg_get_function_identity_arguments(p_resolver) !~ '^[a-z_]* ?text$'
|
|
245
|
+
or (select prorettype from pg_proc where oid = p_resolver) <> 'uuid'::regtype then
|
|
246
|
+
raise exception 'A subject resolver takes (subject_id text) and returns uuid (the tenant)' using errcode = '22023';
|
|
247
|
+
end if;
|
|
248
|
+
select coalesce(array_agg(distinct a order by a), '{}') into aliases
|
|
249
|
+
from unnest(coalesce(p_aliases, '{}')) a where a is not null and a <> p_subject_type;
|
|
250
|
+
if exists (select 1 from unnest(aliases) a where a !~ '^[a-z][a-z0-9_]{0,39}$') then
|
|
251
|
+
raise exception 'A subject alias is a subject type name' using errcode = '22023';
|
|
252
|
+
end if;
|
|
253
|
+
-- One name means one type within a kind.
|
|
254
|
+
select n into taken from (
|
|
255
|
+
select s.subject_type as n from genesis_chat.subjects s
|
|
256
|
+
where s.kind = p_kind and s.subject_type <> p_subject_type
|
|
257
|
+
union all
|
|
258
|
+
select unnest(s.aliases) from genesis_chat.subjects s
|
|
259
|
+
where s.kind = p_kind and s.subject_type <> p_subject_type
|
|
260
|
+
) x where n = p_subject_type or n = any(aliases) limit 1;
|
|
261
|
+
if taken is not null then
|
|
262
|
+
raise exception 'The subject type % is already registered for kind %', taken, p_kind using errcode = '23505';
|
|
263
|
+
end if;
|
|
264
|
+
insert into genesis_chat.subjects as s (kind, subject_type, resolver, aliases)
|
|
265
|
+
values (p_kind, p_subject_type, p_resolver, aliases)
|
|
266
|
+
on conflict (kind, subject_type) do update set
|
|
267
|
+
resolver = excluded.resolver, aliases = excluded.aliases, updated_at = now();
|
|
268
|
+
end;
|
|
269
|
+
$$;
|
|
270
|
+
|
|
271
|
+
revoke execute on function genesis_chat.register_subject(text, text, regprocedure, text[]) from public, anon, authenticated;
|
|
272
|
+
grant execute on function genesis_chat.register_subject(text, text, regprocedure, text[]) to service_role;
|
|
273
|
+
|
|
274
|
+
comment on function genesis_chat.register_subject(text, text, regprocedure, text[]) is
|
|
275
|
+
'Registers a record type a kind''s conversations are about, with the resolver that names its tenant. Run from a migration or the service role. Genesis Chat.';
|
|
276
|
+
|
|
277
|
+
-- The registration a kind's subject type (or one of its aliases) names, if any.
|
|
278
|
+
create or replace function genesis_chat.subject_of(p_kind text, p_subject_type text)
|
|
279
|
+
returns genesis_chat.subjects
|
|
280
|
+
language sql
|
|
281
|
+
stable
|
|
282
|
+
security definer
|
|
283
|
+
set search_path = ''
|
|
284
|
+
as $$
|
|
285
|
+
select s.* from genesis_chat.subjects s
|
|
286
|
+
where s.kind = p_kind and (s.subject_type = p_subject_type or p_subject_type = any(s.aliases))
|
|
287
|
+
$$;
|
|
288
|
+
|
|
289
|
+
-- The subject's tenant, from its resolver. Null for an unregistered type or an unknown record.
|
|
290
|
+
create or replace function genesis_chat.subject_tenant(p_kind text, p_subject_type text, p_subject_id text)
|
|
291
|
+
returns uuid
|
|
292
|
+
language plpgsql
|
|
293
|
+
stable
|
|
294
|
+
security definer
|
|
295
|
+
set search_path = ''
|
|
296
|
+
as $$
|
|
297
|
+
declare
|
|
298
|
+
s genesis_chat.subjects;
|
|
299
|
+
tenant uuid;
|
|
300
|
+
begin
|
|
301
|
+
s := genesis_chat.subject_of(p_kind, p_subject_type);
|
|
302
|
+
if s.kind is null or p_subject_id is null then
|
|
303
|
+
return null;
|
|
304
|
+
end if;
|
|
305
|
+
execute format('select %s($1::text)', s.resolver::regproc) into tenant using p_subject_id;
|
|
306
|
+
return tenant;
|
|
307
|
+
end;
|
|
308
|
+
$$;
|
|
309
|
+
|
|
310
|
+
revoke execute on function genesis_chat.subject_of(text, text), genesis_chat.subject_tenant(text, text, text) from public, anon, authenticated;
|
|
311
|
+
|
|
312
|
+
-- The resolver a block ships for a subject type it hasn't registered (yet):
|
|
313
|
+
-- private.genesis_chat_resolve_<subject_type>(subject_id text) returns uuid. Null when the type
|
|
314
|
+
-- isn't a subject type name, there's no such function, or it doesn't return uuid.
|
|
315
|
+
create or replace function genesis_chat.conventional_resolver(p_subject_type text)
|
|
316
|
+
returns regprocedure
|
|
317
|
+
language plpgsql
|
|
318
|
+
stable
|
|
319
|
+
security definer
|
|
320
|
+
set search_path = ''
|
|
321
|
+
as $$
|
|
322
|
+
declare
|
|
323
|
+
fn regprocedure;
|
|
324
|
+
begin
|
|
325
|
+
if p_subject_type is null or p_subject_type !~ '^[a-z][a-z0-9_]{0,39}$' then
|
|
326
|
+
return null;
|
|
327
|
+
end if;
|
|
328
|
+
fn := to_regprocedure(format('private.%I(text)', 'genesis_chat_resolve_' || p_subject_type));
|
|
329
|
+
if fn is null or (select prorettype from pg_proc where oid = fn) <> 'uuid'::regtype then
|
|
330
|
+
return null;
|
|
331
|
+
end if;
|
|
332
|
+
return fn;
|
|
333
|
+
end;
|
|
334
|
+
$$;
|
|
335
|
+
|
|
336
|
+
revoke execute on function genesis_chat.conventional_resolver(text) from public, anon, authenticated;
|
|
337
|
+
grant execute on function genesis_chat.conventional_resolver(text) to service_role;
|
|
338
|
+
|
|
339
|
+
grant execute on function genesis_chat.subject_of(text, text), genesis_chat.subject_tenant(text, text, text) to service_role;
|
|
340
|
+
|
|
341
|
+
-- ── conversations.tenant_id, filled for what's there ─────────────────────────
|
|
342
|
+
|
|
343
|
+
alter table genesis_chat.conversations add column tenant_id uuid;
|
|
344
|
+
|
|
345
|
+
do $$
|
|
346
|
+
declare
|
|
347
|
+
hook boolean := to_regprocedure('private.genesis_chat_conversation_tenant(uuid)') is not null;
|
|
348
|
+
r record;
|
|
349
|
+
missing integer;
|
|
350
|
+
begin
|
|
351
|
+
if hook then
|
|
352
|
+
execute 'update genesis_chat.conversations c set tenant_id = private.genesis_chat_conversation_tenant(c.id) where c.tenant_id is null';
|
|
353
|
+
end if;
|
|
354
|
+
|
|
355
|
+
-- A record's tenant, from a resolver named for its subject type.
|
|
356
|
+
for r in select distinct c.subject_type, genesis_chat.conventional_resolver(c.subject_type) as fn
|
|
357
|
+
from genesis_chat.conversations c
|
|
358
|
+
where c.tenant_id is null and c.subject_type is not null
|
|
359
|
+
and genesis_chat.conventional_resolver(c.subject_type) is not null loop
|
|
360
|
+
execute format('update genesis_chat.conversations c set tenant_id = %s(c.subject_id)
|
|
361
|
+
where c.tenant_id is null and c.subject_type = $1', r.fn::regproc)
|
|
362
|
+
using r.subject_type;
|
|
363
|
+
end loop;
|
|
364
|
+
|
|
365
|
+
-- The one tenant all its members (past and present, observers aside) share.
|
|
366
|
+
update genesis_chat.conversations c set tenant_id = x.t
|
|
367
|
+
from (
|
|
368
|
+
select m.conversation_id, (array_agg(s))[1] as t
|
|
369
|
+
from (select conversation_id, array_agg(distinct user_id) as users
|
|
370
|
+
from genesis_chat.members where role <> 'observer' group by conversation_id) m
|
|
371
|
+
cross join lateral genesis_chat.shared_tenants(m.users) s
|
|
372
|
+
group by m.conversation_id
|
|
373
|
+
having count(*) = 1
|
|
374
|
+
) x
|
|
375
|
+
where c.id = x.conversation_id and c.tenant_id is null;
|
|
376
|
+
|
|
377
|
+
-- Its creator's only tenant.
|
|
378
|
+
update genesis_chat.conversations c set tenant_id = (select t from genesis_chat.tenants_of(c.created_by) t)
|
|
379
|
+
where c.tenant_id is null and c.created_by is not null
|
|
380
|
+
and (select count(*) from genesis_chat.tenants_of(c.created_by)) = 1;
|
|
381
|
+
|
|
382
|
+
-- The only tenant of any one of its members (a support conversation's one member).
|
|
383
|
+
update genesis_chat.conversations c set tenant_id = (
|
|
384
|
+
select (array_agg(distinct t))[1] from genesis_chat.members m
|
|
385
|
+
cross join lateral genesis_chat.tenants_of(m.user_id) t
|
|
386
|
+
where m.conversation_id = c.id and m.role <> 'observer'
|
|
387
|
+
and (select count(*) from genesis_chat.tenants_of(m.user_id)) = 1
|
|
388
|
+
having count(distinct t) = 1)
|
|
389
|
+
where c.tenant_id is null;
|
|
390
|
+
|
|
391
|
+
select count(*) into missing from genesis_chat.conversations where tenant_id is null;
|
|
392
|
+
if missing > 0 then
|
|
393
|
+
raise exception 'genesis-chat 0004: % conversation(s) have no tenant to take. Add private.genesis_viewer_people(viewer) (genesis-supabase 0016), or private.genesis_chat_conversation_tenant(conversation_id) for the ones it can''t place, then apply this again.', missing
|
|
394
|
+
using errcode = '23502';
|
|
395
|
+
end if;
|
|
396
|
+
end;
|
|
397
|
+
$$;
|
|
398
|
+
|
|
399
|
+
alter table genesis_chat.conversations alter column tenant_id set not null;
|
|
400
|
+
create index conversations_tenant_kind_idx on genesis_chat.conversations (tenant_id, kind, last_message_at desc);
|
|
401
|
+
|
|
402
|
+
-- A direct conversation is one per pair per tenant: its key names the tenant.
|
|
403
|
+
update genesis_chat.conversations c
|
|
404
|
+
set direct_key = c.kind || ':' || c.tenant_id::text || ':' || substr(c.direct_key, length(c.kind) + 2)
|
|
405
|
+
where c.direct_key is not null and c.direct_key not like c.kind || ':' || c.tenant_id::text || ':%';
|
|
406
|
+
|
|
407
|
+
-- ── Who observes ──────────────────────────────────────────────────────────────
|
|
408
|
+
|
|
409
|
+
-- The kind's observer check, asked only about people of the conversation's tenant who aren't,
|
|
410
|
+
-- and never were, members of it.
|
|
411
|
+
create or replace function genesis_chat.observes(p_conversation_id uuid, p_user_id uuid default auth.uid())
|
|
412
|
+
returns boolean
|
|
413
|
+
language plpgsql
|
|
414
|
+
stable
|
|
415
|
+
security definer
|
|
416
|
+
set search_path = ''
|
|
417
|
+
as $$
|
|
418
|
+
declare
|
|
419
|
+
check_fn regprocedure;
|
|
420
|
+
gate boolean;
|
|
421
|
+
tenant uuid;
|
|
422
|
+
result boolean;
|
|
423
|
+
begin
|
|
424
|
+
if p_user_id is null then
|
|
425
|
+
return false;
|
|
426
|
+
end if;
|
|
427
|
+
select k.observer_check, k.observer_tenant_gate, c.tenant_id into check_fn, gate, tenant
|
|
428
|
+
from genesis_chat.conversations c join genesis_chat.kinds k on k.kind = c.kind
|
|
429
|
+
where c.id = p_conversation_id;
|
|
430
|
+
if check_fn is null then
|
|
431
|
+
return false;
|
|
432
|
+
end if;
|
|
433
|
+
if exists (select 1 from genesis_chat.members m
|
|
434
|
+
where m.conversation_id = p_conversation_id and m.user_id = p_user_id and m.role <> 'observer') then
|
|
435
|
+
return false;
|
|
436
|
+
end if;
|
|
437
|
+
-- The kind's check decides by the record's own tenant when the kind says so (support's, by the ticket).
|
|
438
|
+
if gate and not exists (select 1 from genesis_chat.tenants_of(p_user_id) t where t = tenant) then
|
|
439
|
+
return false;
|
|
440
|
+
end if;
|
|
441
|
+
execute format('select %s($1::uuid, $2::uuid)', check_fn::regproc) into result using p_conversation_id, p_user_id;
|
|
442
|
+
return coalesce(result, false);
|
|
443
|
+
end;
|
|
444
|
+
$$;
|
|
445
|
+
|
|
446
|
+
-- ── Starting conversations ────────────────────────────────────────────────────
|
|
447
|
+
|
|
448
|
+
drop function genesis_chat.start_conversation(text, uuid[], text, text, text);
|
|
449
|
+
|
|
450
|
+
-- Starts a conversation of `p_kind` with these people, if the kind's start check says the
|
|
451
|
+
-- signed-in person may, in the tenant they all share: the subject's when there is one, else the
|
|
452
|
+
-- one they share (`p_tenant_id` chooses among several). A direct conversation with someone in a
|
|
453
|
+
-- tenant is found again, not made twice.
|
|
454
|
+
create function genesis_chat.start_conversation(
|
|
455
|
+
p_kind text,
|
|
456
|
+
p_member_ids uuid[],
|
|
457
|
+
p_title text default null,
|
|
458
|
+
p_subject_type text default null,
|
|
459
|
+
p_subject_id text default null,
|
|
460
|
+
p_tenant_id uuid default null
|
|
461
|
+
)
|
|
462
|
+
returns uuid
|
|
463
|
+
language plpgsql
|
|
464
|
+
security definer
|
|
465
|
+
set search_path = ''
|
|
466
|
+
as $$
|
|
467
|
+
declare
|
|
468
|
+
me uuid := auth.uid();
|
|
469
|
+
k genesis_chat.kinds;
|
|
470
|
+
s genesis_chat.subjects;
|
|
471
|
+
others uuid[];
|
|
472
|
+
everyone uuid[];
|
|
473
|
+
shared uuid[];
|
|
474
|
+
tenant uuid;
|
|
475
|
+
stype text := p_subject_type;
|
|
476
|
+
allowed boolean;
|
|
477
|
+
key text;
|
|
478
|
+
conv uuid;
|
|
479
|
+
fn regprocedure;
|
|
480
|
+
begin
|
|
481
|
+
if me is null then
|
|
482
|
+
raise exception 'Sign in to start a conversation' using errcode = '42501';
|
|
483
|
+
end if;
|
|
484
|
+
select * into k from genesis_chat.kinds where kind = p_kind;
|
|
485
|
+
if k.kind is null then
|
|
486
|
+
raise exception 'No such kind of conversation: %', p_kind using errcode = '22023';
|
|
487
|
+
end if;
|
|
488
|
+
select coalesce(array_agg(distinct x), '{}') into others
|
|
489
|
+
from unnest(coalesce(p_member_ids, '{}')) x where x is not null and x <> me;
|
|
490
|
+
if cardinality(others) = 0 then
|
|
491
|
+
raise exception 'Choose someone to talk to' using errcode = '22023';
|
|
492
|
+
end if;
|
|
493
|
+
if k.max_members is not null and cardinality(others) + 1 > k.max_members then
|
|
494
|
+
raise exception 'A % has at most % members', lower(k.label), k.max_members using errcode = '22023';
|
|
495
|
+
end if;
|
|
496
|
+
if k.start_check is null then
|
|
497
|
+
raise exception 'You can''t start this kind of conversation' using errcode = '42501';
|
|
498
|
+
end if;
|
|
499
|
+
|
|
500
|
+
-- The kind's start check first, before anything about the subject or tenants is said: someone
|
|
501
|
+
-- it refuses learns nothing about which records exist (review finding 5).
|
|
502
|
+
s := genesis_chat.subject_of(p_kind, p_subject_type);
|
|
503
|
+
if s.kind is not null then
|
|
504
|
+
stype := s.subject_type;
|
|
505
|
+
end if;
|
|
506
|
+
execute format('select %s($1::uuid[], $2::text, $3::text, $4::uuid)', k.start_check::regproc)
|
|
507
|
+
into allowed using others, stype, p_subject_id, me;
|
|
508
|
+
if not coalesce(allowed, false) then
|
|
509
|
+
raise exception 'You can''t start a conversation with these people' using errcode = '42501';
|
|
510
|
+
end if;
|
|
511
|
+
|
|
512
|
+
everyone := others || me;
|
|
513
|
+
select coalesce(array_agg(t), '{}') into shared from genesis_chat.shared_tenants(everyone) t;
|
|
514
|
+
-- An unregistered subject type with a resolver named for it is the record's tenant too.
|
|
515
|
+
fn := case when s.kind is null and p_subject_id is not null then genesis_chat.conventional_resolver(p_subject_type) end;
|
|
516
|
+
if s.kind is not null or fn is not null then
|
|
517
|
+
if s.kind is not null then
|
|
518
|
+
tenant := genesis_chat.subject_tenant(p_kind, stype, p_subject_id);
|
|
519
|
+
else
|
|
520
|
+
execute format('select %s($1)', fn::regproc) into tenant using p_subject_id;
|
|
521
|
+
end if;
|
|
522
|
+
if tenant is null then
|
|
523
|
+
raise exception 'No such %', replace(stype, '_', ' ') using errcode = '22023';
|
|
524
|
+
end if;
|
|
525
|
+
if p_tenant_id is not null and p_tenant_id <> tenant then
|
|
526
|
+
raise exception 'That % belongs to another workspace', replace(stype, '_', ' ') using errcode = '22023';
|
|
527
|
+
end if;
|
|
528
|
+
if not (tenant = any(shared)
|
|
529
|
+
or (k.cross_tenant and exists (select 1 from genesis_chat.tenants_of(me) t where t = tenant))) then
|
|
530
|
+
raise exception 'You can''t start a conversation with these people' using errcode = '42501';
|
|
531
|
+
end if;
|
|
532
|
+
elsif p_tenant_id is not null then
|
|
533
|
+
if not (p_tenant_id = any(shared)
|
|
534
|
+
or (k.cross_tenant and exists (select 1 from genesis_chat.tenants_of(me) t where t = p_tenant_id))) then
|
|
535
|
+
raise exception 'You can''t start a conversation with these people' using errcode = '42501';
|
|
536
|
+
end if;
|
|
537
|
+
tenant := p_tenant_id;
|
|
538
|
+
elsif cardinality(shared) = 1 then
|
|
539
|
+
tenant := shared[1];
|
|
540
|
+
elsif cardinality(shared) > 1 then
|
|
541
|
+
raise exception 'Choose the workspace this conversation is in' using errcode = '22023';
|
|
542
|
+
elsif k.cross_tenant then
|
|
543
|
+
-- No tenant in common, and the kind allows that: the starter's (their only one).
|
|
544
|
+
select coalesce(array_agg(t), '{}') into shared from genesis_chat.tenants_of(me) t;
|
|
545
|
+
if cardinality(shared) = 1 then
|
|
546
|
+
tenant := shared[1];
|
|
547
|
+
elsif cardinality(shared) > 1 then
|
|
548
|
+
raise exception 'Choose the workspace this conversation is in' using errcode = '22023';
|
|
549
|
+
else
|
|
550
|
+
raise exception 'You can''t start a conversation with these people' using errcode = '42501';
|
|
551
|
+
end if;
|
|
552
|
+
else
|
|
553
|
+
raise exception 'You can''t start a conversation with these people' using errcode = '42501';
|
|
554
|
+
end if;
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
if k.max_members = 2 then
|
|
558
|
+
key := p_kind || ':' || tenant::text || ':' || least(me, others[1])::text || ':' || greatest(me, others[1])::text;
|
|
559
|
+
perform pg_advisory_xact_lock(hashtextextended(key, 0));
|
|
560
|
+
select id into conv from genesis_chat.conversations where direct_key = key;
|
|
561
|
+
if conv is not null then
|
|
562
|
+
insert into genesis_chat.members (conversation_id, user_id, role)
|
|
563
|
+
select conv, u, 'member' from unnest(array[me, others[1]]) u
|
|
564
|
+
on conflict (conversation_id, user_id) do update set left_at = null, role = 'member', joined_at =
|
|
565
|
+
case when genesis_chat.members.left_at is null then genesis_chat.members.joined_at else now() end;
|
|
566
|
+
return conv;
|
|
567
|
+
end if;
|
|
568
|
+
end if;
|
|
569
|
+
|
|
570
|
+
insert into genesis_chat.conversations (kind, tenant_id, title, subject_type, subject_id, direct_key, created_by)
|
|
571
|
+
values (p_kind, tenant, nullif(btrim(p_title), ''), stype, p_subject_id, key, me)
|
|
572
|
+
returning id into conv;
|
|
573
|
+
insert into genesis_chat.members (conversation_id, user_id, role)
|
|
574
|
+
values (conv, me, case when k.max_members = 2 then 'member' else 'owner' end);
|
|
575
|
+
insert into genesis_chat.members (conversation_id, user_id, role)
|
|
576
|
+
select conv, u, 'member' from unnest(others) u;
|
|
577
|
+
return conv;
|
|
578
|
+
end;
|
|
579
|
+
$$;
|
|
580
|
+
|
|
581
|
+
drop function genesis_chat.ensure_conversation(text, text, text, text, jsonb);
|
|
582
|
+
|
|
583
|
+
-- The server's side: the conversation for one of the app's records, made once, in the record's
|
|
584
|
+
-- tenant: its registered resolver's; for a type that isn't registered, `p_tenant_id`, or else a
|
|
585
|
+
-- resolver named for the type (`private.genesis_chat_resolve_<subject_type>(text)`) when one exists.
|
|
586
|
+
create function genesis_chat.ensure_conversation(
|
|
587
|
+
p_kind text,
|
|
588
|
+
p_subject_type text,
|
|
589
|
+
p_subject_id text,
|
|
590
|
+
p_title text default null,
|
|
591
|
+
p_settings jsonb default '{}',
|
|
592
|
+
p_tenant_id uuid default null
|
|
593
|
+
)
|
|
594
|
+
returns uuid
|
|
595
|
+
language plpgsql
|
|
596
|
+
security definer
|
|
597
|
+
set search_path = ''
|
|
598
|
+
as $$
|
|
599
|
+
declare
|
|
600
|
+
s genesis_chat.subjects;
|
|
601
|
+
names text[];
|
|
602
|
+
stype text := p_subject_type;
|
|
603
|
+
tenant uuid := p_tenant_id;
|
|
604
|
+
conv uuid;
|
|
605
|
+
fn regprocedure;
|
|
606
|
+
begin
|
|
607
|
+
if p_subject_type is null or p_subject_id is null then
|
|
608
|
+
raise exception 'A subject (type and id) is required' using errcode = '22023';
|
|
609
|
+
end if;
|
|
610
|
+
s := genesis_chat.subject_of(p_kind, p_subject_type);
|
|
611
|
+
if s.kind is not null then
|
|
612
|
+
stype := s.subject_type;
|
|
613
|
+
names := s.subject_type || s.aliases;
|
|
614
|
+
tenant := genesis_chat.subject_tenant(p_kind, stype, p_subject_id);
|
|
615
|
+
if tenant is null then
|
|
616
|
+
raise exception 'No such %: %', replace(stype, '_', ' '), p_subject_id using errcode = '22023';
|
|
617
|
+
end if;
|
|
618
|
+
if p_tenant_id is not null and p_tenant_id <> tenant then
|
|
619
|
+
raise exception 'The % belongs to tenant %, not %', replace(stype, '_', ' '), tenant, p_tenant_id using errcode = '22023';
|
|
620
|
+
end if;
|
|
621
|
+
else
|
|
622
|
+
names := array[p_subject_type];
|
|
623
|
+
if p_subject_type !~ '^[a-z][a-z0-9_]{0,39}$' then
|
|
624
|
+
raise exception 'A subject type is lowercase letters, digits and _ (at most 40)' using errcode = '22023';
|
|
625
|
+
end if;
|
|
626
|
+
-- Not registered: a resolver named for the type, when there is one (as the backfill uses).
|
|
627
|
+
fn := genesis_chat.conventional_resolver(p_subject_type);
|
|
628
|
+
if tenant is null and fn is not null then
|
|
629
|
+
execute format('select %s($1)', fn::regproc) into tenant using p_subject_id;
|
|
630
|
+
if tenant is null then
|
|
631
|
+
raise exception 'No such %: %', replace(p_subject_type, '_', ' '), p_subject_id using errcode = '22023';
|
|
632
|
+
end if;
|
|
633
|
+
end if;
|
|
634
|
+
if tenant is null then
|
|
635
|
+
raise exception 'A conversation needs its tenant: register the subject type % for kind % (genesis_chat.register_subject), or pass p_tenant_id', p_subject_type, p_kind
|
|
636
|
+
using errcode = '22023';
|
|
637
|
+
end if;
|
|
638
|
+
end if;
|
|
639
|
+
|
|
640
|
+
-- Under any of its names: conversations made before a type was renamed are found too.
|
|
641
|
+
select id into conv from genesis_chat.conversations
|
|
642
|
+
where kind = p_kind and subject_type = any(names) and subject_id = p_subject_id
|
|
643
|
+
order by (subject_type = stype) desc, created_at limit 1;
|
|
644
|
+
if conv is null then
|
|
645
|
+
insert into genesis_chat.conversations (kind, tenant_id, subject_type, subject_id, title, settings)
|
|
646
|
+
values (p_kind, tenant, stype, p_subject_id, nullif(btrim(p_title), ''), coalesce(p_settings, '{}'))
|
|
647
|
+
on conflict (kind, subject_type, subject_id) where subject_type is not null do nothing
|
|
648
|
+
returning id into conv;
|
|
649
|
+
if conv is null then
|
|
650
|
+
select id into conv from genesis_chat.conversations
|
|
651
|
+
where kind = p_kind and subject_type = stype and subject_id = p_subject_id;
|
|
652
|
+
end if;
|
|
653
|
+
else
|
|
654
|
+
if exists (select 1 from genesis_chat.conversations where id = conv and tenant_id <> tenant) then
|
|
655
|
+
raise exception 'The conversation for this % is in another tenant', replace(stype, '_', ' ') using errcode = '22023';
|
|
656
|
+
end if;
|
|
657
|
+
if p_title is not null then
|
|
658
|
+
update genesis_chat.conversations set title = nullif(btrim(p_title), '')
|
|
659
|
+
where id = conv and title is distinct from nullif(btrim(p_title), '');
|
|
660
|
+
end if;
|
|
661
|
+
end if;
|
|
662
|
+
return conv;
|
|
663
|
+
end;
|
|
664
|
+
$$;
|
|
665
|
+
|
|
666
|
+
-- ── Adding people ─────────────────────────────────────────────────────────────
|
|
667
|
+
|
|
668
|
+
-- As in 0001, and only people of the conversation's tenant (anyone, for a kind that crosses tenants).
|
|
669
|
+
create or replace function genesis_chat.add_members(p_conversation_id uuid, p_user_ids uuid[])
|
|
670
|
+
returns void
|
|
671
|
+
language plpgsql
|
|
672
|
+
security definer
|
|
673
|
+
set search_path = ''
|
|
674
|
+
as $$
|
|
675
|
+
declare
|
|
676
|
+
me uuid := auth.uid();
|
|
677
|
+
c genesis_chat.conversations;
|
|
678
|
+
k genesis_chat.kinds;
|
|
679
|
+
adding uuid[];
|
|
680
|
+
allowed boolean;
|
|
681
|
+
begin
|
|
682
|
+
select * into c from genesis_chat.conversations where id = p_conversation_id;
|
|
683
|
+
select * into k from genesis_chat.kinds where kind = c.kind;
|
|
684
|
+
if c.id is null or coalesce(genesis_chat.my_role(c.id), '') not in ('owner', 'admin') or k.membership <> 'admins' then
|
|
685
|
+
raise exception 'You can''t add people to this conversation' using errcode = '42501';
|
|
686
|
+
end if;
|
|
687
|
+
select coalesce(array_agg(distinct x), '{}') into adding from unnest(coalesce(p_user_ids, '{}')) x where x is not null;
|
|
688
|
+
if k.start_check is null then
|
|
689
|
+
raise exception 'You can''t add people to this conversation' using errcode = '42501';
|
|
690
|
+
end if;
|
|
691
|
+
if not k.cross_tenant and exists (select 1 from unnest(adding) u
|
|
692
|
+
where not exists (select 1 from genesis_chat.tenants_of(u) t where t = c.tenant_id)) then
|
|
693
|
+
raise exception 'You can''t add these people' using errcode = '42501';
|
|
694
|
+
end if;
|
|
695
|
+
execute format('select %s($1::uuid[], $2::text, $3::text, $4::uuid)', k.start_check::regproc)
|
|
696
|
+
into allowed using adding, c.subject_type, c.subject_id, me;
|
|
697
|
+
if not coalesce(allowed, false) then
|
|
698
|
+
raise exception 'You can''t add these people' using errcode = '42501';
|
|
699
|
+
end if;
|
|
700
|
+
insert into genesis_chat.members as m (conversation_id, user_id, role)
|
|
701
|
+
select c.id, u, 'member' from unnest(adding) u
|
|
702
|
+
on conflict (conversation_id, user_id) do update set left_at = null, role = 'member', joined_at = now()
|
|
703
|
+
where m.left_at is not null or m.role = 'observer';
|
|
704
|
+
if k.max_members is not null and (select count(*) from genesis_chat.members
|
|
705
|
+
where conversation_id = c.id and left_at is null and role <> 'observer') > k.max_members then
|
|
706
|
+
raise exception 'A % has at most % members', lower(k.label), k.max_members using errcode = '22023';
|
|
707
|
+
end if;
|
|
708
|
+
end;
|
|
709
|
+
$$;
|
|
710
|
+
|
|
711
|
+
-- ── Grants, by name ───────────────────────────────────────────────────────────
|
|
712
|
+
|
|
713
|
+
revoke execute on function genesis_chat.start_conversation(text, uuid[], text, text, text, uuid) from public, anon;
|
|
714
|
+
grant execute on function genesis_chat.start_conversation(text, uuid[], text, text, text, uuid) to authenticated, service_role;
|
|
715
|
+
revoke execute on function genesis_chat.ensure_conversation(text, text, text, text, jsonb, uuid) from public, anon, authenticated;
|
|
716
|
+
grant execute on function genesis_chat.ensure_conversation(text, text, text, text, jsonb, uuid) to service_role;
|