BlueGit

ict rusty-teams

Files

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

remote.rs

Initial commit
metis Daniel Däschle · pushed by metis · 7 weeks ago
06e0ae51 History
remote.rs 13.5 KiB Download Raw
1use chatsvc::{ConversationRef, MemberHorizon, Messages, Receipts};
2use chrono::{DateTime, Utc};
3use graph::{
4 Channel, Chat, Graph, Member, Message, MessageTarget, OutgoingMention, Photo, Presence, Team,
5 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 ) -> Result<Message>;
86 async fn send_channel_message(
87 &self,
88 team_id: &str,
89 channel_id: &str,
90 html: &str,
91 subject: Option<&str>,
92 mentions: &[OutgoingMention],
93 ) -> Result<Message>;
94 async fn reply_to_channel_message(
95 &self,
96 team_id: &str,
97 channel_id: &str,
98 message_id: &str,
99 html: &str,
100 mentions: &[OutgoingMention],
101 ) -> Result<Message>;
102 async fn mark_chat_read(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()>;
103 async fn set_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()>;
104 async fn unset_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()>;
105 async fn edit_message(
106 &self,
107 target: &MessageTarget,
108 html: &str,
109 mentions: &[OutgoingMention],
110 ) -> Result<()>;
111 async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()>;
112 async fn create_one_on_one(&self, my_user_id: &str, user_id: &str) -> Result<Chat>;
113 async fn create_group(
114 &self,
115 my_user_id: &str,
116 user_ids: &[String],
117 topic: Option<&str>,
118 ) -> Result<Chat>;
119 async fn search_people(&self, query: &str) -> Result<Vec<User>>;
120
121 async fn reply_with_quote(
122 &self,
123 _chat_id: &str,
124 _quoted_message_id: &str,
125 _html: &str,
126 _mentions: &[OutgoingMention],
127 ) -> Result<Message> {
128 Err(Error::Unsupported("a quoted reply"))
129 }
130
131 async fn hosted_content(&self, _url: &str) -> Result<Photo> {
132 Err(Error::Unsupported("hosted content"))
133 }
134
135 async fn user_photos(&self, _user_ids: &[String]) -> Result<Vec<Result<Option<Photo>>>> {
136 Err(Error::Unsupported("profile photos"))
137 }
138
139 async fn presences(&self, _user_ids: &[String]) -> Result<Vec<Presence>> {
140 Err(Error::Unsupported("presence"))
141 }
142
143 async fn consumption_horizons(&self, _conversation_id: &str) -> Result<Vec<MemberHorizon>> {
144 Err(Error::Unsupported("read receipts"))
145 }
146}
147
148impl Remote for Graph {
149 async fn me(&self) -> Result<User> {
150 Ok(Graph::me(self).await?)
151 }
152
153 async fn chats_page(&self, top: usize) -> Result<ChatsPage> {
154 Ok(Graph::chats_page(self, top).await?.into())
155 }
156
157 async fn chats_at(&self, next_link: &str) -> Result<ChatsPage> {
158 Ok(Graph::chats_at(self, next_link).await?.into())
159 }
160
161 async fn chat(&self, chat_id: &str) -> Result<Chat> {
162 Ok(Graph::chat(self, chat_id).await?)
163 }
164
165 async fn chat_members(&self, chat_id: &str) -> Result<Vec<Member>> {
166 Ok(Graph::chat_members(self, chat_id).await?)
167 }
168
169 async fn joined_teams(&self) -> Result<Vec<Team>> {
170 Ok(Graph::joined_teams(self).await?)
171 }
172
173 async fn channels_for_teams(&self, team_ids: &[String]) -> Result<Vec<Result<Vec<Channel>>>> {
174 let listings = Graph::channels_for_teams(self, team_ids).await?;
175 Ok(listings
176 .into_iter()
177 .map(|listing| listing.map_err(Into::into))
178 .collect())
179 }
180
181 async fn chat_messages(
182 &self,
183 chat_id: &str,
184 before: Option<DateTime<Utc>>,
185 top: usize,
186 ) -> Result<RemotePage> {
187 Ok(Graph::chat_messages(self, chat_id, before, top)
188 .await?
189 .into())
190 }
191
192 async fn chat_message(&self, chat_id: &str, message_id: &str) -> Result<Message> {
193 Ok(Graph::chat_message(self, chat_id, message_id).await?)
194 }
195
196 async fn channel_messages(
197 &self,
198 team_id: &str,
199 channel_id: &str,
200 top: usize,
201 ) -> Result<RemotePage> {
202 Ok(Graph::channel_messages(self, team_id, channel_id, top)
203 .await?
204 .into())
205 }
206
207 async fn channel_messages_at(&self, next_link: &str) -> Result<RemotePage> {
208 Ok(Graph::channel_messages_at(self, next_link).await?.into())
209 }
210
211 async fn channel_message(
212 &self,
213 team_id: &str,
214 channel_id: &str,
215 message_id: &str,
216 ) -> Result<Message> {
217 Ok(Graph::channel_message(self, team_id, channel_id, message_id).await?)
218 }
219
220 async fn channel_reply(
221 &self,
222 team_id: &str,
223 channel_id: &str,
224 message_id: &str,
225 reply_id: &str,
226 ) -> Result<Message> {
227 Ok(Graph::channel_reply(self, team_id, channel_id, message_id, reply_id).await?)
228 }
229
230 async fn channel_delta(
231 &self,
232 team_id: &str,
233 channel_id: &str,
234 modified_after: Option<DateTime<Utc>>,
235 ) -> Result<DeltaPage> {
236 Ok(
237 Graph::channel_delta(self, team_id, channel_id, modified_after)
238 .await?
239 .into(),
240 )
241 }
242
243 async fn channel_delta_at(&self, link: &str) -> Result<DeltaPage> {
244 Ok(Graph::channel_delta_at(self, link).await?.into())
245 }
246
247 async fn channel_replies(
248 &self,
249 team_id: &str,
250 channel_id: &str,
251 root_ids: &[String],
252 ) -> Result<Vec<Result<Vec<Message>>>> {
253 let listings = Graph::channel_replies(self, team_id, channel_id, root_ids).await?;
254 Ok(listings
255 .into_iter()
256 .map(|listing| listing.map_err(Into::into))
257 .collect())
258 }
259
260 async fn send_chat_message(
261 &self,
262 chat_id: &str,
263 html: &str,
264 mentions: &[OutgoingMention],
265 ) -> Result<Message> {
266 Ok(Graph::send_chat_message(self, chat_id, html, mentions).await?)
267 }
268
269 async fn send_channel_message(
270 &self,
271 team_id: &str,
272 channel_id: &str,
273 html: &str,
274 subject: Option<&str>,
275 mentions: &[OutgoingMention],
276 ) -> Result<Message> {
277 Ok(Graph::send_channel_message(self, team_id, channel_id, html, subject, mentions).await?)
278 }
279
280 async fn reply_to_channel_message(
281 &self,
282 team_id: &str,
283 channel_id: &str,
284 message_id: &str,
285 html: &str,
286 mentions: &[OutgoingMention],
287 ) -> Result<Message> {
288 Ok(
289 Graph::reply_to_channel_message(self, team_id, channel_id, message_id, html, mentions)
290 .await?,
291 )
292 }
293
294 async fn mark_chat_read(&self, chat_id: &str, user_id: &str, tenant_id: &str) -> Result<()> {
295 Ok(Graph::mark_chat_read(self, chat_id, user_id, tenant_id).await?)
296 }
297
298 async fn set_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> {
299 Ok(Graph::set_reaction(self, target, reaction_type).await?)
300 }
301
302 async fn unset_reaction(&self, target: &MessageTarget, reaction_type: &str) -> Result<()> {
303 Ok(Graph::unset_reaction(self, target, reaction_type).await?)
304 }
305
306 async fn edit_message(
307 &self,
308 target: &MessageTarget,
309 html: &str,
310 mentions: &[OutgoingMention],
311 ) -> Result<()> {
312 match chatsvc_conversation(target) {
313 Some(conversation) if mentions.is_empty() => Ok(Messages::new(self.session())
314 .edit_message(&conversation, target.message_id(), html)
315 .await?),
316 Some(_) => Err(Error::Unsupported(
317 "editing a channel message with mentions",
318 )),
319 None => Ok(Graph::edit_message(self, target, html, mentions).await?),
320 }
321 }
322
323 async fn soft_delete_message(&self, user_id: &str, target: &MessageTarget) -> Result<()> {
324 match chatsvc_conversation(target) {
325 Some(conversation) => Ok(Messages::new(self.session())
326 .soft_delete_message(&conversation, target.message_id())
327 .await?),
328 None => Ok(Graph::soft_delete_message(self, user_id, target).await?),
329 }
330 }
331
332 async fn create_one_on_one(&self, my_user_id: &str, user_id: &str) -> Result<Chat> {
333 Ok(Graph::create_one_on_one(self, my_user_id, user_id).await?)
334 }
335
336 async fn create_group(
337 &self,
338 my_user_id: &str,
339 user_ids: &[String],
340 topic: Option<&str>,
341 ) -> Result<Chat> {
342 Ok(Graph::create_group(self, my_user_id, user_ids, topic).await?)
343 }
344
345 async fn search_people(&self, query: &str) -> Result<Vec<User>> {
346 Ok(Graph::search_people(self, query).await?)
347 }
348
349 async fn reply_with_quote(
350 &self,
351 chat_id: &str,
352 quoted_message_id: &str,
353 html: &str,
354 mentions: &[OutgoingMention],
355 ) -> Result<Message> {
356 Ok(Graph::reply_with_quote(self, chat_id, quoted_message_id, html, mentions).await?)
357 }
358
359 async fn hosted_content(&self, url: &str) -> Result<Photo> {
360 Ok(Graph::download_hosted_content(self, url).await?)
361 }
362
363 async fn user_photos(&self, user_ids: &[String]) -> Result<Vec<Result<Option<Photo>>>> {
364 let photos = Graph::user_photos(self, user_ids).await?;
365 Ok(photos
366 .into_iter()
367 .map(|photo| photo.map_err(Into::into))
368 .collect())
369 }
370
371 async fn presences(&self, user_ids: &[String]) -> Result<Vec<Presence>> {
372 Ok(Graph::presences(self, user_ids).await?)
373 }
374
375 async fn consumption_horizons(&self, conversation_id: &str) -> Result<Vec<MemberHorizon>> {
376 Ok(Receipts::new(self.session())
377 .consumption_horizons(conversation_id)
378 .await?)
379 }
380}
381
382fn chatsvc_conversation(target: &MessageTarget) -> Option<ConversationRef> {
383 match target {
384 MessageTarget::Chat { chat_id, .. } => {
385 CHATSVC_CHAT_WRITES.then(|| ConversationRef::chat(chat_id))
386 }
387 MessageTarget::Channel {
388 channel_id,
389 root_id: None,
390 ..
391 } => Some(ConversationRef::channel_root(channel_id)),
392 MessageTarget::Channel {
393 channel_id,
394 root_id: Some(root_id),
395 ..
396 } => Some(ConversationRef::channel_reply(channel_id, root_id)),
397 }
398}
399
400impl From<graph::Page<Message>> for RemotePage {
401 fn from(page: graph::Page<Message>) -> Self {
402 RemotePage {
403 items: page.items,
404 next_link: page.next_link,
405 }
406 }
407}
408
409impl From<graph::Page<Message>> for DeltaPage {
410 fn from(page: graph::Page<Message>) -> Self {
411 DeltaPage {
412 items: page.items,
413 next_link: page.next_link,
414 delta_link: page.delta_link,
415 }
416 }
417}
418
419impl From<graph::Page<Chat>> for ChatsPage {
420 fn from(page: graph::Page<Chat>) -> Self {
421 ChatsPage {
422 items: page.items,
423 next_link: page.next_link,
424 }
425 }
426}
427
428#[cfg(test)]
429mod tests {
430 use super::*;
431
432 #[test]
433 fn channel_targets_route_to_chatsvc() {
434 let root = MessageTarget::channel("t", "19:c@thread.tacv2", "m1", None);
435 let reply = MessageTarget::channel("t", "19:c@thread.tacv2", "r1", Some("m1"));
436 assert_eq!(
437 chatsvc_conversation(&root),
438 Some(ConversationRef::channel_root("19:c@thread.tacv2"))
439 );
440 assert_eq!(
441 chatsvc_conversation(&reply),
442 Some(ConversationRef::channel_reply("19:c@thread.tacv2", "m1"))
443 );
444 }
445
446 #[test]
447 fn chats_follow_the_feature_switch() {
448 let chat = MessageTarget::chat("19:a@thread.v2", "m1");
449 assert_eq!(chatsvc_conversation(&chat).is_some(), CHATSVC_CHAT_WRITES);
450 }
451}