summaryrefslogtreecommitdiffstats
path: root/src/handlers.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/handlers.rs')
-rw-r--r--src/handlers.rs97
1 files changed, 97 insertions, 0 deletions
diff --git a/src/handlers.rs b/src/handlers.rs
new file mode 100644
index 0000000..6826847
--- /dev/null
+++ b/src/handlers.rs
@@ -0,0 +1,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"),
+ }
+ }
+ }
+}