Update vault on send events and replies
This commit is contained in:
parent
4e4eb036e0
commit
57351f65be
3 changed files with 102 additions and 62 deletions
|
|
@ -69,13 +69,21 @@ pub struct Joining {
|
|||
}
|
||||
|
||||
impl Joining {
|
||||
fn on_data(&mut self, data: Data) {
|
||||
fn on_data(&mut self, data: &Data) -> anyhow::Result<()> {
|
||||
match data {
|
||||
Data::BounceEvent(p) => self.bounce = Some(p),
|
||||
Data::HelloEvent(p) => self.hello = Some(p),
|
||||
Data::SnapshotEvent(p) => self.snapshot = Some(p),
|
||||
Data::BounceEvent(p) => self.bounce = Some(p.clone()),
|
||||
Data::HelloEvent(p) => self.hello = Some(p.clone()),
|
||||
Data::SnapshotEvent(p) => self.snapshot = Some(p.clone()),
|
||||
d @ (Data::JoinEvent(_)
|
||||
| Data::NetworkEvent(_)
|
||||
| Data::NickEvent(_)
|
||||
| Data::EditMessageEvent(_)
|
||||
| Data::PartEvent(_)
|
||||
| Data::PmInitiateEvent(_)
|
||||
| Data::SendEvent(_)) => bail!("unexpected {}", d.packet_type()),
|
||||
_ => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn joined(&self) -> Option<Joined> {
|
||||
|
|
@ -105,13 +113,14 @@ pub struct Joined {
|
|||
}
|
||||
|
||||
impl Joined {
|
||||
fn on_data(&mut self, data: Data) {
|
||||
fn on_data(&mut self, data: &Data) {
|
||||
match data {
|
||||
Data::JoinEvent(p) => {
|
||||
self.listing.insert(p.0.id.clone(), p.0);
|
||||
self.listing.insert(p.0.id.clone(), p.0.clone());
|
||||
}
|
||||
Data::SendEvent(p) => {
|
||||
self.listing.insert(p.0.sender.id.clone(), p.0.sender);
|
||||
self.listing
|
||||
.insert(p.0.sender.id.clone(), p.0.sender.clone());
|
||||
}
|
||||
Data::PartEvent(p) => {
|
||||
self.listing.remove(&p.0.id);
|
||||
|
|
@ -125,12 +134,12 @@ impl Joined {
|
|||
}
|
||||
Data::NickEvent(p) => {
|
||||
if let Some(session) = self.listing.get_mut(&p.id) {
|
||||
session.name = p.to;
|
||||
session.name = p.to.clone();
|
||||
}
|
||||
}
|
||||
Data::NickReply(p) => {
|
||||
assert_eq!(self.session.id, p.id);
|
||||
self.session.name = p.to;
|
||||
self.session.name = p.to.clone();
|
||||
}
|
||||
// The who reply is broken and can't be trusted right now, so we'll
|
||||
// not even look at it.
|
||||
|
|
@ -253,13 +262,6 @@ impl State {
|
|||
self.replies.complete(id, packet.content.clone());
|
||||
}
|
||||
|
||||
// Shovel events and successful replies into self.packet_tx. Assumes
|
||||
// that no even ever errors and that erroring replies are not
|
||||
// interesting.
|
||||
if let Ok(data) = &packet.content {
|
||||
self.packet_tx.send(data.clone())?;
|
||||
}
|
||||
|
||||
// Play a game of table tennis
|
||||
match &packet.content {
|
||||
Ok(Data::PingReply(p)) => self.last_euph_pong = p.time,
|
||||
|
|
@ -272,10 +274,10 @@ impl State {
|
|||
}
|
||||
|
||||
// Update internal state
|
||||
if let Ok(data) = packet.content {
|
||||
if let Ok(data) = &packet.content {
|
||||
match &mut self.status {
|
||||
Status::Joining(joining) => {
|
||||
joining.on_data(data);
|
||||
joining.on_data(data)?;
|
||||
if let Some(joined) = joining.joined() {
|
||||
self.status = Status::Joined(joined);
|
||||
}
|
||||
|
|
@ -284,6 +286,13 @@ impl State {
|
|||
}
|
||||
}
|
||||
|
||||
// Shovel events and successful replies into self.packet_tx. Assumes
|
||||
// that no even ever errors and that erroring replies are not
|
||||
// interesting.
|
||||
if let Ok(data) = packet.content {
|
||||
self.packet_tx.send(data)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ use tokio_tungstenite::tungstenite;
|
|||
use crate::ui::UiEvent;
|
||||
use crate::vault::EuphVault;
|
||||
|
||||
use super::api::Data;
|
||||
use super::api::{Data, Snowflake};
|
||||
use super::conn::{self, ConnRx, ConnTx, Status};
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
|
|
@ -33,6 +33,7 @@ struct State {
|
|||
vault: EuphVault,
|
||||
ui_event_tx: mpsc::UnboundedSender<UiEvent>,
|
||||
conn_tx: Option<ConnTx>,
|
||||
last_msg_id: Option<Snowflake>,
|
||||
}
|
||||
|
||||
impl State {
|
||||
|
|
@ -93,7 +94,10 @@ impl State {
|
|||
while let Some(event) = event_rx.recv().await {
|
||||
match event {
|
||||
Event::Connected(conn_tx) => self.conn_tx = Some(conn_tx),
|
||||
Event::Disconnected => self.conn_tx = None,
|
||||
Event::Disconnected => {
|
||||
self.conn_tx = None;
|
||||
self.last_msg_id = None;
|
||||
}
|
||||
Event::Data(data) => self.on_data(data).await?,
|
||||
Event::Status(reply_tx) => self.on_status(reply_tx).await,
|
||||
}
|
||||
|
|
@ -101,7 +105,7 @@ impl State {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
async fn on_data(&self, data: Data) -> anyhow::Result<()> {
|
||||
async fn on_data(&mut self, data: Data) -> anyhow::Result<()> {
|
||||
match data {
|
||||
Data::BounceEvent(_) => {
|
||||
error!("e&{}: auth not implemented", self.name);
|
||||
|
|
@ -135,9 +139,15 @@ impl State {
|
|||
self.name, d.from_nick, d.from_room
|
||||
);
|
||||
}
|
||||
Data::SendEvent(_) => {}
|
||||
Data::SendEvent(d) => {
|
||||
let id = d.0.id;
|
||||
self.vault.add_message(d.0, self.last_msg_id);
|
||||
self.last_msg_id = Some(id);
|
||||
let _ = self.ui_event_tx.send(UiEvent::Redraw);
|
||||
}
|
||||
Data::SnapshotEvent(d) => {
|
||||
info!("e&{}: successfully joined", self.name);
|
||||
self.last_msg_id = d.log.last().map(|m| m.id);
|
||||
self.vault.add_messages(d.log, None);
|
||||
let _ = self.ui_event_tx.send(UiEvent::Redraw);
|
||||
}
|
||||
|
|
@ -145,6 +155,12 @@ impl State {
|
|||
self.vault.add_messages(d.log, d.before);
|
||||
let _ = self.ui_event_tx.send(UiEvent::Redraw);
|
||||
}
|
||||
Data::SendReply(d) => {
|
||||
let id = d.0.id;
|
||||
self.vault.add_message(d.0, self.last_msg_id);
|
||||
self.last_msg_id = Some(id);
|
||||
let _ = self.ui_event_tx.send(UiEvent::Redraw);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
Ok(())
|
||||
|
|
@ -181,6 +197,7 @@ impl Room {
|
|||
vault,
|
||||
ui_event_tx,
|
||||
conn_tx: None,
|
||||
last_msg_id: None,
|
||||
};
|
||||
|
||||
task::spawn(state.run(canary_rx, event_tx.clone(), event_rx));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue