diff --git a/src/app/memory_ui.rs b/src/app/memory_ui.rs index 17963b7..df1b9b4 100644 --- a/src/app/memory_ui.rs +++ b/src/app/memory_ui.rs @@ -64,7 +64,8 @@ pub struct MemPage { limit: usize, rx_list: Option<((String, String, usize), Receiver)>, rx_over: Option>, - rx_write: Vec>, + /// Changes on their way, with the words typed into 新增 (put back if not kept). + rx_write: Vec<(Receiver, Option)>, need_list: bool, need_over: bool, editing: Option<(i64, String)>, @@ -171,6 +172,15 @@ impl App { } if let Some((q, rx)) = &st.rx { if let Ok(v) = rx.try_recv() { + // Unanswered (the brain unreachable, or too old to have a shelf) is not "the + // person picked none": nothing is ticked, and the brain chooses for itself. + if v.get("error").is_some() || !v["hits"].is_array() { + st.asked.clear(); + st.hits.clear(); + st.toggled.clear(); + st.rx = None; + return; + } st.asked = q.clone(); st.hits = v["hits"].as_array().cloned().unwrap_or_default(); st.ms = v["ms"].as_f64().unwrap_or(0.0); @@ -308,14 +318,24 @@ impl App { } if let Some(rx) = &pg.rx_over { if let Ok(v) = rx.try_recv() { - pg.overview = v; + // An unanswered header says why, not 「記得的事 0」 as if they were gone. + match v["error"].as_str() { + Some(e) => pg.note = e.to_string(), + None => pg.overview = v, + } pg.rx_over = None; } } let mut done = vec![]; - for (i, rx) in pg.rx_write.iter().enumerate() { + for (i, (rx, typed)) in pg.rx_write.iter().enumerate() { if let Ok(v) = rx.try_recv() { pg.note = v["text"].as_str().or(v["error"].as_str()).unwrap_or("").to_string(); + // Not kept: the words go back in the box, to try again. + if v.get("error").is_some() && pg.adding.trim().is_empty() { + if let Some(t) = typed { + pg.adding = t.clone(); + } + } done.push(i); } } @@ -361,7 +381,7 @@ impl App { }); for w in writes { let rx = self.mem_call(w); - pg.rx_write.push(rx); + pg.rx_write.push((rx, None)); } if !close { self.mem_page = Some(pg); @@ -407,8 +427,8 @@ impl App { let enter = a.lost_focus() && ui.input(|i| i.key_pressed(Key::Enter)); if (ui.add(secondary_button("記住")).clicked() || enter) && !pg.adding.trim().is_empty() { let text = std::mem::take(&mut pg.adding); - let rx = self.mem_call(json!({"op": "add", "bot": pg.bot, "text": text})); - pg.rx_write.push(rx); + let rx = self.mem_call(json!({"op": "add", "bot": pg.bot, "text": text.clone()})); + pg.rx_write.push((rx, Some(text))); } }); ui.add_space(8.0); diff --git a/src/remote.rs b/src/remote.rs index 555adb1..8ce0424 100644 --- a/src/remote.rs +++ b/src/remote.rs @@ -647,7 +647,7 @@ pub fn handle(req: Req, via_relay: bool) -> Resp { hub().push(Cmd::DeviceSeen(format!("-{h}"))); Resp::json(&json!({"ok": true})) } - _ => Resp::error(404, "沒有這個網址"), + _ => Resp::error(404, NO_SUCH_PATH), } } @@ -1492,6 +1492,23 @@ pub(crate) fn to_brain(s: &Settings, method: &str, rest: &str, query: &[(&str, S } fn to_brain_raw(s: &Settings, method: &str, rest: &str, query: &[(&str, String)], body: &[u8], secs: u64) -> Result<(u16, Vec), String> { + let (st, bytes) = to_brain_any(s, method, rest, query, body, secs)?; + // The brain's own 「沒有這個網址」 (the account server passes every /api/sync path through): + // it runs an AnyBot from before this was asked of it — say so, not "no such address". + if st == 404 && serde_json::from_slice::(&bytes).map(|v| v["error"] == NO_SUCH_PATH).unwrap_or(false) { + return Err(brain_too_old(&s.brain_name)); + } + Ok((st, bytes)) +} + +const NO_SUCH_PATH: &str = "沒有這個網址"; + +fn brain_too_old(name: &str) -> String { + let name = if name.trim().is_empty() { "大腦" } else { name.trim() }; + format!("{name}的 AnyBot 是舊版,還沒有這個功能。請更新{name}:在那台機器的 AnyBot 資料夾執行 make update") +} + +fn to_brain_any(s: &Settings, method: &str, rest: &str, query: &[(&str, String)], body: &[u8], secs: u64) -> Result<(u16, Vec), String> { if s.brain_id.is_empty() { return Err("沒有選大腦".into()); } @@ -1956,6 +1973,35 @@ mod tests { assert_eq!(ok.status, 200); } + #[test] + fn a_brain_too_old_for_a_request_says_to_update_it() { + // A stand-in account server passing /api/sync/… to a brain from before 記憶 and 傳送: + // its own 「沒有這個網址」 for /memory; for /sync/x a real 404 of a newer brain's. + let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); + let port = l.local_addr().unwrap().port(); + std::thread::spawn(move || { + for c in l.incoming().take(2) { + let mut c = c.unwrap(); + let mut got = vec![]; + let mut buf = [0u8; 4096]; + while !got.windows(4).any(|w| w == b"\r\n\r\n") { + match std::io::Read::read(&mut c, &mut buf) { + Ok(0) | Err(_) => break, + Ok(n) => got.extend_from_slice(&buf[..n]), + } + } + let head = String::from_utf8_lossy(&got).to_string(); + let r = if head.starts_with("GET /relay/hosts/b-old/api/sync/memory ") { Resp::error(404, NO_SUCH_PATH) } else { Resp::error(404, "找不到這個 Bot") }; + httpd::write_response(&mut c, &r); + } + }); + let s = Settings { relay_url: format!("http://127.0.0.1:{port}"), host_token: "t".into(), brain_id: "b-old".into(), brain_name: "雲端大腦".into(), ..Default::default() }; + let e = to_brain(&s, "GET", "/memory", &[], &[], 10).unwrap_err(); + assert!(e.contains("雲端大腦的 AnyBot 是舊版") && e.contains("make update"), "{e}"); + // Any other refusal reads as the brain said it. + assert_eq!(to_brain(&s, "GET", "/x", &[], &[], 10).unwrap_err(), "找不到這個 Bot"); + } + #[test] fn roles_gate_the_sync_and_hands_apis() { // 大腦 sync (mirror) and 裝置連結 (device) are separate doors on the hands machine: