@mccaskie-labs/workbench-library 0.0.0-stage → 0.1.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/CHANGELOG.md +5 -0
- package/README.md +62 -2
- package/dist/access.d.ts +7 -0
- package/dist/access.js +46 -0
- package/dist/cli.d.ts +12 -0
- package/dist/cli.js +142 -0
- package/dist/config.d.ts +7 -0
- package/dist/config.js +84 -0
- package/dist/embed.d.ts +6 -0
- package/dist/embed.js +53 -0
- package/dist/errors.d.ts +5 -0
- package/dist/errors.js +11 -0
- package/dist/feed.d.ts +14 -0
- package/dist/feed.js +74 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +16 -0
- package/dist/library.d.ts +156 -0
- package/dist/library.js +422 -0
- package/dist/links.d.ts +37 -0
- package/dist/links.js +68 -0
- package/dist/migration.d.ts +4 -0
- package/dist/migration.js +619 -0
- package/dist/sponsors.d.ts +3 -0
- package/dist/sponsors.js +8 -0
- package/dist/starters.d.ts +20 -0
- package/dist/starters.js +16 -0
- package/dist/styles.d.ts +33 -0
- package/dist/styles.js +86 -0
- package/dist/text.d.ts +3 -0
- package/dist/text.js +26 -0
- package/dist/time.d.ts +6 -0
- package/dist/time.js +36 -0
- package/dist/types.d.ts +245 -0
- package/dist/types.js +3 -0
- package/dist/ui.d.ts +42 -0
- package/dist/ui.js +148 -0
- package/dist/words.d.ts +4 -0
- package/dist/words.js +128 -0
- package/package.json +52 -4
|
@@ -0,0 +1,619 @@
|
|
|
1
|
+
// The app's own library tables (docs/capabilities/28-library.md, "Data
|
|
2
|
+
// stored"), copied into the project's supabase/migrations as one file and its
|
|
3
|
+
// down file.
|
|
4
|
+
//
|
|
5
|
+
// Only the service role reaches them: the functions are granted to
|
|
6
|
+
// service_role alone and the tables have RLS on with no policies. The app
|
|
7
|
+
// calls createLibrary with its service client, server side, and tells it who
|
|
8
|
+
// is looking; the package decides what they may see.
|
|
9
|
+
//
|
|
10
|
+
// The one personal detail is the member id in library_access (who started or
|
|
11
|
+
// bought what). Ended and revoked rows are deleted retention.ended_access_days
|
|
12
|
+
// after they end (library_cleanup), and library_forget deletes a member's rows
|
|
13
|
+
// at once. Files live in the app's R2 bucket; library_files keeps the key,
|
|
14
|
+
// name, type and size only.
|
|
15
|
+
//
|
|
16
|
+
// It creates functions, so Control asks for approval to apply it even on
|
|
17
|
+
// staging. The down file drops everything the up file made.
|
|
18
|
+
const SIGNATURES = [
|
|
19
|
+
"library_collection_save(jsonb, timestamptz)",
|
|
20
|
+
"library_collection_delete(text)",
|
|
21
|
+
"library_collections()",
|
|
22
|
+
"library_view(text, text, timestamptz)",
|
|
23
|
+
"library_item_save(jsonb, timestamptz)",
|
|
24
|
+
"library_item_delete(uuid)",
|
|
25
|
+
"library_file_add(text, text, text, bigint, timestamptz)",
|
|
26
|
+
"library_file_get(uuid, timestamptz)",
|
|
27
|
+
"library_file_delete(uuid)",
|
|
28
|
+
"library_sponsor_save(jsonb)",
|
|
29
|
+
"library_sponsor_delete(uuid)",
|
|
30
|
+
"library_enrol(text, text, timestamptz)",
|
|
31
|
+
"library_grant(text, text, text, timestamptz, timestamptz)",
|
|
32
|
+
"library_revoke(text, timestamptz)",
|
|
33
|
+
"library_mine(text, timestamptz)",
|
|
34
|
+
"library_forget(text)",
|
|
35
|
+
"library_cleanup(timestamptz, integer)",
|
|
36
|
+
"library_tick(text, timestamptz)",
|
|
37
|
+
];
|
|
38
|
+
const HELPERS = [
|
|
39
|
+
"library_collection_json(public.library_collections)",
|
|
40
|
+
"library_item_json(public.library_items)",
|
|
41
|
+
"library_file_json(public.library_files)",
|
|
42
|
+
"library_started(uuid, text, timestamptz)",
|
|
43
|
+
"library_live(public.library_items, timestamptz)",
|
|
44
|
+
];
|
|
45
|
+
const SLUG = `'^[a-z0-9]([a-z0-9-]{0,78}[a-z0-9])?$'`;
|
|
46
|
+
const up = `create table public.library_collections (
|
|
47
|
+
id uuid primary key default gen_random_uuid(),
|
|
48
|
+
slug text not null unique check (slug ~ ${SLUG}),
|
|
49
|
+
kind text not null check (kind in ('course', 'show', 'shelf')),
|
|
50
|
+
title text not null check (char_length(title) between 1 and 200),
|
|
51
|
+
summary text check (char_length(summary) <= 2000),
|
|
52
|
+
cover_file uuid,
|
|
53
|
+
access text not null check (access in ('everyone', 'signed_in', 'feature', 'bought')),
|
|
54
|
+
feature text check (char_length(feature) between 1 and 100),
|
|
55
|
+
price_cents bigint check (price_cents between 1 and 100000000),
|
|
56
|
+
access_months integer check (access_months between 1 and 600),
|
|
57
|
+
status text not null default 'draft' check (status in ('draft', 'published')),
|
|
58
|
+
show_details jsonb check (show_details is null or jsonb_typeof(show_details) = 'object'),
|
|
59
|
+
created_at timestamptz not null default now(),
|
|
60
|
+
updated_at timestamptz not null default now(),
|
|
61
|
+
check ((access = 'feature') = (feature is not null)),
|
|
62
|
+
check (access = 'bought' or (price_cents is null and access_months is null))
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
comment on table public.library_collections is 'Courses, shows and shelves the owner publishes (workbench-library). No visitor details; show_details may hold the owner''s own name and email for the podcast feed. Service role only.';
|
|
66
|
+
|
|
67
|
+
create table public.library_files (
|
|
68
|
+
id uuid primary key default gen_random_uuid(),
|
|
69
|
+
key text not null unique check (char_length(key) between 1 and 400),
|
|
70
|
+
name text not null check (char_length(name) between 1 and 200),
|
|
71
|
+
type text not null check (type ~ '^[a-z]+/[a-z0-9.+-]+$'),
|
|
72
|
+
bytes bigint not null check (bytes between 1 and 524288000),
|
|
73
|
+
created_at timestamptz not null default now()
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
comment on table public.library_files is 'Files in the app''s R2 bucket used by the library: the key, name, type and size, never the contents (workbench-library). Service role only.';
|
|
77
|
+
|
|
78
|
+
alter table public.library_collections add foreign key (cover_file) references public.library_files (id) on delete set null;
|
|
79
|
+
|
|
80
|
+
create table public.library_items (
|
|
81
|
+
id uuid primary key default gen_random_uuid(),
|
|
82
|
+
collection_id uuid not null references public.library_collections (id) on delete cascade,
|
|
83
|
+
slug text not null check (slug ~ ${SLUG}),
|
|
84
|
+
position integer not null check (position >= 0),
|
|
85
|
+
title text not null check (char_length(title) between 1 and 200),
|
|
86
|
+
body text not null default '' check (char_length(body) <= 100000),
|
|
87
|
+
media_kind text not null default 'none' check (media_kind in ('none', 'embed', 'file')),
|
|
88
|
+
embed_url text check (char_length(embed_url) between 1 and 500),
|
|
89
|
+
file_id uuid references public.library_files (id) on delete restrict,
|
|
90
|
+
seconds integer check (seconds between 1 and 86400),
|
|
91
|
+
preview boolean not null default false,
|
|
92
|
+
drip_days integer check (drip_days between 0 and 3650),
|
|
93
|
+
publish_at timestamptz,
|
|
94
|
+
status text not null default 'draft' check (status in ('draft', 'published')),
|
|
95
|
+
episode integer check (episode between 0 and 100000),
|
|
96
|
+
season integer check (season between 1 and 1000),
|
|
97
|
+
announced_at timestamptz,
|
|
98
|
+
created_at timestamptz not null default now(),
|
|
99
|
+
updated_at timestamptz not null default now(),
|
|
100
|
+
unique (collection_id, slug),
|
|
101
|
+
check ((media_kind = 'embed') = (embed_url is not null)),
|
|
102
|
+
check ((media_kind = 'file') = (file_id is not null))
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
comment on table public.library_items is 'Lessons, episodes and resources: words, media, order, preview, drip days and publish time (workbench-library). No personal details. Service role only.';
|
|
106
|
+
|
|
107
|
+
create index library_items_collection on public.library_items (collection_id, position);
|
|
108
|
+
create index library_items_file on public.library_items (file_id) where file_id is not null;
|
|
109
|
+
create index library_items_scheduled on public.library_items (publish_at) where announced_at is null and publish_at is not null;
|
|
110
|
+
|
|
111
|
+
create table public.library_sponsors (
|
|
112
|
+
id uuid primary key default gen_random_uuid(),
|
|
113
|
+
collection_id uuid not null references public.library_collections (id) on delete cascade,
|
|
114
|
+
name text not null check (char_length(name) between 1 and 200),
|
|
115
|
+
url text check (url ~ '^https://' and char_length(url) <= 500),
|
|
116
|
+
message text not null check (char_length(message) between 1 and 500),
|
|
117
|
+
starts_on date not null,
|
|
118
|
+
ends_on date not null,
|
|
119
|
+
created_at timestamptz not null default now(),
|
|
120
|
+
check (ends_on >= starts_on)
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
comment on table public.library_sponsors is 'Sponsor slots on a show, with dates: the sponsor''s business name, link and line (workbench-library). Service role only.';
|
|
124
|
+
|
|
125
|
+
create index library_sponsors_collection on public.library_sponsors (collection_id, starts_on);
|
|
126
|
+
|
|
127
|
+
create table public.library_access (
|
|
128
|
+
id uuid primary key default gen_random_uuid(),
|
|
129
|
+
collection_id uuid not null references public.library_collections (id) on delete cascade,
|
|
130
|
+
member text not null check (char_length(member) between 1 and 200),
|
|
131
|
+
source text not null check (source in ('enrol', 'grant')),
|
|
132
|
+
ref text unique check (char_length(ref) between 1 and 200),
|
|
133
|
+
started_at timestamptz not null,
|
|
134
|
+
ends_at timestamptz,
|
|
135
|
+
revoked_at timestamptz,
|
|
136
|
+
notified_at timestamptz,
|
|
137
|
+
created_at timestamptz not null default now(),
|
|
138
|
+
check ((source = 'grant') = (ref is not null))
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
comment on table public.library_access is 'Who started or bought which collection: the app''s member id (a personal detail), the payment id, when it started and ends. Deleted retention.ended_access_days after it ends or is revoked, or at once by library_forget (workbench-library). Service role only.';
|
|
142
|
+
|
|
143
|
+
create unique index library_access_enrol on public.library_access (collection_id, member) where source = 'enrol';
|
|
144
|
+
create index library_access_member on public.library_access (member, collection_id);
|
|
145
|
+
|
|
146
|
+
alter table public.library_collections enable row level security;
|
|
147
|
+
alter table public.library_files enable row level security;
|
|
148
|
+
alter table public.library_items enable row level security;
|
|
149
|
+
alter table public.library_sponsors enable row level security;
|
|
150
|
+
alter table public.library_access enable row level security;
|
|
151
|
+
|
|
152
|
+
revoke all on public.library_collections, public.library_files, public.library_items, public.library_sponsors, public.library_access from anon, authenticated;
|
|
153
|
+
|
|
154
|
+
create function public.library_collection_json(c public.library_collections)
|
|
155
|
+
returns jsonb
|
|
156
|
+
language sql
|
|
157
|
+
immutable
|
|
158
|
+
security invoker
|
|
159
|
+
set search_path = ''
|
|
160
|
+
as $$
|
|
161
|
+
select jsonb_build_object('id', c.id, 'slug', c.slug, 'kind', c.kind, 'title', c.title, 'summary', c.summary, 'cover_file', c.cover_file,
|
|
162
|
+
'access', c.access, 'feature', c.feature, 'price_cents', c.price_cents, 'access_months', c.access_months, 'status', c.status,
|
|
163
|
+
'show', c.show_details, 'created_at', c.created_at, 'updated_at', c.updated_at);
|
|
164
|
+
$$;
|
|
165
|
+
|
|
166
|
+
create function public.library_item_json(i public.library_items)
|
|
167
|
+
returns jsonb
|
|
168
|
+
language sql
|
|
169
|
+
immutable
|
|
170
|
+
security invoker
|
|
171
|
+
set search_path = ''
|
|
172
|
+
as $$
|
|
173
|
+
select jsonb_build_object('id', i.id, 'collection', i.collection_id, 'slug', i.slug, 'position', i.position, 'title', i.title, 'body', i.body,
|
|
174
|
+
'media', case i.media_kind when 'embed' then jsonb_build_object('kind', 'embed', 'url', i.embed_url)
|
|
175
|
+
when 'file' then jsonb_build_object('kind', 'file', 'file', i.file_id) else null end,
|
|
176
|
+
'seconds', i.seconds, 'preview', i.preview, 'drip_days', i.drip_days, 'publish_at', i.publish_at, 'status', i.status,
|
|
177
|
+
'episode', i.episode, 'season', i.season, 'created_at', i.created_at, 'updated_at', i.updated_at);
|
|
178
|
+
$$;
|
|
179
|
+
|
|
180
|
+
create function public.library_file_json(f public.library_files)
|
|
181
|
+
returns jsonb
|
|
182
|
+
language sql
|
|
183
|
+
immutable
|
|
184
|
+
security invoker
|
|
185
|
+
set search_path = ''
|
|
186
|
+
as $$
|
|
187
|
+
select jsonb_build_object('id', f.id, 'key', f.key, 'name', f.name, 'type', f.type, 'bytes', f.bytes, 'created_at', f.created_at);
|
|
188
|
+
$$;
|
|
189
|
+
|
|
190
|
+
-- When the member's earliest access that still holds began, or null.
|
|
191
|
+
create function public.library_started(p_collection uuid, p_member text, p_now timestamptz)
|
|
192
|
+
returns timestamptz
|
|
193
|
+
language sql
|
|
194
|
+
stable
|
|
195
|
+
security invoker
|
|
196
|
+
set search_path = ''
|
|
197
|
+
as $$
|
|
198
|
+
select min(a.started_at) from public.library_access a
|
|
199
|
+
where a.collection_id = p_collection and a.member = p_member and a.revoked_at is null and (a.ends_at is null or a.ends_at > p_now);
|
|
200
|
+
$$;
|
|
201
|
+
|
|
202
|
+
-- Published and its time has come.
|
|
203
|
+
create function public.library_live(i public.library_items, p_now timestamptz)
|
|
204
|
+
returns boolean
|
|
205
|
+
language sql
|
|
206
|
+
immutable
|
|
207
|
+
security invoker
|
|
208
|
+
set search_path = ''
|
|
209
|
+
as $$
|
|
210
|
+
select i.status = 'published' and (i.publish_at is null or i.publish_at <= p_now);
|
|
211
|
+
$$;
|
|
212
|
+
|
|
213
|
+
-- Makes or changes a collection (by id; no id makes one).
|
|
214
|
+
create function public.library_collection_save(p jsonb, p_now timestamptz)
|
|
215
|
+
returns jsonb
|
|
216
|
+
language plpgsql
|
|
217
|
+
security invoker
|
|
218
|
+
set search_path = ''
|
|
219
|
+
as $$
|
|
220
|
+
declare
|
|
221
|
+
c public.library_collections;
|
|
222
|
+
begin
|
|
223
|
+
if p->>'id' is null then
|
|
224
|
+
insert into public.library_collections (slug, kind, title, summary, cover_file, access, feature, price_cents, access_months, status, show_details, created_at, updated_at)
|
|
225
|
+
values (p->>'slug', p->>'kind', p->>'title', p->>'summary', (p->>'cover_file')::uuid, p->>'access', p->>'feature', (p->>'price_cents')::bigint,
|
|
226
|
+
(p->>'access_months')::integer, coalesce(p->>'status', 'draft'), case when jsonb_typeof(p->'show') = 'object' then p->'show' end, p_now, p_now)
|
|
227
|
+
returning * into c;
|
|
228
|
+
else
|
|
229
|
+
update public.library_collections set slug = p->>'slug', kind = p->>'kind', title = p->>'title', summary = p->>'summary',
|
|
230
|
+
cover_file = (p->>'cover_file')::uuid, access = p->>'access', feature = p->>'feature', price_cents = (p->>'price_cents')::bigint,
|
|
231
|
+
access_months = (p->>'access_months')::integer, status = coalesce(p->>'status', 'draft'),
|
|
232
|
+
show_details = case when jsonb_typeof(p->'show') = 'object' then p->'show' end, updated_at = p_now
|
|
233
|
+
where id = (p->>'id')::uuid
|
|
234
|
+
returning * into c;
|
|
235
|
+
if c.id is null then raise exception 'library_not_found'; end if;
|
|
236
|
+
end if;
|
|
237
|
+
return public.library_collection_json(c);
|
|
238
|
+
exception when unique_violation then
|
|
239
|
+
raise exception 'library_slug_taken';
|
|
240
|
+
end;
|
|
241
|
+
$$;
|
|
242
|
+
|
|
243
|
+
-- Deletes a collection with its items, sponsors and access rows, and the
|
|
244
|
+
-- files only it used. Answers the R2 keys to delete.
|
|
245
|
+
create function public.library_collection_delete(p_slug text)
|
|
246
|
+
returns jsonb
|
|
247
|
+
language plpgsql
|
|
248
|
+
security invoker
|
|
249
|
+
set search_path = ''
|
|
250
|
+
as $$
|
|
251
|
+
declare
|
|
252
|
+
c public.library_collections;
|
|
253
|
+
doomed uuid[];
|
|
254
|
+
keys jsonb;
|
|
255
|
+
begin
|
|
256
|
+
select * into c from public.library_collections where slug = p_slug for update;
|
|
257
|
+
if c.id is null then raise exception 'library_not_found'; end if;
|
|
258
|
+
select coalesce(array_agg(f.id), '{}'), coalesce(jsonb_agg(f.key order by f.key), '[]'::jsonb) into doomed, keys
|
|
259
|
+
from public.library_files f
|
|
260
|
+
where (f.id = c.cover_file or f.id in (select file_id from public.library_items where collection_id = c.id))
|
|
261
|
+
and not exists (select 1 from public.library_items i where i.file_id = f.id and i.collection_id <> c.id)
|
|
262
|
+
and not exists (select 1 from public.library_collections o where o.cover_file = f.id and o.id <> c.id);
|
|
263
|
+
delete from public.library_collections where id = c.id;
|
|
264
|
+
delete from public.library_files where id = any (doomed);
|
|
265
|
+
return jsonb_build_object('keys', keys);
|
|
266
|
+
end;
|
|
267
|
+
$$;
|
|
268
|
+
|
|
269
|
+
create function public.library_collections()
|
|
270
|
+
returns jsonb
|
|
271
|
+
language sql
|
|
272
|
+
stable
|
|
273
|
+
security invoker
|
|
274
|
+
set search_path = ''
|
|
275
|
+
as $$
|
|
276
|
+
select coalesce(jsonb_agg(public.library_collection_json(c) order by c.created_at, c.id), '[]'::jsonb) from public.library_collections c;
|
|
277
|
+
$$;
|
|
278
|
+
|
|
279
|
+
-- A collection with every item, the files they use, its sponsors and when
|
|
280
|
+
-- this member started (null member: a visitor). The package works out what
|
|
281
|
+
-- the viewer may see.
|
|
282
|
+
create function public.library_view(p_slug text, p_member text, p_now timestamptz)
|
|
283
|
+
returns jsonb
|
|
284
|
+
language sql
|
|
285
|
+
stable
|
|
286
|
+
security invoker
|
|
287
|
+
set search_path = ''
|
|
288
|
+
as $$
|
|
289
|
+
select case when c.id is null then null else jsonb_build_object(
|
|
290
|
+
'collection', public.library_collection_json(c),
|
|
291
|
+
'items', coalesce((select jsonb_agg(public.library_item_json(i) order by i.position, i.created_at) from public.library_items i where i.collection_id = c.id), '[]'::jsonb),
|
|
292
|
+
'files', coalesce((select jsonb_agg(public.library_file_json(f)) from public.library_files f
|
|
293
|
+
where f.id = c.cover_file or f.id in (select file_id from public.library_items where collection_id = c.id)), '[]'::jsonb),
|
|
294
|
+
'sponsors', coalesce((select jsonb_agg(jsonb_build_object('id', s.id, 'collection', s.collection_id, 'name', s.name, 'url', s.url, 'message', s.message,
|
|
295
|
+
'starts_on', s.starts_on, 'ends_on', s.ends_on) order by s.starts_on) from public.library_sponsors s where s.collection_id = c.id), '[]'::jsonb),
|
|
296
|
+
'started_at', case when p_member is null then null else public.library_started(c.id, p_member, p_now) end
|
|
297
|
+
) end
|
|
298
|
+
from (select 1) one left join public.library_collections c on c.slug = p_slug;
|
|
299
|
+
$$;
|
|
300
|
+
|
|
301
|
+
-- Makes or changes an item. No position puts it last.
|
|
302
|
+
create function public.library_item_save(p jsonb, p_now timestamptz)
|
|
303
|
+
returns jsonb
|
|
304
|
+
language plpgsql
|
|
305
|
+
security invoker
|
|
306
|
+
set search_path = ''
|
|
307
|
+
as $$
|
|
308
|
+
declare
|
|
309
|
+
cid uuid;
|
|
310
|
+
i public.library_items;
|
|
311
|
+
pos integer;
|
|
312
|
+
begin
|
|
313
|
+
select id into cid from public.library_collections where slug = p->>'collection' for update;
|
|
314
|
+
if cid is null then raise exception 'library_not_found'; end if;
|
|
315
|
+
pos := coalesce((p->>'position')::integer, (select coalesce(max(position) + 1, 0) from public.library_items where collection_id = cid));
|
|
316
|
+
if p->>'id' is null then
|
|
317
|
+
insert into public.library_items (collection_id, slug, position, title, body, media_kind, embed_url, file_id, seconds, preview, drip_days, publish_at, status, episode, season, created_at, updated_at)
|
|
318
|
+
values (cid, p->>'slug', pos, p->>'title', coalesce(p->>'body', ''), coalesce(p->>'media_kind', 'none'), p->>'embed_url', (p->>'file_id')::uuid,
|
|
319
|
+
(p->>'seconds')::integer, coalesce((p->>'preview')::boolean, false), (p->>'drip_days')::integer, (p->>'publish_at')::timestamptz,
|
|
320
|
+
coalesce(p->>'status', 'draft'), (p->>'episode')::integer, (p->>'season')::integer, p_now, p_now)
|
|
321
|
+
returning * into i;
|
|
322
|
+
else
|
|
323
|
+
update public.library_items set slug = p->>'slug', position = pos, title = p->>'title', body = coalesce(p->>'body', ''),
|
|
324
|
+
media_kind = coalesce(p->>'media_kind', 'none'), embed_url = p->>'embed_url', file_id = (p->>'file_id')::uuid, seconds = (p->>'seconds')::integer,
|
|
325
|
+
preview = coalesce((p->>'preview')::boolean, false), drip_days = (p->>'drip_days')::integer,
|
|
326
|
+
-- A new publish time is announced again by the tick.
|
|
327
|
+
announced_at = case when publish_at is distinct from (p->>'publish_at')::timestamptz then null else announced_at end,
|
|
328
|
+
publish_at = (p->>'publish_at')::timestamptz, status = coalesce(p->>'status', 'draft'),
|
|
329
|
+
episode = (p->>'episode')::integer, season = (p->>'season')::integer, updated_at = p_now
|
|
330
|
+
where id = (p->>'id')::uuid and collection_id = cid
|
|
331
|
+
returning * into i;
|
|
332
|
+
if i.id is null then raise exception 'library_not_found'; end if;
|
|
333
|
+
end if;
|
|
334
|
+
return public.library_item_json(i);
|
|
335
|
+
exception when unique_violation then
|
|
336
|
+
raise exception 'library_slug_taken';
|
|
337
|
+
end;
|
|
338
|
+
$$;
|
|
339
|
+
|
|
340
|
+
-- Deletes an item, and its file when nothing else uses it: answers the R2 key.
|
|
341
|
+
create function public.library_item_delete(p_id uuid)
|
|
342
|
+
returns jsonb
|
|
343
|
+
language plpgsql
|
|
344
|
+
security invoker
|
|
345
|
+
set search_path = ''
|
|
346
|
+
as $$
|
|
347
|
+
declare
|
|
348
|
+
i public.library_items;
|
|
349
|
+
k text;
|
|
350
|
+
begin
|
|
351
|
+
delete from public.library_items where id = p_id returning * into i;
|
|
352
|
+
if i.id is null then return jsonb_build_object('deleted', false, 'key', null); end if;
|
|
353
|
+
if i.file_id is not null
|
|
354
|
+
and not exists (select 1 from public.library_items where file_id = i.file_id)
|
|
355
|
+
and not exists (select 1 from public.library_collections where cover_file = i.file_id) then
|
|
356
|
+
delete from public.library_files where id = i.file_id returning key into k;
|
|
357
|
+
end if;
|
|
358
|
+
return jsonb_build_object('deleted', true, 'key', k);
|
|
359
|
+
end;
|
|
360
|
+
$$;
|
|
361
|
+
|
|
362
|
+
create function public.library_file_add(p_key text, p_name text, p_type text, p_bytes bigint, p_now timestamptz)
|
|
363
|
+
returns jsonb
|
|
364
|
+
language sql
|
|
365
|
+
security invoker
|
|
366
|
+
set search_path = ''
|
|
367
|
+
as $$
|
|
368
|
+
insert into public.library_files (key, name, type, bytes, created_at) values (p_key, p_name, p_type, p_bytes, p_now)
|
|
369
|
+
returning public.library_file_json(library_files);
|
|
370
|
+
$$;
|
|
371
|
+
|
|
372
|
+
-- A file, and whether anyone may fetch it now without a signed link: it's
|
|
373
|
+
-- the cover of a published collection, or the media of a live item that is
|
|
374
|
+
-- a preview or sits in a published collection open to everyone.
|
|
375
|
+
create function public.library_file_get(p_id uuid, p_now timestamptz)
|
|
376
|
+
returns jsonb
|
|
377
|
+
language sql
|
|
378
|
+
stable
|
|
379
|
+
security invoker
|
|
380
|
+
set search_path = ''
|
|
381
|
+
as $$
|
|
382
|
+
select case when f.id is null then null else jsonb_build_object('file', public.library_file_json(f), 'public',
|
|
383
|
+
exists (select 1 from public.library_collections c where c.cover_file = f.id and c.status = 'published')
|
|
384
|
+
or exists (select 1 from public.library_items i join public.library_collections c on c.id = i.collection_id
|
|
385
|
+
where i.file_id = f.id and c.status = 'published' and public.library_live(i, p_now) and (i.preview or c.access = 'everyone'))) end
|
|
386
|
+
from (select 1) one left join public.library_files f on f.id = p_id;
|
|
387
|
+
$$;
|
|
388
|
+
|
|
389
|
+
-- Deletes a file nothing uses. Answers its R2 key, or fails when it's in use.
|
|
390
|
+
create function public.library_file_delete(p_id uuid)
|
|
391
|
+
returns jsonb
|
|
392
|
+
language plpgsql
|
|
393
|
+
security invoker
|
|
394
|
+
set search_path = ''
|
|
395
|
+
as $$
|
|
396
|
+
declare
|
|
397
|
+
k text;
|
|
398
|
+
begin
|
|
399
|
+
if exists (select 1 from public.library_items where file_id = p_id) or exists (select 1 from public.library_collections where cover_file = p_id) then
|
|
400
|
+
raise exception 'library_file_in_use';
|
|
401
|
+
end if;
|
|
402
|
+
delete from public.library_files where id = p_id returning key into k;
|
|
403
|
+
return jsonb_build_object('key', k);
|
|
404
|
+
end;
|
|
405
|
+
$$;
|
|
406
|
+
|
|
407
|
+
-- Makes or changes a sponsor slot. Slots on one show don't overlap.
|
|
408
|
+
create function public.library_sponsor_save(p jsonb)
|
|
409
|
+
returns jsonb
|
|
410
|
+
language plpgsql
|
|
411
|
+
security invoker
|
|
412
|
+
set search_path = ''
|
|
413
|
+
as $$
|
|
414
|
+
declare
|
|
415
|
+
cid uuid;
|
|
416
|
+
s public.library_sponsors;
|
|
417
|
+
begin
|
|
418
|
+
select id into cid from public.library_collections where slug = p->>'collection' and kind = 'show' for update;
|
|
419
|
+
if cid is null then raise exception 'library_not_found'; end if;
|
|
420
|
+
if exists (select 1 from public.library_sponsors o where o.collection_id = cid and o.id is distinct from (p->>'id')::uuid
|
|
421
|
+
and o.starts_on <= (p->>'ends_on')::date and o.ends_on >= (p->>'starts_on')::date) then
|
|
422
|
+
raise exception 'library_sponsor_overlap';
|
|
423
|
+
end if;
|
|
424
|
+
if p->>'id' is null then
|
|
425
|
+
insert into public.library_sponsors (collection_id, name, url, message, starts_on, ends_on)
|
|
426
|
+
values (cid, p->>'name', p->>'url', p->>'message', (p->>'starts_on')::date, (p->>'ends_on')::date) returning * into s;
|
|
427
|
+
else
|
|
428
|
+
update public.library_sponsors set name = p->>'name', url = p->>'url', message = p->>'message', starts_on = (p->>'starts_on')::date, ends_on = (p->>'ends_on')::date
|
|
429
|
+
where id = (p->>'id')::uuid and collection_id = cid returning * into s;
|
|
430
|
+
if s.id is null then raise exception 'library_not_found'; end if;
|
|
431
|
+
end if;
|
|
432
|
+
return jsonb_build_object('id', s.id, 'collection', s.collection_id, 'name', s.name, 'url', s.url, 'message', s.message, 'starts_on', s.starts_on, 'ends_on', s.ends_on);
|
|
433
|
+
end;
|
|
434
|
+
$$;
|
|
435
|
+
|
|
436
|
+
create function public.library_sponsor_delete(p_id uuid)
|
|
437
|
+
returns boolean
|
|
438
|
+
language plpgsql
|
|
439
|
+
security invoker
|
|
440
|
+
set search_path = ''
|
|
441
|
+
as $$
|
|
442
|
+
begin
|
|
443
|
+
delete from public.library_sponsors where id = p_id;
|
|
444
|
+
return found;
|
|
445
|
+
end;
|
|
446
|
+
$$;
|
|
447
|
+
|
|
448
|
+
-- Starts a member on a collection, once: the drip counts from this day.
|
|
449
|
+
create function public.library_enrol(p_slug text, p_member text, p_now timestamptz)
|
|
450
|
+
returns jsonb
|
|
451
|
+
language plpgsql
|
|
452
|
+
security invoker
|
|
453
|
+
set search_path = ''
|
|
454
|
+
as $$
|
|
455
|
+
declare
|
|
456
|
+
cid uuid;
|
|
457
|
+
begin
|
|
458
|
+
select id into cid from public.library_collections where slug = p_slug;
|
|
459
|
+
if cid is null then raise exception 'library_not_found'; end if;
|
|
460
|
+
insert into public.library_access (collection_id, member, source, started_at, created_at) values (cid, p_member, 'enrol', p_now, p_now)
|
|
461
|
+
on conflict (collection_id, member) where source = 'enrol' do nothing;
|
|
462
|
+
return jsonb_build_object('started_at', public.library_started(cid, p_member, p_now));
|
|
463
|
+
end;
|
|
464
|
+
$$;
|
|
465
|
+
|
|
466
|
+
-- Lets a member in after a payment, once per payment ref: the same ref again
|
|
467
|
+
-- answers the first grant (a webhook retried); the same ref for another
|
|
468
|
+
-- member or collection fails.
|
|
469
|
+
create function public.library_grant(p_slug text, p_member text, p_ref text, p_ends timestamptz, p_now timestamptz)
|
|
470
|
+
returns jsonb
|
|
471
|
+
language plpgsql
|
|
472
|
+
security invoker
|
|
473
|
+
set search_path = ''
|
|
474
|
+
as $$
|
|
475
|
+
declare
|
|
476
|
+
cid uuid;
|
|
477
|
+
a public.library_access;
|
|
478
|
+
begin
|
|
479
|
+
select id into cid from public.library_collections where slug = p_slug;
|
|
480
|
+
if cid is null then raise exception 'library_not_found'; end if;
|
|
481
|
+
insert into public.library_access (collection_id, member, source, ref, started_at, ends_at, created_at)
|
|
482
|
+
values (cid, p_member, 'grant', p_ref, p_now, p_ends, p_now)
|
|
483
|
+
on conflict (ref) do nothing
|
|
484
|
+
returning * into a;
|
|
485
|
+
if a.id is not null then
|
|
486
|
+
return jsonb_build_object('repeat', false, 'started_at', a.started_at, 'ends_at', a.ends_at);
|
|
487
|
+
end if;
|
|
488
|
+
select * into a from public.library_access where ref = p_ref;
|
|
489
|
+
if a.collection_id <> cid or a.member <> p_member then raise exception 'library_ref_reused'; end if;
|
|
490
|
+
return jsonb_build_object('repeat', true, 'started_at', a.started_at, 'ends_at', a.ends_at, 'revoked', a.revoked_at is not null);
|
|
491
|
+
end;
|
|
492
|
+
$$;
|
|
493
|
+
|
|
494
|
+
create function public.library_revoke(p_ref text, p_now timestamptz)
|
|
495
|
+
returns jsonb
|
|
496
|
+
language plpgsql
|
|
497
|
+
security invoker
|
|
498
|
+
set search_path = ''
|
|
499
|
+
as $$
|
|
500
|
+
begin
|
|
501
|
+
update public.library_access set revoked_at = p_now where ref = p_ref and revoked_at is null;
|
|
502
|
+
return jsonb_build_object('revoked', found);
|
|
503
|
+
end;
|
|
504
|
+
$$;
|
|
505
|
+
|
|
506
|
+
-- What the member has started or bought and still holds, for the member area.
|
|
507
|
+
create function public.library_mine(p_member text, p_now timestamptz)
|
|
508
|
+
returns jsonb
|
|
509
|
+
language sql
|
|
510
|
+
stable
|
|
511
|
+
security invoker
|
|
512
|
+
set search_path = ''
|
|
513
|
+
as $$
|
|
514
|
+
select coalesce(jsonb_agg(jsonb_build_object('collection', public.library_collection_json(c), 'started_at', x.started_at, 'ends_at', x.ends_at, 'bought', x.bought)
|
|
515
|
+
order by x.started_at desc), '[]'::jsonb)
|
|
516
|
+
from (
|
|
517
|
+
select a.collection_id, min(a.started_at) as started_at,
|
|
518
|
+
case when bool_or(a.ends_at is null) then null else max(a.ends_at) end as ends_at, bool_or(a.source = 'grant') as bought
|
|
519
|
+
from public.library_access a
|
|
520
|
+
where a.member = p_member and a.revoked_at is null and (a.ends_at is null or a.ends_at > p_now)
|
|
521
|
+
group by a.collection_id
|
|
522
|
+
) x join public.library_collections c on c.id = x.collection_id and c.status = 'published';
|
|
523
|
+
$$;
|
|
524
|
+
|
|
525
|
+
create function public.library_forget(p_member text)
|
|
526
|
+
returns jsonb
|
|
527
|
+
language plpgsql
|
|
528
|
+
security invoker
|
|
529
|
+
set search_path = ''
|
|
530
|
+
as $$
|
|
531
|
+
declare
|
|
532
|
+
n integer;
|
|
533
|
+
begin
|
|
534
|
+
delete from public.library_access where member = p_member;
|
|
535
|
+
get diagnostics n = row_count;
|
|
536
|
+
return jsonb_build_object('deleted', n);
|
|
537
|
+
end;
|
|
538
|
+
$$;
|
|
539
|
+
|
|
540
|
+
create function public.library_cleanup(p_now timestamptz, p_days integer)
|
|
541
|
+
returns jsonb
|
|
542
|
+
language plpgsql
|
|
543
|
+
security invoker
|
|
544
|
+
set search_path = ''
|
|
545
|
+
as $$
|
|
546
|
+
declare
|
|
547
|
+
n integer;
|
|
548
|
+
begin
|
|
549
|
+
delete from public.library_access
|
|
550
|
+
where coalesce(revoked_at, ends_at) is not null
|
|
551
|
+
and coalesce(revoked_at, ends_at) <= p_now - make_interval(days => p_days);
|
|
552
|
+
get diagnostics n = row_count;
|
|
553
|
+
return jsonb_build_object('deleted', n);
|
|
554
|
+
end;
|
|
555
|
+
$$;
|
|
556
|
+
|
|
557
|
+
-- Once each: the dripped lessons that opened for a member since the last
|
|
558
|
+
-- tick (midnight in the project's time zone, drip_days after they started),
|
|
559
|
+
-- and the scheduled items whose time came. Rows another tick holds are
|
|
560
|
+
-- skipped, so two crons at once never answer the same opening twice.
|
|
561
|
+
create function public.library_tick(p_tz text, p_now timestamptz)
|
|
562
|
+
returns jsonb
|
|
563
|
+
language plpgsql
|
|
564
|
+
security invoker
|
|
565
|
+
set search_path = ''
|
|
566
|
+
as $$
|
|
567
|
+
declare
|
|
568
|
+
opened jsonb;
|
|
569
|
+
published jsonb;
|
|
570
|
+
begin
|
|
571
|
+
with held as (
|
|
572
|
+
select a.* from public.library_access a
|
|
573
|
+
join public.library_collections c on c.id = a.collection_id and c.status = 'published'
|
|
574
|
+
where a.revoked_at is null and (a.ends_at is null or a.ends_at > p_now) and coalesce(a.notified_at, a.started_at) < p_now
|
|
575
|
+
for update of a skip locked
|
|
576
|
+
), due as (
|
|
577
|
+
select h.id as access_id, h.member, h.collection_id, i.id as item_id,
|
|
578
|
+
(((h.started_at at time zone p_tz)::date + i.drip_days)::timestamp at time zone p_tz) as at
|
|
579
|
+
from held h join public.library_items i on i.collection_id = h.collection_id
|
|
580
|
+
where i.drip_days > 0 and not i.preview and public.library_live(i, p_now)
|
|
581
|
+
), hits as (
|
|
582
|
+
select d.* from due d join held h on h.id = d.access_id
|
|
583
|
+
where d.at > coalesce(h.notified_at, h.started_at) and d.at <= p_now
|
|
584
|
+
), marked as (
|
|
585
|
+
update public.library_access a set notified_at = p_now from held h where a.id = h.id returning a.id
|
|
586
|
+
)
|
|
587
|
+
select coalesce(jsonb_agg(jsonb_build_object('member', member, 'collection', collection_id, 'item', item_id, 'at', at) order by at, member), '[]'::jsonb)
|
|
588
|
+
into opened from hits;
|
|
589
|
+
|
|
590
|
+
with live as (
|
|
591
|
+
select i.id from public.library_items i join public.library_collections c on c.id = i.collection_id and c.status = 'published'
|
|
592
|
+
where i.announced_at is null and i.publish_at is not null and public.library_live(i, p_now)
|
|
593
|
+
for update of i skip locked
|
|
594
|
+
), done as (
|
|
595
|
+
update public.library_items i set announced_at = p_now from live where i.id = live.id
|
|
596
|
+
returning i.collection_id, i.id, i.publish_at
|
|
597
|
+
)
|
|
598
|
+
select coalesce(jsonb_agg(jsonb_build_object('collection', collection_id, 'item', id, 'at', publish_at) order by publish_at), '[]'::jsonb) into published from done;
|
|
599
|
+
|
|
600
|
+
return jsonb_build_object('opened', opened, 'published', published);
|
|
601
|
+
end;
|
|
602
|
+
$$;
|
|
603
|
+
|
|
604
|
+
${HELPERS.map((s) => `revoke execute on function public.${s} from public, anon, authenticated;`).join("\n")}
|
|
605
|
+
${SIGNATURES.map((s) => `revoke execute on function public.${s} from public, anon, authenticated;`).join("\n")}
|
|
606
|
+
${HELPERS.map((s) => `grant execute on function public.${s} to service_role;`).join("\n")}
|
|
607
|
+
${SIGNATURES.map((s) => `grant execute on function public.${s} to service_role;`).join("\n")}
|
|
608
|
+
grant select, insert, update, delete on public.library_collections, public.library_files, public.library_items, public.library_sponsors, public.library_access to service_role;
|
|
609
|
+
`;
|
|
610
|
+
const down = `${[...SIGNATURES].reverse().map((s) => `drop function public.${s};`).join("\n")}
|
|
611
|
+
${[...HELPERS].reverse().map((s) => `drop function public.${s};`).join("\n")}
|
|
612
|
+
drop table public.library_access;
|
|
613
|
+
drop table public.library_sponsors;
|
|
614
|
+
drop table public.library_items;
|
|
615
|
+
alter table public.library_collections drop constraint library_collections_cover_file_fkey;
|
|
616
|
+
drop table public.library_files;
|
|
617
|
+
drop table public.library_collections;
|
|
618
|
+
`;
|
|
619
|
+
export const libraryMigration = { up, down };
|
package/dist/sponsors.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Sponsor slots: a slot covers every episode that came out between its start
|
|
2
|
+
// and end days, in the project's time zone.
|
|
3
|
+
import { dayIn } from "./time.js";
|
|
4
|
+
/** The sponsors whose slot covers the day an item came out. */
|
|
5
|
+
export function sponsorsFor(item, sponsors, timeZone) {
|
|
6
|
+
const day = dayIn(item.publish_at ?? item.created_at, timeZone);
|
|
7
|
+
return sponsors.filter((s) => s.starts_on <= day && day <= s.ends_on);
|
|
8
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export declare const STARTER_LIBRARY: {
|
|
2
|
+
readonly format: 1;
|
|
3
|
+
readonly country: "AU";
|
|
4
|
+
readonly time_zone: "Australia/Sydney";
|
|
5
|
+
readonly currency: "AUD";
|
|
6
|
+
readonly kinds: readonly ["course", "show", "shelf"];
|
|
7
|
+
readonly files: {
|
|
8
|
+
readonly max_mb: 100;
|
|
9
|
+
readonly types: readonly ["audio/mpeg", "audio/mp4", "application/pdf", "image/jpeg", "image/png", "application/zip"];
|
|
10
|
+
};
|
|
11
|
+
readonly embeds: readonly ["youtube", "vimeo"];
|
|
12
|
+
readonly link_minutes: 60;
|
|
13
|
+
readonly paths: {
|
|
14
|
+
readonly base: "/learn";
|
|
15
|
+
readonly files: "/library/files";
|
|
16
|
+
};
|
|
17
|
+
readonly retention: {
|
|
18
|
+
readonly ended_access_days: 365;
|
|
19
|
+
};
|
|
20
|
+
};
|
package/dist/starters.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// The library.json `npx workbench-library init` writes: courses and a show in
|
|
2
|
+
// Sydney, audio, PDFs and pictures up to 100 MB, YouTube and Vimeo players,
|
|
3
|
+
// gated links that last an hour, ended access kept a year. The client
|
|
4
|
+
// questions (capabilities/library.json, offer) set the rest.
|
|
5
|
+
export const STARTER_LIBRARY = {
|
|
6
|
+
format: 1,
|
|
7
|
+
country: "AU",
|
|
8
|
+
time_zone: "Australia/Sydney",
|
|
9
|
+
currency: "AUD",
|
|
10
|
+
kinds: ["course", "show", "shelf"],
|
|
11
|
+
files: { max_mb: 100, types: ["audio/mpeg", "audio/mp4", "application/pdf", "image/jpeg", "image/png", "application/zip"] },
|
|
12
|
+
embeds: ["youtube", "vimeo"],
|
|
13
|
+
link_minutes: 60,
|
|
14
|
+
paths: { base: "/learn", files: "/library/files" },
|
|
15
|
+
retention: { ended_access_days: 365 },
|
|
16
|
+
};
|