|
1 | 1 | //! Keep the executable workflow and its client-visible definition in sync. |
2 | 2 |
|
3 | | -use buzz_core::{kind::KIND_WORKFLOW_DEF, CommunityId}; |
| 3 | +use buzz_core::{kind::KIND_WORKFLOW_DEF, CommunityId, StoredEvent}; |
4 | 4 | use buzz_datastore_tracing::datastore_span; |
5 | 5 | use chrono::{DateTime, Utc}; |
6 | | -use sqlx::Row; |
| 6 | +use nostr::Event; |
| 7 | +use sqlx::{Postgres, Row, Transaction}; |
7 | 8 | use uuid::Uuid; |
8 | 9 |
|
9 | 10 | use crate::{Db, DbError, Result}; |
@@ -33,75 +34,127 @@ impl Db { |
33 | 34 | d_tag: &str, |
34 | 35 | deletion_created_at_secs: i64, |
35 | 36 | ) -> Result<WorkflowDeletionOutcome> { |
36 | | - let cutoff = DateTime::from_timestamp(deletion_created_at_secs, 0) |
37 | | - .ok_or(DbError::InvalidTimestamp(deletion_created_at_secs))?; |
38 | 37 | let mut tx = self.begin_event_write_transaction().await?; |
39 | | - let lock_key = crate::store::replaceable::event_replacement_lock_key( |
| 38 | + let outcome = delete_workflow_in_transaction( |
| 39 | + &mut tx, |
40 | 40 | community_id, |
41 | | - KIND_WORKFLOW_DEF as i32, |
42 | 41 | owner_pubkey, |
43 | | - Some(d_tag.as_bytes()), |
44 | | - ); |
45 | | - crate::observability::observe_advisory_lock( |
46 | | - crate::observability::LockType::Replacement, |
47 | | - sqlx::query("SELECT pg_advisory_xact_lock($1)") |
48 | | - .bind(lock_key) |
49 | | - .execute(&mut *tx), |
| 42 | + d_tag, |
| 43 | + deletion_created_at_secs, |
50 | 44 | ) |
51 | 45 | .await?; |
| 46 | + tx.commit().await?; |
| 47 | + Ok(outcome) |
| 48 | + } |
52 | 49 |
|
53 | | - let head: Option<DateTime<Utc>> = sqlx::query_scalar( |
54 | | - "SELECT created_at FROM events WHERE community_id = $1 AND kind = $2 \ |
55 | | - AND pubkey = $3 AND d_tag = $4 AND deleted_at IS NULL \ |
56 | | - ORDER BY created_at DESC LIMIT 1", |
| 50 | + /// Commit the public deletion request and both workflow projections together. |
| 51 | + /// |
| 52 | + /// The returned boolean grants dispatch ownership: either this transaction |
| 53 | + /// inserted the request, or it repaired an older relay's incomplete deletion. |
| 54 | + /// Concurrent identical requests cannot split those two outcomes across commits. |
| 55 | + /// Authorization of the coordinate is the caller's responsibility. |
| 56 | + pub async fn insert_workflow_deletion( |
| 57 | + &self, |
| 58 | + community_id: CommunityId, |
| 59 | + event: &Event, |
| 60 | + owner_pubkey: &[u8], |
| 61 | + d_tag: &str, |
| 62 | + ) -> Result<(StoredEvent, bool, Option<Uuid>)> { |
| 63 | + let mut tx = self.begin_event_write_transaction().await?; |
| 64 | + let (stored, inserted) = |
| 65 | + crate::event::insert_event_in_transaction(&mut tx, community_id, event, None).await?; |
| 66 | + if inserted { |
| 67 | + // Unlike best-effort indexing for ordinary events, deletion fails |
| 68 | + // closed: its public request and discoverability commit together. |
| 69 | + crate::runtime::insert_mentions_in_transaction(&mut tx, community_id, event, None) |
| 70 | + .await?; |
| 71 | + } |
| 72 | + let outcome = delete_workflow_in_transaction( |
| 73 | + &mut tx, |
| 74 | + community_id, |
| 75 | + owner_pubkey, |
| 76 | + d_tag, |
| 77 | + event.created_at.as_secs() as i64, |
57 | 78 | ) |
58 | | - .bind(community_id.as_uuid()) |
59 | | - .bind(KIND_WORKFLOW_DEF as i32) |
60 | | - .bind(owner_pubkey) |
61 | | - .bind(d_tag) |
62 | | - .fetch_optional(&mut *tx) |
63 | 79 | .await?; |
64 | | - if head.is_some_and(|created_at| created_at > cutoff) { |
65 | | - return Ok(WorkflowDeletionOutcome::default()); |
66 | | - } |
| 80 | + tx.commit().await?; |
| 81 | + Ok((stored, inserted || outcome.changed, outcome.channel_id)) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +async fn delete_workflow_in_transaction( |
| 86 | + tx: &mut Transaction<'_, Postgres>, |
| 87 | + community_id: CommunityId, |
| 88 | + owner_pubkey: &[u8], |
| 89 | + d_tag: &str, |
| 90 | + deletion_created_at_secs: i64, |
| 91 | +) -> Result<WorkflowDeletionOutcome> { |
| 92 | + let cutoff = DateTime::from_timestamp(deletion_created_at_secs, 0) |
| 93 | + .ok_or(DbError::InvalidTimestamp(deletion_created_at_secs))?; |
| 94 | + let lock_key = crate::store::replaceable::event_replacement_lock_key( |
| 95 | + community_id, |
| 96 | + KIND_WORKFLOW_DEF as i32, |
| 97 | + owner_pubkey, |
| 98 | + Some(d_tag.as_bytes()), |
| 99 | + ); |
| 100 | + crate::observability::observe_advisory_lock( |
| 101 | + crate::observability::LockType::Replacement, |
| 102 | + sqlx::query("SELECT pg_advisory_xact_lock($1)") |
| 103 | + .bind(lock_key) |
| 104 | + .execute(&mut **tx), |
| 105 | + ) |
| 106 | + .await?; |
| 107 | + |
| 108 | + let head: Option<DateTime<Utc>> = sqlx::query_scalar( |
| 109 | + "SELECT created_at FROM events WHERE community_id = $1 AND kind = $2 \ |
| 110 | + AND pubkey = $3 AND d_tag = $4 AND deleted_at IS NULL \ |
| 111 | + ORDER BY created_at DESC LIMIT 1", |
| 112 | + ) |
| 113 | + .bind(community_id.as_uuid()) |
| 114 | + .bind(KIND_WORKFLOW_DEF as i32) |
| 115 | + .bind(owner_pubkey) |
| 116 | + .bind(d_tag) |
| 117 | + .fetch_optional(&mut **tx) |
| 118 | + .await?; |
| 119 | + if head.is_some_and(|created_at| created_at > cutoff) { |
| 120 | + return Ok(WorkflowDeletionOutcome::default()); |
| 121 | + } |
67 | 122 |
|
68 | | - // UUID coordinates are canonical; retain the legacy name-based path. |
69 | | - // The owner predicate remains in the mutation, not just a prior check. |
70 | | - let workflow_id = Uuid::parse_str(d_tag).ok(); |
71 | | - let row = sqlx::query( |
72 | | - "DELETE FROM workflows WHERE community_id = $1 AND owner_pubkey = $2 \ |
| 123 | + // UUID coordinates are canonical; retain the legacy name-based path. |
| 124 | + // The owner predicate remains in the mutation, not just a prior check. |
| 125 | + let workflow_id = Uuid::parse_str(d_tag).ok(); |
| 126 | + let row = sqlx::query( |
| 127 | + "DELETE FROM workflows WHERE community_id = $1 AND owner_pubkey = $2 \ |
73 | 128 | AND id = COALESCE($3::uuid, (SELECT id FROM workflows \ |
74 | 129 | WHERE community_id = $1 AND owner_pubkey = $2 AND name = $4 LIMIT 1)) \ |
75 | 130 | RETURNING channel_id", |
76 | | - ) |
77 | | - .bind(community_id.as_uuid()) |
78 | | - .bind(owner_pubkey) |
79 | | - .bind(workflow_id) |
80 | | - .bind(d_tag) |
81 | | - .fetch_optional(&mut *tx) |
82 | | - .await?; |
83 | | - let definitions = sqlx::query( |
84 | | - "UPDATE events SET deleted_at = NOW() WHERE community_id = $1 AND kind = $2 \ |
| 131 | + ) |
| 132 | + .bind(community_id.as_uuid()) |
| 133 | + .bind(owner_pubkey) |
| 134 | + .bind(workflow_id) |
| 135 | + .bind(d_tag) |
| 136 | + .fetch_optional(&mut **tx) |
| 137 | + .await?; |
| 138 | + let definitions = sqlx::query( |
| 139 | + "UPDATE events SET deleted_at = NOW() WHERE community_id = $1 AND kind = $2 \ |
85 | 140 | AND pubkey = $3 AND d_tag = $4 AND deleted_at IS NULL AND created_at <= $5", |
86 | | - ) |
87 | | - .bind(community_id.as_uuid()) |
88 | | - .bind(KIND_WORKFLOW_DEF as i32) |
89 | | - .bind(owner_pubkey) |
90 | | - .bind(d_tag) |
91 | | - .bind(cutoff) |
92 | | - .execute(&mut *tx) |
93 | | - .await?; |
94 | | - let changed = row.is_some() || definitions.rows_affected() > 0; |
95 | | - let channel_id = row |
96 | | - .map(|row| row.try_get("channel_id")) |
97 | | - .transpose()? |
98 | | - .flatten(); |
99 | | - tx.commit().await?; |
100 | | - Ok(WorkflowDeletionOutcome { |
101 | | - changed, |
102 | | - channel_id, |
103 | | - }) |
104 | | - } |
| 141 | + ) |
| 142 | + .bind(community_id.as_uuid()) |
| 143 | + .bind(KIND_WORKFLOW_DEF as i32) |
| 144 | + .bind(owner_pubkey) |
| 145 | + .bind(d_tag) |
| 146 | + .bind(cutoff) |
| 147 | + .execute(&mut **tx) |
| 148 | + .await?; |
| 149 | + let changed = row.is_some() || definitions.rows_affected() > 0; |
| 150 | + let channel_id = row |
| 151 | + .map(|row| row.try_get("channel_id")) |
| 152 | + .transpose()? |
| 153 | + .flatten(); |
| 154 | + Ok(WorkflowDeletionOutcome { |
| 155 | + changed, |
| 156 | + channel_id, |
| 157 | + }) |
105 | 158 | } |
106 | 159 |
|
107 | 160 | #[cfg(test)] |
|
0 commit comments