diff options
Diffstat (limited to 'filamento/examples/example.rs')
-rw-r--r-- | filamento/examples/example.rs | 69 |
1 files changed, 63 insertions, 6 deletions
diff --git a/filamento/examples/example.rs b/filamento/examples/example.rs index 74a9aa1..8119743 100644 --- a/filamento/examples/example.rs +++ b/filamento/examples/example.rs @@ -1,17 +1,56 @@ -use std::{path::Path, str::FromStr, time::Duration}; +use std::{path::Path, str::FromStr, sync::Arc, time::Duration}; -use filamento::{Client, db::Db}; +use filamento::{Client, db::Db, files::FileStore}; use jid::JID; +use tokio::io::{self, AsyncReadExt}; use tracing::info; +#[derive(Clone, Debug)] +pub struct Files; + +impl FileStore for Files { + type Err = Arc<io::Error>; + + async fn is_stored(&self, name: &str) -> Result<bool, Self::Err> { + tracing::debug!("checking if {} is stored", name); + let res = tokio::fs::try_exists(format!("files/{}", name)) + .await + .map_err(|err| Arc::new(err)); + tracing::debug!("file check res: {:?}", res); + res + } + + async fn store(&self, name: &str, data: &[u8]) -> Result<(), Self::Err> { + tracing::debug!("storing {} is stored", name); + let res = tokio::fs::write(format!("files/{}", name), data) + .await + .map_err(|err| Arc::new(err)); + tracing::debug!("file store res: {:?}", res); + res + } + + async fn delete(&self, name: &str) -> Result<(), Self::Err> { + tracing::debug!("deleting {}", name); + let res = tokio::fs::remove_file(format!("files/{}", name)) + .await + .map_err(|err| Arc::new(err)); + tracing::debug!("file delete res: {:?}", res); + res + } +} + #[tokio::main] async fn main() { tracing_subscriber::fmt::init(); let db = Db::create_connect_and_migrate(Path::new("./filamento.db")) .await .unwrap(); - let (client, mut recv) = - Client::new("test@blos.sm".try_into().unwrap(), "slayed".to_string(), db); + let (client, mut recv) = Client::new( + "test@blos.sm/testing2".try_into().unwrap(), + "slayed".to_string(), + db, + Files, + ); tokio::spawn(async move { while let Some(msg) = recv.recv().await { @@ -22,7 +61,16 @@ async fn main() { client.connect().await.unwrap(); tokio::time::sleep(Duration::from_secs(5)).await; info!("changing nick"); - client.change_nick("britney".to_string()).await.unwrap(); + client + .change_nick(Some("britney".to_string())) + .await + .unwrap(); + let mut profile_pic = tokio::fs::File::open("files/britney_starbies.jpg") + .await + .unwrap(); + let mut data = Vec::new(); + profile_pic.read_to_end(&mut data).await.unwrap(); + client.change_avatar(Some(data)).await.unwrap(); info!("sending message"); client .send_message( @@ -34,7 +82,16 @@ async fn main() { .await .unwrap(); info!("sent message"); - tokio::time::sleep(Duration::from_secs(5)).await; + client + .send_message( + JID::from_str("cel@blos.sm").unwrap(), + filamento::chat::Body { + body: "hallo 2".to_string(), + }, + ) + .await + .unwrap(); + tokio::time::sleep(Duration::from_secs(15)).await; // info!("sending disco query"); // let info = client.disco_info(None, None).await.unwrap(); // info!("got disco result: {:#?}", info); |