| 1 | use chatsvc::{ |
| 2 | CardActions, ChatApp, ConversationRef, Conversations, InvokeRequest, InvokeResponse, |
| 3 | MemberHorizon, 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 | |
| 222 | impl Remote for Graph { |
| 223 | async fn me(&self) -> Result<User> { |
| 224 | Ok(Graph::me(self).await?) |
| 225 | } |
| 226 | |
| 227 | async fn chats_page(&self, top: usize) -> Result<ChatsPage> { |
| 228 | Ok(Graph::chats_page(self, top).await?.into()) |
| 229 | } |
| 230 | |
| 231 | async fn chats_at(&self, next_link: &str) -> Result<ChatsPage> { |
| 232 | Ok(Graph::chats_at(self, next_link).await?.into()) |
| 233 | } |
| 234 | |
| 235 | async fn chat(&self, chat_id: &str) -> Result<Chat> { |
| 236 | Ok(Graph::chat(self, chat_id).await?) |
| 237 | } |
| 238 | |
| 239 | async fn chat_members(&self, chat_id: &str) -> Result<Vec<Member>> { |
| 240 | Ok(Graph::chat_members(self, chat_id).await?) |
| 241 | } |
| 242 | |
| 243 | async fn joined_teams(&self) -> Result<Vec<Team>> { |
| 244 | Ok(Graph::joined_teams(self).await?) |
| 245 | } |
| 246 | |
| 247 | async fn channels_for_teams(&self, team_ids: &[String]) -> Result<Vec<Result<Vec<Channel>>>> { |
| 248 | let listings = Graph::channels_for_teams(self, team_ids).await?; |
| 249 | Ok(listings |
| 250 | .into_iter() |
| 251 | .map(|listing| listing.map_err(Into::into)) |
| 252 | .collect()) |
| 253 | } |
| 254 | |
| 255 | async fn chat_messages( |
| 256 | &self, |
| 257 | chat_id: &str, |
| 258 | before: Option<DateTime<Utc>>, |
| 259 | top: usize, |
| 260 | ) -> Result<RemotePage> { |
| 261 | Ok(Graph::chat_messages(self, chat_id, before, top) |
| 262 | .await? |
| 263 | .into()) |
| 264 | } |
| 265 | |
| 266 | async fn chat_message(&self, chat_id: &str, message_id: &str) -> Result<Message> { |
| 267 | Ok(Graph::chat_message(self, chat_id, message_id).await?) |
| 268 | } |
| 269 | |
| 270 | async fn channel_messages( |
| 271 | &self, |
| 272 | team_id: &str, |
| 273 | channel_id: &str, |
| 274 | top: usize, |
| 275 | ) -> Result<RemotePage> { |
| 276 | Ok(Graph::channel_messages(self, team_id, channel_id, top) |
| 277 | .await? |
| 278 | .into()) |
| 279 | } |
| 280 | |
| 281 | async fn channel_messages_at(&self, next_link: &str) -> Result<RemotePage> { |
| 282 | Ok(Graph::channel_messages_at(self, next_link).await?.into()) |
| 283 | } |
| 284 | |
| 285 | async fn channel_message( |
| 286 | &self, |
| 287 | team_id: &str, |
| 288 | channel_id: &str, |
| 289 | message_id: &str, |
| 290 | ) -> Result<Message> { |
| 291 | Ok(Graph::channel_message(self, team_id, channel_id, message_id).await?) |
| 292 | } |
| 293 | |
| 294 | async fn channel_reply( |
| 295 | &self, |
| 296 | team_id: &str, |
| 297 | channel_id: &str, |
| 298 | message_id: &str, |
| 299 | reply_id: &str, |
| 300 | ) -> Result<Message> { |
| 301 | Ok(Graph::channel_reply(self, team_id, channel_id, message_id, reply_id).await?) |
| 302 | } |
| 303 | |
| 304 | async fn channel_delta( |
| 305 | &self, |
| 306 | team_id: &str, |
| 307 | channel_id: &str, |
| 308 | modified_after: Option<DateTime<Utc>>, |
| 309 | ) -> Result<DeltaPage> { |
| 310 | Ok( |
| 311 | Graph::channel_delta(self, team_id, channel_id, modified_after) |
| 312 | .await? |
| 313 | .into(), |
| 314 | ) |
| 315 | } |
| 316 | |
| 317 | async fn channel_delta_at(&self, link: &str) -> Result<DeltaPage> { |
| 318 | Ok(Graph::channel_delta_at(self, link).await?.into()) |
| 319 | } |
| 320 | |
| 321 | async fn channel_replies( |
| 322 | &self, |
| 323 | team_id: &str, |
| 324 | channel_id: &str, |
| 325 | root_ids: &[String], |
| 326 | ) -> Result<Vec<Result<Vec<Message>>>> { |
| 327 | let listings = Graph::channel_replies(self, team_id, channel_id, root_ids).await?; |
| 328 | Ok(listings |
| 329 | .into_iter() |
| 330 | .map(|listing| listing.map_err(Into::into)) |
| 331 | .collect()) |
| 332 | } |
| 333 | |
| 334 | async fn send_chat_message( |
| 335 | &self, |
| 336 | chat_id: &str, |
| 337 | html: &str, |
| 338 | mentions: &[OutgoingMention], |
| 339 | extras: &MessageExtras, |
| 340 | ) -> Result<Message> { |
| 341 | Ok(Graph::send_chat_message(self, chat_id, html, mentions, extras).await?) |
| 342 | } |
| 343 | |
| 344 | async fn send_channel_message( |
| 345 | &self, |
| 346 | team_id: &str, |
| 347 | channel_id: &str, |
| 348 | html: &str, |
| 349 | subject: Option<&str>, |
| 350 | mentions: &[OutgoingMention], |
| 351 | extras: &MessageExtras, |
| 352 | ) -> Result<Message> { |
| 353 | Ok( |
| 354 | Graph::send_channel_message(self, team_id, channel_id, html, subject, mentions, extras) |
| 355 | .await?, |
| 356 | ) |
| 357 | } |
| 358 | |
| 359 | async fn reply_to_channel_message( |
| 360 | &self, |
| 361 | team_id: &str, |
| 362 | channel_id: &str, |
| 363 | message_id: &str, |
| 364 | html: &str, |
| 365 | mentions: &[OutgoingMention], |
| 366 | extras: &MessageExtras, |
| 367 | ) -> Result<Message> { |
| 368 | Ok(Graph::reply_to_channel_message( |
| 369 | self, team_id, channel_id, message_id, html, mentions, extras, |
| 370 | ) |
| 371 | .await?) |
| 372 | } |
| 373 | |
| 374 | async fn mark_chat_read(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()> { |
| 375 | Ok(Graph::mark_chat_read(self, chat_id, user_id, tenant_id).await?) |
| 376 | } |
| 377 | |
| 378 | async fn mark_chat_unread( |
| 379 | &self, |
| 380 | chat_id: &str, |
| 381 | user_id: &str, |
| 382 | tenant_id: &str, |
| 383 | last_read_at: DateTime<Utc>, |
| 384 | ) -> Result<()> { |
| 385 | Ok(Graph::mark_chat_unread(self, chat_id, user_id, tenant_id, last_read_at).await?) |
| 386 | } |
| 387 | |
| 388 | async fn hide_chat(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()> { |
| 389 | Ok(Graph::hide_chat(self, chat_id, user_id, tenant_id).await?) |
| 390 | } |
| 391 | |
| 392 | async fn unhide_chat(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()> { |
| 393 | Ok(Graph::unhide_chat(self, chat_id, user_id, tenant_id).await?) |
| 394 | } |
| 395 | |
| 396 | async fn leave_chat(&self, chat_id: &str, user_id: &str) -> Result<()> { |
| 397 | Ok(Graph::leave_chat(self, chat_id, user_id).await?) |
| 398 | } |
| 399 | |
| 400 | async fn muted_chat_states(&self) -> Result<Vec<(String, bool)>> { |
| 401 | Ok(Conversations::new(self.session()) |
| 402 | .conversation_mute_states() |
| 403 | .await?) |
| 404 | } |
| 405 | |
| 406 | async fn set_chat_muted(&self, chat_id: &str, muted: bool) -> Result<()> { |
| 407 | Ok(Conversations::new(self.session()) |
| 408 | .set_alerts(chat_id, !muted) |
| 409 | .await?) |
| 410 | } |
| 411 | |
| 412 | async fn send_typing(&self, conversation: &ConversationRef, active: bool) -> Result<()> { |
| 413 | Ok(Messages::new(self.session()) |
| 414 | .send_typing(conversation, active) |
| 415 | .await?) |
| 416 | } |
| 417 | |
| 418 | async fn set_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> { |
| 419 | if let Some(key) = chatsvc::emotion_key(reaction_type) { |
| 420 | return Ok(Messages::new(self.session()) |
| 421 | .set_emotion(&conversation_ref(target), target.message_id(), key) |
| 422 | .await?); |
| 423 | } |
| 424 | Ok(Graph::set_reaction(self, target, reaction_type).await?) |
| 425 | } |
| 426 | |
| 427 | async fn unset_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> { |
| 428 | let keys = chatsvc::emotion_keys(reaction_type); |
| 429 | if !keys.is_empty() { |
| 430 | return Ok(Messages::new(self.session()) |
| 431 | .unset_emotions(&conversation_ref(target), target.message_id(), keys) |
| 432 | .await?); |
| 433 | } |
| 434 | Ok(Graph::unset_reaction(self, target, reaction_type).await?) |
| 435 | } |
| 436 | |
| 437 | async fn edit_message( |
| 438 | &self, |
| 439 | target: &MessageTarget, |
| 440 | html: &str, |
| 441 | mentions: &[OutgoingMention], |
| 442 | extras: &MessageExtras, |
| 443 | ) -> Result<()> { |
| 444 | match chatsvc_conversation(target) { |
| 445 | Some(conversation) if mentions.is_empty() => { |
| 446 | let html = format!("{}{html}{}", extras.kept_html(), extras.files_html()); |
| 447 | Ok(Messages::new(self.session()) |
| 448 | .edit_message(&conversation, target.message_id(), &html) |
| 449 | .await?) |
| 450 | } |
| 451 | Some(_) => Err(Error::Unsupported( |
| 452 | "editing a channel message with mentions", |
| 453 | )), |
| 454 | None => Ok(Graph::edit_message(self, target, html, mentions, extras).await?), |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()> { |
| 459 | match chatsvc_conversation(target) { |
| 460 | Some(conversation) => Ok(Messages::new(self.session()) |
| 461 | .soft_delete_message(&conversation, target.message_id()) |
| 462 | .await?), |
| 463 | None => Ok(Graph::soft_delete_message(self, user_id, target).await?), |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | async fn create_one_on_one(&self, my_user_id: &str, user_id: &str) -> Result<Chat> { |
| 468 | Ok(Graph::create_one_on_one(self, my_user_id, user_id).await?) |
| 469 | } |
| 470 | |
| 471 | async fn create_group( |
| 472 | &self, |
| 473 | my_user_id: &str, |
| 474 | user_ids: &[String], |
| 475 | topic: Option<&str>, |
| 476 | ) -> Result<Chat> { |
| 477 | Ok(Graph::create_group(self, my_user_id, user_ids, topic).await?) |
| 478 | } |
| 479 | |
| 480 | async fn search_people(&self, query: &str) -> Result<Vec<User>> { |
| 481 | Ok(Graph::search_people(self, query).await?) |
| 482 | } |
| 483 | |
| 484 | async fn reply_with_quote( |
| 485 | &self, |
| 486 | chat_id: &str, |
| 487 | quoted_message_id: &str, |
| 488 | html: &str, |
| 489 | mentions: &[OutgoingMention], |
| 490 | extras: &MessageExtras, |
| 491 | ) -> Result<Message> { |
| 492 | Ok( |
| 493 | Graph::reply_with_quote(self, chat_id, quoted_message_id, html, mentions, extras) |
| 494 | .await?, |
| 495 | ) |
| 496 | } |
| 497 | |
| 498 | async fn channel_files_folder(&self, team_id: &str, channel_id: &str) -> Result<DriveFolder> { |
| 499 | Ok(Graph::channel_files_folder(self, team_id, channel_id).await?) |
| 500 | } |
| 501 | |
| 502 | async fn upload_file( |
| 503 | &self, |
| 504 | destination: &UploadDestination, |
| 505 | file_name: &str, |
| 506 | bytes: &[u8], |
| 507 | progress: &(dyn Fn(u8) + Send + Sync), |
| 508 | ) -> Result<UploadedFile> { |
| 509 | Ok(Graph::upload_file(self, destination, file_name, bytes, progress).await?) |
| 510 | } |
| 511 | |
| 512 | async fn share_file(&self, file: &UploadedFile, user_ids: &[String]) -> Result<()> { |
| 513 | Ok(Graph::share_item(self, file, user_ids).await?) |
| 514 | } |
| 515 | |
| 516 | async fn delete_file(&self, file: &UploadedFile) -> Result<()> { |
| 517 | Ok(Graph::delete_drive_item(self, &file.drive_id, &file.item_id).await?) |
| 518 | } |
| 519 | |
| 520 | async fn resolve_share(&self, open_url: &str) -> Result<SharedFile> { |
| 521 | Ok(Graph::resolve_share(self, open_url).await?) |
| 522 | } |
| 523 | |
| 524 | async fn download_range( |
| 525 | &self, |
| 526 | download_url: &str, |
| 527 | start: u64, |
| 528 | end: Option<u64>, |
| 529 | ) -> Result<Vec<u8>> { |
| 530 | Ok(Graph::download_range(self, download_url, start, end).await?) |
| 531 | } |
| 532 | |
| 533 | async fn hosted_content(&self, url: &str) -> Result<Photo> { |
| 534 | Ok(Graph::download_hosted_content(self, url).await?) |
| 535 | } |
| 536 | |
| 537 | async fn user_photos(&self, user_ids: &[String]) -> Result<Vec<Result<Option<Photo>>>> { |
| 538 | let photos = Graph::user_photos(self, user_ids).await?; |
| 539 | Ok(photos |
| 540 | .into_iter() |
| 541 | .map(|photo| photo.map_err(Into::into)) |
| 542 | .collect()) |
| 543 | } |
| 544 | |
| 545 | async fn presences(&self, user_ids: &[String]) -> Result<Vec<Presence>> { |
| 546 | Ok(Graph::presences(self, user_ids).await?) |
| 547 | } |
| 548 | |
| 549 | async fn subscribe_presence( |
| 550 | &self, |
| 551 | endpoint_id: &str, |
| 552 | trouter_uri: &str, |
| 553 | user_ids: &[String], |
| 554 | purge: bool, |
| 555 | ) -> Result<()> { |
| 556 | Ok(Graph::subscribe_presence(self, endpoint_id, trouter_uri, user_ids, purge).await?) |
| 557 | } |
| 558 | |
| 559 | async fn consumption_horizons(&self, conversation_id: &str) -> Result<Vec<MemberHorizon>> { |
| 560 | Ok(Receipts::new(self.session()) |
| 561 | .consumption_horizons(conversation_id) |
| 562 | .await?) |
| 563 | } |
| 564 | |
| 565 | async fn chat_apps(&self, chat_id: &str) -> Result<Vec<ChatApp>> { |
| 566 | Ok(CardActions::new(self.session()).chat_apps(chat_id).await?) |
| 567 | } |
| 568 | |
| 569 | async fn invoke_card(&self, invoke: InvokeRequest) -> Result<InvokeResponse> { |
| 570 | Ok(CardActions::new(self.session()).invoke(invoke).await?) |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | fn chatsvc_conversation(target: &MessageTarget) -> Option<ConversationRef> { |
| 575 | match target { |
| 576 | MessageTarget::Chat { .. } => CHATSVC_CHAT_WRITES.then(|| conversation_ref(target)), |
| 577 | MessageTarget::Channel { .. } => Some(conversation_ref(target)), |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | fn conversation_ref(target: &MessageTarget) -> ConversationRef { |
| 582 | match target { |
| 583 | MessageTarget::Chat { chat_id, .. } => ConversationRef::chat(chat_id), |
| 584 | MessageTarget::Channel { |
| 585 | channel_id, |
| 586 | root_id: None, |
| 587 | .. |
| 588 | } => ConversationRef::channel_root(channel_id), |
| 589 | MessageTarget::Channel { |
| 590 | channel_id, |
| 591 | root_id: Some(root_id), |
| 592 | .. |
| 593 | } => ConversationRef::channel_reply(channel_id, root_id), |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | impl From<graph::Page<Message>> for RemotePage { |
| 598 | fn from(page: graph::Page<Message>) -> Self { |
| 599 | RemotePage { |
| 600 | items: page.items, |
| 601 | next_link: page.next_link, |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | impl From<graph::Page<Message>> for DeltaPage { |
| 607 | fn from(page: graph::Page<Message>) -> Self { |
| 608 | DeltaPage { |
| 609 | items: page.items, |
| 610 | next_link: page.next_link, |
| 611 | delta_link: page.delta_link, |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | impl From<graph::Page<Chat>> for ChatsPage { |
| 617 | fn from(page: graph::Page<Chat>) -> Self { |
| 618 | ChatsPage { |
| 619 | items: page.items, |
| 620 | next_link: page.next_link, |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | #[cfg(test)] |
| 626 | mod tests { |
| 627 | use super::*; |
| 628 | |
| 629 | #[test] |
| 630 | fn channel_targets_route_to_chatsvc() { |
| 631 | let root = MessageTarget::channel("t", "19:c@thread.tacv2", "m1", None); |
| 632 | let reply = MessageTarget::channel("t", "19:c@thread.tacv2", "r1", Some("m1")); |
| 633 | assert_eq!( |
| 634 | chatsvc_conversation(&root), |
| 635 | Some(ConversationRef::channel_root("19:c@thread.tacv2")) |
| 636 | ); |
| 637 | assert_eq!( |
| 638 | chatsvc_conversation(&reply), |
| 639 | Some(ConversationRef::channel_reply("19:c@thread.tacv2", "m1")) |
| 640 | ); |
| 641 | } |
| 642 | |
| 643 | #[test] |
| 644 | fn reactions_reach_chats_regardless_of_the_feature_switch() { |
| 645 | let chat = MessageTarget::chat("19:a@thread.v2", "m1"); |
| 646 | assert_eq!( |
| 647 | conversation_ref(&chat), |
| 648 | ConversationRef::chat("19:a@thread.v2") |
| 649 | ); |
| 650 | } |
| 651 | |
| 652 | #[test] |
| 653 | fn chats_follow_the_feature_switch() { |
| 654 | let chat = MessageTarget::chat("19:a@thread.v2", "m1"); |
| 655 | assert_eq!(chatsvc_conversation(&chat).is_some(), CHATSVC_CHAT_WRITES); |
| 656 | } |
| 657 | } |