@@ -262,6 +262,53 @@ async fn replace_parameterized_event_in_transaction_impl(
262262 let tags_json = serde_json:: to_value ( & event. tags ) ?;
263263 crate :: channel_members:: guard_channel_roster_event ( tx, kind_i32, channel_id, & tags_json)
264264 . await ?;
265+
266+ let sig_bytes = event. sig . serialize ( ) ;
267+ let insert_result = sqlx:: query (
268+ "INSERT INTO events (community_id, id, pubkey, created_at, kind, tags, content, sig, received_at, channel_id, d_tag, not_before) \
269+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) \
270+ ON CONFLICT DO NOTHING",
271+ )
272+ . bind ( community_id. as_uuid ( ) )
273+ . bind ( incoming_id)
274+ . bind ( pubkey_bytes. as_slice ( ) )
275+ . bind ( created_at)
276+ . bind ( kind_i32)
277+ . bind ( & tags_json)
278+ . bind ( & event. content )
279+ . bind ( sig_bytes. as_slice ( ) )
280+ . bind ( received_at)
281+ . bind ( channel_id)
282+ . bind ( d_tag)
283+ . bind ( crate :: event:: extract_not_before ( event) )
284+ . execute ( tx. conn ( ) )
285+ . await ?;
286+
287+ if insert_result. rows_affected ( ) == 0 {
288+ return Ok ( false ) ;
289+ }
290+
291+ if is_nip_rs {
292+ sqlx:: query (
293+ "INSERT INTO parameterized_event_watermarks \
294+ (community_id, kind, pubkey, d_tag, created_at, event_id) \
295+ VALUES ($1, $2, $3, $4, $5, $6) \
296+ ON CONFLICT (community_id, kind, pubkey, d_tag) DO UPDATE SET \
297+ created_at = EXCLUDED.created_at, event_id = EXCLUDED.event_id",
298+ )
299+ . bind ( community_id. as_uuid ( ) )
300+ . bind ( kind_i32)
301+ . bind ( pubkey_bytes. as_slice ( ) )
302+ . bind ( d_tag)
303+ . bind ( created_at)
304+ . bind ( incoming_id)
305+ . execute ( tx. conn ( ) )
306+ . await ?;
307+ }
308+
309+ crate :: insert_mentions_in_transaction ( & mut * tx, event, channel_id) . await ?;
310+ // Authorize retained references while the previous head is still live.
311+ crate :: store:: event_follow_up:: record_admitted_media ( tx, event, channel_id) . await ?;
265312 if existing. is_some ( ) {
266313 let previous_nip_rs_hard_delete: Option < String > = if is_nip_rs {
267314 sqlx:: query_scalar (
@@ -279,16 +326,17 @@ async fn replace_parameterized_event_in_transaction_impl(
279326 }
280327 let statement = if hard_delete_superseded {
281328 "DELETE FROM events \
282- WHERE community_id = $1 AND kind = $2 AND pubkey = $3 AND d_tag = $4 AND deleted_at IS NULL"
329+ WHERE community_id = $1 AND kind = $2 AND pubkey = $3 AND d_tag = $4 AND deleted_at IS NULL AND id <> $5 "
283330 } else {
284331 "UPDATE events SET deleted_at = NOW() \
285- WHERE community_id = $1 AND kind = $2 AND pubkey = $3 AND d_tag = $4 AND deleted_at IS NULL"
332+ WHERE community_id = $1 AND kind = $2 AND pubkey = $3 AND d_tag = $4 AND deleted_at IS NULL AND id <> $5 "
286333 } ;
287334 sqlx:: query ( statement)
288335 . bind ( community_id. as_uuid ( ) )
289336 . bind ( kind_i32)
290337 . bind ( pubkey_bytes. as_slice ( ) )
291338 . bind ( d_tag)
339+ . bind ( incoming_id)
292340 . execute ( tx. conn ( ) )
293341 . await ?;
294342
@@ -311,50 +359,6 @@ async fn replace_parameterized_event_in_transaction_impl(
311359 }
312360 }
313361
314- let sig_bytes = event. sig . serialize ( ) ;
315- let insert_result = sqlx:: query (
316- "INSERT INTO events (community_id, id, pubkey, created_at, kind, tags, content, sig, received_at, channel_id, d_tag, not_before) \
317- VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) \
318- ON CONFLICT DO NOTHING",
319- )
320- . bind ( community_id. as_uuid ( ) )
321- . bind ( incoming_id)
322- . bind ( pubkey_bytes. as_slice ( ) )
323- . bind ( created_at)
324- . bind ( kind_i32)
325- . bind ( & tags_json)
326- . bind ( & event. content )
327- . bind ( sig_bytes. as_slice ( ) )
328- . bind ( received_at)
329- . bind ( channel_id)
330- . bind ( d_tag)
331- . bind ( crate :: event:: extract_not_before ( event) )
332- . execute ( tx. conn ( ) )
333- . await ?;
334-
335- if insert_result. rows_affected ( ) == 0 {
336- return Ok ( false ) ;
337- }
338-
339- if is_nip_rs {
340- sqlx:: query (
341- "INSERT INTO parameterized_event_watermarks \
342- (community_id, kind, pubkey, d_tag, created_at, event_id) \
343- VALUES ($1, $2, $3, $4, $5, $6) \
344- ON CONFLICT (community_id, kind, pubkey, d_tag) DO UPDATE SET \
345- created_at = EXCLUDED.created_at, event_id = EXCLUDED.event_id",
346- )
347- . bind ( community_id. as_uuid ( ) )
348- . bind ( kind_i32)
349- . bind ( pubkey_bytes. as_slice ( ) )
350- . bind ( d_tag)
351- . bind ( created_at)
352- . bind ( incoming_id)
353- . execute ( tx. conn ( ) )
354- . await ?;
355- }
356-
357- crate :: insert_mentions_in_transaction ( & mut * tx, event, channel_id) . await ?;
358362 Ok ( true )
359363 }
360364 . await ;
@@ -383,7 +387,7 @@ async fn replace_parameterized_event_in_transaction_impl(
383387 . execute ( tx. conn ( ) )
384388 . await ?;
385389 if status == ParameterizedReplaceStatus :: Inserted {
386- crate :: store:: event_follow_up:: after_admitted_insert ( tx, event, channel_id) . await ? ;
390+ crate :: store:: event_follow_up:: stage_admitted_insert ( tx, event, channel_id) ;
387391 }
388392
389393 Ok ( ParameterizedReplaceResult :: new (
@@ -492,20 +496,6 @@ impl Db {
492496 )
493497 . await ?;
494498
495- // Soft-delete the old event (if any). IS NOT DISTINCT FROM for NULL safety.
496- sqlx:: query (
497- "UPDATE events SET deleted_at = NOW() \
498- WHERE community_id = $1 AND kind = $2 AND pubkey = $3 \
499- AND channel_id IS NOT DISTINCT FROM $4 \
500- AND deleted_at IS NULL",
501- )
502- . bind ( community_id. as_uuid ( ) )
503- . bind ( kind_i32)
504- . bind ( pubkey_bytes. as_slice ( ) )
505- . bind ( channel_id)
506- . execute ( tx. conn ( ) )
507- . await ?;
508-
509499 // Insert the new event inside the same transaction.
510500 let sig_bytes = event. sig . serialize ( ) ;
511501 let received_at = chrono:: Utc :: now ( ) ;
@@ -532,8 +522,7 @@ impl Db {
532522
533523 let was_inserted = insert_result. rows_affected ( ) > 0 ;
534524 if !was_inserted {
535- // ON CONFLICT fired — the event ID already exists. Rollback the
536- // soft-delete so we don't lose the previous replaceable event.
525+ // ON CONFLICT fired — leave the previously-live event intact.
537526 tx. rollback ( ) . await ?;
538527 return Ok ( (
539528 StoredEvent :: with_received_at (
@@ -546,9 +535,25 @@ impl Db {
546535 ) ) ;
547536 }
548537
538+ // Authorize retained references while the previous head is still live.
549539 crate :: store:: event_follow_up:: after_admitted_insert ( & mut tx, event, channel_id)
550540 . await ?;
551541
542+ // Soft-delete the old event (if any). IS NOT DISTINCT FROM for NULL safety.
543+ sqlx:: query (
544+ "UPDATE events SET deleted_at = NOW() \
545+ WHERE community_id = $1 AND kind = $2 AND pubkey = $3 \
546+ AND channel_id IS NOT DISTINCT FROM $4 \
547+ AND deleted_at IS NULL AND id <> $5",
548+ )
549+ . bind ( community_id. as_uuid ( ) )
550+ . bind ( kind_i32)
551+ . bind ( pubkey_bytes. as_slice ( ) )
552+ . bind ( channel_id)
553+ . bind ( incoming_id)
554+ . execute ( tx. conn ( ) )
555+ . await ?;
556+
552557 // The replaceable event and its denormalized mention index are one
553558 // authoritative discovery write. An indexing error must roll back the
554559 // new event and restore the previously-live event.
0 commit comments