BlueGit

ict rusty-teams

Files

rusty-teams / crates / core / src / remote.rs

remote.rs

remote.rs 18.4 KiB Download Raw
1use chatsvc::{
2 CardActions, ChatApp, ConversationRef, InvokeRequest, InvokeResponse, MemberHorizon, Messages,
3 Receipts,
4};
5use chrono::{DateTime, Utc};
6use graph::{
7 Channel, Chat, DriveFolder, Graph, Member, Message, MessageExtras, MessageTarget,
8 OutgoingMention, Photo, Presence, SharedFile, Team, UploadDestination, UploadedFile, User,
9};
10
11use crate::error::{Error, Result};
12
13pub const CHATSVC_CHAT_WRITES: bool = false;
14
15#[derive(Debug, Clone)]
16pub struct RemotePage {
17 pub items: Vec<Message>,
18 pub next_link: Option<String>,
19}
20
21#[derive(Debug, Clone)]
22pub 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)]
29pub struct ChatsPage {
30 pub items: Vec<Chat>,
31 pub next_link: Option<String>,
32}
33
34#[allow(async_fn_in_trait)]
35pub 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 set_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()>;
110 async fn unset_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()>;
111 async fn edit_message(
112 &self,
113 target: &MessageTarget,
114 html: &str,
115 mentions: &[OutgoingMention],
116 extras: &MessageExtras,
117 ) -> Result<()>;
118 async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()>;
119 async fn create_one_on_one(&self, my_user_id: &str, user_id: &str) -> Result<Chat>;
120 async fn create_group(
121 &self,
122 my_user_id: &str,
123 user_ids: &[String],
124 topic: Option<&str>,
125 ) -> Result<Chat>;
126 async fn search_people(&self, query: &str) -> Result<Vec<User>>;
127
128 async fn reply_with_quote(
129 &self,
130 _chat_id: &str,
131 _quoted_message_id: &str,
132 _html: &str,
133 _mentions: &[OutgoingMention],
134 _extras: &MessageExtras,
135 ) -> Result<Message> {
136 Err(Error::Unsupported("a quoted reply"))
137 }
138
139 async fn channel_files_folder(&self, _team_id: &str, _channel_id: &str) -> Result<DriveFolder> {
140 Err(Error::Unsupported("a channel files folder"))
141 }
142
143 async fn upload_file(
144 &self,
145 _destination: &UploadDestination,
146 _file_name: &str,
147 _bytes: &[u8],
148 _progress: &(dyn Fn(u8) + Send + Sync),
149 ) -> Result<UploadedFile> {
150 Err(Error::Unsupported("a file upload"))
151 }
152
153 async fn share_file(&self, _file: &UploadedFile, _user_ids: &[String]) -> Result<()> {
154 Err(Error::Unsupported("sharing a file"))
155 }
156
157 async fn delete_file(&self, _file: &UploadedFile) -> Result<()> {
158 Err(Error::Unsupported("deleting a file"))
159 }
160
161 async fn resolve_share(&self, _open_url: &str) -> Result<SharedFile> {
162 Err(Error::Unsupported("resolving a shared file"))
163 }
164
165 async fn download_range(
166 &self,
167 _download_url: &str,
168 _start: u64,
169 _end: Option<u64>,
170 ) -> Result<Vec<u8>> {
171 Err(Error::Unsupported("a file download"))
172 }
173
174 async fn hosted_content(&self, _url: &str) -> Result<Photo> {
175 Err(Error::Unsupported("hosted content"))
176 }
177
178 async fn user_photos(&self, _user_ids: &[String]) -> Result<Vec<Result<Option<Photo>>>> {
179 Err(Error::Unsupported("profile photos"))
180 }
181
182 async fn presences(&self, _user_ids: &[String]) -> Result<Vec<Presence>> {
183 Err(Error::Unsupported("presence"))
184 }
185
186 async fn subscribe_presence(
187 &self,
188 _endpoint_id: &str,
189 _trouter_uri: &str,
190 _user_ids: &[String],
191 _purge: bool,
192 ) -> Result<()> {
193 Err(Error::Unsupported("presence subscriptions"))
194 }
195
196 async fn consumption_horizons(&self, _conversation_id: &str) -> Result<Vec<MemberHorizon>> {
197 Err(Error::Unsupported("read receipts"))
198 }
199
200 async fn chat_apps(&self, _chat_id: &str) -> Result<Vec<ChatApp>> {
201 Err(Error::Unsupported("chat apps"))
202 }
203
204 async fn invoke_card(&self, _invoke: InvokeRequest) -> Result<InvokeResponse> {
205 Err(Error::Unsupported("card actions"))
206 }
207}
208
209impl Remote for Graph {
210 async fn me(&self) -> Result<User> {
211 Ok(Graph::me(self).await?)
212 }
213
214 async fn chats_page(&self, top: usize) -> Result<ChatsPage> {
215 Ok(Graph::chats_page(self, top).await?.into())
216 }
217
218 async fn chats_at(&self, next_link: &str) -> Result<ChatsPage> {
219 Ok(Graph::chats_at(self, next_link).await?.into())
220 }
221
222 async fn chat(&self, chat_id: &str) -> Result<Chat> {
223 Ok(Graph::chat(self, chat_id).await?)
224 }
225
226 async fn chat_members(&self, chat_id: &str) -> Result<Vec<Member>> {
227 Ok(Graph::chat_members(self, chat_id).await?)
228 }
229
230 async fn joined_teams(&self) -> Result<Vec<Team>> {
231 Ok(Graph::joined_teams(self).await?)
232 }
233
234 async fn channels_for_teams(&self, team_ids: &[String]) -> Result<Vec<Result<Vec<Channel>>>> {
235 let listings = Graph::channels_for_teams(self, team_ids).await?;
236 Ok(listings
237 .into_iter()
238 .map(|listing| listing.map_err(Into::into))
239 .collect())
240 }
241
242 async fn chat_messages(
243 &self,
244 chat_id: &str,
245 before: Option<DateTime<Utc>>,
246 top: usize,
247 ) -> Result<RemotePage> {
248 Ok(Graph::chat_messages(self, chat_id, before, top)
249 .await?
250 .into())
251 }
252
253 async fn chat_message(&self, chat_id: &str, message_id: &str) -> Result<Message> {
254 Ok(Graph::chat_message(self, chat_id, message_id).await?)
255 }
256
257 async fn channel_messages(
258 &self,
259 team_id: &str,
260 channel_id: &str,
261 top: usize,
262 ) -> Result<RemotePage> {
263 Ok(Graph::channel_messages(self, team_id, channel_id, top)
264 .await?
265 .into())
266 }
267
268 async fn channel_messages_at(&self, next_link: &str) -> Result<RemotePage> {
269 Ok(Graph::channel_messages_at(self, next_link).await?.into())
270 }
271
272 async fn channel_message(
273 &self,
274 team_id: &str,
275 channel_id: &str,
276 message_id: &str,
277 ) -> Result<Message> {
278 Ok(Graph::channel_message(self, team_id, channel_id, message_id).await?)
279 }
280
281 async fn channel_reply(
282 &self,
283 team_id: &str,
284 channel_id: &str,
285 message_id: &str,
286 reply_id: &str,
287 ) -> Result<Message> {
288 Ok(Graph::channel_reply(self, team_id, channel_id, message_id, reply_id).await?)
289 }
290
291 async fn channel_delta(
292 &self,
293 team_id: &str,
294 channel_id: &str,
295 modified_after: Option<DateTime<Utc>>,
296 ) -> Result<DeltaPage> {
297 Ok(
298 Graph::channel_delta(self, team_id, channel_id, modified_after)
299 .await?
300 .into(),
301 )
302 }
303
304 async fn channel_delta_at(&self, link: &str) -> Result<DeltaPage> {
305 Ok(Graph::channel_delta_at(self, link).await?.into())
306 }
307
308 async fn channel_replies(
309 &self,
310 team_id: &str,
311 channel_id: &str,
312 root_ids: &[String],
313 ) -> Result<Vec<Result<Vec<Message>>>> {
314 let listings = Graph::channel_replies(self, team_id, channel_id, root_ids).await?;
315 Ok(listings
316 .into_iter()
317 .map(|listing| listing.map_err(Into::into))
318 .collect())
319 }
320
321 async fn send_chat_message(
322 &self,
323 chat_id: &str,
324 html: &str,
325 mentions: &[OutgoingMention],
326 extras: &MessageExtras,
327 ) -> Result<Message> {
328 Ok(Graph::send_chat_message(self, chat_id, html, mentions, extras).await?)
329 }
330
331 async fn send_channel_message(
332 &self,
333 team_id: &str,
334 channel_id: &str,
335 html: &str,
336 subject: Option<&str>,
337 mentions: &[OutgoingMention],
338 extras: &MessageExtras,
339 ) -> Result<Message> {
340 Ok(
341 Graph::send_channel_message(self, team_id, channel_id, html, subject, mentions, extras)
342 .await?,
343 )
344 }
345
346 async fn reply_to_channel_message(
347 &self,
348 team_id: &str,
349 channel_id: &str,
350 message_id: &str,
351 html: &str,
352 mentions: &[OutgoingMention],
353 extras: &MessageExtras,
354 ) -> Result<Message> {
355 Ok(Graph::reply_to_channel_message(
356 self, team_id, channel_id, message_id, html, mentions, extras,
357 )
358 .await?)
359 }
360
361 async fn mark_chat_read(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()> {
362 Ok(Graph::mark_chat_read(self, chat_id, user_id, tenant_id).await?)
363 }
364
365 async fn set_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> {
366 if let Some(key) = chatsvc::emotion_key(reaction_type) {
367 return Ok(Messages::new(self.session())
368 .set_emotion(&conversation_ref(target), target.message_id(), key)
369 .await?);
370 }
371 Ok(Graph::set_reaction(self, target, reaction_type).await?)
372 }
373
374 async fn unset_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> {
375 let keys = chatsvc::emotion_keys(reaction_type);
376 if !keys.is_empty() {
377 return Ok(Messages::new(self.session())
378 .unset_emotions(&conversation_ref(target), target.message_id(), keys)
379 .await?);
380 }
381 Ok(Graph::unset_reaction(self, target, reaction_type).await?)
382 }
383
384 async fn edit_message(
385 &self,
386 target: &MessageTarget,
387 html: &str,
388 mentions: &[OutgoingMention],
389 extras: &MessageExtras,
390 ) -> Result<()> {
391 match chatsvc_conversation(target) {
392 Some(conversation) if mentions.is_empty() => {
393 let html = format!("{}{html}{}", extras.kept_html(), extras.files_html());
394 Ok(Messages::new(self.session())
395 .edit_message(&conversation, target.message_id(), &html)
396 .await?)
397 }
398 Some(_) => Err(Error::Unsupported(
399 "editing a channel message with mentions",
400 )),
401 None => Ok(Graph::edit_message(self, target, html, mentions, extras).await?),
402 }
403 }
404
405 async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()> {
406 match chatsvc_conversation(target) {
407 Some(conversation) => Ok(Messages::new(self.session())
408 .soft_delete_message(&conversation, target.message_id())
409 .await?),
410 None => Ok(Graph::soft_delete_message(self, user_id, target).await?),
411 }
412 }
413
414 async fn create_one_on_one(&self, my_user_id: &str, user_id: &str) -> Result<Chat> {
415 Ok(Graph::create_one_on_one(self, my_user_id, user_id).await?)
416 }
417
418 async fn create_group(
419 &self,
420 my_user_id: &str,
421 user_ids: &[String],
422 topic: Option<&str>,
423 ) -> Result<Chat> {
424 Ok(Graph::create_group(self, my_user_id, user_ids, topic).await?)
425 }
426
427 async fn search_people(&self, query: &str) -> Result<Vec<User>> {
428 Ok(Graph::search_people(self, query).await?)
429 }
430
431 async fn reply_with_quote(
432 &self,
433 chat_id: &str,
434 quoted_message_id: &str,
435 html: &str,
436 mentions: &[OutgoingMention],
437 extras: &MessageExtras,
438 ) -> Result<Message> {
439 Ok(
440 Graph::reply_with_quote(self, chat_id, quoted_message_id, html, mentions, extras)
441 .await?,
442 )
443 }
444
445 async fn channel_files_folder(&self, team_id: &str, channel_id: &str) -> Result<DriveFolder> {
446 Ok(Graph::channel_files_folder(self, team_id, channel_id).await?)
447 }
448
449 async fn upload_file(
450 &self,
451 destination: &UploadDestination,
452 file_name: &str,
453 bytes: &[u8],
454 progress: &(dyn Fn(u8) + Send + Sync),
455 ) -> Result<UploadedFile> {
456 Ok(Graph::upload_file(self, destination, file_name, bytes, progress).await?)
457 }
458
459 async fn share_file(&self, file: &UploadedFile, user_ids: &[String]) -> Result<()> {
460 Ok(Graph::share_item(self, file, user_ids).await?)
461 }
462
463 async fn delete_file(&self, file: &UploadedFile) -> Result<()> {
464 Ok(Graph::delete_drive_item(self, &file.drive_id, &file.item_id).await?)
465 }
466
467 async fn resolve_share(&self, open_url: &str) -> Result<SharedFile> {
468 Ok(Graph::resolve_share(self, open_url).await?)
469 }
470
471 async fn download_range(
472 &self,
473 download_url: &str,
474 start: u64,
475 end: Option<u64>,
476 ) -> Result<Vec<u8>> {
477 Ok(Graph::download_range(self, download_url, start, end).await?)
478 }
479
480 async fn hosted_content(&self, url: &str) -> Result<Photo> {
481 Ok(Graph::download_hosted_content(self, url).await?)
482 }
483
484 async fn user_photos(&self, user_ids: &[String]) -> Result<Vec<Result<Option<Photo>>>> {
485 let photos = Graph::user_photos(self, user_ids).await?;
486 Ok(photos
487 .into_iter()
488 .map(|photo| photo.map_err(Into::into))
489 .collect())
490 }
491
492 async fn presences(&self, user_ids: &[String]) -> Result<Vec<Presence>> {
493 Ok(Graph::presences(self, user_ids).await?)
494 }
495
496 async fn subscribe_presence(
497 &self,
498 endpoint_id: &str,
499 trouter_uri: &str,
500 user_ids: &[String],
501 purge: bool,
502 ) -> Result<()> {
503 Ok(Graph::subscribe_presence(self, endpoint_id, trouter_uri, user_ids, purge).await?)
504 }
505
506 async fn consumption_horizons(&self, conversation_id: &str) -> Result<Vec<MemberHorizon>> {
507 Ok(Receipts::new(self.session())
508 .consumption_horizons(conversation_id)
509 .await?)
510 }
511
512 async fn chat_apps(&self, chat_id: &str) -> Result<Vec<ChatApp>> {
513 Ok(CardActions::new(self.session()).chat_apps(chat_id).await?)
514 }
515
516 async fn invoke_card(&self, invoke: InvokeRequest) -> Result<InvokeResponse> {
517 Ok(CardActions::new(self.session()).invoke(invoke).await?)
518 }
519}
520
521fn chatsvc_conversation(target: &MessageTarget) -> Option<ConversationRef> {
522 match target {
523 MessageTarget::Chat { .. } => CHATSVC_CHAT_WRITES.then(|| conversation_ref(target)),
524 MessageTarget::Channel { .. } => Some(conversation_ref(target)),
525 }
526}
527
528fn conversation_ref(target: &MessageTarget) -> ConversationRef {
529 match target {
530 MessageTarget::Chat { chat_id, .. } => ConversationRef::chat(chat_id),
531 MessageTarget::Channel {
532 channel_id,
533 root_id: None,
534 ..
535 } => ConversationRef::channel_root(channel_id),
536 MessageTarget::Channel {
537 channel_id,
538 root_id: Some(root_id),
539 ..
540 } => ConversationRef::channel_reply(channel_id, root_id),
541 }
542}
543
544impl From<graph::Page<Message>> for RemotePage {
545 fn from(page: graph::Page<Message>) -> Self {
546 RemotePage {
547 items: page.items,
548 next_link: page.next_link,
549 }
550 }
551}
552
553impl From<graph::Page<Message>> for DeltaPage {
554 fn from(page: graph::Page<Message>) -> Self {
555 DeltaPage {
556 items: page.items,
557 next_link: page.next_link,
558 delta_link: page.delta_link,
559 }
560 }
561}
562
563impl From<graph::Page<Chat>> for ChatsPage {
564 fn from(page: graph::Page<Chat>) -> Self {
565 ChatsPage {
566 items: page.items,
567 next_link: page.next_link,
568 }
569 }
570}
571
572#[cfg(test)]
573mod tests {
574 use super::*;
575
576 #[test]
577 fn channel_targets_route_to_chatsvc() {
578 let root = MessageTarget::channel("t", "19:c@thread.tacv2", "m1", None);
579 let reply = MessageTarget::channel("t", "19:c@thread.tacv2", "r1", Some("m1"));
580 assert_eq!(
581 chatsvc_conversation(&root),
582 Some(ConversationRef::channel_root("19:c@thread.tacv2"))
583 );
584 assert_eq!(
585 chatsvc_conversation(&reply),
586 Some(ConversationRef::channel_reply("19:c@thread.tacv2", "m1"))
587 );
588 }
589
590 #[test]
591 fn reactions_reach_chats_regardless_of_the_feature_switch() {
592 let chat = MessageTarget::chat("19:a@thread.v2", "m1");
593 assert_eq!(
594 conversation_ref(&chat),
595 ConversationRef::chat("19:a@thread.v2")
596 );
597 }
598
599 #[test]
600 fn chats_follow_the_feature_switch() {
601 let chat = MessageTarget::chat("19:a@thread.v2", "m1");
602 assert_eq!(chatsvc_conversation(&chat).is_some(), CHATSVC_CHAT_WRITES);
603 }
604}