use std::collections::HashSet;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use rust_tg_bot_raw::types::update::Update;
use super::base::{Handler, HandlerCallback, HandlerResult, MatchResult};
pub struct ChatJoinRequestHandler {
callback: HandlerCallback,
chat_ids: HashSet<i64>,
usernames: HashSet<String>,
block: bool,
}
impl ChatJoinRequestHandler {
pub fn new(
callback: HandlerCallback,
chat_ids: HashSet<i64>,
usernames: HashSet<String>,
block: bool,
) -> Self {
let usernames = usernames
.into_iter()
.map(|u| u.trim_start_matches('@').to_lowercase())
.collect();
Self {
callback,
chat_ids,
usernames,
block,
}
}
}
impl Handler for ChatJoinRequestHandler {
fn check_update(&self, update: &Update) -> Option<MatchResult> {
let cjr = update.chat_join_request()?;
if self.chat_ids.is_empty() && self.usernames.is_empty() {
return Some(MatchResult::Empty);
}
if self.chat_ids.contains(&cjr.chat.id) {
return Some(MatchResult::Empty);
}
if let Some(username) = cjr.from_user.username.as_deref() {
if self.usernames.contains(&username.to_lowercase()) {
return Some(MatchResult::Empty);
}
}
None
}
fn handle_update(
&self,
update: Arc<Update>,
match_result: MatchResult,
) -> Pin<Box<dyn Future<Output = HandlerResult> + Send>> {
(self.callback)(update, match_result)
}
fn block(&self) -> bool {
self.block
}
}