Skip to content

Commit

Permalink
Added more filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Kayanski committed May 21, 2024
1 parent 318fe81 commit 16321e7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
33 changes: 26 additions & 7 deletions contracts/client/src/handlers/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use ibcmail::{
msg::{MessageFilter, MessagesResponse},
state::{RECEIVED, SENT},
},
MessageId, MessageStatus,
Message, MessageId, MessageStatus,
};

use crate::{
Expand Down Expand Up @@ -101,12 +101,8 @@ fn query_messages_list(
let (_, message) = i?;
next_key = Some(message.id.clone());

if let Some(filter) = &filter {
if let Some(sender) = &filter.from {
if message.sender != *sender {
continue;
}
}
if !matches_filter(&filter, &message)? {
continue;
}

messages.push(message);
Expand All @@ -132,3 +128,26 @@ fn query_config(deps: Deps) -> StdResult<ConfigResponse> {
let _config = CONFIG.load(deps.storage)?;
Ok(ConfigResponse {})
}

// Keeping a result here in case we need to parse things or use Addr in the future
/// Verifies that the given message matches the filter, if any
fn matches_filter(filter: &Option<MessageFilter>, message: &Message) -> StdResult<bool> {
if let Some(filter) = &filter {
if let Some(sender) = &filter.from {
if message.sender != *sender {
return Ok(false);
}
}
if let Some(contains) = &filter.contains {
if !message.body.contains(contains) && !message.subject.contains(contains) {
return Ok(false);
}
}
if let Some(sent_after) = &filter.sent_after {
if message.timestamp.nanos() < sent_after.nanos() {
return Ok(false);
}
}
}
Ok(true)
}
3 changes: 3 additions & 0 deletions packages/ibcmail/src/client/msg.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cosmwasm_schema::QueryResponses;
use cosmwasm_std::Timestamp;

use crate::{client::ClientApp, Message, MessageId, MessageStatus, NewMessage, Route, Sender};

Expand Down Expand Up @@ -51,6 +52,8 @@ pub enum ClientQueryMsg {
#[cosmwasm_schema::cw_serde]
pub struct MessageFilter {
pub from: Option<Sender>,
pub contains: Option<String>,
pub sent_after: Option<Timestamp>,
}

#[cosmwasm_schema::cw_serde]
Expand Down

0 comments on commit 16321e7

Please sign in to comment.