fix: sync monoforum suggested post flow

This commit is contained in:
A 2026-07-22 16:08:44 +08:00
parent 511f25efc2
commit 401a8f148e
35 changed files with 2415 additions and 144 deletions

View file

@ -0,0 +1 @@
DROP TABLE IF EXISTS public.suggested_post_approvals;

View file

@ -0,0 +1,39 @@
-- Durable suggested-post approval/payment/publication state. The row is the
-- idempotency key for a monoforum suggestion; message/update rows remain the
-- client-visible source of truth and are written in the same transaction.
CREATE TABLE public.suggested_post_approvals (
monoforum_id bigint NOT NULL,
suggestion_message_id integer NOT NULL,
parent_channel_id bigint NOT NULL,
actor_user_id bigint NOT NULL,
payer_user_id bigint NOT NULL,
state text NOT NULL,
price_kind text NOT NULL DEFAULT '',
price_amount bigint NOT NULL DEFAULT 0,
price_nanos integer NOT NULL DEFAULT 0,
schedule_date integer NOT NULL DEFAULT 0,
approval_service_message_id integer NOT NULL DEFAULT 0,
published_message_id integer NOT NULL DEFAULT 0,
settlement_due integer NOT NULL DEFAULT 0,
final_service_message_id integer NOT NULL DEFAULT 0,
created_at integer NOT NULL,
updated_at integer NOT NULL,
PRIMARY KEY (monoforum_id, suggestion_message_id),
CONSTRAINT suggested_post_approvals_shape_check CHECK (
monoforum_id>0 AND suggestion_message_id>0 AND parent_channel_id>0 AND
actor_user_id>0 AND payer_user_id>0 AND created_at>0 AND updated_at>=created_at AND
state IN ('balance_low','rejected','scheduled','published','completed','refunded') AND
price_kind IN ('','stars','ton') AND price_amount>=0 AND price_nanos BETWEEN 0 AND 999999999 AND
((price_kind='' AND price_amount=0 AND price_nanos=0) OR
(price_kind='stars' AND price_amount>0) OR
(price_kind='ton' AND price_amount>0 AND price_nanos=0)) AND
schedule_date>=0 AND approval_service_message_id>=0 AND published_message_id>=0 AND
settlement_due>=0 AND final_service_message_id>=0)
);
CREATE INDEX suggested_post_approvals_schedule_idx
ON public.suggested_post_approvals(schedule_date,monoforum_id,suggestion_message_id)
WHERE state='scheduled';
CREATE INDEX suggested_post_approvals_settlement_idx
ON public.suggested_post_approvals(settlement_due,monoforum_id,suggestion_message_id)
WHERE state='published';

View file

@ -0,0 +1,17 @@
-- The data backfill is intentionally retained on rollback. Restore only the
-- pre-0134 shape constraint, which allowed zero schedule_date in every state.
ALTER TABLE suggested_post_approvals
DROP CONSTRAINT suggested_post_approvals_shape_check;
ALTER TABLE suggested_post_approvals
ADD CONSTRAINT suggested_post_approvals_shape_check CHECK (
monoforum_id>0 AND suggestion_message_id>0 AND parent_channel_id>0 AND
actor_user_id>0 AND payer_user_id>0 AND created_at>0 AND updated_at>=created_at AND
state IN ('balance_low','rejected','scheduled','published','completed','refunded') AND
price_kind IN ('','stars','ton') AND price_amount>=0 AND price_nanos BETWEEN 0 AND 999999999 AND
((price_kind='' AND price_amount=0 AND price_nanos=0) OR
(price_kind='stars' AND price_amount>0) OR
(price_kind='ton' AND price_amount>0 AND price_nanos=0)) AND
schedule_date>=0 AND approval_service_message_id>=0 AND published_message_id>=0 AND
settlement_due>=0 AND final_service_message_id>=0
);

View file

@ -0,0 +1,93 @@
-- TDesktop omits schedule_date for "Publish Now", while the approval action
-- renderer always formats an absolute publication date. Backfill rows written
-- by the initial lifecycle implementation and keep current/history/difference
-- projections on the same effective timestamp.
UPDATE channel_messages m
SET suggested_post = jsonb_set(
m.suggested_post,
'{ScheduleDate}',
to_jsonb(a.created_at),
true
)
FROM suggested_post_approvals a
WHERE a.schedule_date = 0
AND a.state IN ('scheduled', 'published', 'completed', 'refunded')
AND m.channel_id = a.monoforum_id
AND m.id = a.suggestion_message_id
AND COALESCE((m.suggested_post->>'Accepted')::boolean, false)
AND COALESCE((m.suggested_post->>'ScheduleDate')::integer, 0) = 0;
UPDATE channel_messages m
SET action = jsonb_set(
m.action,
'{SuggestedPostScheduleDate}',
to_jsonb(a.created_at),
true
)
FROM suggested_post_approvals a
WHERE a.schedule_date = 0
AND a.state IN ('scheduled', 'published', 'completed', 'refunded')
AND m.channel_id = a.monoforum_id
AND m.id = a.approval_service_message_id
AND m.action->>'Type' = 'suggested_post_approval'
AND NOT COALESCE((m.action->>'SuggestedPostRejected')::boolean, false)
AND NOT COALESCE((m.action->>'SuggestedPostBalanceTooLow')::boolean, false)
AND COALESCE((m.action->>'SuggestedPostScheduleDate')::integer, 0) = 0;
UPDATE channel_update_events e
SET payload = jsonb_set(
e.payload,
'{message,SuggestedPost,ScheduleDate}',
to_jsonb(a.created_at),
true
)
FROM suggested_post_approvals a
WHERE a.schedule_date = 0
AND a.state IN ('scheduled', 'published', 'completed', 'refunded')
AND e.channel_id = a.monoforum_id
AND e.message_id = a.suggestion_message_id
AND e.event_type = 'edit_channel_message'
AND COALESCE((e.payload #>> '{message,SuggestedPost,Accepted}')::boolean, false)
AND COALESCE((e.payload #>> '{message,SuggestedPost,ScheduleDate}')::integer, 0) = 0;
UPDATE channel_update_events e
SET payload = jsonb_set(
e.payload,
'{message,Action,SuggestedPostScheduleDate}',
to_jsonb(a.created_at),
true
)
FROM suggested_post_approvals a
WHERE a.schedule_date = 0
AND a.state IN ('scheduled', 'published', 'completed', 'refunded')
AND e.channel_id = a.monoforum_id
AND e.message_id = a.approval_service_message_id
AND e.event_type = 'new_channel_message'
AND e.payload #>> '{message,Action,Type}' = 'suggested_post_approval'
AND NOT COALESCE((e.payload #>> '{message,Action,SuggestedPostRejected}')::boolean, false)
AND NOT COALESCE((e.payload #>> '{message,Action,SuggestedPostBalanceTooLow}')::boolean, false)
AND COALESCE((e.payload #>> '{message,Action,SuggestedPostScheduleDate}')::integer, 0) = 0;
UPDATE suggested_post_approvals
SET schedule_date = created_at,
updated_at = GREATEST(updated_at, created_at)
WHERE schedule_date = 0
AND state IN ('scheduled', 'published', 'completed', 'refunded');
ALTER TABLE suggested_post_approvals
DROP CONSTRAINT suggested_post_approvals_shape_check;
ALTER TABLE suggested_post_approvals
ADD CONSTRAINT suggested_post_approvals_shape_check CHECK (
monoforum_id>0 AND suggestion_message_id>0 AND parent_channel_id>0 AND
actor_user_id>0 AND payer_user_id>0 AND created_at>0 AND updated_at>=created_at AND
state IN ('balance_low','rejected','scheduled','published','completed','refunded') AND
price_kind IN ('','stars','ton') AND price_amount>=0 AND price_nanos BETWEEN 0 AND 999999999 AND
((price_kind='' AND price_amount=0 AND price_nanos=0) OR
(price_kind='stars' AND price_amount>0) OR
(price_kind='ton' AND price_amount>0 AND price_nanos=0)) AND
schedule_date>=0 AND
(state IN ('balance_low','rejected') OR schedule_date>0) AND
approval_service_message_id>=0 AND published_message_id>=0 AND
settlement_due>=0 AND final_service_message_id>=0
);