Skip to content

Commit

Permalink
Merge pull request #14 from Desiders/add-chat-in-thread-strategy
Browse files Browse the repository at this point in the history
Add `ChatInThread` strategy and fix chat type in middleware
  • Loading branch information
Desiders authored Apr 28, 2024
2 parents 33e974a + 8f85072 commit eb60492
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
26 changes: 23 additions & 3 deletions telers/src/fsm/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@ use std::fmt::{self, Display};

/// Strategy for storing and retrieving data.
///
/// If you use `UserInChat` strategy, you have possible to store different data and state for different chats.
/// If you use `Chat` strategy, then all users in the chat will have the same data and state.
/// If you use `GlobalUser` strategy, then the user will have the same data and state in all chats.
/// # Variants
/// * [`Strategy::UserInChat`] - `user_id` + `chat_id`.
/// If you use `UserInChat` strategy, you have possible to store different state for user+chat pairs,
/// so each user in each chat will have its own state.
/// * [`Strategy::Chat`] - `chat_id` + `chat_id`.
/// If you use `Chat` strategy, then all users in the chat will have the same state.
/// * [`Strategy::GlobalUser`] - `user_id` + `user_id`.
/// If you use `GlobalUser` strategy, then the user will have the same state in all chats,
/// so each user will have its own state in all chats.
/// * [`Strategy::UserInThread`] - `user_id` + `chat_id` + `message_thread_id`.
/// If you use `UserInThread` strategy, you have possible to store different state for user+chat+thread pairs,
/// so each user in each thread in each chat will have its own state.
/// * [`Strategy::ChatInThread`] - `chat_id` + `chat_id` + `message_thread_id`.
/// If you use `ChatInThread` strategy, you have possible to store different state for chat+thread pairs,
/// so each thread in each chat will have its own state.
///
/// In case of direct messages, `chat_id` and `user_id` will be equal, so all strategies will work the same way.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand All @@ -17,6 +29,8 @@ pub enum Strategy {
GlobalUser,
/// `user_id` + `chat_id` + `message_thread_id`
UserInThread,
/// `chat_id` + `chat_id` + `message_thread_id`
ChatInThread,
}

impl Display for Strategy {
Expand All @@ -40,6 +54,7 @@ impl Strategy {
Strategy::Chat => "chat",
Strategy::GlobalUser => "global_user",
Strategy::UserInThread => "user_in_thread",
Strategy::ChatInThread => "chat_in_thread",
}
}
}
Expand Down Expand Up @@ -75,6 +90,11 @@ impl Strategy {
user_id,
message_thread_id,
},
Strategy::ChatInThread => IdPair {
chat_id,
user_id: chat_id,
message_thread_id,
},
}
}
}
4 changes: 2 additions & 2 deletions telers/src/middlewares/outer/fsm_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
Context, Storage,
},
router::Request,
types::User,
types::{Chat, User},
};

use async_trait::async_trait;
Expand Down Expand Up @@ -85,7 +85,7 @@ where
let message_thread_id = context.get("event_message_thread_id");

let user_id = user.and_then(|user| user.downcast_ref().map(|user: &User| user.id));
let chat_id = chat.and_then(|user| user.downcast_ref().map(|chat: &User| chat.id));
let chat_id = chat.and_then(|chat| chat.downcast_ref().map(|chat: &Chat| chat.id()));
let message_thread_id = message_thread_id
.and_then(|message_thread_id| message_thread_id.downcast_ref().copied());

Expand Down

0 comments on commit eb60492

Please sign in to comment.