summaryrefslogtreecommitdiffstats
path: root/src/handlers.rs
blob: 6826847a923f659e3afc6b09af803cf6044a954b (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
use crate::{airdata::AsyncData, lossy};
use log::{debug, error, info, trace, warn};
use paperplane::client;
use paperplane::types;

pub async fn update_message_content(cache: AsyncData, update: types::UpdateMessageContent) {
    let chat = cache.get_chat_title_lossy(update.chat_id).await;
    let was_message = cache
        .get_msg(update.message_id)
        .await
        .and_then(|m| Some((m.sender_user_id, m.content.clone())));
    let now = update.new_content;
    let from;
    let was;
    if let Some((user_id, was_message)) = was_message {
        from = cache.get_username_lossy(user_id).await;
        was = Some(was_message);
    } else {
        from = lossy::LossyUsername::Unknown;
        was = None
    }
    info!(
        "EDIT: from: {} chat: {} was: {:?} now: {:?}",
        from, chat, was, now
    );
}

async fn download_file(
    client: &client::Client,
    file_id: i32,
) -> Result<paperplane::types::File, client::Error> {
    Ok(client
        .send(paperplane::methods::DownloadFile {
            file_id,
            priority: 32,
            offset: 0,
            limit: 0,
            synchronous: true,
        })?
        .await?)
}

pub async fn update_new_message(
    client: &client::Client,
    cache: AsyncData,
    update: types::UpdateNewMessage,
) {
    let message = update.message;
    let username = cache.get_username_lossy(message.sender_user_id).await;
    let chat_title = cache.get_chat_title_lossy(message.chat_id).await;
    debug!(
        "NEW:  from {} in chat {}: {:?}",
        username, chat_title, message.content
    );
    // TODO: do not serialize with json
    let _ = cache.insert_message(message.clone()).await;
    use paperplane::types::MessageContent::*;
    match message.content {
        MessagePhoto(msg_photo) => {
            for size in msg_photo.photo.sizes.iter() {
                let pre_file = &size.photo;
                match download_file(client, pre_file.id).await {
                    Ok(post_file) => trace!(
                        "File {}: download complete. Path: {}",
                        post_file.id,
                        post_file.local.path
                    ),
                    Err(e) => error!(
                        "File {}: download failed. Chat: {}. Err: {}",
                        pre_file.id, chat_title, e
                    ),
                }
            }
        }
        _ => {}
    }
}

pub async fn update_delete_messages(cache: AsyncData, update: types::UpdateDeleteMessages) {
    if update.from_cache {
        debug!(
            "delete msgs from cache from chat {}",
            cache.get_chat_title_lossy(update.chat_id).await
        )
    } else {
        warn!(
            "chat {} deleted messages:",
            cache.get_chat_title_lossy(update.chat_id).await
        );
        for id in update.message_ids {
            match cache.get_msg(id).await {
                Some(msg) => warn!("    DEL: {:?}", msg.content),
                None => warn!("    DEL: UNK"),
            }
        }
    }
}