BlueGit

ict rusty-teams

Files

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

translate_sync.rs

translate_sync.rs 5.6 KiB Download Raw
1use std::collections::HashSet;
2
3use chatsvc::{
4 ConversationRef, Language, LanguageSettings, MAX_TRANSLATE_BATCH, MessageLanguage,
5 TranslateRequest, Translation, TranslationTrigger,
6};
7use chatsvc::sound_settings::SoundSettings;
8use serde_json::Value;
9
10use crate::engine::{Conversation, SyncEngine};
11use crate::error::{Error, Result};
12use crate::remote::Remote;
13
14const STAMP_PAGE_SIZE: usize = 50;
15const MAX_STAMP_REPLY_THREADS: usize = 5;
16const STAMP_CACHE_MESSAGES: usize = 100;
17
18impl<R: Remote> SyncEngine<R> {
19 pub async fn language_stamps(&self, conversation_id: &str) -> Result<Vec<MessageLanguage>> {
20 let targets =
21 match self.resolve(conversation_id).await? {
22 Conversation::Chat => vec![ConversationRef::chat(conversation_id)],
23 Conversation::Channel { .. } => {
24 let cached =
25 self.store
26 .messages(conversation_id, None, STAMP_CACHE_MESSAGES)?;
27 let mut seen = HashSet::new();
28 let reply_roots = cached
29 .iter()
30 .filter_map(|record| record.reply_to_id.as_deref())
31 .filter(|root_id| seen.insert(*root_id))
32 .take(MAX_STAMP_REPLY_THREADS);
33 std::iter::once(ConversationRef::channel_root(conversation_id))
34 .chain(reply_roots.map(|root_id| {
35 ConversationRef::channel_reply(conversation_id, root_id)
36 }))
37 .collect()
38 }
39 };
40 let mut stamps = Vec::new();
41 let mut last_error = None;
42 for target in &targets {
43 match self.remote.message_languages(target, STAMP_PAGE_SIZE).await {
44 Ok(found) => stamps.extend(found),
45 Err(error) => last_error = Some(error),
46 }
47 }
48 match last_error {
49 Some(error) if stamps.is_empty() => Err(error),
50 _ => Ok(stamps),
51 }
52 }
53
54 /// One entry per answered message; a message the service skipped has none.
55 pub async fn translate_messages(
56 &self,
57 conversation_id: &str,
58 message_ids: &[String],
59 to_language: &str,
60 trigger: &TranslationTrigger,
61 ) -> Result<Vec<Translation>> {
62 let conversation = self.resolve(conversation_id).await?;
63 let mut translations = Vec::new();
64 match conversation {
65 Conversation::Chat => {
66 for batch in message_ids.chunks(MAX_TRANSLATE_BATCH) {
67 let requests: Vec<TranslateRequest> =
68 batch.iter().map(|id| TranslateRequest::new(id)).collect();
69 translations.extend(
70 self.remote
71 .translate_chat_messages(
72 conversation_id,
73 to_language,
74 &requests,
75 trigger,
76 )
77 .await?,
78 );
79 }
80 }
81 Conversation::Channel { team_id } => {
82 let records = self.store.messages_by_id(conversation_id, message_ids)?;
83 let mut by_root: Vec<(String, Vec<TranslateRequest>)> = Vec::new();
84 for message_id in message_ids {
85 let record = records
86 .get(message_id)
87 .ok_or(Error::Unsupported("translating an uncached message"))?;
88 let root_id = record
89 .reply_to_id
90 .clone()
91 .unwrap_or_else(|| record.message_id.clone());
92 match by_root.iter_mut().find(|(root, _)| *root == root_id) {
93 Some((_, requests)) => requests.push(TranslateRequest::new(message_id)),
94 None => by_root.push((root_id, vec![TranslateRequest::new(message_id)])),
95 }
96 }
97 for (root_id, requests) in by_root {
98 for batch in requests.chunks(MAX_TRANSLATE_BATCH) {
99 translations.extend(
100 self.remote
101 .translate_channel_messages(
102 &team_id,
103 conversation_id,
104 &root_id,
105 to_language,
106 batch,
107 trigger,
108 )
109 .await?,
110 );
111 }
112 }
113 }
114 }
115 Ok(translations)
116 }
117
118 pub async fn translation_languages(&self, locale: &str) -> Result<Vec<Language>> {
119 self.remote.translation_languages(locale).await
120 }
121
122 pub async fn language_settings(&self) -> Result<LanguageSettings> {
123 self.remote.language_settings().await
124 }
125
126 pub async fn patch_language_settings(&self, patch: Value) -> Result<()> {
127 self.remote.patch_language_settings(patch).await
128 }
129
130 pub async fn sound_settings(&self) -> Result<SoundSettings> {
131 self.remote.sound_settings().await
132 }
133
134 pub async fn save_sound_settings(
135 &self,
136 user_mri: &str,
137 previous: &SoundSettings,
138 updated: &SoundSettings,
139 ) -> Result<()> {
140 self.remote
141 .save_sound_settings(user_mri, previous, updated)
142 .await
143 }
144}