Page MenuHomePhabricator

D9463.diff
No OneTemporary

D9463.diff

diff --git a/shared/tunnelbroker_messages/src/lib.rs b/shared/tunnelbroker_messages/src/lib.rs
--- a/shared/tunnelbroker_messages/src/lib.rs
+++ b/shared/tunnelbroker_messages/src/lib.rs
@@ -1,3 +1,4 @@
pub mod messages;
pub use messages::*;
+pub use send_confirmation::*;
diff --git a/shared/tunnelbroker_messages/src/messages/mod.rs b/shared/tunnelbroker_messages/src/messages/mod.rs
--- a/shared/tunnelbroker_messages/src/messages/mod.rs
+++ b/shared/tunnelbroker_messages/src/messages/mod.rs
@@ -2,11 +2,13 @@
pub mod keys;
pub mod message_to_device;
pub mod message_to_device_request;
+pub mod send_confirmation;
pub mod session;
pub use keys::*;
pub use message_to_device::*;
pub use message_to_device_request::*;
+pub use send_confirmation::*;
pub use session::*;
use serde::{Deserialize, Serialize};
@@ -21,4 +23,5 @@
// the first matching pattern it encounters.
MessageToDeviceRequest(MessageToDeviceRequest),
MessageToDevice(MessageToDevice),
+ MessageToDeviceRequestStatus(MessageToDeviceRequestStatus),
}
diff --git a/shared/tunnelbroker_messages/src/messages/send_confirmation.rs b/shared/tunnelbroker_messages/src/messages/send_confirmation.rs
new file mode 100644
--- /dev/null
+++ b/shared/tunnelbroker_messages/src/messages/send_confirmation.rs
@@ -0,0 +1,86 @@
+// Message sent from Tunnelbroker to WebSocket clients to inform that message
+// was processed, saved in DDB and will be delivered.
+
+use serde::{Deserialize, Serialize};
+
+#[derive(Serialize, Deserialize, PartialEq, Debug)]
+pub struct Failure {
+ pub id: String,
+ pub error: String,
+}
+
+#[derive(Serialize, Deserialize, PartialEq, Debug)]
+#[serde(tag = "type", content = "data")]
+pub enum MessageSentStatus {
+ /// The message with the provided ID (String) has been processed
+ /// by the Tunnelbroker and is queued for delivery.
+ Success(String),
+ /// 'Failure' contains information about the message ID
+ /// along with the specific error message.
+ Error(Failure),
+ /// The request was invalid (e.g., Bytes instead of Text).
+ /// In this case, the ID cannot be retrieved.
+ InvalidRequest,
+ /// The JSON could not be serialized, which is why the entire message is
+ /// returned back.
+ /// It becomes impossible to retrieve the message ID in such circumstances.
+ SerializationError(String),
+}
+
+#[derive(Serialize, Deserialize, PartialEq, Debug)]
+#[serde(tag = "type", rename_all = "camelCase")]
+pub struct MessageToDeviceRequestStatus {
+ #[serde(rename = "clientMessageIDs")]
+ pub client_message_ids: Vec<MessageSentStatus>,
+}
+
+#[cfg(test)]
+mod send_confirmation_tests {
+ use super::*;
+
+ #[test]
+ fn test_send_confirmation_deserialization() {
+ let example_payload = r#"{
+ "type": "MessageToDeviceRequestStatus",
+ "clientMessageIDs": [
+ {"type": "Success", "data": "id123"},
+ {"type": "Success", "data": "id456"},
+ {"type": "Error", "data": {"id": "id789", "error": "Something went wrong"}},
+ {"type": "SerializationError", "data": "message"},
+ {"type": "InvalidRequest"}
+ ]
+ }"#;
+
+ let request =
+ serde_json::from_str::<MessageToDeviceRequestStatus>(example_payload)
+ .unwrap();
+
+ let expected_client_message_ids = vec![
+ MessageSentStatus::Success("id123".to_string()),
+ MessageSentStatus::Success("id456".to_string()),
+ MessageSentStatus::Error(Failure {
+ id: String::from("id789"),
+ error: String::from("Something went wrong"),
+ }),
+ MessageSentStatus::SerializationError("message".to_string()),
+ MessageSentStatus::InvalidRequest,
+ ];
+
+ assert_eq!(request.client_message_ids, expected_client_message_ids);
+ }
+
+ #[test]
+ fn test_send_confirmation_deserialization_empty_vec() {
+ let example_payload = r#"{
+ "type": "MessageToDeviceRequestStatus",
+ "clientMessageIDs": []
+ }"#;
+
+ let request =
+ serde_json::from_str::<MessageToDeviceRequestStatus>(example_payload)
+ .unwrap();
+ let expected_client_message_ids: Vec<MessageSentStatus> = Vec::new();
+
+ assert_eq!(request.client_message_ids, expected_client_message_ids);
+ }
+}

File Metadata

Mime Type
text/plain
Expires
Tue, Dec 24, 3:00 AM (19 h, 44 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2698083
Default Alt Text
D9463.diff (4 KB)

Event Timeline