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