use chatsvc::{ BotProfile, BotProfiles, CallLogEntry, CardActions, ChatApp, ConversationRef, ConversationState, Conversations, ForcedAvailability, ForwardResult, Gif, Gifs, InvokeRequest, InvokeResponse, Language, LanguageSettings, LanguageSettingsClient, LinkInfo, MemberHorizon, MessageLanguage, MessageEmotions, MessageLinks, MessageWindow, Messages, PinnedMessage, Pins, PresenceService, PresenceStatus, Receipts, RecordingPolicy, SavedMessage, ScheduledDraft, ScheduledDrafts, StatusNote, TranslateRequest, Translation, TranslationTrigger, Translator, WindowDirection, WorkLocationKind, }; use chatsvc::sound_settings::{SoundSettings, SoundSettingsClient}; use chrono::{DateTime, Utc}; use graph::{ Channel, Chat, DriveEntry, DriveFolder, Graph, Member, Message, MessageExtras, MessageTarget, OutgoingMention, Photo, Presence, SharedFile, Team, UploadDestination, UploadedFile, User, }; use crate::error::{Error, Result}; use crate::profile::PersonProfile; pub const CHATSVC_CHAT_WRITES: bool = false; #[derive(Debug, Clone)] pub struct RemotePage { pub items: Vec, pub next_link: Option, } #[derive(Debug, Clone)] pub struct DeltaPage { pub items: Vec, pub next_link: Option, pub delta_link: Option, } #[derive(Debug, Clone)] pub struct ChatsPage { pub items: Vec, pub next_link: Option, } #[allow(async_fn_in_trait)] pub trait Remote { async fn me(&self) -> Result; async fn chats_page(&self, top: usize) -> Result; async fn chats_at(&self, next_link: &str) -> Result; async fn chat(&self, chat_id: &str) -> Result; async fn chat_members(&self, chat_id: &str) -> Result>; async fn joined_teams(&self) -> Result>; async fn channels_for_teams(&self, team_ids: &[String]) -> Result>>>; async fn chat_messages( &self, chat_id: &str, before: Option>, top: usize, ) -> Result; async fn chat_message(&self, chat_id: &str, message_id: &str) -> Result; async fn channel_messages( &self, team_id: &str, channel_id: &str, top: usize, ) -> Result; async fn channel_messages_at(&self, next_link: &str) -> Result; async fn channel_message( &self, team_id: &str, channel_id: &str, message_id: &str, ) -> Result; async fn channel_reply( &self, team_id: &str, channel_id: &str, message_id: &str, reply_id: &str, ) -> Result; async fn channel_delta( &self, team_id: &str, channel_id: &str, modified_after: Option>, ) -> Result; async fn channel_delta_at(&self, link: &str) -> Result; async fn channel_replies( &self, team_id: &str, channel_id: &str, root_ids: &[String], ) -> Result>>>; async fn send_chat_message( &self, chat_id: &str, html: &str, mentions: &[OutgoingMention], extras: &MessageExtras, ) -> Result; async fn send_channel_message( &self, team_id: &str, channel_id: &str, html: &str, subject: Option<&str>, mentions: &[OutgoingMention], extras: &MessageExtras, ) -> Result; async fn reply_to_channel_message( &self, team_id: &str, channel_id: &str, message_id: &str, html: &str, mentions: &[OutgoingMention], extras: &MessageExtras, ) -> Result; async fn mark_chat_read(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()>; async fn mark_chat_unread( &self, chat_id: &str, user_id: &str, tenant_id: &str, last_read_at: DateTime, ) -> Result<()>; async fn hide_chat(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()>; async fn unhide_chat(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()>; async fn remove_chat_member(&self, chat_id: &str, user_id: &str) -> Result<()>; async fn chat_states(&self) -> Result>; async fn set_chat_muted(&self, chat_id: &str, muted: bool) -> Result<()>; async fn set_read_horizon(&self, conversation_id: &str, horizon: &str) -> Result<()>; async fn set_channel_notifications( &self, channel_id: &str, notifications: store::ChannelNotifications, ) -> Result<()>; async fn send_typing(&self, conversation: &ConversationRef, active: bool) -> Result<()>; async fn set_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()>; async fn unset_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()>; async fn edit_message( &self, target: &MessageTarget, html: &str, mentions: &[OutgoingMention], extras: &MessageExtras, ) -> Result<()>; async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()>; async fn create_one_on_one(&self, my_user_id: &str, user_id: &str) -> Result; async fn create_group( &self, my_user_id: &str, user_ids: &[String], topic: Option<&str>, ) -> Result; async fn search_people(&self, query: &str) -> Result>; async fn reply_with_quote( &self, _chat_id: &str, _quoted_message_id: &str, _html: &str, _mentions: &[OutgoingMention], _extras: &MessageExtras, ) -> Result { Err(Error::Unsupported("a quoted reply")) } async fn channel_files_folder(&self, _team_id: &str, _channel_id: &str) -> Result { Err(Error::Unsupported("a channel files folder")) } async fn channel_files_root(&self, _team_id: &str, _channel_id: &str) -> Result { Err(Error::Unsupported("a channel library")) } async fn channel_tab_web_url( &self, _team_id: &str, _channel_id: &str, _tab_id: &str, ) -> Result> { Err(Error::Unsupported("a channel tab link")) } async fn list_children(&self, _folder: &DriveFolder) -> Result> { Err(Error::Unsupported("a folder listing")) } async fn create_folder(&self, _parent: &DriveFolder, _name: &str) -> Result { Err(Error::Unsupported("creating a folder")) } async fn upload_file( &self, _destination: &UploadDestination, _file_name: &str, _bytes: &[u8], _progress: &(dyn Fn(u8) + Send + Sync), ) -> Result { Err(Error::Unsupported("a file upload")) } async fn share_file(&self, _file: &UploadedFile, _user_ids: &[String]) -> Result<()> { Err(Error::Unsupported("sharing a file")) } async fn delete_file(&self, _file: &UploadedFile) -> Result<()> { Err(Error::Unsupported("deleting a file")) } async fn resolve_share(&self, _open_url: &str) -> Result { Err(Error::Unsupported("resolving a shared file")) } async fn resolve_drive_item(&self, _drive_id: &str, _item_id: &str) -> Result { Err(Error::Unsupported("resolving a drive item")) } async fn download_range( &self, _download_url: &str, _start: u64, _end: Option, ) -> Result> { Err(Error::Unsupported("a file download")) } async fn hosted_content(&self, _url: &str) -> Result { Err(Error::Unsupported("hosted content")) } async fn external_image(&self, _url: &str) -> Result { Err(Error::Unsupported("external images")) } async fn user_photos(&self, _user_ids: &[String]) -> Result>>> { Err(Error::Unsupported("profile photos")) } async fn presences(&self, _user_ids: &[String]) -> Result> { Err(Error::Unsupported("presence")) } async fn person_profile(&self, _user_id: &str) -> Result { Err(Error::Unsupported("profile cards")) } async fn bot_profiles(&self, _bot_ids: &[String]) -> Result> { Err(Error::Unsupported("bot profiles")) } async fn subscribe_presence( &self, _endpoint_id: &str, _trouter_uri: &str, _user_ids: &[String], _purge: bool, ) -> Result<()> { Err(Error::Unsupported("presence subscriptions")) } async fn own_status(&self, _user_id: &str) -> Result { Err(Error::Unsupported("own status")) } async fn set_availability(&self, _forced: Option<&ForcedAvailability>) -> Result<()> { Err(Error::Unsupported("setting availability")) } async fn set_status_note(&self, _note: Option<&StatusNote>) -> Result<()> { Err(Error::Unsupported("setting the status message")) } async fn set_work_location( &self, _location: Option<(WorkLocationKind, DateTime)>, ) -> Result<()> { Err(Error::Unsupported("setting the work location")) } async fn consumption_horizons(&self, _conversation_id: &str) -> Result> { Err(Error::Unsupported("read receipts")) } async fn forward_messages( &self, _source_conversation_id: &str, _target_conversation_id: &str, _message_ids: &[String], _comment_html: &str, ) -> Result { Err(Error::Unsupported("forwarding messages")) } async fn set_saved( &self, _conversation_id: &str, _root_id: &str, _message_id: &str, _saved: bool, ) -> Result<()> { Err(Error::Unsupported("saving messages")) } async fn list_saved(&self) -> Result> { Err(Error::Unsupported("saved messages")) } async fn list_call_logs(&self) -> Result> { Err(Error::Unsupported("call history")) } async fn chat_message_window( &self, _chat_id: &str, _anchor_id: &str, _direction: WindowDirection, _page_size: usize, ) -> Result { Err(Error::Unsupported("message windows")) } async fn chat_messages_by_id( &self, _chat_id: &str, _message_ids: &[String], ) -> Result>> { Err(Error::Unsupported("batched message reads")) } async fn chat_pins(&self, _chat_id: &str) -> Result> { Err(Error::Unsupported("pinned messages")) } async fn pin_message(&self, _chat_id: &str, _message_id: &str) -> Result<()> { Err(Error::Unsupported("pinning messages")) } async fn unpin_message( &self, _chat_id: &str, _message_id: &str, _parent_id: Option<&str>, ) -> Result<()> { Err(Error::Unsupported("pinning messages")) } async fn chat_apps(&self, _chat_id: &str) -> Result> { Err(Error::Unsupported("chat apps")) } async fn invoke_card(&self, _invoke: InvokeRequest) -> Result { Err(Error::Unsupported("card actions")) } async fn message_links( &self, _conversation: &ConversationRef, _page_size: usize, ) -> Result> { Err(Error::Unsupported("link previews")) } async fn message_emotions( &self, _conversation: &ConversationRef, _newest_message_id: &str, _page_size: usize, ) -> Result> { Err(Error::Unsupported("chat service reactions")) } async fn set_message_links(&self, _target: &MessageTarget, _links_json: &str) -> Result<()> { Err(Error::Unsupported("link previews")) } async fn link_info(&self, _url: &str) -> Result { Err(Error::Unsupported("link previews")) } async fn message_languages( &self, _conversation: &ConversationRef, _page_size: usize, ) -> Result> { Err(Error::Unsupported("message languages")) } async fn translate_chat_messages( &self, _chat_id: &str, _to_language: &str, _messages: &[TranslateRequest], _trigger: &TranslationTrigger, ) -> Result> { Err(Error::Unsupported("translation")) } async fn translate_channel_messages( &self, _team_id: &str, _channel_id: &str, _root_id: &str, _to_language: &str, _messages: &[TranslateRequest], _trigger: &TranslationTrigger, ) -> Result> { Err(Error::Unsupported("translation")) } async fn translation_languages(&self, _locale: &str) -> Result> { Err(Error::Unsupported("translation")) } async fn language_settings(&self) -> Result { Err(Error::Unsupported("translation settings")) } async fn patch_language_settings(&self, _patch: serde_json::Value) -> Result<()> { Err(Error::Unsupported("translation settings")) } async fn recording_policy(&self) -> Result { Err(Error::Unsupported("recording policy")) } async fn sound_settings(&self) -> Result { Err(Error::Unsupported("sound settings")) } async fn save_sound_settings( &self, _user_mri: &str, _previous: &SoundSettings, _updated: &SoundSettings, ) -> Result<()> { Err(Error::Unsupported("sound settings")) } async fn search_gifs(&self, _query: &str) -> Result> { Err(Error::Unsupported("GIF search")) } async fn create_scheduled( &self, _inner_thread_id: &str, _html: &str, _send_at: DateTime, _display_name: &str, ) -> Result { Err(Error::Unsupported("schedule send")) } async fn scheduled_drafts(&self) -> Result> { Err(Error::Unsupported("schedule send")) } async fn update_scheduled( &self, _draft: &ScheduledDraft, _html: &str, _display_name: &str, ) -> Result { Err(Error::Unsupported("schedule send")) } async fn cancel_scheduled(&self, _draft_id: &str) -> Result<()> { Err(Error::Unsupported("schedule send")) } } impl Remote for Graph { async fn me(&self) -> Result { Ok(Graph::me(self).await?) } async fn chats_page(&self, top: usize) -> Result { Ok(Graph::chats_page(self, top).await?.into()) } async fn chats_at(&self, next_link: &str) -> Result { Ok(Graph::chats_at(self, next_link).await?.into()) } async fn chat(&self, chat_id: &str) -> Result { Ok(Graph::chat(self, chat_id).await?) } async fn chat_members(&self, chat_id: &str) -> Result> { Ok(Graph::chat_members(self, chat_id).await?) } async fn joined_teams(&self) -> Result> { Ok(Graph::joined_teams(self).await?) } async fn channels_for_teams(&self, team_ids: &[String]) -> Result>>> { let listings = Graph::channels_for_teams(self, team_ids).await?; Ok(listings .into_iter() .map(|listing| listing.map_err(Into::into)) .collect()) } async fn chat_messages( &self, chat_id: &str, before: Option>, top: usize, ) -> Result { Ok(Graph::chat_messages(self, chat_id, before, top) .await? .into()) } async fn chat_message(&self, chat_id: &str, message_id: &str) -> Result { Ok(Graph::chat_message(self, chat_id, message_id).await?) } async fn channel_messages( &self, team_id: &str, channel_id: &str, top: usize, ) -> Result { Ok(Graph::channel_messages(self, team_id, channel_id, top) .await? .into()) } async fn channel_messages_at(&self, next_link: &str) -> Result { Ok(Graph::channel_messages_at(self, next_link).await?.into()) } async fn channel_message( &self, team_id: &str, channel_id: &str, message_id: &str, ) -> Result { Ok(Graph::channel_message(self, team_id, channel_id, message_id).await?) } async fn channel_reply( &self, team_id: &str, channel_id: &str, message_id: &str, reply_id: &str, ) -> Result { Ok(Graph::channel_reply(self, team_id, channel_id, message_id, reply_id).await?) } async fn channel_delta( &self, team_id: &str, channel_id: &str, modified_after: Option>, ) -> Result { Ok( Graph::channel_delta(self, team_id, channel_id, modified_after) .await? .into(), ) } async fn channel_delta_at(&self, link: &str) -> Result { Ok(Graph::channel_delta_at(self, link).await?.into()) } async fn channel_replies( &self, team_id: &str, channel_id: &str, root_ids: &[String], ) -> Result>>> { let listings = Graph::channel_replies(self, team_id, channel_id, root_ids).await?; Ok(listings .into_iter() .map(|listing| listing.map_err(Into::into)) .collect()) } async fn send_chat_message( &self, chat_id: &str, html: &str, mentions: &[OutgoingMention], extras: &MessageExtras, ) -> Result { Ok(Graph::send_chat_message(self, chat_id, html, mentions, extras).await?) } async fn send_channel_message( &self, team_id: &str, channel_id: &str, html: &str, subject: Option<&str>, mentions: &[OutgoingMention], extras: &MessageExtras, ) -> Result { Ok( Graph::send_channel_message(self, team_id, channel_id, html, subject, mentions, extras) .await?, ) } async fn reply_to_channel_message( &self, team_id: &str, channel_id: &str, message_id: &str, html: &str, mentions: &[OutgoingMention], extras: &MessageExtras, ) -> Result { Ok(Graph::reply_to_channel_message( self, team_id, channel_id, message_id, html, mentions, extras, ) .await?) } async fn mark_chat_read(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()> { Ok(Graph::mark_chat_read(self, chat_id, user_id, tenant_id).await?) } async fn mark_chat_unread( &self, chat_id: &str, user_id: &str, tenant_id: &str, last_read_at: DateTime, ) -> Result<()> { Ok(Graph::mark_chat_unread(self, chat_id, user_id, tenant_id, last_read_at).await?) } async fn hide_chat(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()> { Ok(Graph::hide_chat(self, chat_id, user_id, tenant_id).await?) } async fn unhide_chat(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()> { Ok(Graph::unhide_chat(self, chat_id, user_id, tenant_id).await?) } async fn remove_chat_member(&self, chat_id: &str, user_id: &str) -> Result<()> { Ok(Graph::remove_chat_member(self, chat_id, user_id).await?) } async fn chat_states(&self) -> Result> { Ok(Conversations::new(self.session()) .conversation_states() .await?) } async fn set_chat_muted(&self, chat_id: &str, muted: bool) -> Result<()> { Ok(Conversations::new(self.session()) .set_alerts(chat_id, !muted) .await?) } async fn set_read_horizon(&self, conversation_id: &str, horizon: &str) -> Result<()> { Ok(Conversations::new(self.session()) .set_read_horizon(conversation_id, horizon) .await?) } async fn set_channel_notifications( &self, channel_id: &str, notifications: store::ChannelNotifications, ) -> Result<()> { Ok(Conversations::new(self.session()) .set_channel_notifications( channel_id, crate::folders::chatsvc_notifications(notifications), ) .await?) } async fn send_typing(&self, conversation: &ConversationRef, active: bool) -> Result<()> { Ok(Messages::new(self.session()) .send_typing(conversation, active) .await?) } async fn set_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> { if let Some(key) = chatsvc::reaction_key(reaction_type) { return Ok(Messages::new(self.session()) .set_emotion(&conversation_ref(target), target.message_id(), &key) .await?); } Ok(Graph::set_reaction(self, target, reaction_type).await?) } async fn unset_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> { let keys = chatsvc::reaction_keys(reaction_type); if !keys.is_empty() { let keys: Vec<&str> = keys.iter().map(String::as_str).collect(); return Ok(Messages::new(self.session()) .unset_emotions(&conversation_ref(target), target.message_id(), &keys) .await?); } Ok(Graph::unset_reaction(self, target, reaction_type).await?) } async fn edit_message( &self, target: &MessageTarget, html: &str, mentions: &[OutgoingMention], extras: &MessageExtras, ) -> Result<()> { match chatsvc_conversation(target) { Some(conversation) if mentions.is_empty() => { let html = format!("{}{html}{}", extras.kept_html(), extras.files_html()); Ok(Messages::new(self.session()) .edit_message(&conversation, target.message_id(), &html) .await?) } Some(_) => Err(Error::Unsupported( "editing a channel message with mentions", )), None => Ok(Graph::edit_message(self, target, html, mentions, extras).await?), } } async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()> { match chatsvc_conversation(target) { Some(conversation) => Ok(Messages::new(self.session()) .soft_delete_message(&conversation, target.message_id()) .await?), None => Ok(Graph::soft_delete_message(self, user_id, target).await?), } } async fn create_one_on_one(&self, my_user_id: &str, user_id: &str) -> Result { Ok(Graph::create_one_on_one(self, my_user_id, user_id).await?) } async fn create_group( &self, my_user_id: &str, user_ids: &[String], topic: Option<&str>, ) -> Result { Ok(Graph::create_group(self, my_user_id, user_ids, topic).await?) } async fn search_people(&self, query: &str) -> Result> { Ok(Graph::search_people(self, query).await?) } async fn reply_with_quote( &self, chat_id: &str, quoted_message_id: &str, html: &str, mentions: &[OutgoingMention], extras: &MessageExtras, ) -> Result { Ok( Graph::reply_with_quote(self, chat_id, quoted_message_id, html, mentions, extras) .await?, ) } async fn channel_files_folder(&self, team_id: &str, channel_id: &str) -> Result { Ok(Graph::channel_files_folder(self, team_id, channel_id).await?) } async fn channel_files_root(&self, team_id: &str, channel_id: &str) -> Result { Ok(Graph::channel_files_root(self, team_id, channel_id).await?) } async fn channel_tab_web_url( &self, team_id: &str, channel_id: &str, tab_id: &str, ) -> Result> { Ok(Graph::channel_tab_web_url(self, team_id, channel_id, tab_id).await?) } async fn list_children(&self, folder: &DriveFolder) -> Result> { Ok(Graph::list_children(self, folder).await?) } async fn create_folder(&self, parent: &DriveFolder, name: &str) -> Result { Ok(Graph::create_folder(self, parent, name).await?) } async fn upload_file( &self, destination: &UploadDestination, file_name: &str, bytes: &[u8], progress: &(dyn Fn(u8) + Send + Sync), ) -> Result { Ok(Graph::upload_file(self, destination, file_name, bytes, progress).await?) } async fn share_file(&self, file: &UploadedFile, user_ids: &[String]) -> Result<()> { Ok(Graph::share_item(self, file, user_ids).await?) } async fn delete_file(&self, file: &UploadedFile) -> Result<()> { Ok(Graph::delete_drive_item(self, &file.drive_id, &file.item_id).await?) } async fn resolve_share(&self, open_url: &str) -> Result { Ok(Graph::resolve_share(self, open_url).await?) } async fn resolve_drive_item(&self, drive_id: &str, item_id: &str) -> Result { Ok(Graph::resolve_drive_item(self, drive_id, item_id).await?) } async fn download_range( &self, download_url: &str, start: u64, end: Option, ) -> Result> { Ok(Graph::download_range(self, download_url, start, end).await?) } async fn hosted_content(&self, url: &str) -> Result { if chatsvc::is_link_image_url(url) { let image = Messages::new(self.session()).link_image(url).await?; return Ok(Photo { bytes: image.bytes, content_type: image.content_type, }); } Ok(Graph::download_hosted_content(self, url).await?) } async fn external_image(&self, url: &str) -> Result { Ok(Graph::download_external_image(self, url).await?) } async fn user_photos(&self, user_ids: &[String]) -> Result>>> { let photos = Graph::user_photos(self, user_ids).await?; Ok(photos .into_iter() .map(|photo| photo.map_err(Into::into)) .collect()) } async fn presences(&self, user_ids: &[String]) -> Result> { Ok(Graph::presences(self, user_ids).await?) } async fn person_profile(&self, user_id: &str) -> Result { crate::profile::load_profile(self, user_id).await } async fn bot_profiles(&self, bot_ids: &[String]) -> Result> { Ok(BotProfiles::new(self.session()).fetch(bot_ids).await?) } async fn subscribe_presence( &self, endpoint_id: &str, trouter_uri: &str, user_ids: &[String], purge: bool, ) -> Result<()> { Ok(Graph::subscribe_presence(self, endpoint_id, trouter_uri, user_ids, purge).await?) } async fn own_status(&self, user_id: &str) -> Result { Ok(PresenceService::new(self.session()).status(user_id).await?) } async fn set_availability(&self, forced: Option<&ForcedAvailability>) -> Result<()> { Ok(PresenceService::new(self.session()) .set_availability(forced) .await?) } async fn set_status_note(&self, note: Option<&StatusNote>) -> Result<()> { Ok(PresenceService::new(self.session()).set_note(note).await?) } async fn set_work_location( &self, location: Option<(WorkLocationKind, DateTime)>, ) -> Result<()> { Ok(PresenceService::new(self.session()) .set_work_location(location) .await?) } async fn consumption_horizons(&self, conversation_id: &str) -> Result> { Ok(Receipts::new(self.session()) .consumption_horizons(conversation_id) .await?) } async fn forward_messages( &self, source_conversation_id: &str, target_conversation_id: &str, message_ids: &[String], comment_html: &str, ) -> Result { Ok(Messages::new(self.session()) .forward_messages( source_conversation_id, target_conversation_id, message_ids, comment_html, ) .await?) } async fn set_saved( &self, conversation_id: &str, root_id: &str, message_id: &str, saved: bool, ) -> Result<()> { Ok(Messages::new(self.session()) .set_saved(conversation_id, root_id, message_id, saved) .await?) } async fn list_saved(&self) -> Result> { Ok(Messages::new(self.session()).list_saved().await?) } async fn list_call_logs(&self) -> Result> { Ok(Messages::new(self.session()).list_call_logs().await?) } async fn chat_message_window( &self, chat_id: &str, anchor_id: &str, direction: WindowDirection, page_size: usize, ) -> Result { Ok(Messages::new(self.session()) .message_window(chat_id, anchor_id, direction, page_size) .await?) } async fn chat_messages_by_id( &self, chat_id: &str, message_ids: &[String], ) -> Result>> { let messages = Graph::chat_messages_by_id(self, chat_id, message_ids).await?; Ok(messages .into_iter() .map(|message| message.map_err(Into::into)) .collect()) } async fn chat_pins(&self, chat_id: &str) -> Result> { Ok(Pins::new(self.session()).chat_pins(chat_id).await?) } async fn pin_message(&self, chat_id: &str, message_id: &str) -> Result<()> { Ok(Pins::new(self.session()) .pin_message(chat_id, message_id) .await?) } async fn unpin_message( &self, chat_id: &str, message_id: &str, parent_id: Option<&str>, ) -> Result<()> { Ok(Pins::new(self.session()) .unpin_message(chat_id, message_id, parent_id) .await?) } async fn chat_apps(&self, chat_id: &str) -> Result> { Ok(CardActions::new(self.session()).chat_apps(chat_id).await?) } async fn invoke_card(&self, invoke: InvokeRequest) -> Result { Ok(CardActions::new(self.session()).invoke(invoke).await?) } async fn message_links( &self, conversation: &ConversationRef, page_size: usize, ) -> Result> { Ok(Messages::new(self.session()) .list_message_links(conversation, page_size) .await?) } async fn message_emotions( &self, conversation: &ConversationRef, newest_message_id: &str, page_size: usize, ) -> Result> { Ok(Messages::new(self.session()) .message_emotions(&conversation.conversation_id(), newest_message_id, page_size) .await?) } async fn set_message_links(&self, target: &MessageTarget, links_json: &str) -> Result<()> { Ok(Messages::new(self.session()) .set_links(&conversation_ref(target), target.message_id(), links_json) .await?) } async fn link_info(&self, url: &str) -> Result { Ok(Messages::new(self.session()).link_info(url).await?) } async fn message_languages( &self, conversation: &ConversationRef, page_size: usize, ) -> Result> { Ok(Messages::new(self.session()) .list_language_stamps(conversation, page_size) .await?) } async fn translate_chat_messages( &self, chat_id: &str, to_language: &str, messages: &[TranslateRequest], trigger: &TranslationTrigger, ) -> Result> { Ok(Translator::new(self.session()) .translate_chat(chat_id, to_language, messages, trigger) .await?) } async fn translate_channel_messages( &self, team_id: &str, channel_id: &str, root_id: &str, to_language: &str, messages: &[TranslateRequest], trigger: &TranslationTrigger, ) -> Result> { Ok(Translator::new(self.session()) .translate_channel(team_id, channel_id, root_id, to_language, messages, trigger) .await?) } async fn translation_languages(&self, locale: &str) -> Result> { Ok(Translator::new(self.session()).languages(locale).await?) } async fn language_settings(&self) -> Result { Ok(LanguageSettingsClient::new(self.session()).read().await?) } async fn recording_policy(&self) -> Result { Ok(LanguageSettingsClient::new(self.session()) .read_recording_policy() .await?) } async fn patch_language_settings(&self, patch: serde_json::Value) -> Result<()> { Ok(LanguageSettingsClient::new(self.session()) .patch(patch) .await?) } async fn sound_settings(&self) -> Result { Ok(SoundSettingsClient::new(self.session()).read().await?) } async fn save_sound_settings( &self, user_mri: &str, previous: &SoundSettings, updated: &SoundSettings, ) -> Result<()> { Ok(SoundSettingsClient::new(self.session()) .write(user_mri, previous, updated) .await?) } async fn search_gifs(&self, query: &str) -> Result> { Ok(Gifs::new(self.session()).search(query).await?) } async fn create_scheduled( &self, inner_thread_id: &str, html: &str, send_at: DateTime, display_name: &str, ) -> Result { Ok(ScheduledDrafts::new(self.session()) .create(inner_thread_id, html, send_at, display_name) .await?) } async fn scheduled_drafts(&self) -> Result> { Ok(ScheduledDrafts::new(self.session()).list().await?) } async fn update_scheduled( &self, draft: &ScheduledDraft, html: &str, display_name: &str, ) -> Result { Ok(ScheduledDrafts::new(self.session()) .update(draft, html, display_name) .await?) } async fn cancel_scheduled(&self, draft_id: &str) -> Result<()> { Ok(ScheduledDrafts::new(self.session()) .cancel(draft_id) .await?) } } fn chatsvc_conversation(target: &MessageTarget) -> Option { match target { MessageTarget::Chat { .. } => CHATSVC_CHAT_WRITES.then(|| conversation_ref(target)), MessageTarget::Channel { .. } => Some(conversation_ref(target)), } } fn conversation_ref(target: &MessageTarget) -> ConversationRef { match target { MessageTarget::Chat { chat_id, .. } => ConversationRef::chat(chat_id), MessageTarget::Channel { channel_id, root_id: None, .. } => ConversationRef::channel_root(channel_id), MessageTarget::Channel { channel_id, root_id: Some(root_id), .. } => ConversationRef::channel_reply(channel_id, root_id), } } impl From> for RemotePage { fn from(page: graph::Page) -> Self { RemotePage { items: page.items, next_link: page.next_link, } } } impl From> for DeltaPage { fn from(page: graph::Page) -> Self { DeltaPage { items: page.items, next_link: page.next_link, delta_link: page.delta_link, } } } impl From> for ChatsPage { fn from(page: graph::Page) -> Self { ChatsPage { items: page.items, next_link: page.next_link, } } } #[cfg(test)] mod tests { use super::*; #[test] fn channel_targets_route_to_chatsvc() { let root = MessageTarget::channel("t", "19:c@thread.tacv2", "m1", None); let reply = MessageTarget::channel("t", "19:c@thread.tacv2", "r1", Some("m1")); assert_eq!( chatsvc_conversation(&root), Some(ConversationRef::channel_root("19:c@thread.tacv2")) ); assert_eq!( chatsvc_conversation(&reply), Some(ConversationRef::channel_reply("19:c@thread.tacv2", "m1")) ); } #[test] fn reactions_reach_chats_regardless_of_the_feature_switch() { let chat = MessageTarget::chat("19:a@thread.v2", "m1"); assert_eq!( conversation_ref(&chat), ConversationRef::chat("19:a@thread.v2") ); } #[test] fn chats_follow_the_feature_switch() { let chat = MessageTarget::chat("19:a@thread.v2", "m1"); assert_eq!(chatsvc_conversation(&chat).is_some(), CHATSVC_CHAT_WRITES); } }