@rocapine/community 0.17.0 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/lib/index.js +2 -2
- package/lib/install-shared.d.ts +1 -1
- package/lib/install-shared.js +1 -0
- package/lib/substitute.d.ts +15 -0
- package/lib/substitute.js +25 -0
- package/package.json +1 -1
- package/templates/migrations/core/017_activity_metrics.sql +284 -0
- package/templates/migrations/presence/001_presence.sql +139 -0
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ version. The binary is named `community`.
|
|
|
13
13
|
## `init`
|
|
14
14
|
|
|
15
15
|
```bash
|
|
16
|
-
npx @rocapine/community init [--modules core,push,polls,reaction,inbox,translation,images,attachments,badges,streaks] [--project-url <url>] [--anon-key <key>] [--dir supabase]
|
|
16
|
+
npx @rocapine/community init [--modules core,push,polls,reaction,inbox,translation,images,attachments,badges,presence,streaks] [--project-url <url>] [--anon-key <key>] [--dir supabase]
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
Copies the migrations and Edge Functions for the requested modules into
|
|
@@ -21,10 +21,10 @@ Copies the migrations and Edge Functions for the requested modules into
|
|
|
21
21
|
relative to the current directory), and writes a `community-sdk.json`
|
|
22
22
|
manifest at the repo root recording what was installed.
|
|
23
23
|
|
|
24
|
-
- `--modules` — comma-separated (`core,push,polls,reaction,inbox,translation,images,attachments,badges,streaks`). `core`
|
|
24
|
+
- `--modules` — comma-separated (`core,push,polls,reaction,inbox,translation,images,attachments,badges,presence,streaks`). `core`
|
|
25
25
|
is always implied even if omitted. Omit the flag entirely to install every
|
|
26
26
|
module. Regardless of the order you type them, modules are installed
|
|
27
|
-
`core → push → polls → reaction → inbox → translation → images → attachments → badges → streaks` — this order is load-bearing:
|
|
27
|
+
`core → push → polls → reaction → inbox → translation → images → attachments → badges → presence → streaks` — this order is load-bearing:
|
|
28
28
|
the inbox module's reaction trigger is guarded at install time and must
|
|
29
29
|
find the reaction module's table already present, so reaction has to land
|
|
30
30
|
first. `streaks` (deprecated: it moves an existing streak install onto badges) always brings `badges`. Requesting `inbox` without `reaction`, or `translation` without `polls`, prints a (non-fatal) warning (reactions missing from the inbox; poll labels left untranslated).
|
package/lib/index.js
CHANGED
|
@@ -60,7 +60,7 @@ program
|
|
|
60
60
|
program
|
|
61
61
|
.command("init")
|
|
62
62
|
.description("Copy the community backend into a Supabase project: migrations, Edge Functions, and a community-sdk.json manifest")
|
|
63
|
-
.option("--modules <modules>", "comma-separated module list (core,push,polls,reaction,inbox,translation,images,attachments,badges,streaks) — core is always implied; defaults to all modules")
|
|
63
|
+
.option("--modules <modules>", "comma-separated module list (core,push,polls,reaction,inbox,translation,images,attachments,badges,presence,streaks) — core is always implied; defaults to all modules")
|
|
64
64
|
.option("--project-url <url>", "Supabase project URL, e.g. https://<ref>.supabase.co — optional, only fills in the printed settings seed statement")
|
|
65
65
|
.option("--anon-key <key>", "Supabase anon key — optional, only fills in the printed settings seed statement")
|
|
66
66
|
.option("--dir <dir>", "target Supabase directory, relative to the current directory", "supabase")
|
|
@@ -103,7 +103,7 @@ program
|
|
|
103
103
|
.command("adopt")
|
|
104
104
|
.description("Register an already-live community backend (e.g. an app that hand-rolled its own before this SDK existed) with community-sdk.json, without copying any files")
|
|
105
105
|
.requiredOption("--schema-version <n>", "the community-sdk schema version this app's live backend corresponds to")
|
|
106
|
-
.requiredOption("--modules <modules>", "comma-separated module list (core,push,polls,reaction,inbox,translation,images,attachments,badges,streaks) — core is always implied")
|
|
106
|
+
.requiredOption("--modules <modules>", "comma-separated module list (core,push,polls,reaction,inbox,translation,images,attachments,badges,presence,streaks) — core is always implied")
|
|
107
107
|
.option("--dir <dir>", "Supabase directory this app would use, relative to the current directory", "supabase")
|
|
108
108
|
.action(async (opts) => {
|
|
109
109
|
try {
|
package/lib/install-shared.d.ts
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* single source of truth for module names, order, and the function sets
|
|
11
11
|
* each module pulls in.
|
|
12
12
|
*/
|
|
13
|
-
export declare const MODULE_ORDER: readonly ["core", "push", "polls", "reaction", "inbox", "translation", "images", "attachments", "badges", "streaks"];
|
|
13
|
+
export declare const MODULE_ORDER: readonly ["core", "push", "polls", "reaction", "inbox", "translation", "images", "attachments", "badges", "presence", "streaks"];
|
|
14
14
|
export type Module = (typeof MODULE_ORDER)[number];
|
|
15
15
|
export declare function functionsForModules(modules: readonly Module[]): string[];
|
|
16
16
|
/**
|
package/lib/install-shared.js
CHANGED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Replaces the `__SUPABASE_PROJECT_URL__` / `__SUPABASE_ANON_KEY__` placeholders
|
|
3
|
+
* baked into the shipped migration SQL (see core/003, push/002, reaction/001)
|
|
4
|
+
* with the values for the app's own Supabase project.
|
|
5
|
+
*
|
|
6
|
+
* Throws if any `__SUPABASE...` token remains after substitution — a guard
|
|
7
|
+
* against a third, unhandled placeholder silently reaching the app's
|
|
8
|
+
* migrations directory (the migration's own DO block re-checks this at
|
|
9
|
+
* `db push` time, but failing fast here gives a clearer error before any
|
|
10
|
+
* SQL is written to disk).
|
|
11
|
+
*/
|
|
12
|
+
export declare function substitutePlaceholders(sql: string, values: {
|
|
13
|
+
projectUrl: string;
|
|
14
|
+
anonKey: string;
|
|
15
|
+
}): string;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.substitutePlaceholders = substitutePlaceholders;
|
|
4
|
+
/**
|
|
5
|
+
* Replaces the `__SUPABASE_PROJECT_URL__` / `__SUPABASE_ANON_KEY__` placeholders
|
|
6
|
+
* baked into the shipped migration SQL (see core/003, push/002, reaction/001)
|
|
7
|
+
* with the values for the app's own Supabase project.
|
|
8
|
+
*
|
|
9
|
+
* Throws if any `__SUPABASE...` token remains after substitution — a guard
|
|
10
|
+
* against a third, unhandled placeholder silently reaching the app's
|
|
11
|
+
* migrations directory (the migration's own DO block re-checks this at
|
|
12
|
+
* `db push` time, but failing fast here gives a clearer error before any
|
|
13
|
+
* SQL is written to disk).
|
|
14
|
+
*/
|
|
15
|
+
function substitutePlaceholders(sql, values) {
|
|
16
|
+
const result = sql
|
|
17
|
+
.split("__SUPABASE_PROJECT_URL__")
|
|
18
|
+
.join(values.projectUrl)
|
|
19
|
+
.split("__SUPABASE_ANON_KEY__")
|
|
20
|
+
.join(values.anonKey);
|
|
21
|
+
if (result.includes("__SUPABASE")) {
|
|
22
|
+
throw new Error("community-sdk: unsubstituted __SUPABASE placeholder remains after substitution");
|
|
23
|
+
}
|
|
24
|
+
return result;
|
|
25
|
+
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
-- community-sdk core/017_activity_metrics.sql (schema v1)
|
|
2
|
+
-- admin_daily_metrics, fixed for the dashboard's Activity section (service role only):
|
|
3
|
+
-- days in the team's zone (tz, an IANA name; two arguments mean UTC), house accounts out of
|
|
4
|
+
-- every count, posts and comments of any status but deleted, the reply rate per day (a reply is
|
|
5
|
+
-- a comment not deleted by someone other than the author within 24 h; posts under 24 h old are
|
|
6
|
+
-- left out), new posters on their first post that isn't deleted, and likes, poll votes,
|
|
7
|
+
-- reactions, unanswered posts, contributors and reactors. Poll votes and reactions are null
|
|
8
|
+
-- when the polls / reaction module is not installed. Also: new members (profiles created in the
|
|
9
|
+
-- window, per day and in all), active members (distinct members with any post, comment, like,
|
|
10
|
+
-- comment like, poll vote or reaction, per day and over the period), members_total (profiles
|
|
11
|
+
-- created before the window's end) and median_first_reply_minutes (median minutes to the first
|
|
12
|
+
-- non-deleted reply by someone else, over posts at least 24 h old; null when none was replied).
|
|
13
|
+
-- created_at indexes keep the window scans cheap. Replaces the two-argument version of core/004.
|
|
14
|
+
-- Also admin_activity_breakdown(from_date, to_date, tz): the Activity page's breakdowns in one
|
|
15
|
+
-- call (service role only): a 7 x 24 heatmap (Monday first, hours in tz) of member posts and
|
|
16
|
+
-- comments, those by author language (profiles.locale, from the push module; null when the
|
|
17
|
+
-- column is absent: then languages is null), moderation counts (reports by members, items the AI hid whatever their status or review now,
|
|
18
|
+
-- team moderation actions) against the previous period of the same length, how many of the previous period's
|
|
19
|
+
-- contributors came back, and the five top posts (whole content, image, verse, poll with votes). Same zone check and window as above.
|
|
20
|
+
-- Requires core/004. Fully idempotent.
|
|
21
|
+
|
|
22
|
+
create index if not exists posts_created_at_idx on public.posts (created_at);
|
|
23
|
+
create index if not exists comments_created_at_idx on public.comments (created_at);
|
|
24
|
+
create index if not exists likes_created_at_idx on public.likes (created_at);
|
|
25
|
+
create index if not exists comment_likes_created_at_idx on public.comment_likes (created_at);
|
|
26
|
+
create index if not exists profiles_created_at_idx on public.profiles (created_at);
|
|
27
|
+
create index if not exists reports_created_at_idx on public.reports (created_at);
|
|
28
|
+
do $$
|
|
29
|
+
begin
|
|
30
|
+
if to_regclass('public.poll_votes') is not null then
|
|
31
|
+
create index if not exists poll_votes_created_at_idx on public.poll_votes (created_at);
|
|
32
|
+
end if;
|
|
33
|
+
if to_regclass('public.post_reactions') is not null then
|
|
34
|
+
create index if not exists post_reactions_created_at_idx on public.post_reactions (created_at);
|
|
35
|
+
end if;
|
|
36
|
+
end $$;
|
|
37
|
+
|
|
38
|
+
drop function if exists public.admin_daily_metrics(date, date);
|
|
39
|
+
|
|
40
|
+
create or replace function public.admin_daily_metrics(from_date date, to_date date, tz text default 'UTC')
|
|
41
|
+
returns jsonb
|
|
42
|
+
language plpgsql
|
|
43
|
+
stable
|
|
44
|
+
set search_path = public
|
|
45
|
+
as $$
|
|
46
|
+
declare
|
|
47
|
+
t0 timestamptz;
|
|
48
|
+
t1 timestamptz;
|
|
49
|
+
-- Member rows of an optional module in the window: per-day counts and the distinct [user, day] pairs.
|
|
50
|
+
extra text := $q$
|
|
51
|
+
with r as (
|
|
52
|
+
select x.user_id, (x.created_at at time zone $3)::date as day
|
|
53
|
+
from %I x join profiles a on a.id = x.user_id and not a.is_house
|
|
54
|
+
where x.created_at >= $1 and x.created_at < $2)
|
|
55
|
+
select coalesce((select jsonb_object_agg(day, n) from (select day, count(*) as n from r group by day) s), '{}'),
|
|
56
|
+
coalesce((select jsonb_agg(distinct jsonb_build_array(user_id, day)) from r), '[]')$q$;
|
|
57
|
+
vote_days jsonb;
|
|
58
|
+
vote_pairs jsonb := '[]';
|
|
59
|
+
reaction_days jsonb;
|
|
60
|
+
reaction_pairs jsonb := '[]';
|
|
61
|
+
result jsonb;
|
|
62
|
+
begin
|
|
63
|
+
if tz is null or tz not in (select name from pg_timezone_names) then
|
|
64
|
+
raise exception 'invalid_time_zone' using errcode = '22023';
|
|
65
|
+
end if;
|
|
66
|
+
t0 := from_date::timestamp at time zone tz;
|
|
67
|
+
t1 := (to_date + 1)::timestamp at time zone tz;
|
|
68
|
+
|
|
69
|
+
if to_regclass('public.poll_votes') is not null then
|
|
70
|
+
execute format(extra, 'poll_votes') into vote_days, vote_pairs using t0, t1, tz;
|
|
71
|
+
end if;
|
|
72
|
+
if to_regclass('public.post_reactions') is not null then
|
|
73
|
+
execute format(extra, 'post_reactions') into reaction_days, reaction_pairs using t0, t1, tz;
|
|
74
|
+
end if;
|
|
75
|
+
|
|
76
|
+
with
|
|
77
|
+
days as (select generate_series(from_date, to_date, interval '1 day')::date as day),
|
|
78
|
+
-- first_reply_min: minutes to the first non-deleted comment by someone other than the author.
|
|
79
|
+
m_posts as (
|
|
80
|
+
select author_id, topic, day, eligible, coalesce(first_reply_min <= 1440, false) as replied,
|
|
81
|
+
first_reply_min is not null as answered, first_reply_min
|
|
82
|
+
from (
|
|
83
|
+
select p.author_id, p.topic, (p.created_at at time zone tz)::date as day,
|
|
84
|
+
p.created_at <= now() - interval '24 hours' as eligible,
|
|
85
|
+
(select extract(epoch from min(c.created_at) - p.created_at) / 60 from comments c
|
|
86
|
+
where c.post_id = p.id and c.status <> 'deleted' and c.author_id <> p.author_id) as first_reply_min
|
|
87
|
+
from posts p join profiles a on a.id = p.author_id and not a.is_house
|
|
88
|
+
where p.status <> 'deleted' and p.created_at >= t0 and p.created_at < t1) q),
|
|
89
|
+
m_comments as (
|
|
90
|
+
select c.author_id, (c.created_at at time zone tz)::date as day
|
|
91
|
+
from comments c join profiles a on a.id = c.author_id and not a.is_house
|
|
92
|
+
where c.status <> 'deleted' and c.created_at >= t0 and c.created_at < t1),
|
|
93
|
+
m_likes as (
|
|
94
|
+
select l.user_id, (l.created_at at time zone tz)::date as day
|
|
95
|
+
from likes l join profiles a on a.id = l.user_id and not a.is_house
|
|
96
|
+
where l.created_at >= t0 and l.created_at < t1
|
|
97
|
+
union all
|
|
98
|
+
select l.user_id, (l.created_at at time zone tz)::date
|
|
99
|
+
from comment_likes l join profiles a on a.id = l.user_id and not a.is_house
|
|
100
|
+
where l.created_at >= t0 and l.created_at < t1),
|
|
101
|
+
writes as (select author_id, day from m_posts union all select author_id, day from m_comments),
|
|
102
|
+
extra_pairs as (
|
|
103
|
+
select (e->>0)::uuid as who, (e->>1)::date as day
|
|
104
|
+
from jsonb_array_elements(vote_pairs || reaction_pairs) e),
|
|
105
|
+
activity as (
|
|
106
|
+
select author_id as who, day from writes
|
|
107
|
+
union all select user_id, day from m_likes
|
|
108
|
+
union all select who, day from extra_pairs),
|
|
109
|
+
active_by_day as (select day, count(distinct who) as n from activity group by day),
|
|
110
|
+
new_by_day as (
|
|
111
|
+
select (created_at at time zone tz)::date as day, count(*) as n
|
|
112
|
+
from profiles where not is_house and created_at >= t0 and created_at < t1 group by 1),
|
|
113
|
+
-- A new poster has a post in the window, so only those authors are looked up.
|
|
114
|
+
new_posters as (
|
|
115
|
+
select (min(p.created_at) at time zone tz)::date as day
|
|
116
|
+
from posts p
|
|
117
|
+
where p.status <> 'deleted' and p.author_id in (select author_id from m_posts)
|
|
118
|
+
group by p.author_id
|
|
119
|
+
having min(p.created_at) >= t0),
|
|
120
|
+
hidden as (
|
|
121
|
+
select (p.moderated_at at time zone tz)::date as day
|
|
122
|
+
from posts p join profiles a on a.id = p.author_id and not a.is_house
|
|
123
|
+
where p.status = 'hidden' and p.moderated_at >= t0 and p.moderated_at < t1),
|
|
124
|
+
contributions as (select author_id, count(*) as n from writes group by author_id),
|
|
125
|
+
-- One pass per day: every row tagged with its kind, then counted by kind.
|
|
126
|
+
events as (
|
|
127
|
+
select day, 'post' as kind, author_id as who, eligible, eligible and replied as replied from m_posts
|
|
128
|
+
union all select day, 'comment', author_id, false, false from m_comments
|
|
129
|
+
union all select day, 'like', user_id, false, false from m_likes
|
|
130
|
+
union all select day, 'new_poster', null, false, false from new_posters
|
|
131
|
+
union all select day, 'hidden', null, false, false from hidden),
|
|
132
|
+
per_day as (
|
|
133
|
+
select day,
|
|
134
|
+
count(*) filter (where kind = 'post') as posts,
|
|
135
|
+
count(*) filter (where kind = 'comment') as comments,
|
|
136
|
+
count(*) filter (where kind = 'new_poster') as new_posters,
|
|
137
|
+
count(*) filter (where kind = 'hidden') as hidden,
|
|
138
|
+
count(*) filter (where kind = 'like') as likes,
|
|
139
|
+
count(*) filter (where replied) as replied,
|
|
140
|
+
count(*) filter (where eligible) as eligible,
|
|
141
|
+
count(distinct who) filter (where kind in ('post', 'comment')) as contributors
|
|
142
|
+
from events group by day)
|
|
143
|
+
select jsonb_build_object(
|
|
144
|
+
'tz', tz,
|
|
145
|
+
'daily', (select coalesce(jsonb_agg(jsonb_build_object(
|
|
146
|
+
'day', d.day,
|
|
147
|
+
'posts', coalesce(x.posts, 0),
|
|
148
|
+
'comments', coalesce(x.comments, 0),
|
|
149
|
+
'new_posters', coalesce(x.new_posters, 0),
|
|
150
|
+
'hidden', coalesce(x.hidden, 0),
|
|
151
|
+
'likes', coalesce(x.likes, 0),
|
|
152
|
+
'poll_votes', case when vote_days is not null then coalesce((vote_days->>d.day::text)::int, 0) end,
|
|
153
|
+
'reactions', case when reaction_days is not null then coalesce((reaction_days->>d.day::text)::int, 0) end,
|
|
154
|
+
'replied_24h', coalesce(x.replied, 0),
|
|
155
|
+
'reply_eligible', coalesce(x.eligible, 0),
|
|
156
|
+
'contributors', coalesce(x.contributors, 0),
|
|
157
|
+
'new_members', coalesce(nm.n, 0),
|
|
158
|
+
'active_members', coalesce(am.n, 0)
|
|
159
|
+
) order by d.day), '[]'::jsonb) from days d left join per_day x on x.day = d.day
|
|
160
|
+
left join new_by_day nm on nm.day = d.day left join active_by_day am on am.day = d.day),
|
|
161
|
+
'topics', (select coalesce(jsonb_agg(jsonb_build_object('topic', topic, 'count', n) order by n desc, topic), '[]')
|
|
162
|
+
from (select coalesce(topic, '(sans)') as topic, count(*) as n from m_posts group by 1) t),
|
|
163
|
+
'reply_rate', (select coalesce(count(*) filter (where replied)::float / nullif(count(*), 0), 0)
|
|
164
|
+
from m_posts where eligible),
|
|
165
|
+
'unanswered', (select count(*) from m_posts where eligible and not answered),
|
|
166
|
+
'contributors', (select count(*) from contributions),
|
|
167
|
+
'new_members', (select coalesce(sum(n), 0) from new_by_day),
|
|
168
|
+
'active_members', (select count(distinct who) from activity),
|
|
169
|
+
'members_total', (select count(*) from profiles where not is_house and created_at < t1),
|
|
170
|
+
'median_first_reply_minutes', (select percentile_cont(0.5) within group (order by first_reply_min)
|
|
171
|
+
from m_posts where eligible and first_reply_min is not null),
|
|
172
|
+
'reactors', (select count(distinct u) from (
|
|
173
|
+
select user_id as u from m_likes
|
|
174
|
+
union all select (e->>0)::uuid from jsonb_array_elements(vote_pairs) e
|
|
175
|
+
union all select (e->>0)::uuid from jsonb_array_elements(reaction_pairs) e) r
|
|
176
|
+
where u not in (select author_id from contributions)),
|
|
177
|
+
'contribution_buckets', (select jsonb_build_object(
|
|
178
|
+
'1', count(*) filter (where n = 1),
|
|
179
|
+
'2-3', count(*) filter (where n between 2 and 3),
|
|
180
|
+
'4-7', count(*) filter (where n between 4 and 7),
|
|
181
|
+
'8+', count(*) filter (where n >= 8)) from contributions))
|
|
182
|
+
into result;
|
|
183
|
+
return result;
|
|
184
|
+
end;
|
|
185
|
+
$$;
|
|
186
|
+
|
|
187
|
+
revoke execute on function public.admin_daily_metrics(date, date, text) from public, anon, authenticated;
|
|
188
|
+
|
|
189
|
+
create or replace function public.admin_activity_breakdown(from_date date, to_date date, tz text default 'UTC')
|
|
190
|
+
returns jsonb
|
|
191
|
+
language plpgsql
|
|
192
|
+
stable
|
|
193
|
+
set search_path = public
|
|
194
|
+
as $$
|
|
195
|
+
declare
|
|
196
|
+
t0 timestamptz;
|
|
197
|
+
t1 timestamptz;
|
|
198
|
+
pt0 timestamptz; -- the previous period: as many days, right before from_date
|
|
199
|
+
polls jsonb;
|
|
200
|
+
-- profiles.locale comes from the push module: without it, languages is null.
|
|
201
|
+
has_locale boolean := exists (select 1 from pg_attribute
|
|
202
|
+
where attrelid = 'public.profiles'::regclass and attname = 'locale' and not attisdropped);
|
|
203
|
+
result jsonb;
|
|
204
|
+
begin
|
|
205
|
+
if tz is null or tz not in (select name from pg_timezone_names) then
|
|
206
|
+
raise exception 'invalid_time_zone' using errcode = '22023';
|
|
207
|
+
end if;
|
|
208
|
+
t0 := from_date::timestamp at time zone tz;
|
|
209
|
+
t1 := (to_date + 1)::timestamp at time zone tz;
|
|
210
|
+
pt0 := (from_date - greatest(to_date - from_date + 1, 0))::timestamp at time zone tz;
|
|
211
|
+
|
|
212
|
+
with
|
|
213
|
+
-- Member posts and comments (not deleted) of both periods; locale is read through to_jsonb
|
|
214
|
+
-- so the function also compiles where the push module's column does not exist.
|
|
215
|
+
writes as (
|
|
216
|
+
select p.author_id, p.created_at, case when has_locale then to_jsonb(a)->>'locale' end as locale
|
|
217
|
+
from posts p join profiles a on a.id = p.author_id and not a.is_house
|
|
218
|
+
where p.status <> 'deleted' and p.created_at >= pt0 and p.created_at < t1
|
|
219
|
+
union all
|
|
220
|
+
select c.author_id, c.created_at, case when has_locale then to_jsonb(a)->>'locale' end
|
|
221
|
+
from comments c join profiles a on a.id = c.author_id and not a.is_house
|
|
222
|
+
where c.status <> 'deleted' and c.created_at >= pt0 and c.created_at < t1),
|
|
223
|
+
cur as (select * from writes where created_at >= t0),
|
|
224
|
+
heat as (
|
|
225
|
+
select extract(isodow from created_at at time zone tz)::int - 1 as dow,
|
|
226
|
+
extract(hour from created_at at time zone tz)::int as hr, count(*) as n
|
|
227
|
+
from cur group by 1, 2),
|
|
228
|
+
mod as (
|
|
229
|
+
select w.k,
|
|
230
|
+
(select count(*) from reports r join profiles a on a.id = r.reporter_id and not a.is_house
|
|
231
|
+
where r.created_at >= w.a and r.created_at < w.b) as reports,
|
|
232
|
+
(select count(*) from (
|
|
233
|
+
select 1 from posts p join profiles a on a.id = p.author_id and not a.is_house
|
|
234
|
+
where p.moderation_reason is not null and p.moderated_at >= w.a and p.moderated_at < w.b
|
|
235
|
+
union all
|
|
236
|
+
select 1 from comments c join profiles a on a.id = c.author_id and not a.is_house
|
|
237
|
+
where c.moderation_reason is not null and c.moderated_at >= w.a and c.moderated_at < w.b) q) as ai_hidden,
|
|
238
|
+
(select count(*) from moderation_actions m where m.created_at >= w.a and m.created_at < w.b
|
|
239
|
+
and m.action in ('hide','restore','confirm_hide','reject_report','ban','unban')) as team_actions
|
|
240
|
+
from (values ('cur', t0, t1), ('prev', pt0, t0)) w(k, a, b)),
|
|
241
|
+
top as (select * from (
|
|
242
|
+
-- image_path and attachment come from optional modules: read through to_jsonb so their absence is null.
|
|
243
|
+
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,
|
|
244
|
+
(select count(*) from likes l join profiles u on u.id = l.user_id and not u.is_house where l.post_id = p.id) as likes,
|
|
245
|
+
(select count(*) from comments c join profiles u on u.id = c.author_id and not u.is_house
|
|
246
|
+
where c.post_id = p.id and c.status <> 'deleted') as comments
|
|
247
|
+
from posts p join profiles a on a.id = p.author_id and not a.is_house
|
|
248
|
+
where p.status <> 'deleted' and p.created_at >= t0 and p.created_at < t1) q
|
|
249
|
+
order by likes + comments desc, created_at desc, id limit 5)
|
|
250
|
+
select jsonb_build_object(
|
|
251
|
+
'tz', tz,
|
|
252
|
+
'heatmap', (select jsonb_agg((select jsonb_agg(coalesce((select n from heat where heat.dow = d and heat.hr = hh), 0) order by hh)
|
|
253
|
+
from generate_series(0, 23) hh) order by d) from generate_series(0, 6) d),
|
|
254
|
+
'languages', case when has_locale then (select coalesce(jsonb_agg(jsonb_build_object('locale', locale, 'count', n)
|
|
255
|
+
order by n desc, locale), '[]') from (select locale, count(*) as n from cur group by locale) l) end,
|
|
256
|
+
'moderation', (select jsonb_build_object('reports', c.reports, 'ai_hidden', c.ai_hidden, 'team_actions', c.team_actions,
|
|
257
|
+
'previous', jsonb_build_object('reports', v.reports, 'ai_hidden', v.ai_hidden, 'team_actions', v.team_actions))
|
|
258
|
+
from mod c, mod v where c.k = 'cur' and v.k = 'prev'),
|
|
259
|
+
'returning', jsonb_build_object(
|
|
260
|
+
'previous_contributors', (select count(distinct author_id) from writes where created_at < t0),
|
|
261
|
+
'returned', (select count(distinct author_id) from cur
|
|
262
|
+
where author_id in (select author_id from writes where created_at < t0))),
|
|
263
|
+
'top_posts', (select coalesce(jsonb_agg(jsonb_build_object('id', id, 'content', content, 'image_path', image_path,
|
|
264
|
+
'attachment', attachment, 'poll', null, 'topic', topic,
|
|
265
|
+
'author_id', author_id, 'author_name', author_name, 'likes', likes, 'comments', comments,
|
|
266
|
+
'created_at', to_char(created_at at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.MS"Z"'))
|
|
267
|
+
order by likes + comments desc, created_at desc, id), '[]') from top))
|
|
268
|
+
into result;
|
|
269
|
+
-- A top post's poll (polls module): its options in order with their vote counts.
|
|
270
|
+
if to_regclass('public.poll_options') is not null and jsonb_array_length(result->'top_posts') > 0 then
|
|
271
|
+
execute $q$select coalesce(jsonb_object_agg(post_id, opts), '{}') from (
|
|
272
|
+
select o.post_id, jsonb_agg(jsonb_build_object('label', o.label,
|
|
273
|
+
'votes', (select count(*) from public.poll_votes v where v.option_id = o.id)) order by o.idx) as opts
|
|
274
|
+
from public.poll_options o where o.post_id = any($1) group by o.post_id) x$q$
|
|
275
|
+
into polls
|
|
276
|
+
using (select array_agg((e->>'id')::uuid) from jsonb_array_elements(result->'top_posts') e);
|
|
277
|
+
result := jsonb_set(result, '{top_posts}', (select jsonb_agg(jsonb_set(e, '{poll}', coalesce(polls->(e->>'id'), 'null'))
|
|
278
|
+
order by n) from jsonb_array_elements(result->'top_posts') with ordinality t(e, n)));
|
|
279
|
+
end if;
|
|
280
|
+
return result;
|
|
281
|
+
end;
|
|
282
|
+
$$;
|
|
283
|
+
|
|
284
|
+
revoke execute on function public.admin_activity_breakdown(date, date, text) from public, anon, authenticated;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
-- community-sdk presence/001_presence.sql (schema v1)
|
|
2
|
+
-- Presence module: a green dot on online members, a live online count, and a
|
|
3
|
+
-- per-member switch to hide. The app calls touch_presence() while it is open; a
|
|
4
|
+
-- member is online when their last touch is within the project's grace window
|
|
5
|
+
-- (presence_settings, default 5 minutes).
|
|
6
|
+
--
|
|
7
|
+
-- The timestamp is never readable by clients (only user_id and visible are, and
|
|
8
|
+
-- only for the member's own row): readers get a boolean through online_member_ids()
|
|
9
|
+
-- or a number through online_member_count(). Both skip banned and (for the ids)
|
|
10
|
+
-- hidden and mutually blocked members. The caller counts as online (whoever calls the
|
|
11
|
+
-- readers has the app open) even before their first touch lands, unless banned;
|
|
12
|
+
-- a hidden caller is counted but never listed.
|
|
13
|
+
-- Fully idempotent.
|
|
14
|
+
|
|
15
|
+
create table if not exists public.member_presence (
|
|
16
|
+
user_id uuid primary key references public.profiles(id) on delete cascade,
|
|
17
|
+
last_active_at timestamptz not null default now(),
|
|
18
|
+
visible boolean not null default true
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
create index if not exists member_presence_last_active_idx
|
|
22
|
+
on public.member_presence (last_active_at);
|
|
23
|
+
|
|
24
|
+
alter table public.member_presence enable row level security;
|
|
25
|
+
|
|
26
|
+
-- A member reads only their own row, and only these columns (user_id because the
|
|
27
|
+
-- owner filters on it: column privileges apply to WHERE columns too).
|
|
28
|
+
drop policy if exists "read own presence" on public.member_presence;
|
|
29
|
+
create policy "read own presence" on public.member_presence
|
|
30
|
+
for select to authenticated using (user_id = auth.uid());
|
|
31
|
+
|
|
32
|
+
revoke all on public.member_presence from public, anon, authenticated;
|
|
33
|
+
grant select (user_id, visible) on public.member_presence to authenticated;
|
|
34
|
+
|
|
35
|
+
create or replace function public.touch_presence()
|
|
36
|
+
returns void
|
|
37
|
+
language sql security definer set search_path = public
|
|
38
|
+
as $$
|
|
39
|
+
insert into member_presence (user_id, last_active_at)
|
|
40
|
+
select auth.uid(), now()
|
|
41
|
+
where auth.uid() is not null
|
|
42
|
+
on conflict (user_id) do update set last_active_at = excluded.last_active_at;
|
|
43
|
+
$$;
|
|
44
|
+
|
|
45
|
+
create or replace function public.set_presence_visible(p_visible boolean)
|
|
46
|
+
returns void
|
|
47
|
+
language sql security definer set search_path = public
|
|
48
|
+
as $$
|
|
49
|
+
-- A first row starts long idle: only touch_presence makes someone online.
|
|
50
|
+
insert into member_presence (user_id, last_active_at, visible)
|
|
51
|
+
select auth.uid(), 'epoch', p_visible
|
|
52
|
+
where auth.uid() is not null
|
|
53
|
+
on conflict (user_id) do update set visible = excluded.visible;
|
|
54
|
+
$$;
|
|
55
|
+
|
|
56
|
+
-- The grace window is a per-project setting, never a client parameter: if callers
|
|
57
|
+
-- chose it, sweeping 1..60 would tell them when each member was last active.
|
|
58
|
+
-- One row, readable only by the security definer RPCs below. Change it from the
|
|
59
|
+
-- SQL editor (service role): select public.presence_set_grace_minutes(10);
|
|
60
|
+
create table if not exists public.presence_settings (
|
|
61
|
+
id boolean primary key default true check (id),
|
|
62
|
+
grace_minutes int not null default 5 check (grace_minutes between 1 and 60)
|
|
63
|
+
);
|
|
64
|
+
insert into public.presence_settings (id) values (true) on conflict do nothing;
|
|
65
|
+
alter table public.presence_settings enable row level security;
|
|
66
|
+
revoke all on public.presence_settings from public, anon, authenticated;
|
|
67
|
+
|
|
68
|
+
-- The check constraint rejects values outside 1..60.
|
|
69
|
+
create or replace function public.presence_set_grace_minutes(p_minutes int)
|
|
70
|
+
returns void
|
|
71
|
+
language sql security definer set search_path = public
|
|
72
|
+
as $$
|
|
73
|
+
insert into presence_settings (id, grace_minutes) values (true, p_minutes)
|
|
74
|
+
on conflict (id) do update set grace_minutes = excluded.grace_minutes;
|
|
75
|
+
$$;
|
|
76
|
+
|
|
77
|
+
-- Earlier drafts took the grace as a parameter.
|
|
78
|
+
drop function if exists public.online_member_count(int);
|
|
79
|
+
drop function if exists public.online_member_ids(uuid[], int);
|
|
80
|
+
|
|
81
|
+
-- Members online within the grace window, hidden members included: a count
|
|
82
|
+
-- reveals no one. The caller counts even without a fresh row (union dedups). A banned
|
|
83
|
+
-- caller gets 0.
|
|
84
|
+
create or replace function public.online_member_count()
|
|
85
|
+
returns int
|
|
86
|
+
language sql stable security definer set search_path = public
|
|
87
|
+
as $$
|
|
88
|
+
select count(*)::int from (
|
|
89
|
+
select mp.user_id
|
|
90
|
+
from member_presence mp
|
|
91
|
+
join profiles p on p.id = mp.user_id
|
|
92
|
+
where not p.is_banned
|
|
93
|
+
and mp.last_active_at > now() - make_interval(
|
|
94
|
+
mins => coalesce((select grace_minutes from presence_settings), 5))
|
|
95
|
+
union
|
|
96
|
+
select p.id from profiles p where p.id = auth.uid() and not p.is_banned
|
|
97
|
+
) online
|
|
98
|
+
where not exists (select 1 from profiles b where b.id = auth.uid() and b.is_banned);
|
|
99
|
+
$$;
|
|
100
|
+
|
|
101
|
+
-- Which of the first 200 of p_ids are online, visible, not banned and not
|
|
102
|
+
-- blocked either way with the caller. The caller is online when asking, unless
|
|
103
|
+
-- they hid themselves (a missing row means visible). A banned caller gets '{}'.
|
|
104
|
+
create or replace function public.online_member_ids(p_ids uuid[])
|
|
105
|
+
returns uuid[]
|
|
106
|
+
language sql stable security definer set search_path = public
|
|
107
|
+
as $$
|
|
108
|
+
select coalesce(array_agg(id), '{}') from (
|
|
109
|
+
select mp.user_id as id
|
|
110
|
+
from member_presence mp
|
|
111
|
+
join profiles p on p.id = mp.user_id
|
|
112
|
+
where mp.user_id = any (p_ids[1:200])
|
|
113
|
+
and mp.visible
|
|
114
|
+
and not p.is_banned
|
|
115
|
+
and mp.last_active_at > now() - make_interval(
|
|
116
|
+
mins => coalesce((select grace_minutes from presence_settings), 5))
|
|
117
|
+
and not (mp.user_id = any ((select public.blocked_member_ids())::uuid[]))
|
|
118
|
+
union
|
|
119
|
+
select p.id
|
|
120
|
+
from profiles p
|
|
121
|
+
left join member_presence mp on mp.user_id = p.id
|
|
122
|
+
where p.id = auth.uid()
|
|
123
|
+
and p.id = any (p_ids[1:200])
|
|
124
|
+
and not p.is_banned
|
|
125
|
+
and coalesce(mp.visible, true)
|
|
126
|
+
) online
|
|
127
|
+
where not exists (select 1 from profiles b where b.id = auth.uid() and b.is_banned);
|
|
128
|
+
$$;
|
|
129
|
+
|
|
130
|
+
revoke execute on function public.touch_presence() from public, anon;
|
|
131
|
+
revoke execute on function public.set_presence_visible(boolean) from public, anon;
|
|
132
|
+
revoke execute on function public.online_member_count() from public, anon;
|
|
133
|
+
revoke execute on function public.online_member_ids(uuid[]) from public, anon;
|
|
134
|
+
grant execute on function public.touch_presence() to authenticated;
|
|
135
|
+
grant execute on function public.set_presence_visible(boolean) to authenticated;
|
|
136
|
+
grant execute on function public.online_member_count() to authenticated;
|
|
137
|
+
grant execute on function public.online_member_ids(uuid[]) to authenticated;
|
|
138
|
+
revoke execute on function public.presence_set_grace_minutes(int) from public, anon, authenticated;
|
|
139
|
+
grant execute on function public.presence_set_grace_minutes(int) to service_role;
|