Handle `PUBLISH TO ` without pgwire coupling.
(
state: &SharedState,
identity: &AuthenticatedIdentity,
sql: &str,
)
| 41 | |
| 42 | /// Handle `PUBLISH TO <topic> <payload>` without pgwire coupling. |
| 43 | async fn handle_publish( |
| 44 | state: &SharedState, |
| 45 | identity: &AuthenticatedIdentity, |
| 46 | sql: &str, |
| 47 | ) -> crate::Result<DispatchOutcome> { |
| 48 | let prefix = "PUBLISH TO "; |
| 49 | let upper = sql.to_uppercase(); |
| 50 | if !upper.starts_with(prefix) { |
| 51 | return Err(crate::Error::BadRequest { |
| 52 | detail: "expected PUBLISH TO <topic> <payload>".into(), |
| 53 | }); |
| 54 | } |
| 55 | |
| 56 | let rest = sql[prefix.len()..].trim(); |
| 57 | |
| 58 | let (topic_name, payload_part) = |
| 59 | rest.split_once(char::is_whitespace) |
| 60 | .ok_or_else(|| crate::Error::BadRequest { |
| 61 | detail: "expected payload after topic name in PUBLISH TO".into(), |
| 62 | })?; |
| 63 | let topic_name = topic_name.to_lowercase(); |
| 64 | |
| 65 | let payload = parse_payload(payload_part.trim())?; |
| 66 | |
| 67 | let tenant_id = identity.tenant_id.as_u64(); |
| 68 | let tenant = identity.tenant_id; |
| 69 | |
| 70 | use crate::event::topic::publish::PublishError; |
| 71 | |
| 72 | match crate::event::topic::publish::publish_to_topic(state, tenant_id, &topic_name, &payload) { |
| 73 | Ok(_seq) => Ok(DispatchOutcome { |
| 74 | rows_affected: 1, |
| 75 | rows: Vec::new(), |
| 76 | }), |
| 77 | Err(PublishError::RemoteHome { leader_node, .. }) => { |
| 78 | crate::event::topic::publish::publish_remote( |
| 79 | state, |
| 80 | tenant_id, |
| 81 | &topic_name, |
| 82 | &payload, |
| 83 | leader_node, |
| 84 | ) |
| 85 | .await |
| 86 | .map_err(|e| crate::Error::Dispatch { |
| 87 | detail: format!("remote publish to '{topic_name}' failed: {e}"), |
| 88 | })?; |
| 89 | Ok(DispatchOutcome { |
| 90 | rows_affected: 1, |
| 91 | rows: Vec::new(), |
| 92 | }) |
| 93 | } |
| 94 | Err(PublishError::TopicNotFound(t)) => Err(crate::Error::CollectionNotFound { |
| 95 | tenant_id: tenant, |
| 96 | collection: t, |
| 97 | }), |
| 98 | Err(PublishError::RemoteError(e)) => Err(crate::Error::Dispatch { |
| 99 | detail: format!("remote publish to '{topic_name}' failed: {e}"), |
| 100 | }), |
no test coverage detected