MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / handle_publish

Function handle_publish

nodedb/src/control/sql_dispatch/dispatch.rs:43–102  ·  view source on GitHub ↗

Handle `PUBLISH TO ` without pgwire coupling.

(
    state: &SharedState,
    identity: &AuthenticatedIdentity,
    sql: &str,
)

Source from the content-addressed store, hash-verified

41
42/// Handle `PUBLISH TO <topic> <payload>` without pgwire coupling.
43async 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 }),

Callers 1

dispatch_sqlFunction · 0.70

Calls 5

parse_payloadFunction · 0.85
publish_to_topicFunction · 0.85
publish_remoteFunction · 0.85
lenMethod · 0.45
as_u64Method · 0.45

Tested by

no test coverage detected