use chatsvc::{ CardActions, ChatApp, ConversationRef, Conversations, InvokeRequest, InvokeResponse, MemberHorizon, Messages, Receipts, }; use chrono::{DateTime, Utc}; use graph::{ Channel, Chat, DriveFolder, Graph, Member, Message, MessageExtras, MessageTarget, OutgoingMention, Photo, Presence, SharedFile, Team, UploadDestination, UploadedFile, User, }; use crate::error::{Error, Result}; 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 leave_chat(&self, chat_id: &str, user_id: &str) -> Result<()>; async fn muted_chat_states(&self) -> Result>; async fn set_chat_muted(&self, chat_id: &str, muted: 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 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 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 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 subscribe_presence( &self, _endpoint_id: &str, _trouter_uri: &str, _user_ids: &[String], _purge: bool, ) -> Result<()> { Err(Error::Unsupported("presence subscriptions")) } async fn consumption_horizons(&self, _conversation_id: &str) -> Result> { Err(Error::Unsupported("read receipts")) } 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")) } } 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 leave_chat(&self, chat_id: &str, user_id: &str) -> Result<()> { Ok(Graph::leave_chat(self, chat_id, user_id).await?) } async fn muted_chat_states(&self) -> Result> { Ok(Conversations::new(self.session()) .conversation_mute_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_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> { if let Some(key) = chatsvc::emotion_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::emotion_keys(reaction_type); if !keys.is_empty() { 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 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 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 { Ok(Graph::download_hosted_content(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 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 consumption_horizons(&self, conversation_id: &str) -> Result> { Ok(Receipts::new(self.session()) .consumption_horizons(conversation_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?) } } 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); } }