Skip to content

Commit

Permalink
fix: handle missing ephemeral field and unknown encodings in content …
Browse files Browse the repository at this point in the history
…topics (#61)

* fix(example): only attempt to decode messages sent in the toy chat content topic
* fix: handle missing ephemeral field and unknown encodings
  • Loading branch information
richard-ramos committed May 23, 2023
1 parent 8d825ad commit 803fc37
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
4 changes: 4 additions & 0 deletions examples/toy-chat/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ fn main() -> std::result::Result<(), Box<dyn Error>> {
let shared_messages = Arc::clone(&app.messages);
waku_set_event_callback(move |signal| match signal.event() {
waku_bindings::Event::WakuMessage(event) => {
if event.waku_message().content_topic() != &TOY_CHAT_CONTENT_TOPIC {
return;
}

match <Chat2Message as Message>::decode(event.waku_message().payload()) {
Ok(chat_message) => {
shared_messages.write().unwrap().push(chat_message);
Expand Down
10 changes: 5 additions & 5 deletions waku-bindings/src/general/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub struct WakuMessage {
timestamp: usize,
#[serde(with = "base64_serde", default = "Vec::new")]
meta: Vec<u8>,
#[serde(default)]
ephemeral: bool,
// TODO: implement RLN fields
#[serde(flatten)]
Expand Down Expand Up @@ -310,11 +311,12 @@ pub struct MessageIndex {
}

/// WakuMessage encoding scheme
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Encoding {
Proto,
Rlp,
Rfc26,
Unknown(String),
}

impl Display for Encoding {
Expand All @@ -323,6 +325,7 @@ impl Display for Encoding {
Encoding::Proto => "proto",
Encoding::Rlp => "rlp",
Encoding::Rfc26 => "rfc26",
Encoding::Unknown(value) => value,
};
f.write_str(s)
}
Expand All @@ -336,10 +339,7 @@ impl FromStr for Encoding {
"proto" => Ok(Self::Proto),
"rlp" => Ok(Self::Rlp),
"rfc26" => Ok(Self::Rfc26),
encoding => Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!("Unrecognized encoding: {encoding}"),
)),
encoding => Ok(Self::Unknown(encoding.to_string())),
}
}
}
Expand Down

0 comments on commit 803fc37

Please sign in to comment.