BlueGit

ict rusty-teams

Files

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

translate_sync.rs

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