diff --git a/src/logger.rs b/src/logger.rs index eb4b10b..ed33de5 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -79,6 +79,10 @@ impl MsgStore for Logger { Path::new(vec![*id]) } + async fn msg(&self, id: &usize) -> Option { + self.messages.lock().get(*id).cloned() + } + async fn tree(&self, tree_id: &usize) -> Tree { let msgs = self .messages diff --git a/src/store.rs b/src/store.rs index 24b95d0..9e50eaa 100644 --- a/src/store.rs +++ b/src/store.rs @@ -133,6 +133,7 @@ impl Tree { #[async_trait] pub trait MsgStore { async fn path(&self, id: &M::Id) -> Path; + async fn msg(&self, id: &M::Id) -> Option; async fn tree(&self, tree_id: &M::Id) -> Tree; async fn first_tree_id(&self) -> Option; async fn last_tree_id(&self) -> Option; diff --git a/src/vault/euph.rs b/src/vault/euph.rs index 119baee..40655b4 100644 --- a/src/vault/euph.rs +++ b/src/vault/euph.rs @@ -174,6 +174,18 @@ impl MsgStore for EuphVault { rx.await.unwrap() } + async fn msg(&self, id: &Snowflake) -> Option { + // TODO vault::Error + let (tx, rx) = oneshot::channel(); + let request = EuphRequest::GetMsg { + room: self.room.clone(), + id: *id, + result: tx, + }; + let _ = self.vault.tx.send(request.into()); + rx.await.unwrap() + } + async fn tree(&self, tree_id: &Snowflake) -> Tree { // TODO vault::Error let (tx, rx) = oneshot::channel(); @@ -403,6 +415,11 @@ pub(super) enum EuphRequest { id: Snowflake, result: oneshot::Sender>, }, + GetMsg { + room: String, + id: Snowflake, + result: oneshot::Sender>, + }, GetTree { room: String, root: Snowflake, @@ -506,6 +523,7 @@ impl EuphRequest { } => Self::add_msgs(conn, room, msgs, next_msg, own_user_id), Self::GetLastSpan { room, result } => Self::get_last_span(conn, room, result), Self::GetPath { room, id, result } => Self::get_path(conn, room, id, result), + Self::GetMsg { room, id, result } => Self::get_msg(conn, room, id, result), Self::GetTree { room, root, result } => Self::get_tree(conn, room, root, result), Self::GetFirstTreeId { room, result } => Self::get_first_tree_id(conn, room, result), Self::GetLastTreeId { room, result } => Self::get_last_tree_id(conn, room, result), @@ -900,6 +918,37 @@ impl EuphRequest { Ok(()) } + fn get_msg( + conn: &Connection, + room: String, + id: Snowflake, + result: oneshot::Sender>, + ) -> rusqlite::Result<()> { + let msg = conn + .query_row( + " + SELECT id, parent, time, name, content, seen + FROM euph_msgs + WHERE room = ? + AND id = ? + ", + params![room, WSnowflake(id)], + |row| { + Ok(SmallMessage { + id: row.get::<_, WSnowflake>(0)?.0, + parent: row.get::<_, Option>(1)?.map(|s| s.0), + time: row.get::<_, WTime>(2)?.0, + nick: row.get(3)?, + content: row.get(4)?, + seen: row.get(5)?, + }) + }, + ) + .optional()?; + let _ = result.send(msg); + Ok(()) + } + fn get_tree( conn: &Connection, room: String,