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