1
0
mirror of synced 2025-01-09 07:22:17 +00:00

Nomos chat fixes (#555)

* Make chat messages unique

Since DA will rightfully ignore duplicated messages, we need to add a nonce to make sure
every message is unique, so that a user can send the same message twice.
This nonce is randomly generated for simplicity.

* Clear in_flight status upon error on chat app
This commit is contained in:
Giacomo Pasini 2024-01-08 15:40:19 +01:00 committed by GitHub
parent 285300f365
commit b7d1fd9256
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 22 deletions

View File

@ -36,3 +36,4 @@ crossterm = "0.27"
ratatui = "0.24"
tui-input = "0.8"
ansi-to-tui = "3"
rand = "0.8"

View File

@ -160,9 +160,8 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) {
terminal.draw(|f| ui::ui(f, &app)).unwrap();
if let Ok(update) = app.status_updates.try_recv() {
if let Status::Done = update {
if let Status::Done | Status::Err(_) = update {
app.message_in_flight = false;
app.message_status = None;
}
app.message_status = Some(update);
app.last_updated = Instant::now();
@ -191,6 +190,7 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) {
wire::serialize(&ChatMessage {
author: app.username.clone().unwrap(),
message: app.input.value().into(),
_nonce: rand::random(),
})
.unwrap()
.into(),
@ -223,6 +223,9 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) {
struct ChatMessage {
author: String,
message: String,
// Since DA will rightfully ignore duplicated messages, we need to add a nonce to make sure
// every message is unique. This is randomly generated for simplicity.
_nonce: u64,
}
#[tokio::main]

View File

@ -124,7 +124,12 @@ fn render_messages(f: &mut Frame, app: &App, rect: Rect) {
let messages: Vec<ListItem> = app
.messages
.iter()
.map(|ChatMessage { author, message }| {
.map(
|ChatMessage {
author,
message,
_nonce,
}| {
let content = if author == app.username.as_ref().unwrap() {
static MARGIN: usize = 2;
// pad to make it appear aligned on the right
@ -142,7 +147,8 @@ fn render_messages(f: &mut Frame, app: &App, rect: Rect) {
.alignment(Alignment::Left)
};
ListItem::new(content)
})
},
)
.collect();
let messages =
List::new(messages).block(Block::default().borders(Borders::ALL).title("Messages"));