@rocapine/community 0.15.0 → 0.16.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/package.json
CHANGED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
-- community-sdk core/014_post_drafts.sql (schema v1)
|
|
2
|
+
-- Shared post drafts for the community dashboard (service role only): the team
|
|
3
|
+
-- writes a post under a team account, edits it together (`version` guards
|
|
4
|
+
-- concurrent saves), then publishes it with publish_post_draft. Publishing skips
|
|
5
|
+
-- moderation (the team wrote it), applies pin and "notify everyone", copies the
|
|
6
|
+
-- poll, attachment or image, deletes the draft and logs `publish_post`.
|
|
7
|
+
-- Clients never see this table. Fully idempotent.
|
|
8
|
+
|
|
9
|
+
create table if not exists public.post_drafts (
|
|
10
|
+
id uuid primary key default gen_random_uuid(),
|
|
11
|
+
author_id uuid references public.profiles(id) on delete set null,
|
|
12
|
+
topic text,
|
|
13
|
+
content text not null default '' check (char_length(content) <= 2000),
|
|
14
|
+
image_path text,
|
|
15
|
+
image_width int,
|
|
16
|
+
image_height int,
|
|
17
|
+
poll_options text[],
|
|
18
|
+
attachment jsonb,
|
|
19
|
+
notify boolean not null default false,
|
|
20
|
+
pin boolean not null default false,
|
|
21
|
+
publish_at timestamptz,
|
|
22
|
+
version int not null default 1,
|
|
23
|
+
created_by text not null,
|
|
24
|
+
updated_by text not null,
|
|
25
|
+
created_at timestamptz not null default now(),
|
|
26
|
+
updated_at timestamptz not null default now()
|
|
27
|
+
);
|
|
28
|
+
alter table public.post_drafts enable row level security;
|
|
29
|
+
-- No policy: readable only by the service role.
|
|
30
|
+
revoke all on table public.post_drafts from public, anon, authenticated;
|
|
31
|
+
|
|
32
|
+
alter table public.moderation_actions drop constraint if exists moderation_actions_action_check;
|
|
33
|
+
alter table public.moderation_actions add constraint moderation_actions_action_check check (action in
|
|
34
|
+
('hide','restore','confirm_hide','reject_report','ban','unban',
|
|
35
|
+
'publish_post','create_team_account','update_team_account'));
|
|
36
|
+
|
|
37
|
+
-- Errors are raised with the code as the message (errcode P0001): draft_not_found,
|
|
38
|
+
-- draft_changed, not_a_team_account, author_banned, not_admin, invalid_content,
|
|
39
|
+
-- invalid_image, invalid_poll, invalid_attachment, one_extra_only. Columns of
|
|
40
|
+
-- optional modules (images, attachments) are only named when the draft uses
|
|
41
|
+
-- them, so a backend without those modules still publishes plain and poll drafts.
|
|
42
|
+
create or replace function public.publish_post_draft(p_draft_id uuid, p_version int, p_actor text)
|
|
43
|
+
returns uuid
|
|
44
|
+
language plpgsql security definer set search_path = public
|
|
45
|
+
as $$
|
|
46
|
+
declare
|
|
47
|
+
d post_drafts;
|
|
48
|
+
author profiles;
|
|
49
|
+
new_id uuid;
|
|
50
|
+
n int;
|
|
51
|
+
i int;
|
|
52
|
+
begin
|
|
53
|
+
select * into d from post_drafts where id = p_draft_id for update;
|
|
54
|
+
if not found then raise exception 'draft_not_found'; end if;
|
|
55
|
+
if d.version is distinct from p_version then raise exception 'draft_changed'; end if;
|
|
56
|
+
|
|
57
|
+
select * into author from profiles where id = d.author_id;
|
|
58
|
+
if not found or not author.is_house then raise exception 'not_a_team_account'; end if;
|
|
59
|
+
if author.is_banned then raise exception 'author_banned'; end if;
|
|
60
|
+
if (d.pin or d.notify) and not author.is_admin then raise exception 'not_admin'; end if;
|
|
61
|
+
|
|
62
|
+
-- Like posts_content_check: text is optional when there is an image.
|
|
63
|
+
if char_length(d.content) > 2000 or (char_length(d.content) = 0 and d.image_path is null) then
|
|
64
|
+
raise exception 'invalid_content';
|
|
65
|
+
end if;
|
|
66
|
+
-- Mirrors posts_image_check and posts_image_path_format (images/001). The image lives
|
|
67
|
+
-- in the author's storage folder; the dashboard moves it when the author changes.
|
|
68
|
+
if d.image_path is not null and (coalesce(d.image_width, 0) <= 0 or coalesce(d.image_height, 0) <= 0
|
|
69
|
+
or d.image_path !~ '^[0-9a-f-]{36}/[A-Za-z0-9_-]+\.jpg$'
|
|
70
|
+
or split_part(d.image_path, '/', 1) <> d.author_id::text) then
|
|
71
|
+
raise exception 'invalid_image';
|
|
72
|
+
end if;
|
|
73
|
+
n := coalesce(array_length(d.poll_options, 1), 0);
|
|
74
|
+
if d.poll_options is not null and (n not between 2 and 4
|
|
75
|
+
or exists (select 1 from unnest(d.poll_options) l where coalesce(char_length(l), 0) not between 1 and 60)) then
|
|
76
|
+
raise exception 'invalid_poll';
|
|
77
|
+
end if;
|
|
78
|
+
-- Mirrors posts_attachment_check (attachments/001); coalesce because a null check result would pass.
|
|
79
|
+
if d.attachment is not null and not coalesce(jsonb_typeof(d.attachment) = 'object'
|
|
80
|
+
and d.attachment ? 'type' and d.attachment ? 'id'
|
|
81
|
+
and jsonb_typeof(d.attachment -> 'type') = 'string' and jsonb_typeof(d.attachment -> 'id') = 'string'
|
|
82
|
+
and octet_length(d.attachment::text) <= 512, false) then
|
|
83
|
+
raise exception 'invalid_attachment';
|
|
84
|
+
end if;
|
|
85
|
+
if (d.image_path is not null)::int + (d.poll_options is not null)::int + (d.attachment is not null)::int > 1 then
|
|
86
|
+
raise exception 'one_extra_only';
|
|
87
|
+
end if;
|
|
88
|
+
|
|
89
|
+
-- The image goes in the insert itself: an image-only post needs it to pass posts_content_check.
|
|
90
|
+
if d.image_path is not null then
|
|
91
|
+
insert into posts (author_id, topic, content, status, moderated_at, broadcast_requested, pinned_at,
|
|
92
|
+
image_path, image_width, image_height)
|
|
93
|
+
values (d.author_id, d.topic, d.content, 'visible', now(), d.notify, case when d.pin then now() end,
|
|
94
|
+
d.image_path, d.image_width, d.image_height)
|
|
95
|
+
returning id into new_id;
|
|
96
|
+
else
|
|
97
|
+
insert into posts (author_id, topic, content, status, moderated_at, broadcast_requested, pinned_at)
|
|
98
|
+
values (d.author_id, d.topic, d.content, 'visible', now(), d.notify, case when d.pin then now() end)
|
|
99
|
+
returning id into new_id;
|
|
100
|
+
end if;
|
|
101
|
+
if d.attachment is not null then
|
|
102
|
+
update posts set attachment = d.attachment where id = new_id;
|
|
103
|
+
end if;
|
|
104
|
+
for i in 1..n loop
|
|
105
|
+
insert into poll_options (post_id, idx, label) values (new_id, i - 1, d.poll_options[i]);
|
|
106
|
+
end loop;
|
|
107
|
+
|
|
108
|
+
delete from post_drafts where id = d.id;
|
|
109
|
+
insert into moderation_actions (actor_email, action, target_post_id, target_user_id)
|
|
110
|
+
values (p_actor, 'publish_post', new_id, d.author_id);
|
|
111
|
+
return new_id;
|
|
112
|
+
end;
|
|
113
|
+
$$;
|
|
114
|
+
|
|
115
|
+
revoke execute on function public.publish_post_draft(uuid, int, text) from public, anon, authenticated;
|
|
116
|
+
grant execute on function public.publish_post_draft(uuid, int, text) to service_role;
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
-- community-sdk core/015_scheduled_posts.sql (schema v1)
|
|
2
|
+
-- Scheduled posts for the community dashboard (service role only): a draft gets a
|
|
3
|
+
-- publish_at instant (publish_tz is the zone the team picked, for display only) and
|
|
4
|
+
-- schedule_post_draft sets or clears it. A draft can carry a planned time without being armed:
|
|
5
|
+
-- armed means scheduled_by is not null, and only armed drafts are published by the cron.
|
|
6
|
+
-- Planning a time checks the time and the event window; arming validates the whole post.
|
|
7
|
+
-- check_post_draft holds the validation that publish_post_draft and the scheduler share.
|
|
8
|
+
-- Requires core/014. Fully idempotent.
|
|
9
|
+
-- topic_from / topic_until (nullable dates, until exclusive) hold the date window of an event
|
|
10
|
+
-- topic, which lives in the dashboard's config; the dashboard writes them with the topic.
|
|
11
|
+
|
|
12
|
+
alter table public.post_drafts add column if not exists scheduled_by text;
|
|
13
|
+
alter table public.post_drafts add column if not exists publish_tz text;
|
|
14
|
+
alter table public.post_drafts add column if not exists publish_error text;
|
|
15
|
+
alter table public.post_drafts add column if not exists failed_at timestamptz;
|
|
16
|
+
alter table public.post_drafts add column if not exists topic_from date;
|
|
17
|
+
alter table public.post_drafts add column if not exists topic_until date;
|
|
18
|
+
|
|
19
|
+
-- Only armed drafts are due; the predicate changed in the planned-time revision, so recreate it.
|
|
20
|
+
drop index if exists public.post_drafts_due_idx;
|
|
21
|
+
create index post_drafts_due_idx on public.post_drafts (publish_at)
|
|
22
|
+
where publish_at is not null and publish_error is null and scheduled_by is not null;
|
|
23
|
+
|
|
24
|
+
alter table public.moderation_actions drop constraint if exists moderation_actions_action_check;
|
|
25
|
+
alter table public.moderation_actions add constraint moderation_actions_action_check check (action in
|
|
26
|
+
('hide','restore','confirm_hide','reject_report','ban','unban',
|
|
27
|
+
'publish_post','create_team_account','update_team_account','schedule_post'));
|
|
28
|
+
|
|
29
|
+
-- The event-window part of the check, alone (it needs d.publish_tz): the local date the post goes
|
|
30
|
+
-- out in the draft's zone must fall in [topic_from, topic_until). A draft without a zone is guarded
|
|
31
|
+
-- by the dashboard only. Raises topic_closed.
|
|
32
|
+
create or replace function public.check_post_draft_window(d post_drafts, p_at timestamptz default null)
|
|
33
|
+
returns void
|
|
34
|
+
language plpgsql security definer set search_path = public
|
|
35
|
+
as $$
|
|
36
|
+
begin
|
|
37
|
+
if d.publish_tz is not null and (d.topic_from is not null or d.topic_until is not null)
|
|
38
|
+
and ((coalesce(p_at, now()) at time zone d.publish_tz)::date < d.topic_from
|
|
39
|
+
or (coalesce(p_at, now()) at time zone d.publish_tz)::date >= d.topic_until) then
|
|
40
|
+
raise exception 'topic_closed';
|
|
41
|
+
end if;
|
|
42
|
+
end;
|
|
43
|
+
$$;
|
|
44
|
+
|
|
45
|
+
revoke execute on function public.check_post_draft_window(post_drafts, timestamptz) from public, anon, authenticated;
|
|
46
|
+
|
|
47
|
+
-- Raises the publish error codes (errcode P0001, code as message): not_a_team_account,
|
|
48
|
+
-- author_banned, not_admin, invalid_content, invalid_image, invalid_poll,
|
|
49
|
+
-- invalid_attachment, one_extra_only, invalid_topic, topic_closed. Callers are security definer; nobody else may run it.
|
|
50
|
+
-- Takes the instant the post goes out (default now) for the event window: publishing checks it at
|
|
51
|
+
-- that moment, scheduling checks the chosen time. Replaces the one-argument version of earlier runs.
|
|
52
|
+
drop function if exists public.check_post_draft(post_drafts);
|
|
53
|
+
create or replace function public.check_post_draft(d post_drafts, p_at timestamptz default null)
|
|
54
|
+
returns void
|
|
55
|
+
language plpgsql security definer set search_path = public
|
|
56
|
+
as $$
|
|
57
|
+
declare
|
|
58
|
+
author profiles;
|
|
59
|
+
n int;
|
|
60
|
+
begin
|
|
61
|
+
select * into author from profiles where id = d.author_id;
|
|
62
|
+
if not found or not author.is_house then raise exception 'not_a_team_account'; end if;
|
|
63
|
+
if author.is_banned then raise exception 'author_banned'; end if;
|
|
64
|
+
if (d.pin or d.notify) and not author.is_admin then raise exception 'not_admin'; end if;
|
|
65
|
+
|
|
66
|
+
-- Like posts_content_check: text is optional when there is an image.
|
|
67
|
+
if char_length(d.content) > 2000 or (char_length(d.content) = 0 and d.image_path is null) then
|
|
68
|
+
raise exception 'invalid_content';
|
|
69
|
+
end if;
|
|
70
|
+
-- Mirrors posts_image_check and posts_image_path_format (images/001). The image lives
|
|
71
|
+
-- in the author's storage folder; the dashboard moves it when the author changes.
|
|
72
|
+
if d.image_path is not null and (coalesce(d.image_width, 0) <= 0 or coalesce(d.image_height, 0) <= 0
|
|
73
|
+
or d.image_path !~ '^[0-9a-f-]{36}/[A-Za-z0-9_-]+\.jpg$'
|
|
74
|
+
or split_part(d.image_path, '/', 1) <> d.author_id::text) then
|
|
75
|
+
raise exception 'invalid_image';
|
|
76
|
+
end if;
|
|
77
|
+
n := coalesce(array_length(d.poll_options, 1), 0);
|
|
78
|
+
if d.poll_options is not null and (n not between 2 and 4
|
|
79
|
+
or exists (select 1 from unnest(d.poll_options) l where coalesce(char_length(l), 0) not between 1 and 60)) then
|
|
80
|
+
raise exception 'invalid_poll';
|
|
81
|
+
end if;
|
|
82
|
+
-- Mirrors posts_attachment_check (attachments/001); coalesce because a null check result would pass.
|
|
83
|
+
if d.attachment is not null and not coalesce(jsonb_typeof(d.attachment) = 'object'
|
|
84
|
+
and d.attachment ? 'type' and d.attachment ? 'id'
|
|
85
|
+
and jsonb_typeof(d.attachment -> 'type') = 'string' and jsonb_typeof(d.attachment -> 'id') = 'string'
|
|
86
|
+
and octet_length(d.attachment::text) <= 512, false) then
|
|
87
|
+
raise exception 'invalid_attachment';
|
|
88
|
+
end if;
|
|
89
|
+
if (d.image_path is not null)::int + (d.poll_options is not null)::int + (d.attachment is not null)::int > 1 then
|
|
90
|
+
raise exception 'one_extra_only';
|
|
91
|
+
end if;
|
|
92
|
+
-- Topic rules live in the dashboard; here a draft only needs one, so the cron never posts without it.
|
|
93
|
+
if d.topic is null then raise exception 'invalid_topic'; end if;
|
|
94
|
+
perform check_post_draft_window(d, p_at);
|
|
95
|
+
end;
|
|
96
|
+
$$;
|
|
97
|
+
|
|
98
|
+
revoke execute on function public.check_post_draft(post_drafts, timestamptz) from public, anon, authenticated;
|
|
99
|
+
|
|
100
|
+
-- Same checks and codes as 014 (the validation now lives in check_post_draft, which also raises
|
|
101
|
+
-- invalid_topic: a draft needs a topic to go out).
|
|
102
|
+
create or replace function public.publish_post_draft(p_draft_id uuid, p_version int, p_actor text)
|
|
103
|
+
returns uuid
|
|
104
|
+
language plpgsql security definer set search_path = public
|
|
105
|
+
as $$
|
|
106
|
+
declare
|
|
107
|
+
d post_drafts;
|
|
108
|
+
new_id uuid;
|
|
109
|
+
n int;
|
|
110
|
+
i int;
|
|
111
|
+
begin
|
|
112
|
+
select * into d from post_drafts where id = p_draft_id for update;
|
|
113
|
+
if not found then raise exception 'draft_not_found'; end if;
|
|
114
|
+
if d.version is distinct from p_version then raise exception 'draft_changed'; end if;
|
|
115
|
+
|
|
116
|
+
perform check_post_draft(d);
|
|
117
|
+
n := coalesce(array_length(d.poll_options, 1), 0);
|
|
118
|
+
|
|
119
|
+
-- The image goes in the insert itself: an image-only post needs it to pass posts_content_check.
|
|
120
|
+
if d.image_path is not null then
|
|
121
|
+
insert into posts (author_id, topic, content, status, moderated_at, broadcast_requested, pinned_at,
|
|
122
|
+
image_path, image_width, image_height)
|
|
123
|
+
values (d.author_id, d.topic, d.content, 'visible', now(), d.notify, case when d.pin then now() end,
|
|
124
|
+
d.image_path, d.image_width, d.image_height)
|
|
125
|
+
returning id into new_id;
|
|
126
|
+
else
|
|
127
|
+
insert into posts (author_id, topic, content, status, moderated_at, broadcast_requested, pinned_at)
|
|
128
|
+
values (d.author_id, d.topic, d.content, 'visible', now(), d.notify, case when d.pin then now() end)
|
|
129
|
+
returning id into new_id;
|
|
130
|
+
end if;
|
|
131
|
+
if d.attachment is not null then
|
|
132
|
+
update posts set attachment = d.attachment where id = new_id;
|
|
133
|
+
end if;
|
|
134
|
+
for i in 1..n loop
|
|
135
|
+
insert into poll_options (post_id, idx, label) values (new_id, i - 1, d.poll_options[i]);
|
|
136
|
+
end loop;
|
|
137
|
+
|
|
138
|
+
delete from post_drafts where id = d.id;
|
|
139
|
+
insert into moderation_actions (actor_email, action, target_post_id, target_user_id)
|
|
140
|
+
values (p_actor, 'publish_post', new_id, d.author_id);
|
|
141
|
+
return new_id;
|
|
142
|
+
end;
|
|
143
|
+
$$;
|
|
144
|
+
|
|
145
|
+
revoke execute on function public.publish_post_draft(uuid, int, text) from public, anon, authenticated;
|
|
146
|
+
grant execute on function public.publish_post_draft(uuid, int, text) to service_role;
|
|
147
|
+
|
|
148
|
+
-- Plans, arms or clears the schedule of a draft and returns its new version. p_arm = false (with a
|
|
149
|
+
-- time) only plans it: the time, the zone and the event window are checked, not the post, and the
|
|
150
|
+
-- draft stays unarmed (scheduled_by null), so the cron leaves it alone; with the current time it
|
|
151
|
+
-- disarms a draft and keeps its time, with no check (the time may have passed); "the current
|
|
152
|
+
-- time" is compared at millisecond precision, since the dashboard round-trips times through JS
|
|
153
|
+
-- Dates, and p_tz must be the current zone. Re-sending it to an unarmed draft with no error does
|
|
154
|
+
-- nothing. p_arm = true (also when p_arm is null) validates the whole post (check_post_draft)
|
|
155
|
+
-- and arms it. A null p_publish_at clears the time and disarms. Logged (schedule_post) only when the
|
|
156
|
+
-- armed state changes or an armed draft's time changes.
|
|
157
|
+
-- Errors: draft_not_found, draft_changed, invalid_time (also a missing or unknown p_tz), plus the
|
|
158
|
+
-- check_post_draft codes (topic_closed for a time outside the event window). Clearing a draft with
|
|
159
|
+
-- no time, no error and no arming does nothing.
|
|
160
|
+
drop function if exists public.schedule_post_draft(uuid, int, timestamptz, text, text);
|
|
161
|
+
create or replace function public.schedule_post_draft(
|
|
162
|
+
p_draft_id uuid, p_version int, p_publish_at timestamptz, p_tz text, p_actor text, p_arm boolean default true)
|
|
163
|
+
returns int
|
|
164
|
+
language plpgsql security definer set search_path = public
|
|
165
|
+
as $$
|
|
166
|
+
declare
|
|
167
|
+
d post_drafts;
|
|
168
|
+
was_armed boolean;
|
|
169
|
+
now_armed boolean := p_publish_at is not null and coalesce(p_arm, true);
|
|
170
|
+
keep boolean; -- unschedule: disarm and keep the current time, which may be past, so nothing is re-checked
|
|
171
|
+
begin
|
|
172
|
+
select * into d from post_drafts where id = p_draft_id for update;
|
|
173
|
+
if not found then raise exception 'draft_not_found'; end if;
|
|
174
|
+
if d.version is distinct from p_version then raise exception 'draft_changed'; end if;
|
|
175
|
+
was_armed := d.scheduled_by is not null;
|
|
176
|
+
keep := p_publish_at is not null and not coalesce(p_arm, true)
|
|
177
|
+
and date_trunc('milliseconds', p_publish_at) = date_trunc('milliseconds', d.publish_at)
|
|
178
|
+
and p_tz is not distinct from d.publish_tz;
|
|
179
|
+
|
|
180
|
+
if keep then
|
|
181
|
+
if not was_armed and d.publish_error is null then return d.version; end if; -- nothing changes
|
|
182
|
+
elsif p_publish_at is not null then
|
|
183
|
+
if p_publish_at <= now() or p_publish_at > now() + interval '1 year 1 day' then
|
|
184
|
+
raise exception 'invalid_time';
|
|
185
|
+
end if;
|
|
186
|
+
-- The zone is for display only, but it must be an IANA name (not a POSIX string the dashboard can't show).
|
|
187
|
+
if p_tz is null or p_tz not in (select name from pg_timezone_names) then raise exception 'invalid_time'; end if;
|
|
188
|
+
d.publish_tz := p_tz;
|
|
189
|
+
if now_armed then
|
|
190
|
+
perform check_post_draft(d, p_publish_at); -- the whole post, event window included
|
|
191
|
+
else
|
|
192
|
+
perform check_post_draft_window(d, p_publish_at);
|
|
193
|
+
end if;
|
|
194
|
+
elsif d.publish_at is null and d.publish_error is null and not was_armed then
|
|
195
|
+
return d.version; -- nothing to clear
|
|
196
|
+
end if;
|
|
197
|
+
|
|
198
|
+
update post_drafts set
|
|
199
|
+
publish_at = p_publish_at,
|
|
200
|
+
publish_tz = case when p_publish_at is not null then p_tz end,
|
|
201
|
+
scheduled_by = case when now_armed then p_actor end,
|
|
202
|
+
publish_error = null,
|
|
203
|
+
failed_at = null,
|
|
204
|
+
version = version + 1,
|
|
205
|
+
updated_by = p_actor,
|
|
206
|
+
updated_at = now()
|
|
207
|
+
where id = d.id;
|
|
208
|
+
|
|
209
|
+
if was_armed <> now_armed or (now_armed and d.publish_at is distinct from p_publish_at) then
|
|
210
|
+
insert into moderation_actions (actor_email, action, target_user_id, note)
|
|
211
|
+
values (p_actor, 'schedule_post', d.author_id,
|
|
212
|
+
'draft ' || d.id || case when now_armed
|
|
213
|
+
then ' at ' || to_char(p_publish_at at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"') else ' unscheduled' end);
|
|
214
|
+
end if;
|
|
215
|
+
return d.version + 1;
|
|
216
|
+
end;
|
|
217
|
+
$$;
|
|
218
|
+
|
|
219
|
+
revoke execute on function public.schedule_post_draft(uuid, int, timestamptz, text, text, boolean) from public, anon, authenticated;
|
|
220
|
+
grant execute on function public.schedule_post_draft(uuid, int, timestamptz, text, text, boolean) to service_role;
|
|
221
|
+
|
|
222
|
+
-- Publishes the armed drafts (scheduled_by set) whose time has come and returns how many went out. A draft more than
|
|
223
|
+
-- 30 minutes late is marked 'missed' instead; a draft that fails keeps its publish_at and
|
|
224
|
+
-- stores the error (the code for a P0001 error, else the message; cut to 200 characters),
|
|
225
|
+
-- so the next run skips it until the team reschedules it. A transient error (serialization
|
|
226
|
+
-- failure, deadlock, lock not available) leaves the draft untouched to be retried next run;
|
|
227
|
+
-- the 30-minute rule bounds the retries. Run by pg_cron every minute.
|
|
228
|
+
create or replace function public.publish_due_post_drafts()
|
|
229
|
+
returns int
|
|
230
|
+
language plpgsql security definer set search_path = public
|
|
231
|
+
as $$
|
|
232
|
+
declare
|
|
233
|
+
r record;
|
|
234
|
+
n int := 0;
|
|
235
|
+
begin
|
|
236
|
+
for r in
|
|
237
|
+
select id, version, scheduled_by, publish_at from post_drafts
|
|
238
|
+
where publish_at <= now() and publish_error is null and scheduled_by is not null
|
|
239
|
+
order by publish_at for update skip locked
|
|
240
|
+
loop
|
|
241
|
+
if r.publish_at < now() - interval '30 minutes' then
|
|
242
|
+
update post_drafts set publish_error = 'missed', failed_at = now() where id = r.id;
|
|
243
|
+
else
|
|
244
|
+
begin
|
|
245
|
+
perform publish_post_draft(r.id, r.version, r.scheduled_by);
|
|
246
|
+
n := n + 1;
|
|
247
|
+
exception
|
|
248
|
+
when serialization_failure or deadlock_detected or lock_not_available then
|
|
249
|
+
raise log 'publish_due_post_drafts: transient % on draft %', sqlstate, r.id;
|
|
250
|
+
when others then
|
|
251
|
+
update post_drafts set failed_at = now(),
|
|
252
|
+
publish_error = left(sqlerrm, 200)
|
|
253
|
+
where id = r.id;
|
|
254
|
+
end;
|
|
255
|
+
end if;
|
|
256
|
+
end loop;
|
|
257
|
+
return n;
|
|
258
|
+
end;
|
|
259
|
+
$$;
|
|
260
|
+
|
|
261
|
+
revoke execute on function public.publish_due_post_drafts() from public, anon, authenticated, service_role;
|
|
262
|
+
|
|
263
|
+
do $$
|
|
264
|
+
begin
|
|
265
|
+
perform cron.schedule('post-drafts-publish', '* * * * *', $cron$ select public.publish_due_post_drafts(); $cron$);
|
|
266
|
+
exception when others then
|
|
267
|
+
if sqlerrm like '%already exists%' then
|
|
268
|
+
raise notice 'cron job post-drafts-publish already scheduled, skipping';
|
|
269
|
+
else
|
|
270
|
+
raise;
|
|
271
|
+
end if;
|
|
272
|
+
end;
|
|
273
|
+
$$;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
-- community-sdk images/002_draft_images.sql (schema v1)
|
|
2
|
+
-- The nightly sweep keeps images that a shared post draft (core/014_post_drafts.sql)
|
|
3
|
+
-- points at, so a draft written yesterday still has its picture today.
|
|
4
|
+
-- Redefines post_images_unreferenced with one more reference check. Idempotent.
|
|
5
|
+
|
|
6
|
+
create or replace function public.post_images_unreferenced(p_limit int default 500)
|
|
7
|
+
returns setof text
|
|
8
|
+
language sql
|
|
9
|
+
stable
|
|
10
|
+
security definer
|
|
11
|
+
set search_path = ''
|
|
12
|
+
as $$
|
|
13
|
+
select o.name
|
|
14
|
+
from storage.objects o
|
|
15
|
+
where o.bucket_id = 'post-images'
|
|
16
|
+
and o.created_at < now() - interval '24 hours'
|
|
17
|
+
and not exists (
|
|
18
|
+
select 1 from public.posts p
|
|
19
|
+
where p.image_path = o.name and p.status <> 'deleted'
|
|
20
|
+
)
|
|
21
|
+
and not exists (
|
|
22
|
+
select 1 from public.comments c
|
|
23
|
+
where c.image_path = o.name and c.status <> 'deleted'
|
|
24
|
+
)
|
|
25
|
+
and not exists (
|
|
26
|
+
select 1 from public.post_drafts d
|
|
27
|
+
where d.image_path = o.name
|
|
28
|
+
)
|
|
29
|
+
order by o.created_at
|
|
30
|
+
limit p_limit;
|
|
31
|
+
$$;
|
|
32
|
+
|
|
33
|
+
revoke all on function public.post_images_unreferenced(int) from public, anon, authenticated;
|
|
34
|
+
grant execute on function public.post_images_unreferenced(int) to service_role;
|