| 1 | use chatsvc::{ |
| 2 | CardActions, ChatApp, ConversationRef, Conversations, Gif, Gifs, InvokeRequest, InvokeResponse, |
| 3 | LinkInfo, MemberHorizon, MessageLinks, Messages, Receipts, ScheduledDraft, |
| 4 | ScheduledDrafts, |
| 5 | }; |
| 6 | use chrono::{DateTime, Utc}; |
| 7 | use graph::{ |
| 8 | Channel, Chat, DriveFolder, Graph, Member, Message, MessageExtras, MessageTarget, |
| 9 | OutgoingMention, Photo, Presence, SharedFile, Team, UploadDestination, UploadedFile, User, |
| 10 | }; |
| 11 | |
| 12 | use crate::error::{Error, Result}; |
| 13 | |
| 14 | pub const CHATSVC_CHAT_WRITES: bool = false; |
| 15 | |
| 16 | #[derive(Debug, Clone)] |
| 17 | pub struct RemotePage { |
| 18 | pub items: Vec<Message>, |
| 19 | pub next_link: Option<String>, |
| 20 | } |
| 21 | |
| 22 | #[derive(Debug, Clone)] |
| 23 | pub struct DeltaPage { |
| 24 | pub items: Vec<Message>, |
| 25 | pub next_link: Option<String>, |
| 26 | pub delta_link: Option<String>, |
| 27 | } |
| 28 | |
| 29 | #[derive(Debug, Clone)] |
| 30 | pub struct ChatsPage { |
| 31 | pub items: Vec<Chat>, |
| 32 | pub next_link: Option<String>, |
| 33 | } |
| 34 | |
| 35 | #[allow(async_fn_in_trait)] |
| 36 | pub trait Remote { |
| 37 | async fn me(&self) -> Result<User>; |
| 38 | async fn chats_page(&self, top: usize) -> Result<ChatsPage>; |
| 39 | async fn chats_at(&self, next_link: &str) -> Result<ChatsPage>; |
| 40 | async fn chat(&self, chat_id: &str) -> Result<Chat>; |
| 41 | async fn chat_members(&self, chat_id: &str) -> Result<Vec<Member>>; |
| 42 | async fn joined_teams(&self) -> Result<Vec<Team>>; |
| 43 | async fn channels_for_teams(&self, team_ids: &[String]) -> Result<Vec<Result<Vec<Channel>>>>; |
| 44 | async fn chat_messages( |
| 45 | &self, |
| 46 | chat_id: &str, |
| 47 | before: Option<DateTime<Utc>>, |
| 48 | top: usize, |
| 49 | ) -> Result<RemotePage>; |
| 50 | async fn chat_message(&self, chat_id: &str, message_id: &str) -> Result<Message>; |
| 51 | async fn channel_messages( |
| 52 | &self, |
| 53 | team_id: &str, |
| 54 | channel_id: &str, |
| 55 | top: usize, |
| 56 | ) -> Result<RemotePage>; |
| 57 | async fn channel_messages_at(&self, next_link: &str) -> Result<RemotePage>; |
| 58 | async fn channel_message( |
| 59 | &self, |
| 60 | team_id: &str, |
| 61 | channel_id: &str, |
| 62 | message_id: &str, |
| 63 | ) -> Result<Message>; |
| 64 | async fn channel_reply( |
| 65 | &self, |
| 66 | team_id: &str, |
| 67 | channel_id: &str, |
| 68 | message_id: &str, |
| 69 | reply_id: &str, |
| 70 | ) -> Result<Message>; |
| 71 | async fn channel_delta( |
| 72 | &self, |
| 73 | team_id: &str, |
| 74 | channel_id: &str, |
| 75 | modified_after: Option<DateTime<Utc>>, |
| 76 | ) -> Result<DeltaPage>; |
| 77 | async fn channel_delta_at(&self, link: &str) -> Result<DeltaPage>; |
| 78 | async fn channel_replies( |
| 79 | &self, |
| 80 | team_id: &str, |
| 81 | channel_id: &str, |
| 82 | root_ids: &[String], |
| 83 | ) -> Result<Vec<Result<Vec<Message>>>>; |
| 84 | async fn send_chat_message( |
| 85 | &self, |
| 86 | chat_id: &str, |
| 87 | html: &str, |
| 88 | mentions: &[OutgoingMention], |
| 89 | extras: &MessageExtras, |
| 90 | ) -> Result<Message>; |
| 91 | async fn send_channel_message( |
| 92 | &self, |
| 93 | team_id: &str, |
| 94 | channel_id: &str, |
| 95 | html: &str, |
| 96 | subject: Option<&str>, |
| 97 | mentions: &[OutgoingMention], |
| 98 | extras: &MessageExtras, |
| 99 | ) -> Result<Message>; |
| 100 | async fn reply_to_channel_message( |
| 101 | &self, |
| 102 | team_id: &str, |
| 103 | channel_id: &str, |
| 104 | message_id: &str, |
| 105 | html: &str, |
| 106 | mentions: &[OutgoingMention], |
| 107 | extras: &MessageExtras, |
| 108 | ) -> Result<Message>; |
| 109 | async fn mark_chat_read(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()>; |
| 110 | async fn mark_chat_unread( |
| 111 | &self, |
| 112 | chat_id: &str, |
| 113 | user_id: &str, |
| 114 | tenant_id: &str, |
| 115 | last_read_at: DateTime<Utc>, |
| 116 | ) -> Result<()>; |
| 117 | async fn hide_chat(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()>; |
| 118 | async fn unhide_chat(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()>; |
| 119 | async fn leave_chat(&self, chat_id: &str, user_id: &str) -> Result<()>; |
| 120 | async fn muted_chat_states(&self) -> Result<Vec<(String, bool)>>; |
| 121 | async fn set_chat_muted(&self, chat_id: &str, muted: bool) -> Result<()>; |
| 122 | async fn send_typing(&self, conversation: &ConversationRef, active: bool) -> Result<()>; |
| 123 | async fn set_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()>; |
| 124 | async fn unset_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()>; |
| 125 | async fn edit_message( |
| 126 | &self, |
| 127 | target: &MessageTarget, |
| 128 | html: &str, |
| 129 | mentions: &[OutgoingMention], |
| 130 | extras: &MessageExtras, |
| 131 | ) -> Result<()>; |
| 132 | async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()>; |
| 133 | async fn create_one_on_one(&self, my_user_id: &str, user_id: &str) -> Result<Chat>; |
| 134 | async fn create_group( |
| 135 | &self, |
| 136 | my_user_id: &str, |
| 137 | user_ids: &[String], |
| 138 | topic: Option<&str>, |
| 139 | ) -> Result<Chat>; |
| 140 | async fn search_people(&self, query: &str) -> Result<Vec<User>>; |
| 141 | |
| 142 | async fn reply_with_quote( |
| 143 | &self, |
| 144 | _chat_id: &str, |
| 145 | _quoted_message_id: &str, |
| 146 | _html: &str, |
| 147 | _mentions: &[OutgoingMention], |
| 148 | _extras: &MessageExtras, |
| 149 | ) -> Result<Message> { |
| 150 | Err(Error::Unsupported("a quoted reply")) |
| 151 | } |
| 152 | |
| 153 | async fn channel_files_folder(&self, _team_id: &str, _channel_id: &str) -> Result<DriveFolder> { |
| 154 | Err(Error::Unsupported("a channel files folder")) |
| 155 | } |
| 156 | |
| 157 | async fn upload_file( |
| 158 | &self, |
| 159 | _destination: &UploadDestination, |
| 160 | _file_name: &str, |
| 161 | _bytes: &[u8], |
| 162 | _progress: &(dyn Fn(u8) + Send + Sync), |
| 163 | ) -> Result<UploadedFile> { |
| 164 | Err(Error::Unsupported("a file upload")) |
| 165 | } |
| 166 | |
| 167 | async fn share_file(&self, _file: &UploadedFile, _user_ids: &[String]) -> Result<()> { |
| 168 | Err(Error::Unsupported("sharing a file")) |
| 169 | } |
| 170 | |
| 171 | async fn delete_file(&self, _file: &UploadedFile) -> Result<()> { |
| 172 | Err(Error::Unsupported("deleting a file")) |
| 173 | } |
| 174 | |
| 175 | async fn resolve_share(&self, _open_url: &str) -> Result<SharedFile> { |
| 176 | Err(Error::Unsupported("resolving a shared file")) |
| 177 | } |
| 178 | |
| 179 | async fn download_range( |
| 180 | &self, |
| 181 | _download_url: &str, |
| 182 | _start: u64, |
| 183 | _end: Option<u64>, |
| 184 | ) -> Result<Vec<u8>> { |
| 185 | Err(Error::Unsupported("a file download")) |
| 186 | } |
| 187 | |
| 188 | async fn hosted_content(&self, _url: &str) -> Result<Photo> { |
| 189 | Err(Error::Unsupported("hosted content")) |
| 190 | } |
| 191 | |
| 192 | async fn external_image(&self, _url: &str) -> Result<Photo> { |
| 193 | Err(Error::Unsupported("external images")) |
| 194 | } |
| 195 | |
| 196 | async fn user_photos(&self, _user_ids: &[String]) -> Result<Vec<Result<Option<Photo>>>> { |
| 197 | Err(Error::Unsupported("profile photos")) |
| 198 | } |
| 199 | |
| 200 | async fn presences(&self, _user_ids: &[String]) -> Result<Vec<Presence>> { |
| 201 | Err(Error::Unsupported("presence")) |
| 202 | } |
| 203 | |
| 204 | async fn subscribe_presence( |
| 205 | &self, |
| 206 | _endpoint_id: &str, |
| 207 | _trouter_uri: &str, |
| 208 | _user_ids: &[String], |
| 209 | _purge: bool, |
| 210 | ) -> Result<()> { |
| 211 | Err(Error::Unsupported("presence subscriptions")) |
| 212 | } |
| 213 | |
| 214 | async fn consumption_horizons(&self, _conversation_id: &str) -> Result<Vec<MemberHorizon>> { |
| 215 | Err(Error::Unsupported("read receipts")) |
| 216 | } |
| 217 | |
| 218 | async fn chat_apps(&self, _chat_id: &str) -> Result<Vec<ChatApp>> { |
| 219 | Err(Error::Unsupported("chat apps")) |
| 220 | } |
| 221 | |
| 222 | async fn invoke_card(&self, _invoke: InvokeRequest) -> Result<InvokeResponse> { |
| 223 | Err(Error::Unsupported("card actions")) |
| 224 | } |
| 225 | |
| 226 | async fn message_links( |
| 227 | &self, |
| 228 | _conversation: &ConversationRef, |
| 229 | _page_size: usize, |
| 230 | ) -> Result<Vec<MessageLinks>> { |
| 231 | Err(Error::Unsupported("link previews")) |
| 232 | } |
| 233 | |
| 234 | async fn set_message_links(&self, _target: &MessageTarget, _links_json: &str) -> Result<()> { |
| 235 | Err(Error::Unsupported("link previews")) |
| 236 | } |
| 237 | |
| 238 | async fn link_info(&self, _url: &str) -> Result<LinkInfo> { |
| 239 | Err(Error::Unsupported("link previews")) |
| 240 | } |
| 241 | |
| 242 | async fn search_gifs(&self, _query: &str) -> Result<Vec<Gif>> { |
| 243 | Err(Error::Unsupported("GIF search")) |
| 244 | } |
| 245 | |
| 246 | async fn create_scheduled( |
| 247 | &self, |
| 248 | _inner_thread_id: &str, |
| 249 | _html: &str, |
| 250 | _send_at: DateTime<Utc>, |
| 251 | _display_name: &str, |
| 252 | ) -> Result<ScheduledDraft> { |
| 253 | Err(Error::Unsupported("schedule send")) |
| 254 | } |
| 255 | |
| 256 | async fn scheduled_drafts(&self) -> Result<Vec<ScheduledDraft>> { |
| 257 | Err(Error::Unsupported("schedule send")) |
| 258 | } |
| 259 | |
| 260 | async fn update_scheduled( |
| 261 | &self, |
| 262 | _draft: &ScheduledDraft, |
| 263 | _html: &str, |
| 264 | _display_name: &str, |
| 265 | ) -> Result<ScheduledDraft> { |
| 266 | Err(Error::Unsupported("schedule send")) |
| 267 | } |
| 268 | |
| 269 | async fn cancel_scheduled(&self, _draft_id: &str) -> Result<()> { |
| 270 | Err(Error::Unsupported("schedule send")) |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | impl Remote for Graph { |
| 275 | async fn me(&self) -> Result<User> { |
| 276 | Ok(Graph::me(self).await?) |
| 277 | } |
| 278 | |
| 279 | async fn chats_page(&self, top: usize) -> Result<ChatsPage> { |
| 280 | Ok(Graph::chats_page(self, top).await?.into()) |
| 281 | } |
| 282 | |
| 283 | async fn chats_at(&self, next_link: &str) -> Result<ChatsPage> { |
| 284 | Ok(Graph::chats_at(self, next_link).await?.into()) |
| 285 | } |
| 286 | |
| 287 | async fn chat(&self, chat_id: &str) -> Result<Chat> { |
| 288 | Ok(Graph::chat(self, chat_id).await?) |
| 289 | } |
| 290 | |
| 291 | async fn chat_members(&self, chat_id: &str) -> Result<Vec<Member>> { |
| 292 | Ok(Graph::chat_members(self, chat_id).await?) |
| 293 | } |
| 294 | |
| 295 | async fn joined_teams(&self) -> Result<Vec<Team>> { |
| 296 | Ok(Graph::joined_teams(self).await?) |
| 297 | } |
| 298 | |
| 299 | async fn channels_for_teams(&self, team_ids: &[String]) -> Result<Vec<Result<Vec<Channel>>>> { |
| 300 | let listings = Graph::channels_for_teams(self, team_ids).await?; |
| 301 | Ok(listings |
| 302 | .into_iter() |
| 303 | .map(|listing| listing.map_err(Into::into)) |
| 304 | .collect()) |
| 305 | } |
| 306 | |
| 307 | async fn chat_messages( |
| 308 | &self, |
| 309 | chat_id: &str, |
| 310 | before: Option<DateTime<Utc>>, |
| 311 | top: usize, |
| 312 | ) -> Result<RemotePage> { |
| 313 | Ok(Graph::chat_messages(self, chat_id, before, top) |
| 314 | .await? |
| 315 | .into()) |
| 316 | } |
| 317 | |
| 318 | async fn chat_message(&self, chat_id: &str, message_id: &str) -> Result<Message> { |
| 319 | Ok(Graph::chat_message(self, chat_id, message_id).await?) |
| 320 | } |
| 321 | |
| 322 | async fn channel_messages( |
| 323 | &self, |
| 324 | team_id: &str, |
| 325 | channel_id: &str, |
| 326 | top: usize, |
| 327 | ) -> Result<RemotePage> { |
| 328 | Ok(Graph::channel_messages(self, team_id, channel_id, top) |
| 329 | .await? |
| 330 | .into()) |
| 331 | } |
| 332 | |
| 333 | async fn channel_messages_at(&self, next_link: &str) -> Result<RemotePage> { |
| 334 | Ok(Graph::channel_messages_at(self, next_link).await?.into()) |
| 335 | } |
| 336 | |
| 337 | async fn channel_message( |
| 338 | &self, |
| 339 | team_id: &str, |
| 340 | channel_id: &str, |
| 341 | message_id: &str, |
| 342 | ) -> Result<Message> { |
| 343 | Ok(Graph::channel_message(self, team_id, channel_id, message_id).await?) |
| 344 | } |
| 345 | |
| 346 | async fn channel_reply( |
| 347 | &self, |
| 348 | team_id: &str, |
| 349 | channel_id: &str, |
| 350 | message_id: &str, |
| 351 | reply_id: &str, |
| 352 | ) -> Result<Message> { |
| 353 | Ok(Graph::channel_reply(self, team_id, channel_id, message_id, reply_id).await?) |
| 354 | } |
| 355 | |
| 356 | async fn channel_delta( |
| 357 | &self, |
| 358 | team_id: &str, |
| 359 | channel_id: &str, |
| 360 | modified_after: Option<DateTime<Utc>>, |
| 361 | ) -> Result<DeltaPage> { |
| 362 | Ok( |
| 363 | Graph::channel_delta(self, team_id, channel_id, modified_after) |
| 364 | .await? |
| 365 | .into(), |
| 366 | ) |
| 367 | } |
| 368 | |
| 369 | async fn channel_delta_at(&self, link: &str) -> Result<DeltaPage> { |
| 370 | Ok(Graph::channel_delta_at(self, link).await?.into()) |
| 371 | } |
| 372 | |
| 373 | async fn channel_replies( |
| 374 | &self, |
| 375 | team_id: &str, |
| 376 | channel_id: &str, |
| 377 | root_ids: &[String], |
| 378 | ) -> Result<Vec<Result<Vec<Message>>>> { |
| 379 | let listings = Graph::channel_replies(self, team_id, channel_id, root_ids).await?; |
| 380 | Ok(listings |
| 381 | .into_iter() |
| 382 | .map(|listing| listing.map_err(Into::into)) |
| 383 | .collect()) |
| 384 | } |
| 385 | |
| 386 | async fn send_chat_message( |
| 387 | &self, |
| 388 | chat_id: &str, |
| 389 | html: &str, |
| 390 | mentions: &[OutgoingMention], |
| 391 | extras: &MessageExtras, |
| 392 | ) -> Result<Message> { |
| 393 | Ok(Graph::send_chat_message(self, chat_id, html, mentions, extras).await?) |
| 394 | } |
| 395 | |
| 396 | async fn send_channel_message( |
| 397 | &self, |
| 398 | team_id: &str, |
| 399 | channel_id: &str, |
| 400 | html: &str, |
| 401 | subject: Option<&str>, |
| 402 | mentions: &[OutgoingMention], |
| 403 | extras: &MessageExtras, |
| 404 | ) -> Result<Message> { |
| 405 | Ok( |
| 406 | Graph::send_channel_message(self, team_id, channel_id, html, subject, mentions, extras) |
| 407 | .await?, |
| 408 | ) |
| 409 | } |
| 410 | |
| 411 | async fn reply_to_channel_message( |
| 412 | &self, |
| 413 | team_id: &str, |
| 414 | channel_id: &str, |
| 415 | message_id: &str, |
| 416 | html: &str, |
| 417 | mentions: &[OutgoingMention], |
| 418 | extras: &MessageExtras, |
| 419 | ) -> Result<Message> { |
| 420 | Ok(Graph::reply_to_channel_message( |
| 421 | self, team_id, channel_id, message_id, html, mentions, extras, |
| 422 | ) |
| 423 | .await?) |
| 424 | } |
| 425 | |
| 426 | async fn mark_chat_read(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()> { |
| 427 | Ok(Graph::mark_chat_read(self, chat_id, user_id, tenant_id).await?) |
| 428 | } |
| 429 | |
| 430 | async fn mark_chat_unread( |
| 431 | &self, |
| 432 | chat_id: &str, |
| 433 | user_id: &str, |
| 434 | tenant_id: &str, |
| 435 | last_read_at: DateTime<Utc>, |
| 436 | ) -> Result<()> { |
| 437 | Ok(Graph::mark_chat_unread(self, chat_id, user_id, tenant_id, last_read_at).await?) |
| 438 | } |
| 439 | |
| 440 | async fn hide_chat(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()> { |
| 441 | Ok(Graph::hide_chat(self, chat_id, user_id, tenant_id).await?) |
| 442 | } |
| 443 | |
| 444 | async fn unhide_chat(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()> { |
| 445 | Ok(Graph::unhide_chat(self, chat_id, user_id, tenant_id).await?) |
| 446 | } |
| 447 | |
| 448 | async fn leave_chat(&self, chat_id: &str, user_id: &str) -> Result<()> { |
| 449 | Ok(Graph::leave_chat(self, chat_id, user_id).await?) |
| 450 | } |
| 451 | |
| 452 | async fn muted_chat_states(&self) -> Result<Vec<(String, bool)>> { |
| 453 | Ok(Conversations::new(self.session()) |
| 454 | .conversation_mute_states() |
| 455 | .await?) |
| 456 | } |
| 457 | |
| 458 | async fn set_chat_muted(&self, chat_id: &str, muted: bool) -> Result<()> { |
| 459 | Ok(Conversations::new(self.session()) |
| 460 | .set_alerts(chat_id, !muted) |
| 461 | .await?) |
| 462 | } |
| 463 | |
| 464 | async fn send_typing(&self, conversation: &ConversationRef, active: bool) -> Result<()> { |
| 465 | Ok(Messages::new(self.session()) |
| 466 | .send_typing(conversation, active) |
| 467 | .await?) |
| 468 | } |
| 469 | |
| 470 | async fn set_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> { |
| 471 | if let Some(key) = chatsvc::emotion_key(reaction_type) { |
| 472 | return Ok(Messages::new(self.session()) |
| 473 | .set_emotion(&conversation_ref(target), target.message_id(), key) |
| 474 | .await?); |
| 475 | } |
| 476 | Ok(Graph::set_reaction(self, target, reaction_type).await?) |
| 477 | } |
| 478 | |
| 479 | async fn unset_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> { |
| 480 | let keys = chatsvc::emotion_keys(reaction_type); |
| 481 | if !keys.is_empty() { |
| 482 | return Ok(Messages::new(self.session()) |
| 483 | .unset_emotions(&conversation_ref(target), target.message_id(), keys) |
| 484 | .await?); |
| 485 | } |
| 486 | Ok(Graph::unset_reaction(self, target, reaction_type).await?) |
| 487 | } |
| 488 | |
| 489 | async fn edit_message( |
| 490 | &self, |
| 491 | target: &MessageTarget, |
| 492 | html: &str, |
| 493 | mentions: &[OutgoingMention], |
| 494 | extras: &MessageExtras, |
| 495 | ) -> Result<()> { |
| 496 | match chatsvc_conversation(target) { |
| 497 | Some(conversation) if mentions.is_empty() => { |
| 498 | let html = format!("{}{html}{}", extras.kept_html(), extras.files_html()); |
| 499 | Ok(Messages::new(self.session()) |
| 500 | .edit_message(&conversation, target.message_id(), &html) |
| 501 | .await?) |
| 502 | } |
| 503 | Some(_) => Err(Error::Unsupported( |
| 504 | "editing a channel message with mentions", |
| 505 | )), |
| 506 | None => Ok(Graph::edit_message(self, target, html, mentions, extras).await?), |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()> { |
| 511 | match chatsvc_conversation(target) { |
| 512 | Some(conversation) => Ok(Messages::new(self.session()) |
| 513 | .soft_delete_message(&conversation, target.message_id()) |
| 514 | .await?), |
| 515 | None => Ok(Graph::soft_delete_message(self, user_id, target).await?), |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | async fn create_one_on_one(&self, my_user_id: &str, user_id: &str) -> Result<Chat> { |
| 520 | Ok(Graph::create_one_on_one(self, my_user_id, user_id).await?) |
| 521 | } |
| 522 | |
| 523 | async fn create_group( |
| 524 | &self, |
| 525 | my_user_id: &str, |
| 526 | user_ids: &[String], |
| 527 | topic: Option<&str>, |
| 528 | ) -> Result<Chat> { |
| 529 | Ok(Graph::create_group(self, my_user_id, user_ids, topic).await?) |
| 530 | } |
| 531 | |
| 532 | async fn search_people(&self, query: &str) -> Result<Vec<User>> { |
| 533 | Ok(Graph::search_people(self, query).await?) |
| 534 | } |
| 535 | |
| 536 | async fn reply_with_quote( |
| 537 | &self, |
| 538 | chat_id: &str, |
| 539 | quoted_message_id: &str, |
| 540 | html: &str, |
| 541 | mentions: &[OutgoingMention], |
| 542 | extras: &MessageExtras, |
| 543 | ) -> Result<Message> { |
| 544 | Ok( |
| 545 | Graph::reply_with_quote(self, chat_id, quoted_message_id, html, mentions, extras) |
| 546 | .await?, |
| 547 | ) |
| 548 | } |
| 549 | |
| 550 | async fn channel_files_folder(&self, team_id: &str, channel_id: &str) -> Result<DriveFolder> { |
| 551 | Ok(Graph::channel_files_folder(self, team_id, channel_id).await?) |
| 552 | } |
| 553 | |
| 554 | async fn upload_file( |
| 555 | &self, |
| 556 | destination: &UploadDestination, |
| 557 | file_name: &str, |
| 558 | bytes: &[u8], |
| 559 | progress: &(dyn Fn(u8) + Send + Sync), |
| 560 | ) -> Result<UploadedFile> { |
| 561 | Ok(Graph::upload_file(self, destination, file_name, bytes, progress).await?) |
| 562 | } |
| 563 | |
| 564 | async fn share_file(&self, file: &UploadedFile, user_ids: &[String]) -> Result<()> { |
| 565 | Ok(Graph::share_item(self, file, user_ids).await?) |
| 566 | } |
| 567 | |
| 568 | async fn delete_file(&self, file: &UploadedFile) -> Result<()> { |
| 569 | Ok(Graph::delete_drive_item(self, &file.drive_id, &file.item_id).await?) |
| 570 | } |
| 571 | |
| 572 | async fn resolve_share(&self, open_url: &str) -> Result<SharedFile> { |
| 573 | Ok(Graph::resolve_share(self, open_url).await?) |
| 574 | } |
| 575 | |
| 576 | async fn download_range( |
| 577 | &self, |
| 578 | download_url: &str, |
| 579 | start: u64, |
| 580 | end: Option<u64>, |
| 581 | ) -> Result<Vec<u8>> { |
| 582 | Ok(Graph::download_range(self, download_url, start, end).await?) |
| 583 | } |
| 584 | |
| 585 | async fn hosted_content(&self, url: &str) -> Result<Photo> { |
| 586 | if chatsvc::is_link_image_url(url) { |
| 587 | let image = Messages::new(self.session()).link_image(url).await?; |
| 588 | return Ok(Photo { |
| 589 | bytes: image.bytes, |
| 590 | content_type: image.content_type, |
| 591 | }); |
| 592 | } |
| 593 | Ok(Graph::download_hosted_content(self, url).await?) |
| 594 | } |
| 595 | |
| 596 | async fn external_image(&self, url: &str) -> Result<Photo> { |
| 597 | Ok(Graph::download_external_image(self, url).await?) |
| 598 | } |
| 599 | |
| 600 | async fn user_photos(&self, user_ids: &[String]) -> Result<Vec<Result<Option<Photo>>>> { |
| 601 | let photos = Graph::user_photos(self, user_ids).await?; |
| 602 | Ok(photos |
| 603 | .into_iter() |
| 604 | .map(|photo| photo.map_err(Into::into)) |
| 605 | .collect()) |
| 606 | } |
| 607 | |
| 608 | async fn presences(&self, user_ids: &[String]) -> Result<Vec<Presence>> { |
| 609 | Ok(Graph::presences(self, user_ids).await?) |
| 610 | } |
| 611 | |
| 612 | async fn subscribe_presence( |
| 613 | &self, |
| 614 | endpoint_id: &str, |
| 615 | trouter_uri: &str, |
| 616 | user_ids: &[String], |
| 617 | purge: bool, |
| 618 | ) -> Result<()> { |
| 619 | Ok(Graph::subscribe_presence(self, endpoint_id, trouter_uri, user_ids, purge).await?) |
| 620 | } |
| 621 | |
| 622 | async fn consumption_horizons(&self, conversation_id: &str) -> Result<Vec<MemberHorizon>> { |
| 623 | Ok(Receipts::new(self.session()) |
| 624 | .consumption_horizons(conversation_id) |
| 625 | .await?) |
| 626 | } |
| 627 | |
| 628 | async fn chat_apps(&self, chat_id: &str) -> Result<Vec<ChatApp>> { |
| 629 | Ok(CardActions::new(self.session()).chat_apps(chat_id).await?) |
| 630 | } |
| 631 | |
| 632 | async fn invoke_card(&self, invoke: InvokeRequest) -> Result<InvokeResponse> { |
| 633 | Ok(CardActions::new(self.session()).invoke(invoke).await?) |
| 634 | } |
| 635 | |
| 636 | async fn message_links( |
| 637 | &self, |
| 638 | conversation: &ConversationRef, |
| 639 | page_size: usize, |
| 640 | ) -> Result<Vec<MessageLinks>> { |
| 641 | Ok(Messages::new(self.session()) |
| 642 | .list_message_links(conversation, page_size) |
| 643 | .await?) |
| 644 | } |
| 645 | |
| 646 | async fn set_message_links(&self, target: &MessageTarget, links_json: &str) -> Result<()> { |
| 647 | Ok(Messages::new(self.session()) |
| 648 | .set_links(&conversation_ref(target), target.message_id(), links_json) |
| 649 | .await?) |
| 650 | } |
| 651 | |
| 652 | async fn link_info(&self, url: &str) -> Result<LinkInfo> { |
| 653 | Ok(Messages::new(self.session()).link_info(url).await?) |
| 654 | } |
| 655 | |
| 656 | async fn search_gifs(&self, query: &str) -> Result<Vec<Gif>> { |
| 657 | Ok(Gifs::new(self.session()).search(query).await?) |
| 658 | } |
| 659 | |
| 660 | async fn create_scheduled( |
| 661 | &self, |
| 662 | inner_thread_id: &str, |
| 663 | html: &str, |
| 664 | send_at: DateTime<Utc>, |
| 665 | display_name: &str, |
| 666 | ) -> Result<ScheduledDraft> { |
| 667 | Ok(ScheduledDrafts::new(self.session()) |
| 668 | .create(inner_thread_id, html, send_at, display_name) |
| 669 | .await?) |
| 670 | } |
| 671 | |
| 672 | async fn scheduled_drafts(&self) -> Result<Vec<ScheduledDraft>> { |
| 673 | Ok(ScheduledDrafts::new(self.session()).list().await?) |
| 674 | } |
| 675 | |
| 676 | async fn update_scheduled( |
| 677 | &self, |
| 678 | draft: &ScheduledDraft, |
| 679 | html: &str, |
| 680 | display_name: &str, |
| 681 | ) -> Result<ScheduledDraft> { |
| 682 | Ok(ScheduledDrafts::new(self.session()) |
| 683 | .update(draft, html, display_name) |
| 684 | .await?) |
| 685 | } |
| 686 | |
| 687 | async fn cancel_scheduled(&self, draft_id: &str) -> Result<()> { |
| 688 | Ok(ScheduledDrafts::new(self.session()) |
| 689 | .cancel(draft_id) |
| 690 | .await?) |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | fn chatsvc_conversation(target: &MessageTarget) -> Option<ConversationRef> { |
| 695 | match target { |
| 696 | MessageTarget::Chat { .. } => CHATSVC_CHAT_WRITES.then(|| conversation_ref(target)), |
| 697 | MessageTarget::Channel { .. } => Some(conversation_ref(target)), |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | fn conversation_ref(target: &MessageTarget) -> ConversationRef { |
| 702 | match target { |
| 703 | MessageTarget::Chat { chat_id, .. } => ConversationRef::chat(chat_id), |
| 704 | MessageTarget::Channel { |
| 705 | channel_id, |
| 706 | root_id: None, |
| 707 | .. |
| 708 | } => ConversationRef::channel_root(channel_id), |
| 709 | MessageTarget::Channel { |
| 710 | channel_id, |
| 711 | root_id: Some(root_id), |
| 712 | .. |
| 713 | } => ConversationRef::channel_reply(channel_id, root_id), |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | impl From<graph::Page<Message>> for RemotePage { |
| 718 | fn from(page: graph::Page<Message>) -> Self { |
| 719 | RemotePage { |
| 720 | items: page.items, |
| 721 | next_link: page.next_link, |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | impl From<graph::Page<Message>> for DeltaPage { |
| 727 | fn from(page: graph::Page<Message>) -> Self { |
| 728 | DeltaPage { |
| 729 | items: page.items, |
| 730 | next_link: page.next_link, |
| 731 | delta_link: page.delta_link, |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | impl From<graph::Page<Chat>> for ChatsPage { |
| 737 | fn from(page: graph::Page<Chat>) -> Self { |
| 738 | ChatsPage { |
| 739 | items: page.items, |
| 740 | next_link: page.next_link, |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | #[cfg(test)] |
| 746 | mod tests { |
| 747 | use super::*; |
| 748 | |
| 749 | #[test] |
| 750 | fn channel_targets_route_to_chatsvc() { |
| 751 | let root = MessageTarget::channel("t", "19:c@thread.tacv2", "m1", None); |
| 752 | let reply = MessageTarget::channel("t", "19:c@thread.tacv2", "r1", Some("m1")); |
| 753 | assert_eq!( |
| 754 | chatsvc_conversation(&root), |
| 755 | Some(ConversationRef::channel_root("19:c@thread.tacv2")) |
| 756 | ); |
| 757 | assert_eq!( |
| 758 | chatsvc_conversation(&reply), |
| 759 | Some(ConversationRef::channel_reply("19:c@thread.tacv2", "m1")) |
| 760 | ); |
| 761 | } |
| 762 | |
| 763 | #[test] |
| 764 | fn reactions_reach_chats_regardless_of_the_feature_switch() { |
| 765 | let chat = MessageTarget::chat("19:a@thread.v2", "m1"); |
| 766 | assert_eq!( |
| 767 | conversation_ref(&chat), |
| 768 | ConversationRef::chat("19:a@thread.v2") |
| 769 | ); |
| 770 | } |
| 771 | |
| 772 | #[test] |
| 773 | fn chats_follow_the_feature_switch() { |
| 774 | let chat = MessageTarget::chat("19:a@thread.v2", "m1"); |
| 775 | assert_eq!(chatsvc_conversation(&chat).is_some(), CHATSVC_CHAT_WRITES); |
| 776 | } |
| 777 | } |