//! Implementation of TdClientTrait for TdClient //! //! This file contains the trait implementation that delegates to existing TdClient methods. use super::client::TdClient; use super::r#trait::{ AccountClient, AuthClient, ChatActionClient, ChatClient, ClientState, FileClient, MessageClient, NotificationClient, ReactionClient, UpdateClient, UserClient, }; use super::{ AuthState, ChatInfo, FolderInfo, MessageInfo, ProfileInfo, ReplyInfo, UserCache, UserOnlineStatus, }; use crate::types::{ChatId, MessageId, UserId}; use async_trait::async_trait; use std::borrow::Cow; use std::path::PathBuf; use tdlib_rs::enums::{ChatAction, Update}; #[async_trait] impl AuthClient for TdClient { async fn send_phone_number(&self, phone: String) -> Result<(), String> { self.send_phone_number(phone).await } async fn send_code(&self, code: String) -> Result<(), String> { self.send_code(code).await } async fn send_password(&self, password: String) -> Result<(), String> { self.send_password(password).await } } #[async_trait] impl ChatClient for TdClient { async fn load_chats(&mut self, limit: i32) -> Result<(), String> { self.load_chats(limit).await } async fn load_folder_chats(&mut self, folder_id: i32, limit: i32) -> Result<(), String> { self.load_folder_chats(folder_id, limit).await } async fn leave_chat(&self, chat_id: ChatId) -> Result<(), String> { self.leave_chat(chat_id).await } async fn get_profile_info(&self, chat_id: ChatId) -> Result { self.get_profile_info(chat_id).await } fn chats(&self) -> &[ChatInfo] { self.chats() } fn folders(&self) -> &[FolderInfo] { self.folders() } fn main_chat_list_position(&self) -> i32 { self.main_chat_list_position() } fn set_main_chat_list_position(&mut self, position: i32) { self.set_main_chat_list_position(position) } fn update_chats(&mut self, updater: F) where F: FnOnce(&mut Vec), { updater(self.chats_mut()); } fn update_folders(&mut self, updater: F) where F: FnOnce(&mut Vec), { updater(self.folders_mut()); } } #[async_trait] impl ChatActionClient for TdClient { async fn send_chat_action(&self, chat_id: ChatId, action: ChatAction) { self.send_chat_action(chat_id, action).await } fn clear_stale_typing_status(&mut self) -> bool { self.clear_stale_typing_status() } fn typing_status(&self) -> Option<&(UserId, String, std::time::Instant)> { self.typing_status() } fn set_typing_status(&mut self, status: Option<(UserId, String, std::time::Instant)>) { self.set_typing_status(status) } } #[async_trait] impl MessageClient for TdClient { async fn get_chat_history( &mut self, chat_id: ChatId, limit: i32, ) -> Result, String> { self.get_chat_history(chat_id, limit).await } async fn load_older_messages( &mut self, chat_id: ChatId, from_message_id: MessageId, ) -> Result, String> { self.load_older_messages(chat_id, from_message_id).await } async fn get_pinned_messages(&mut self, chat_id: ChatId) -> Result, String> { self.get_pinned_messages(chat_id).await } async fn load_current_pinned_message(&mut self, chat_id: ChatId) { self.load_current_pinned_message(chat_id).await } async fn search_messages( &self, chat_id: ChatId, query: &str, ) -> Result, String> { self.search_messages(chat_id, query).await } async fn send_message( &mut self, chat_id: ChatId, text: String, reply_to_message_id: Option, reply_info: Option, ) -> Result { TdClient::send_message(self, chat_id, text, reply_to_message_id, reply_info).await } async fn edit_message( &mut self, chat_id: ChatId, message_id: MessageId, new_text: String, ) -> Result { TdClient::edit_message(self, chat_id, message_id, new_text).await } async fn delete_messages( &mut self, chat_id: ChatId, message_ids: Vec, revoke: bool, ) -> Result<(), String> { self.message_manager .delete_messages(chat_id, message_ids, revoke) .await } async fn forward_messages( &mut self, to_chat_id: ChatId, from_chat_id: ChatId, message_ids: Vec, ) -> Result<(), String> { self.message_manager .forward_messages(to_chat_id, from_chat_id, message_ids) .await } async fn set_draft_message(&self, chat_id: ChatId, text: String) -> Result<(), String> { self.set_draft_message(chat_id, text).await } fn current_chat_messages(&self) -> Cow<'_, [MessageInfo]> { Cow::Borrowed(self.current_chat_messages()) } fn current_chat_id(&self) -> Option { self.current_chat_id() } fn current_pinned_message(&self) -> Option { self.current_pinned_message().cloned() } fn push_message(&mut self, msg: MessageInfo) { self.push_message(msg) } async fn fetch_missing_reply_info(&mut self) { self.fetch_missing_reply_info().await } async fn process_pending_view_messages(&mut self) { self.process_pending_view_messages().await } fn clear_current_chat_messages(&mut self) { self.current_chat_messages_mut().clear() } fn set_current_chat_messages(&mut self, messages: Vec) { *self.current_chat_messages_mut() = messages; } fn update_current_chat_messages(&mut self, updater: F) where F: FnOnce(&mut Vec), { updater(self.current_chat_messages_mut()); } fn set_current_chat_id(&mut self, chat_id: Option) { self.set_current_chat_id(chat_id) } fn set_current_pinned_message(&mut self, msg: Option) { self.set_current_pinned_message(msg) } fn pending_view_messages(&self) -> &[(ChatId, Vec)] { self.pending_view_messages() } fn enqueue_pending_view_messages(&mut self, chat_id: ChatId, message_ids: Vec) { self.enqueue_pending_view_messages(chat_id, message_ids); } } #[async_trait] impl UserClient for TdClient { fn get_user_status_by_chat_id(&self, chat_id: ChatId) -> Option<&UserOnlineStatus> { self.get_user_status_by_chat_id(chat_id) } fn pending_user_ids(&self) -> &[UserId] { self.pending_user_ids() } fn user_cache(&self) -> &UserCache { self.user_cache() } fn update_user_cache(&mut self, updater: F) where F: FnOnce(&mut UserCache), { updater(self.user_cache_mut()); } async fn process_pending_user_ids(&mut self) { self.process_pending_user_ids().await } } #[async_trait] impl ReactionClient for TdClient { async fn get_message_available_reactions( &self, chat_id: ChatId, message_id: MessageId, ) -> Result, String> { self.get_message_available_reactions(chat_id, message_id) .await } async fn toggle_reaction( &self, chat_id: ChatId, message_id: MessageId, reaction: String, ) -> Result<(), String> { self.toggle_reaction(chat_id, message_id, reaction).await } } #[async_trait] impl FileClient for TdClient { async fn download_file(&self, file_id: i32) -> Result { self.download_file(file_id).await } async fn download_voice_note(&self, file_id: i32) -> Result { // Voice notes use the same download mechanism as photos self.download_file(file_id).await } } #[async_trait] impl ClientState for TdClient { fn client_id(&self) -> i32 { self.client_id() } async fn get_me(&self) -> Result { self.get_me().await } fn auth_state(&self) -> &AuthState { self.auth_state() } fn network_state(&self) -> super::types::NetworkState { self.network_state.clone() } } impl NotificationClient for TdClient { fn configure_notifications(&mut self, config: &crate::config::NotificationsConfig) { self.configure_notifications(config); } fn sync_notification_muted_chats(&mut self) { self.notification_manager .sync_muted_chats(&self.chat_manager.chats); } } #[async_trait] impl AccountClient for TdClient { async fn recreate_client(&mut self, db_path: PathBuf) -> Result<(), String> { TdClient::recreate_client(self, db_path).await } } impl UpdateClient for TdClient { fn handle_update(&mut self, update: Update) { // Delegate to the real implementation TdClient::handle_update(self, update) } }