version_0_1_1-alpha
This commit is contained in:
parent
c6c9a1674a
commit
7bf5526f27
|
|
@ -64,7 +64,8 @@ pub struct MemPage {
|
||||||
limit: usize,
|
limit: usize,
|
||||||
rx_list: Option<((String, String, usize), Receiver<Value>)>,
|
rx_list: Option<((String, String, usize), Receiver<Value>)>,
|
||||||
rx_over: Option<Receiver<Value>>,
|
rx_over: Option<Receiver<Value>>,
|
||||||
rx_write: Vec<Receiver<Value>>,
|
/// Changes on their way, with the words typed into 新增 (put back if not kept).
|
||||||
|
rx_write: Vec<(Receiver<Value>, Option<String>)>,
|
||||||
need_list: bool,
|
need_list: bool,
|
||||||
need_over: bool,
|
need_over: bool,
|
||||||
editing: Option<(i64, String)>,
|
editing: Option<(i64, String)>,
|
||||||
|
|
@ -171,6 +172,15 @@ impl App {
|
||||||
}
|
}
|
||||||
if let Some((q, rx)) = &st.rx {
|
if let Some((q, rx)) = &st.rx {
|
||||||
if let Ok(v) = rx.try_recv() {
|
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.asked = q.clone();
|
||||||
st.hits = v["hits"].as_array().cloned().unwrap_or_default();
|
st.hits = v["hits"].as_array().cloned().unwrap_or_default();
|
||||||
st.ms = v["ms"].as_f64().unwrap_or(0.0);
|
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 Some(rx) = &pg.rx_over {
|
||||||
if let Ok(v) = rx.try_recv() {
|
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;
|
pg.rx_over = None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let mut done = vec![];
|
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() {
|
if let Ok(v) = rx.try_recv() {
|
||||||
pg.note = v["text"].as_str().or(v["error"].as_str()).unwrap_or("").to_string();
|
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);
|
done.push(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -361,7 +381,7 @@ impl App {
|
||||||
});
|
});
|
||||||
for w in writes {
|
for w in writes {
|
||||||
let rx = self.mem_call(w);
|
let rx = self.mem_call(w);
|
||||||
pg.rx_write.push(rx);
|
pg.rx_write.push((rx, None));
|
||||||
}
|
}
|
||||||
if !close {
|
if !close {
|
||||||
self.mem_page = Some(pg);
|
self.mem_page = Some(pg);
|
||||||
|
|
@ -407,8 +427,8 @@ impl App {
|
||||||
let enter = a.lost_focus() && ui.input(|i| i.key_pressed(Key::Enter));
|
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() {
|
if (ui.add(secondary_button("記住")).clicked() || enter) && !pg.adding.trim().is_empty() {
|
||||||
let text = std::mem::take(&mut pg.adding);
|
let text = std::mem::take(&mut pg.adding);
|
||||||
let rx = self.mem_call(json!({"op": "add", "bot": pg.bot, "text": text}));
|
let rx = self.mem_call(json!({"op": "add", "bot": pg.bot, "text": text.clone()}));
|
||||||
pg.rx_write.push(rx);
|
pg.rx_write.push((rx, Some(text)));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ui.add_space(8.0);
|
ui.add_space(8.0);
|
||||||
|
|
|
||||||
|
|
@ -647,7 +647,7 @@ pub fn handle(req: Req, via_relay: bool) -> Resp {
|
||||||
hub().push(Cmd::DeviceSeen(format!("-{h}")));
|
hub().push(Cmd::DeviceSeen(format!("-{h}")));
|
||||||
Resp::json(&json!({"ok": true}))
|
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<u8>), String> {
|
fn to_brain_raw(s: &Settings, method: &str, rest: &str, query: &[(&str, String)], body: &[u8], secs: u64) -> Result<(u16, Vec<u8>), 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::<Value>(&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<u8>), String> {
|
||||||
if s.brain_id.is_empty() {
|
if s.brain_id.is_empty() {
|
||||||
return Err("沒有選大腦".into());
|
return Err("沒有選大腦".into());
|
||||||
}
|
}
|
||||||
|
|
@ -1956,6 +1973,35 @@ mod tests {
|
||||||
assert_eq!(ok.status, 200);
|
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]
|
#[test]
|
||||||
fn roles_gate_the_sync_and_hands_apis() {
|
fn roles_gate_the_sync_and_hands_apis() {
|
||||||
// 大腦 sync (mirror) and 裝置連結 (device) are separate doors on the hands machine:
|
// 大腦 sync (mirror) and 裝置連結 (device) are separate doors on the hands machine:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue