diff --git a/rust/psibase/src/services/r_events.rs b/rust/psibase/src/services/r_events.rs index d296cf39d..854c58585 100644 --- a/rust/psibase/src/services/r_events.rs +++ b/rust/psibase/src/services/r_events.rs @@ -3,6 +3,11 @@ mod service { use crate::HttpRequest; + #[action] + fn sqlQuery(query: String) -> String { + unimplemented!() + } + #[action] fn serveSys(request: HttpRequest) -> Option { unimplemented!() diff --git a/services/user/Chainmail/plugin/src/lib.rs b/services/user/Chainmail/plugin/src/lib.rs index 2d1d1ae9a..2df118a7b 100644 --- a/services/user/Chainmail/plugin/src/lib.rs +++ b/services/user/Chainmail/plugin/src/lib.rs @@ -2,12 +2,47 @@ mod bindings; use bindings::exports::chainmail::plugin::api::{Error, Guest as API}; +use bindings::exports::chainmail::plugin::queries::{Guest as QUERY, Message}; use bindings::transact::plugin::intf as Transact; use psibase::fracpack::Pack; +use psibase::services::r_events::Wrapper as REventsSvc; use psibase::AccountNumber; struct ChainmailPlugin; +fn build_query( + archived_requested: bool, + sender: Option, + receiver: Option, +) -> String { + let where_clause_sender_receiver = String::from(""); + + let select_clause = format!("DISTINCT sent.rowid as msg_id, archive.event_id, sent.*"); + let from_clause = format!("\"history.chainmail.sent\" AS sent LEFT JOIN \"history.chainmail.archive\" AS archive ON CONCAT(sent.receiver, sent.rowid) = archive.event_id" ); + let where_clause_archived_or_not = format!( + "archive.event_id IS {} NULL", + if archived_requested { "NOT" } else { "" } + ); + let order_by_clause = "sent.ROWID"; + + let sql_query_str = format!( + "SELECT {} FROM {} WHERE {} {} {} ORDER BY {}", + select_clause, + from_clause, + where_clause_archived_or_not, + if sender.is_some() || receiver.is_some() { + "AND" + } else { + "" + }, + where_clause_sender_receiver, + order_by_clause + ); + let resp = REventsSvc::call().sqlQuery(sql_query_str); + println!("response: {}", resp); + resp +} + impl API for ChainmailPlugin { fn send(receiver: String, subject: String, body: String) -> Result<(), Error> { Transact::add_action_to_transaction( @@ -31,4 +66,20 @@ impl API for ChainmailPlugin { } } +impl QUERY for ChainmailPlugin { + fn get_msgs(sender: Option, receiver: Option) -> Result, Error> { + let archived_requested = false; + let _resp = build_query(archived_requested, sender, receiver); + + Ok(vec![]) + } + + fn get_archived_msgs( + _sender: Option, + _receiver: Option, + ) -> Result, Error> { + Ok(vec![]) + } +} + bindings::export!(ChainmailPlugin with_types_in bindings); diff --git a/services/user/Chainmail/plugin/wit/impl.wit b/services/user/Chainmail/plugin/wit/impl.wit index 4c014a05a..5a9c43002 100644 --- a/services/user/Chainmail/plugin/wit/impl.wit +++ b/services/user/Chainmail/plugin/wit/impl.wit @@ -4,4 +4,5 @@ world impl { include host:common/imports; include transact:plugin/imports; export api; + export queries; } diff --git a/services/user/Chainmail/plugin/wit/world.wit b/services/user/Chainmail/plugin/wit/world.wit index f815eb706..fe6139552 100644 --- a/services/user/Chainmail/plugin/wit/world.wit +++ b/services/user/Chainmail/plugin/wit/world.wit @@ -1,5 +1,23 @@ package chainmail:plugin; +interface types { + record message { + msg-id: u64, + receiver: string, + sender: string, + subject: string, + body: string + } +} + +interface queries { + use host:common/types.{error}; + use types.{message}; + + get-msgs: func(sender: option, receiver: option) -> result, error>; + get-archived-msgs: func(sender: option, receiver: option) -> result, error>; +} + interface api { use host:common/types.{error}; @@ -12,4 +30,5 @@ interface api { world imports { import api; + import queries; } \ No newline at end of file