aboutsummaryrefslogblamecommitdiffstats
path: root/filamento/examples/example.rs
blob: 8119743669ef6870e7e10ba745bee4da55c7012d (plain) (tree)
1
2
3
4
5
6
7
8
                                                               
 
                                                  
             
                                    

                  
                       





                                                                      

                                                                  
                  


                                                     


                                                                             

                                                                   
                  


                                                     


                                                                 

                                                                   
                  


                                                      


     


                                    
                                                                        

                  
                                         
                                                    



                             







                                                 
                                                     
                           



                                                 





                                                                             









                                                  
                          









                                                      
























                                                                      
 
use std::{path::Path, str::FromStr, sync::Arc, time::Duration};

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/testing2".try_into().unwrap(),
        "slayed".to_string(),
        db,
        Files,
    );

    tokio::spawn(async move {
        while let Some(msg) = recv.recv().await {
            info!("{:#?}", msg)
        }
    });

    client.connect().await.unwrap();
    tokio::time::sleep(Duration::from_secs(5)).await;
    info!("changing nick");
    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(
            JID::from_str("cel@blos.sm").unwrap(),
            filamento::chat::Body {
                body: "hallo!!!".to_string(),
            },
        )
        .await
        .unwrap();
    info!("sent message");
    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);
    // let items = client.disco_items(None, None).await.unwrap();
    // info!("got disco result: {:#?}", items);
    // let info = client
    //     .disco_info(Some("blos.sm".parse().unwrap()), None)
    //     .await
    //     .unwrap();
    // info!("got disco result: {:#?}", info);
    // let items = client
    //     .disco_items(Some("blos.sm".parse().unwrap()), None)
    //     .await
    //     .unwrap();
    // info!("got disco result: {:#?}", items);
    // let info = client
    //     .disco_info(Some("pubsub.blos.sm".parse().unwrap()), None)
    //     .await
    //     .unwrap();
    // info!("got disco result: {:#?}", info);
    // let items = client
    //     .disco_items(Some("pubsub.blos.sm".parse().unwrap()), None)
    //     .await
    //     .unwrap();
    // info!("got disco result: {:#?}", items);
}