Skip to content

Commit

Permalink
Merge pull request #443 from betrusted-io/chat-status
Browse files Browse the repository at this point in the history
WIP: Chat status
  • Loading branch information
bunnie authored Nov 3, 2023
2 parents 11fd5fd + 5aefcac commit d462412
Show file tree
Hide file tree
Showing 13 changed files with 411 additions and 44 deletions.
38 changes: 35 additions & 3 deletions apps/mtxchat/locales/i18n.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
{
"mtxchat.busy.connecting": {
"en": "Connecting...",
"en-tts": "Connecting..."
},
"mtxchat.busy.updating": {
"en": "Updating...",
"en-tts": "Updating..."
},
"mtxchat.busy.sending": {
"en": "Sending...",
"en-tts": "Sending..."
},
"mtxchat.busy.room_id": {
"en": "Getting room ID...",
"en-tts": "Getting room ID..."
},
"mtxchat.busy.login": {
"en": "Logging in...",
"en-tts": "Logging in..."
},
"mtxchat.busy.rx_events": {
"en": "Receiving events...",
"en-tts": "Receiving events..."
},
"mtxchat.busy.new_listen": {
"en": "Syncing all events from server...",
"en-tts": "Syncing all events from server..."
},
"mtxchat.clock.warning": {
"en": "WARNING: clock not set",
"en-tts": "WARNING: clock not set",
"fr": "AVERTISSEMENT : horloge non réglée",
"ja": "WARNING: clock not set *EN*",
"zh": "WARNING: clock not set *EN*"
},
"mtxchat.status.default" : {
"en": "Use ← for login menu",
"en-tts": "Use ← for login menu"
},
"mtxchat.filter.failed": {
"en": "error: could not create filter",
"en-tts": "error: could not create filter",
Expand Down Expand Up @@ -225,9 +257,9 @@
"ja": "WiFi not connected. Entering read-only mode *EN*",
"zh": "WiFi not connected. Entering read-only mode *EN*"
},
"mtxchat.listen.patience": {
"en": "It takes a while to get new events from the [matrix] server.\nPlease be patient :-)",
"en-tts": "It takes a while to get new events from the [matrix] server.\nPlease be patient."
"mtxchat.wifi.warning_status": {
"en": "No connection! Read-only.",
"en-tts": "No connection! Read-only."
},
"mtxchat.close.item": {
"en": "Close menu",
Expand Down
58 changes: 39 additions & 19 deletions apps/mtxchat/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use listen::listen;
use locales::t;
use modals::Modals;
use pddb::Pddb;
use ticktimer_server::Ticktimer;
use std::fmt::Write as _;
use std::io::{Error, ErrorKind, Read, Write as StdWrite};
use std::sync::Arc;
Expand All @@ -35,8 +36,9 @@ const USER_DOMAIN_KEY: &str = "user_domain";

const DOMAIN_MATRIX: &str = "matrix.org";

const MTX_LONG_TIMEOUT: i32 = 60000; // ms
const WIFI_TIMEOUT: u32 = 10; // seconds
const MTX_LONG_TIMEOUT_MS: i32 = 60000; // ms
const WIFI_TIMEOUT_MS: u32 = 30_000; // ms
const SEND_RETRIES: usize = 3;

pub const CLOCK_NOT_SET_ID: usize = 1;
pub const PDDB_NOT_MOUNTED_ID: usize = 2;
Expand Down Expand Up @@ -83,6 +85,7 @@ pub struct MtxChat<'a> {
modals: Modals,
new_username: bool,
new_room: bool,
tt: Ticktimer,
}
impl<'a> MtxChat<'a> {
pub fn new(chat: &Chat) -> MtxChat {
Expand All @@ -91,6 +94,8 @@ impl<'a> MtxChat<'a> {
let trng = Trng::new(&xns).unwrap();
let pddb = pddb::Pddb::new();
pddb.try_mount();
let status = t!("mtxchat.status.default", locales::LANG).to_owned();
chat.set_status_text(&status);
MtxChat {
chat: chat,
trng: trng,
Expand All @@ -113,6 +118,7 @@ impl<'a> MtxChat<'a> {
modals: modals,
new_username: false,
new_room: false,
tt: ticktimer_server::Ticktimer::new().unwrap(),
}
}

Expand Down Expand Up @@ -248,9 +254,8 @@ impl<'a> MtxChat<'a> {
self.listen();
if self.new_room {
self.new_room = false;
self.modals
.show_notification(t!("mtxchat.listen.patience", locales::LANG), None)
.expect("notification failed");
self.chat.set_status_text(t!("mtxchat.busy.new_listen", locales::LANG));
self.chat.set_busy_state(true);
}
return true;
} else {
Expand All @@ -277,6 +282,8 @@ impl<'a> MtxChat<'a> {
}

pub fn login(&mut self) -> bool {
self.chat.set_status_text(t!("mtxchat.busy.login", locales::LANG));
self.chat.set_busy_state(true);
self.token = self.get(TOKEN_KEY).unwrap_or(None);
self.logged_in = false;

Expand Down Expand Up @@ -335,6 +342,7 @@ impl<'a> MtxChat<'a> {
self.unset(USER_DOMAIN_KEY)
.expect("failed to unset user domain");
}
self.chat.set_busy_state(false);
self.logged_in
}

Expand Down Expand Up @@ -433,12 +441,16 @@ impl<'a> MtxChat<'a> {
(true, Some(token), Some(user_domain), Some(room_alias)) => {
let mut url = Url::parse("https://matrix.org").unwrap();
url.set_host(Some(user_domain)).expect("failed to set host");
self.chat.set_status_text(t!("mtxchat.busy.room_id", locales::LANG));
self.chat.set_busy_state(true);
if let Some(room_id) =
web::get_room_id(&mut url, &room_alias, &token, &mut self.agent)
{
self.set_debug(ROOM_ID_KEY, &room_id);
self.chat.set_busy_state(false);
return Some(room_id);
} else {
self.chat.set_busy_state(false);
"failed to get room_id"
}
}
Expand Down Expand Up @@ -626,14 +638,25 @@ impl<'a> MtxChat<'a> {
&self.room_id,
) {
(true, Some(token), Some(user_domain), Some(room_id)) => {
self.chat.set_status_text(t!("mtxchat.busy.sending", locales::LANG));
self.chat.set_busy_state(true);
log::info!("txn_id = {}", txn_id);
let mut url = Url::parse("https://matrix.org").unwrap();
url.set_host(Some(user_domain)).expect("failed to set host");
if web::send_message(&mut url, &room_id, &text, &txn_id, token, &mut self.agent) {
let mut success = false;
for _ in 0..SEND_RETRIES {
if web::send_message(&mut url, &room_id, &text, &txn_id, token, &mut self.agent) {
success = true;
break;
}
}
let r = if success {
"SENT"
} else {
"FAILED TO SEND"
}
};
self.chat.set_busy_state(false);
r
}
(false, _, _, _) => "Not logged in",
(_, None, _, _) => "No token set",
Expand Down Expand Up @@ -661,25 +684,22 @@ impl<'a> MtxChat<'a> {

while self.wifi_try_modal() {
self.netmgr.connection_manager_wifi_on_and_run().unwrap();
self.modals
.start_progress("Connecting ...", 0, 10, 0)
.expect("no progress bar");
let tt = ticktimer_server::Ticktimer::new().unwrap();
for wait in 0..WIFI_TIMEOUT {
tt.sleep_ms(1000).unwrap();
self.modals
.update_progress(wait)
.expect("no progress update");
self.chat.set_status_text(t!("mtxchat.busy.connecting", locales::LANG));
self.chat.set_busy_state(true);
let start = self.tt.elapsed_ms();
while self.tt.elapsed_ms() - start < WIFI_TIMEOUT_MS as u64 {
if let Some(conf) = self.netmgr.get_ipv4_config() {
if conf.dhcp == com_rs::DhcpState::Bound {
self.modals
.finish_progress()
.expect("failed progress finish");
self.chat.set_busy_state(false);
return true;
}
}
self.tt.sleep_ms(1000).unwrap();
}
self.modals.show_notification(t!("mtxchat.wifi.warning", locales::LANG), None).unwrap();
}
self.chat.set_busy_state(false);
self.chat.set_status_text(t!("mtxchat.wifi.warning_status", locales::LANG));
false
}

Expand Down
25 changes: 12 additions & 13 deletions apps/mtxchat/src/listen.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{get_username, web, MTX_LONG_TIMEOUT};
use crate::{get_username, web, MTX_LONG_TIMEOUT_MS};
use chat::ChatOp;
use modals::Modals;
use std::sync::Arc;
use tls::xtls::TlsConnector;
use url::Url;
use xous::CID;
use xous_ipc::Buffer;
use locales::t;

pub fn listen(
url: &mut Url,
Expand All @@ -16,9 +16,7 @@ pub fn listen(
dialogue_id: &str,
chat_cid: CID,
) {
let xns = xous_names::XousNames::new().unwrap();
let modals = Modals::new(&xns).expect("can't connect to Modals server");
log::info!("client_sync for {} ms...", MTX_LONG_TIMEOUT);
log::info!("client_sync for {} ms...", MTX_LONG_TIMEOUT_MS);

let mut agent = ureq::builder()
.tls_connector(Arc::new(TlsConnector{}))
Expand All @@ -27,7 +25,7 @@ pub fn listen(
url,
filter,
since,
MTX_LONG_TIMEOUT,
MTX_LONG_TIMEOUT_MS,
&room_id,
&token,
&mut agent,
Expand All @@ -38,9 +36,8 @@ pub fn listen(
// TODO resolve suspected race condition
// This progress modal is masking a bug by slowing the loop down
// Precursor "Guru Mediation" `voilated: nonNull::new_unchecked`
modals
.start_progress("Receiving events ...", 0, events.len() as u32, 0)
.expect("no progress bar");
chat::cf_set_status_text(chat_cid, t!("mtxchat.busy.rx_events", locales::LANG));
chat::cf_set_busy_state(chat_cid, true);
let mut event_count = 0;
for event in events {
let sender = event.sender.unwrap_or("anon".to_string());
Expand All @@ -58,12 +55,14 @@ pub fn listen(
}
.expect("failed to convert post into buffer");
event_count += 1;
modals
.update_progress(event_count)
.expect("no progress update");
chat::cf_set_status_text(chat_cid,
&format!("{} {}",
t!("mtxchat.busy.rx_events", locales::LANG),
event_count)
);
}
}
modals.finish_progress().expect("failed progress finish");
chat::cf_set_busy_state(chat_cid, false);
// trigger the chat ui to save the dialogue to the pddb
xous::send_message(
chat_cid,
Expand Down
4 changes: 4 additions & 0 deletions libs/chat/locales/i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
"en": "no dialogues available to read",
"en-tts": "no dialogues available to read"
},
"chat.status.initial" : {
"en": "Use ← to raise menu",
"en-tts": "Use ← to raise menu"
},
"chat.help.navigation": {
"en": "use ↑ & ↓ to scroll thru old posts\nuse ← & → to show menus\nF1-F4 dont do anything yet.",
"en-tts": "use ↑ & ↓ to scroll thru old posts\nuse ← & → to show menus\nF1-F4 dont do anything yet."
Expand Down
22 changes: 22 additions & 0 deletions libs/chat/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,27 @@ pub enum ChatOp {
/// Find a Post by timestamp and Author
PostFind,
PostFlag,
/// Set status bar text
SetStatusText,
/// Run or stop the busy animation.
SetBusyAnimationState,
/// Set the status idle text (to be shown when exiting all busy states)
SetStatusIdleText,
/// Update just the state of the busy animation, if any. Internal opcode.
/// Will skip the update if called too often.
UpdateBusy,
/// Force update the busy bar, without rate throttling. Internal opcode.
UpdateBusyForced,
/// exit the application
Quit,
}

#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
pub(crate) enum BusyAnimOp {
Start,
Pump,
}

#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
pub enum IconOp {
PostMenu = 0,
Expand Down Expand Up @@ -120,3 +137,8 @@ pub enum AuthorFlag {
Hidden,
Right,
}

#[derive(Archive, Serialize, Deserialize, Debug)]
pub struct BusyMessage {
pub busy_msg: xous_ipc::String<128>,
}
Loading

0 comments on commit d462412

Please sign in to comment.