BlueGit

ict rusty-teams

Files

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

remote.rs

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