BlueGit

ict rusty-teams

Files

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

remote.rs

remote.rs 16.3 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 consumption_horizons(&self, _conversation_id: &str) -> Result<Vec<MemberHorizon>> {
184 Err(Error::Unsupported("read receipts"))
185 }
186}
187
188impl Remote for Graph {
189 async fn me(&self) -> Result<User> {
190 Ok(Graph::me(self).await?)
191 }
192
193 async fn chats_page(&self, top: usize) -> Result<ChatsPage> {
194 Ok(Graph::chats_page(self, top).await?.into())
195 }
196
197 async fn chats_at(&self, next_link: &str) -> Result<ChatsPage> {
198 Ok(Graph::chats_at(self, next_link).await?.into())
199 }
200
201 async fn chat(&self, chat_id: &str) -> Result<Chat> {
202 Ok(Graph::chat(self, chat_id).await?)
203 }
204
205 async fn chat_members(&self, chat_id: &str) -> Result<Vec<Member>> {
206 Ok(Graph::chat_members(self, chat_id).await?)
207 }
208
209 async fn joined_teams(&self) -> Result<Vec<Team>> {
210 Ok(Graph::joined_teams(self).await?)
211 }
212
213 async fn channels_for_teams(&self, team_ids: &[String]) -> Result<Vec<Result<Vec<Channel>>>> {
214 let listings = Graph::channels_for_teams(self, team_ids).await?;
215 Ok(listings
216 .into_iter()
217 .map(|listing| listing.map_err(Into::into))
218 .collect())
219 }
220
221 async fn chat_messages(
222 &self,
223 chat_id: &str,
224 before: Option<DateTime<Utc>>,
225 top: usize,
226 ) -> Result<RemotePage> {
227 Ok(Graph::chat_messages(self, chat_id, before, top)
228 .await?
229 .into())
230 }
231
232 async fn chat_message(&self, chat_id: &str, message_id: &str) -> Result<Message> {
233 Ok(Graph::chat_message(self, chat_id, message_id).await?)
234 }
235
236 async fn channel_messages(
237 &self,
238 team_id: &str,
239 channel_id: &str,
240 top: usize,
241 ) -> Result<RemotePage> {
242 Ok(Graph::channel_messages(self, team_id, channel_id, top)
243 .await?
244 .into())
245 }
246
247 async fn channel_messages_at(&self, next_link: &str) -> Result<RemotePage> {
248 Ok(Graph::channel_messages_at(self, next_link).await?.into())
249 }
250
251 async fn channel_message(
252 &self,
253 team_id: &str,
254 channel_id: &str,
255 message_id: &str,
256 ) -> Result<Message> {
257 Ok(Graph::channel_message(self, team_id, channel_id, message_id).await?)
258 }
259
260 async fn channel_reply(
261 &self,
262 team_id: &str,
263 channel_id: &str,
264 message_id: &str,
265 reply_id: &str,
266 ) -> Result<Message> {
267 Ok(Graph::channel_reply(self, team_id, channel_id, message_id, reply_id).await?)
268 }
269
270 async fn channel_delta(
271 &self,
272 team_id: &str,
273 channel_id: &str,
274 modified_after: Option<DateTime<Utc>>,
275 ) -> Result<DeltaPage> {
276 Ok(
277 Graph::channel_delta(self, team_id, channel_id, modified_after)
278 .await?
279 .into(),
280 )
281 }
282
283 async fn channel_delta_at(&self, link: &str) -> Result<DeltaPage> {
284 Ok(Graph::channel_delta_at(self, link).await?.into())
285 }
286
287 async fn channel_replies(
288 &self,
289 team_id: &str,
290 channel_id: &str,
291 root_ids: &[String],
292 ) -> Result<Vec<Result<Vec<Message>>>> {
293 let listings = Graph::channel_replies(self, team_id, channel_id, root_ids).await?;
294 Ok(listings
295 .into_iter()
296 .map(|listing| listing.map_err(Into::into))
297 .collect())
298 }
299
300 async fn send_chat_message(
301 &self,
302 chat_id: &str,
303 html: &str,
304 mentions: &[OutgoingMention],
305 extras: &MessageExtras,
306 ) -> Result<Message> {
307 Ok(Graph::send_chat_message(self, chat_id, html, mentions, extras).await?)
308 }
309
310 async fn send_channel_message(
311 &self,
312 team_id: &str,
313 channel_id: &str,
314 html: &str,
315 subject: Option<&str>,
316 mentions: &[OutgoingMention],
317 extras: &MessageExtras,
318 ) -> Result<Message> {
319 Ok(
320 Graph::send_channel_message(self, team_id, channel_id, html, subject, mentions, extras)
321 .await?,
322 )
323 }
324
325 async fn reply_to_channel_message(
326 &self,
327 team_id: &str,
328 channel_id: &str,
329 message_id: &str,
330 html: &str,
331 mentions: &[OutgoingMention],
332 extras: &MessageExtras,
333 ) -> Result<Message> {
334 Ok(Graph::reply_to_channel_message(
335 self, team_id, channel_id, message_id, html, mentions, extras,
336 )
337 .await?)
338 }
339
340 async fn mark_chat_read(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()> {
341 Ok(Graph::mark_chat_read(self, chat_id, user_id, tenant_id).await?)
342 }
343
344 async fn set_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> {
345 Ok(Graph::set_reaction(self, target, reaction_type).await?)
346 }
347
348 async fn unset_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> {
349 Ok(Graph::unset_reaction(self, target, reaction_type).await?)
350 }
351
352 async fn edit_message(
353 &self,
354 target: &MessageTarget,
355 html: &str,
356 mentions: &[OutgoingMention],
357 extras: &MessageExtras,
358 ) -> Result<()> {
359 match chatsvc_conversation(target) {
360 Some(conversation) if mentions.is_empty() => {
361 let html = format!("{}{html}{}", extras.kept_html(), extras.files_html());
362 Ok(Messages::new(self.session())
363 .edit_message(&conversation, target.message_id(), &html)
364 .await?)
365 }
366 Some(_) => Err(Error::Unsupported(
367 "editing a channel message with mentions",
368 )),
369 None => Ok(Graph::edit_message(self, target, html, mentions, extras).await?),
370 }
371 }
372
373 async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()> {
374 match chatsvc_conversation(target) {
375 Some(conversation) => Ok(Messages::new(self.session())
376 .soft_delete_message(&conversation, target.message_id())
377 .await?),
378 None => Ok(Graph::soft_delete_message(self, user_id, target).await?),
379 }
380 }
381
382 async fn create_one_on_one(&self, my_user_id: &str, user_id: &str) -> Result<Chat> {
383 Ok(Graph::create_one_on_one(self, my_user_id, user_id).await?)
384 }
385
386 async fn create_group(
387 &self,
388 my_user_id: &str,
389 user_ids: &[String],
390 topic: Option<&str>,
391 ) -> Result<Chat> {
392 Ok(Graph::create_group(self, my_user_id, user_ids, topic).await?)
393 }
394
395 async fn search_people(&self, query: &str) -> Result<Vec<User>> {
396 Ok(Graph::search_people(self, query).await?)
397 }
398
399 async fn reply_with_quote(
400 &self,
401 chat_id: &str,
402 quoted_message_id: &str,
403 html: &str,
404 mentions: &[OutgoingMention],
405 extras: &MessageExtras,
406 ) -> Result<Message> {
407 Ok(
408 Graph::reply_with_quote(self, chat_id, quoted_message_id, html, mentions, extras)
409 .await?,
410 )
411 }
412
413 async fn channel_files_folder(&self, team_id: &str, channel_id: &str) -> Result<DriveFolder> {
414 Ok(Graph::channel_files_folder(self, team_id, channel_id).await?)
415 }
416
417 async fn upload_file(
418 &self,
419 destination: &UploadDestination,
420 file_name: &str,
421 bytes: &[u8],
422 progress: &(dyn Fn(u8) + Send + Sync),
423 ) -> Result<UploadedFile> {
424 Ok(Graph::upload_file(self, destination, file_name, bytes, progress).await?)
425 }
426
427 async fn share_file(&self, file: &UploadedFile, user_ids: &[String]) -> Result<()> {
428 Ok(Graph::share_item(self, file, user_ids).await?)
429 }
430
431 async fn delete_file(&self, file: &UploadedFile) -> Result<()> {
432 Ok(Graph::delete_drive_item(self, &file.drive_id, &file.item_id).await?)
433 }
434
435 async fn resolve_share(&self, open_url: &str) -> Result<SharedFile> {
436 Ok(Graph::resolve_share(self, open_url).await?)
437 }
438
439 async fn download_range(
440 &self,
441 download_url: &str,
442 start: u64,
443 end: Option<u64>,
444 ) -> Result<Vec<u8>> {
445 Ok(Graph::download_range(self, download_url, start, end).await?)
446 }
447
448 async fn hosted_content(&self, url: &str) -> Result<Photo> {
449 Ok(Graph::download_hosted_content(self, url).await?)
450 }
451
452 async fn user_photos(&self, user_ids: &[String]) -> Result<Vec<Result<Option<Photo>>>> {
453 let photos = Graph::user_photos(self, user_ids).await?;
454 Ok(photos
455 .into_iter()
456 .map(|photo| photo.map_err(Into::into))
457 .collect())
458 }
459
460 async fn presences(&self, user_ids: &[String]) -> Result<Vec<Presence>> {
461 Ok(Graph::presences(self, user_ids).await?)
462 }
463
464 async fn consumption_horizons(&self, conversation_id: &str) -> Result<Vec<MemberHorizon>> {
465 Ok(Receipts::new(self.session())
466 .consumption_horizons(conversation_id)
467 .await?)
468 }
469}
470
471fn chatsvc_conversation(target: &MessageTarget) -> Option<ConversationRef> {
472 match target {
473 MessageTarget::Chat { chat_id, .. } => {
474 CHATSVC_CHAT_WRITES.then(|| ConversationRef::chat(chat_id))
475 }
476 MessageTarget::Channel {
477 channel_id,
478 root_id: None,
479 ..
480 } => Some(ConversationRef::channel_root(channel_id)),
481 MessageTarget::Channel {
482 channel_id,
483 root_id: Some(root_id),
484 ..
485 } => Some(ConversationRef::channel_reply(channel_id, root_id)),
486 }
487}
488
489impl From<graph::Page<Message>> for RemotePage {
490 fn from(page: graph::Page<Message>) -> Self {
491 RemotePage {
492 items: page.items,
493 next_link: page.next_link,
494 }
495 }
496}
497
498impl From<graph::Page<Message>> for DeltaPage {
499 fn from(page: graph::Page<Message>) -> Self {
500 DeltaPage {
501 items: page.items,
502 next_link: page.next_link,
503 delta_link: page.delta_link,
504 }
505 }
506}
507
508impl From<graph::Page<Chat>> for ChatsPage {
509 fn from(page: graph::Page<Chat>) -> Self {
510 ChatsPage {
511 items: page.items,
512 next_link: page.next_link,
513 }
514 }
515}
516
517#[cfg(test)]
518mod tests {
519 use super::*;
520
521 #[test]
522 fn channel_targets_route_to_chatsvc() {
523 let root = MessageTarget::channel("t", "19:c@thread.tacv2", "m1", None);
524 let reply = MessageTarget::channel("t", "19:c@thread.tacv2", "r1", Some("m1"));
525 assert_eq!(
526 chatsvc_conversation(&root),
527 Some(ConversationRef::channel_root("19:c@thread.tacv2"))
528 );
529 assert_eq!(
530 chatsvc_conversation(&reply),
531 Some(ConversationRef::channel_reply("19:c@thread.tacv2", "m1"))
532 );
533 }
534
535 #[test]
536 fn chats_follow_the_feature_switch() {
537 let chat = MessageTarget::chat("19:a@thread.v2", "m1");
538 assert_eq!(chatsvc_conversation(&chat).is_some(), CHATSVC_CHAT_WRITES);
539 }
540}