aboutsummaryrefslogtreecommitdiffstats
path: root/filamento/examples/example.rs
blob: 8119743669ef6870e7e10ba745bee4da55c7012d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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);
}