BlueGit

ict rusty-teams

Files

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

remote.rs

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