@rocapine/community 0.21.0 → 0.21.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rocapine/community",
3
- "version": "0.21.0",
3
+ "version": "0.21.1",
4
4
  "description": "CLI to install, upgrade and adopt the Rocapine Community SDK's Supabase backend (migrations, Edge Functions) into a project.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -0,0 +1,43 @@
1
+ -- The post and comment length limit (2000 / 1000) applies to new writing, not to history.
2
+ -- It moves from CHECK constraints (which every row must satisfy forever) to a trigger that an
3
+ -- importer can switch off: alter table public.posts disable trigger user (and comments).
4
+ -- Idempotent.
5
+
6
+ create or replace function public.enforce_content_length()
7
+ returns trigger language plpgsql set search_path = '' as $$
8
+ declare
9
+ max_len int := case tg_table_name when 'posts' then 2000 else 1000 end;
10
+ begin
11
+ if char_length(new.content) > max_len then
12
+ raise exception 'content is longer than % characters', max_len using errcode = 'check_violation';
13
+ end if;
14
+ return new;
15
+ end $$;
16
+
17
+ drop trigger if exists posts_content_length on public.posts;
18
+ create trigger posts_content_length
19
+ before insert or update of content on public.posts
20
+ for each row execute function public.enforce_content_length();
21
+
22
+ drop trigger if exists comments_content_length on public.comments;
23
+ create trigger comments_content_length
24
+ before insert or update of content on public.comments
25
+ for each row execute function public.enforce_content_length();
26
+
27
+ -- The checks keep only the minimum. With the images module the text is optional when there is an
28
+ -- image (images/001 added image_path), so keep that branch when the column exists.
29
+ do $$
30
+ declare
31
+ t text;
32
+ cond text;
33
+ begin
34
+ foreach t in array array['posts', 'comments'] loop
35
+ cond := 'char_length(content) >= 1';
36
+ if exists (select 1 from information_schema.columns
37
+ where table_schema = 'public' and table_name = t and column_name = 'image_path') then
38
+ cond := cond || ' or image_path is not null';
39
+ end if;
40
+ execute format('alter table public.%I drop constraint if exists %I', t, t || '_content_check');
41
+ execute format('alter table public.%I add constraint %I check (%s)', t, t || '_content_check', cond);
42
+ end loop;
43
+ end $$;
@@ -0,0 +1,255 @@
1
+ -- community-sdk core/019_placeholders.sql (schema v1)
2
+ -- Placeholder profiles: a real auth user created server-side to hold the posts of an author who is
3
+ -- not in the community yet (a history import). Shown like any member, but left out of the
4
+ -- dashboard counts exactly like house accounts. is_placeholder is not client-writable: core/002
5
+ -- grants authenticated UPDATE on username, amplitude_id and revenuecat_id only.
6
+ -- admin_daily_metrics and admin_activity_breakdown (core/017) are redefined with the one change
7
+ -- not is_house -> not (is_house or is_placeholder). Requires core/017. Fully idempotent.
8
+
9
+ alter table public.profiles add column if not exists is_placeholder boolean not null default false;
10
+
11
+ create or replace function public.admin_daily_metrics(from_date date, to_date date, tz text default 'UTC')
12
+ returns jsonb
13
+ language plpgsql
14
+ stable
15
+ set search_path = public
16
+ as $$
17
+ declare
18
+ t0 timestamptz;
19
+ t1 timestamptz;
20
+ -- Member rows of an optional module in the window: per-day counts and the distinct [user, day] pairs.
21
+ extra text := $q$
22
+ with r as (
23
+ select x.user_id, (x.created_at at time zone $3)::date as day
24
+ from %I x join profiles a on a.id = x.user_id and not (a.is_house or a.is_placeholder)
25
+ where x.created_at >= $1 and x.created_at < $2)
26
+ select coalesce((select jsonb_object_agg(day, n) from (select day, count(*) as n from r group by day) s), '{}'),
27
+ coalesce((select jsonb_agg(distinct jsonb_build_array(user_id, day)) from r), '[]')$q$;
28
+ vote_days jsonb;
29
+ vote_pairs jsonb := '[]';
30
+ reaction_days jsonb;
31
+ reaction_pairs jsonb := '[]';
32
+ result jsonb;
33
+ begin
34
+ if tz is null or tz not in (select name from pg_timezone_names) then
35
+ raise exception 'invalid_time_zone' using errcode = '22023';
36
+ end if;
37
+ t0 := from_date::timestamp at time zone tz;
38
+ t1 := (to_date + 1)::timestamp at time zone tz;
39
+
40
+ if to_regclass('public.poll_votes') is not null then
41
+ execute format(extra, 'poll_votes') into vote_days, vote_pairs using t0, t1, tz;
42
+ end if;
43
+ if to_regclass('public.post_reactions') is not null then
44
+ execute format(extra, 'post_reactions') into reaction_days, reaction_pairs using t0, t1, tz;
45
+ end if;
46
+
47
+ with
48
+ days as (select generate_series(from_date, to_date, interval '1 day')::date as day),
49
+ -- first_reply_min: minutes to the first non-deleted comment by someone other than the author.
50
+ m_posts as (
51
+ select author_id, topic, day, eligible, coalesce(first_reply_min <= 1440, false) as replied,
52
+ first_reply_min is not null as answered, first_reply_min
53
+ from (
54
+ select p.author_id, p.topic, (p.created_at at time zone tz)::date as day,
55
+ p.created_at <= now() - interval '24 hours' as eligible,
56
+ (select extract(epoch from min(c.created_at) - p.created_at) / 60 from comments c
57
+ where c.post_id = p.id and c.status <> 'deleted' and c.author_id <> p.author_id) as first_reply_min
58
+ from posts p join profiles a on a.id = p.author_id and not (a.is_house or a.is_placeholder)
59
+ where p.status <> 'deleted' and p.created_at >= t0 and p.created_at < t1) q),
60
+ m_comments as (
61
+ select c.author_id, (c.created_at at time zone tz)::date as day
62
+ from comments c join profiles a on a.id = c.author_id and not (a.is_house or a.is_placeholder)
63
+ where c.status <> 'deleted' and c.created_at >= t0 and c.created_at < t1),
64
+ m_likes as (
65
+ select l.user_id, (l.created_at at time zone tz)::date as day
66
+ from likes l join profiles a on a.id = l.user_id and not (a.is_house or a.is_placeholder)
67
+ where l.created_at >= t0 and l.created_at < t1
68
+ union all
69
+ select l.user_id, (l.created_at at time zone tz)::date
70
+ from comment_likes l join profiles a on a.id = l.user_id and not (a.is_house or a.is_placeholder)
71
+ where l.created_at >= t0 and l.created_at < t1),
72
+ writes as (select author_id, day from m_posts union all select author_id, day from m_comments),
73
+ extra_pairs as (
74
+ select (e->>0)::uuid as who, (e->>1)::date as day
75
+ from jsonb_array_elements(vote_pairs || reaction_pairs) e),
76
+ activity as (
77
+ select author_id as who, day from writes
78
+ union all select user_id, day from m_likes
79
+ union all select who, day from extra_pairs),
80
+ active_by_day as (select day, count(distinct who) as n from activity group by day),
81
+ new_by_day as (
82
+ select (created_at at time zone tz)::date as day, count(*) as n
83
+ from profiles where not (is_house or is_placeholder) and created_at >= t0 and created_at < t1 group by 1),
84
+ -- A new poster has a post in the window, so only those authors are looked up.
85
+ new_posters as (
86
+ select (min(p.created_at) at time zone tz)::date as day
87
+ from posts p
88
+ where p.status <> 'deleted' and p.author_id in (select author_id from m_posts)
89
+ group by p.author_id
90
+ having min(p.created_at) >= t0),
91
+ hidden as (
92
+ select (p.moderated_at at time zone tz)::date as day
93
+ from posts p join profiles a on a.id = p.author_id and not (a.is_house or a.is_placeholder)
94
+ where p.status = 'hidden' and p.moderated_at >= t0 and p.moderated_at < t1),
95
+ contributions as (select author_id, count(*) as n from writes group by author_id),
96
+ -- One pass per day: every row tagged with its kind, then counted by kind.
97
+ events as (
98
+ select day, 'post' as kind, author_id as who, eligible, eligible and replied as replied from m_posts
99
+ union all select day, 'comment', author_id, false, false from m_comments
100
+ union all select day, 'like', user_id, false, false from m_likes
101
+ union all select day, 'new_poster', null, false, false from new_posters
102
+ union all select day, 'hidden', null, false, false from hidden),
103
+ per_day as (
104
+ select day,
105
+ count(*) filter (where kind = 'post') as posts,
106
+ count(*) filter (where kind = 'comment') as comments,
107
+ count(*) filter (where kind = 'new_poster') as new_posters,
108
+ count(*) filter (where kind = 'hidden') as hidden,
109
+ count(*) filter (where kind = 'like') as likes,
110
+ count(*) filter (where replied) as replied,
111
+ count(*) filter (where eligible) as eligible,
112
+ count(distinct who) filter (where kind in ('post', 'comment')) as contributors
113
+ from events group by day)
114
+ select jsonb_build_object(
115
+ 'tz', tz,
116
+ 'daily', (select coalesce(jsonb_agg(jsonb_build_object(
117
+ 'day', d.day,
118
+ 'posts', coalesce(x.posts, 0),
119
+ 'comments', coalesce(x.comments, 0),
120
+ 'new_posters', coalesce(x.new_posters, 0),
121
+ 'hidden', coalesce(x.hidden, 0),
122
+ 'likes', coalesce(x.likes, 0),
123
+ 'poll_votes', case when vote_days is not null then coalesce((vote_days->>d.day::text)::int, 0) end,
124
+ 'reactions', case when reaction_days is not null then coalesce((reaction_days->>d.day::text)::int, 0) end,
125
+ 'replied_24h', coalesce(x.replied, 0),
126
+ 'reply_eligible', coalesce(x.eligible, 0),
127
+ 'contributors', coalesce(x.contributors, 0),
128
+ 'new_members', coalesce(nm.n, 0),
129
+ 'active_members', coalesce(am.n, 0)
130
+ ) order by d.day), '[]'::jsonb) from days d left join per_day x on x.day = d.day
131
+ left join new_by_day nm on nm.day = d.day left join active_by_day am on am.day = d.day),
132
+ 'topics', (select coalesce(jsonb_agg(jsonb_build_object('topic', topic, 'count', n) order by n desc, topic), '[]')
133
+ from (select coalesce(topic, '(sans)') as topic, count(*) as n from m_posts group by 1) t),
134
+ 'reply_rate', (select coalesce(count(*) filter (where replied)::float / nullif(count(*), 0), 0)
135
+ from m_posts where eligible),
136
+ 'unanswered', (select count(*) from m_posts where eligible and not answered),
137
+ 'contributors', (select count(*) from contributions),
138
+ 'new_members', (select coalesce(sum(n), 0) from new_by_day),
139
+ 'active_members', (select count(distinct who) from activity),
140
+ 'members_total', (select count(*) from profiles where not (is_house or is_placeholder) and created_at < t1),
141
+ 'median_first_reply_minutes', (select percentile_cont(0.5) within group (order by first_reply_min)
142
+ from m_posts where eligible and first_reply_min is not null),
143
+ 'reactors', (select count(distinct u) from (
144
+ select user_id as u from m_likes
145
+ union all select (e->>0)::uuid from jsonb_array_elements(vote_pairs) e
146
+ union all select (e->>0)::uuid from jsonb_array_elements(reaction_pairs) e) r
147
+ where u not in (select author_id from contributions)),
148
+ 'contribution_buckets', (select jsonb_build_object(
149
+ '1', count(*) filter (where n = 1),
150
+ '2-3', count(*) filter (where n between 2 and 3),
151
+ '4-7', count(*) filter (where n between 4 and 7),
152
+ '8+', count(*) filter (where n >= 8)) from contributions))
153
+ into result;
154
+ return result;
155
+ end;
156
+ $$;
157
+
158
+ revoke execute on function public.admin_daily_metrics(date, date, text) from public, anon, authenticated;
159
+
160
+ create or replace function public.admin_activity_breakdown(from_date date, to_date date, tz text default 'UTC')
161
+ returns jsonb
162
+ language plpgsql
163
+ stable
164
+ set search_path = public
165
+ as $$
166
+ declare
167
+ t0 timestamptz;
168
+ t1 timestamptz;
169
+ pt0 timestamptz; -- the previous period: as many days, right before from_date
170
+ polls jsonb;
171
+ -- profiles.locale comes from the push module: without it, languages is null.
172
+ has_locale boolean := exists (select 1 from pg_attribute
173
+ where attrelid = 'public.profiles'::regclass and attname = 'locale' and not attisdropped);
174
+ result jsonb;
175
+ begin
176
+ if tz is null or tz not in (select name from pg_timezone_names) then
177
+ raise exception 'invalid_time_zone' using errcode = '22023';
178
+ end if;
179
+ t0 := from_date::timestamp at time zone tz;
180
+ t1 := (to_date + 1)::timestamp at time zone tz;
181
+ pt0 := (from_date - greatest(to_date - from_date + 1, 0))::timestamp at time zone tz;
182
+
183
+ with
184
+ -- Member posts and comments (not deleted) of both periods; locale is read through to_jsonb
185
+ -- so the function also compiles where the push module's column does not exist.
186
+ writes as (
187
+ select p.author_id, p.created_at, case when has_locale then to_jsonb(a)->>'locale' end as locale
188
+ from posts p join profiles a on a.id = p.author_id and not (a.is_house or a.is_placeholder)
189
+ where p.status <> 'deleted' and p.created_at >= pt0 and p.created_at < t1
190
+ union all
191
+ select c.author_id, c.created_at, case when has_locale then to_jsonb(a)->>'locale' end
192
+ from comments c join profiles a on a.id = c.author_id and not (a.is_house or a.is_placeholder)
193
+ where c.status <> 'deleted' and c.created_at >= pt0 and c.created_at < t1),
194
+ cur as (select * from writes where created_at >= t0),
195
+ heat as (
196
+ select extract(isodow from created_at at time zone tz)::int - 1 as dow,
197
+ extract(hour from created_at at time zone tz)::int as hr, count(*) as n
198
+ from cur group by 1, 2),
199
+ mod as (
200
+ select w.k,
201
+ (select count(*) from reports r join profiles a on a.id = r.reporter_id and not (a.is_house or a.is_placeholder)
202
+ where r.created_at >= w.a and r.created_at < w.b) as reports,
203
+ (select count(*) from (
204
+ select 1 from posts p join profiles a on a.id = p.author_id and not (a.is_house or a.is_placeholder)
205
+ where p.moderation_reason is not null and p.moderated_at >= w.a and p.moderated_at < w.b
206
+ union all
207
+ select 1 from comments c join profiles a on a.id = c.author_id and not (a.is_house or a.is_placeholder)
208
+ where c.moderation_reason is not null and c.moderated_at >= w.a and c.moderated_at < w.b) q) as ai_hidden,
209
+ (select count(*) from moderation_actions m where m.created_at >= w.a and m.created_at < w.b
210
+ and m.action in ('hide','restore','confirm_hide','reject_report','ban','unban')) as team_actions
211
+ from (values ('cur', t0, t1), ('prev', pt0, t0)) w(k, a, b)),
212
+ top as (select * from (
213
+ -- image_path and attachment come from optional modules: read through to_jsonb so their absence is null.
214
+ select p.id, p.content, to_jsonb(p)->>'image_path' as image_path, to_jsonb(p)->'attachment' as attachment, p.topic, p.author_id, a.username as author_name, p.created_at,
215
+ (select count(*) from likes l join profiles u on u.id = l.user_id and not (u.is_house or u.is_placeholder) where l.post_id = p.id) as likes,
216
+ (select count(*) from comments c join profiles u on u.id = c.author_id and not (u.is_house or u.is_placeholder)
217
+ where c.post_id = p.id and c.status <> 'deleted') as comments
218
+ from posts p join profiles a on a.id = p.author_id and not (a.is_house or a.is_placeholder)
219
+ where p.status <> 'deleted' and p.created_at >= t0 and p.created_at < t1) q
220
+ order by likes + comments desc, created_at desc, id limit 5)
221
+ select jsonb_build_object(
222
+ 'tz', tz,
223
+ 'heatmap', (select jsonb_agg((select jsonb_agg(coalesce((select n from heat where heat.dow = d and heat.hr = hh), 0) order by hh)
224
+ from generate_series(0, 23) hh) order by d) from generate_series(0, 6) d),
225
+ 'languages', case when has_locale then (select coalesce(jsonb_agg(jsonb_build_object('locale', locale, 'count', n)
226
+ order by n desc, locale), '[]') from (select locale, count(*) as n from cur group by locale) l) end,
227
+ 'moderation', (select jsonb_build_object('reports', c.reports, 'ai_hidden', c.ai_hidden, 'team_actions', c.team_actions,
228
+ 'previous', jsonb_build_object('reports', v.reports, 'ai_hidden', v.ai_hidden, 'team_actions', v.team_actions))
229
+ from mod c, mod v where c.k = 'cur' and v.k = 'prev'),
230
+ 'returning', jsonb_build_object(
231
+ 'previous_contributors', (select count(distinct author_id) from writes where created_at < t0),
232
+ 'returned', (select count(distinct author_id) from cur
233
+ where author_id in (select author_id from writes where created_at < t0))),
234
+ 'top_posts', (select coalesce(jsonb_agg(jsonb_build_object('id', id, 'content', content, 'image_path', image_path,
235
+ 'attachment', attachment, 'poll', null, 'topic', topic,
236
+ 'author_id', author_id, 'author_name', author_name, 'likes', likes, 'comments', comments,
237
+ 'created_at', to_char(created_at at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"'))
238
+ order by likes + comments desc, created_at desc, id), '[]') from top))
239
+ into result;
240
+ -- A top post's poll (polls module): its options in order with their vote counts.
241
+ if to_regclass('public.poll_options') is not null and jsonb_array_length(result->'top_posts') > 0 then
242
+ execute $q$select coalesce(jsonb_object_agg(post_id, opts), '{}') from (
243
+ select o.post_id, jsonb_agg(jsonb_build_object('label', o.label,
244
+ 'votes', (select count(*) from public.poll_votes v where v.option_id = o.id)) order by o.idx) as opts
245
+ from public.poll_options o where o.post_id = any($1) group by o.post_id) x$q$
246
+ into polls
247
+ using (select array_agg((e->>'id')::uuid) from jsonb_array_elements(result->'top_posts') e);
248
+ result := jsonb_set(result, '{top_posts}', (select jsonb_agg(jsonb_set(e, '{poll}', coalesce(polls->(e->>'id'), 'null'))
249
+ order by n) from jsonb_array_elements(result->'top_posts') with ordinality t(e, n)));
250
+ end if;
251
+ return result;
252
+ end;
253
+ $$;
254
+
255
+ revoke execute on function public.admin_activity_breakdown(date, date, text) from public, anon, authenticated;
@@ -0,0 +1,11 @@
1
+ -- The length maximum now lives in the core triggers (core/018_content_length.sql), so an importer
2
+ -- can switch it off. images/001 puts the maximum back in the checks on a fresh install (it runs
3
+ -- after core): take it out again. Idempotent.
4
+ alter table public.posts drop constraint if exists posts_content_check;
5
+ alter table public.posts add constraint posts_content_check check (
6
+ char_length(content) >= 1 or image_path is not null
7
+ );
8
+ alter table public.comments drop constraint if exists comments_content_check;
9
+ alter table public.comments add constraint comments_content_check check (
10
+ char_length(content) >= 1 or image_path is not null
11
+ );