BlueGit

ict rusty-teams

Files

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

remote.rs

remote.rs 17.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 Ok(Graph::set_reaction(self, target, reaction_type).await?)
367 }
368
369 async fn unset_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> {
370 Ok(Graph::unset_reaction(self, target, reaction_type).await?)
371 }
372
373 async fn edit_message(
374 &self,
375 target: &MessageTarget,
376 html: &str,
377 mentions: &[OutgoingMention],
378 extras: &MessageExtras,
379 ) -> Result<()> {
380 match chatsvc_conversation(target) {
381 Some(conversation) if mentions.is_empty() => {
382 let html = format!("{}{html}{}", extras.kept_html(), extras.files_html());
383 Ok(Messages::new(self.session())
384 .edit_message(&conversation, target.message_id(), &html)
385 .await?)
386 }
387 Some(_) => Err(Error::Unsupported(
388 "editing a channel message with mentions",
389 )),
390 None => Ok(Graph::edit_message(self, target, html, mentions, extras).await?),
391 }
392 }
393
394 async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()> {
395 match chatsvc_conversation(target) {
396 Some(conversation) => Ok(Messages::new(self.session())
397 .soft_delete_message(&conversation, target.message_id())
398 .await?),
399 None => Ok(Graph::soft_delete_message(self, user_id, target).await?),
400 }
401 }
402
403 async fn create_one_on_one(&self, my_user_id: &str, user_id: &str) -> Result<Chat> {
404 Ok(Graph::create_one_on_one(self, my_user_id, user_id).await?)
405 }
406
407 async fn create_group(
408 &self,
409 my_user_id: &str,
410 user_ids: &[String],
411 topic: Option<&str>,
412 ) -> Result<Chat> {
413 Ok(Graph::create_group(self, my_user_id, user_ids, topic).await?)
414 }
415
416 async fn search_people(&self, query: &str) -> Result<Vec<User>> {
417 Ok(Graph::search_people(self, query).await?)
418 }
419
420 async fn reply_with_quote(
421 &self,
422 chat_id: &str,
423 quoted_message_id: &str,
424 html: &str,
425 mentions: &[OutgoingMention],
426 extras: &MessageExtras,
427 ) -> Result<Message> {
428 Ok(
429 Graph::reply_with_quote(self, chat_id, quoted_message_id, html, mentions, extras)
430 .await?,
431 )
432 }
433
434 async fn channel_files_folder(&self, team_id: &str, channel_id: &str) -> Result<DriveFolder> {
435 Ok(Graph::channel_files_folder(self, team_id, channel_id).await?)
436 }
437
438 async fn upload_file(
439 &self,
440 destination: &UploadDestination,
441 file_name: &str,
442 bytes: &[u8],
443 progress: &(dyn Fn(u8) + Send + Sync),
444 ) -> Result<UploadedFile> {
445 Ok(Graph::upload_file(self, destination, file_name, bytes, progress).await?)
446 }
447
448 async fn share_file(&self, file: &UploadedFile, user_ids: &[String]) -> Result<()> {
449 Ok(Graph::share_item(self, file, user_ids).await?)
450 }
451
452 async fn delete_file(&self, file: &UploadedFile) -> Result<()> {
453 Ok(Graph::delete_drive_item(self, &file.drive_id, &file.item_id).await?)
454 }
455
456 async fn resolve_share(&self, open_url: &str) -> Result<SharedFile> {
457 Ok(Graph::resolve_share(self, open_url).await?)
458 }
459
460 async fn download_range(
461 &self,
462 download_url: &str,
463 start: u64,
464 end: Option<u64>,
465 ) -> Result<Vec<u8>> {
466 Ok(Graph::download_range(self, download_url, start, end).await?)
467 }
468
469 async fn hosted_content(&self, url: &str) -> Result<Photo> {
470 Ok(Graph::download_hosted_content(self, url).await?)
471 }
472
473 async fn user_photos(&self, user_ids: &[String]) -> Result<Vec<Result<Option<Photo>>>> {
474 let photos = Graph::user_photos(self, user_ids).await?;
475 Ok(photos
476 .into_iter()
477 .map(|photo| photo.map_err(Into::into))
478 .collect())
479 }
480
481 async fn presences(&self, user_ids: &[String]) -> Result<Vec<Presence>> {
482 Ok(Graph::presences(self, user_ids).await?)
483 }
484
485 async fn subscribe_presence(
486 &self,
487 endpoint_id: &str,
488 trouter_uri: &str,
489 user_ids: &[String],
490 purge: bool,
491 ) -> Result<()> {
492 Ok(Graph::subscribe_presence(self, endpoint_id, trouter_uri, user_ids, purge).await?)
493 }
494
495 async fn consumption_horizons(&self, conversation_id: &str) -> Result<Vec<MemberHorizon>> {
496 Ok(Receipts::new(self.session())
497 .consumption_horizons(conversation_id)
498 .await?)
499 }
500
501 async fn chat_apps(&self, chat_id: &str) -> Result<Vec<ChatApp>> {
502 Ok(CardActions::new(self.session()).chat_apps(chat_id).await?)
503 }
504
505 async fn invoke_card(&self, invoke: InvokeRequest) -> Result<InvokeResponse> {
506 Ok(CardActions::new(self.session()).invoke(invoke).await?)
507 }
508}
509
510fn chatsvc_conversation(target: &MessageTarget) -> Option<ConversationRef> {
511 match target {
512 MessageTarget::Chat { chat_id, .. } => {
513 CHATSVC_CHAT_WRITES.then(|| ConversationRef::chat(chat_id))
514 }
515 MessageTarget::Channel {
516 channel_id,
517 root_id: None,
518 ..
519 } => Some(ConversationRef::channel_root(channel_id)),
520 MessageTarget::Channel {
521 channel_id,
522 root_id: Some(root_id),
523 ..
524 } => Some(ConversationRef::channel_reply(channel_id, root_id)),
525 }
526}
527
528impl From<graph::Page<Message>> for RemotePage {
529 fn from(page: graph::Page<Message>) -> Self {
530 RemotePage {
531 items: page.items,
532 next_link: page.next_link,
533 }
534 }
535}
536
537impl From<graph::Page<Message>> for DeltaPage {
538 fn from(page: graph::Page<Message>) -> Self {
539 DeltaPage {
540 items: page.items,
541 next_link: page.next_link,
542 delta_link: page.delta_link,
543 }
544 }
545}
546
547impl From<graph::Page<Chat>> for ChatsPage {
548 fn from(page: graph::Page<Chat>) -> Self {
549 ChatsPage {
550 items: page.items,
551 next_link: page.next_link,
552 }
553 }
554}
555
556#[cfg(test)]
557mod tests {
558 use super::*;
559
560 #[test]
561 fn channel_targets_route_to_chatsvc() {
562 let root = MessageTarget::channel("t", "19:c@thread.tacv2", "m1", None);
563 let reply = MessageTarget::channel("t", "19:c@thread.tacv2", "r1", Some("m1"));
564 assert_eq!(
565 chatsvc_conversation(&root),
566 Some(ConversationRef::channel_root("19:c@thread.tacv2"))
567 );
568 assert_eq!(
569 chatsvc_conversation(&reply),
570 Some(ConversationRef::channel_reply("19:c@thread.tacv2", "m1"))
571 );
572 }
573
574 #[test]
575 fn chats_follow_the_feature_switch() {
576 let chat = MessageTarget::chat("19:a@thread.v2", "m1");
577 assert_eq!(chatsvc_conversation(&chat).is_some(), CHATSVC_CHAT_WRITES);
578 }
579}