diff --git a/src/export.rs b/src/export.rs index 6a3033e..8dfb8b6 100644 --- a/src/export.rs +++ b/src/export.rs @@ -23,11 +23,11 @@ pub async fn export(vault: &Vault, room: String, file: &Path) -> anyhow::Result< let mut exported_trees = 0; let mut exported_msgs = 0; - let mut tree_id = vault.first_tree().await; + let mut tree_id = vault.first_tree_id().await; while let Some(some_tree_id) = tree_id { let tree = vault.tree(&some_tree_id).await; write_tree(&mut file, &tree, some_tree_id, 0)?; - tree_id = vault.next_tree(&some_tree_id).await; + tree_id = vault.next_tree_id(&some_tree_id).await; exported_trees += 1; exported_msgs += tree.len(); diff --git a/src/logger.rs b/src/logger.rs index 9194c6a..fffe825 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -77,21 +77,21 @@ impl MsgStore for Logger { Tree::new(*root, msgs) } - async fn prev_tree(&self, tree: &usize) -> Option { + async fn prev_tree_id(&self, tree: &usize) -> Option { tree.checked_sub(1) } - async fn next_tree(&self, tree: &usize) -> Option { + async fn next_tree_id(&self, tree: &usize) -> Option { let len = self.messages.lock().len(); tree.checked_add(1).filter(|t| *t < len) } - async fn first_tree(&self) -> Option { + async fn first_tree_id(&self) -> Option { let empty = self.messages.lock().is_empty(); Some(0).filter(|_| !empty) } - async fn last_tree(&self) -> Option { + async fn last_tree_id(&self) -> Option { self.messages.lock().len().checked_sub(1) } } diff --git a/src/store.rs b/src/store.rs index e0898da..f400553 100644 --- a/src/store.rs +++ b/src/store.rs @@ -142,8 +142,8 @@ impl Tree { pub trait MsgStore { async fn path(&self, id: &M::Id) -> Path; async fn tree(&self, root: &M::Id) -> Tree; - async fn prev_tree(&self, tree: &M::Id) -> Option; - async fn next_tree(&self, tree: &M::Id) -> Option; - async fn first_tree(&self) -> Option; - async fn last_tree(&self) -> Option; + async fn prev_tree_id(&self, tree: &M::Id) -> Option; + async fn next_tree_id(&self, tree: &M::Id) -> Option; + async fn first_tree_id(&self) -> Option; + async fn last_tree_id(&self) -> Option; } diff --git a/src/ui/chat/tree/cursor.rs b/src/ui/chat/tree/cursor.rs index dff3988..f299707 100644 --- a/src/ui/chat/tree/cursor.rs +++ b/src/ui/chat/tree/cursor.rs @@ -85,7 +85,7 @@ impl> InnerTreeViewState { } else if tree.parent(id).is_none() { // We're at the root of our tree, so we need to move to the root of // the previous tree. - if let Some(prev_tree_id) = store.prev_tree(tree.root()).await { + if let Some(prev_tree_id) = store.prev_tree_id(tree.root()).await { *tree = store.tree(&prev_tree_id).await; *id = prev_tree_id; true @@ -107,7 +107,7 @@ impl> InnerTreeViewState { } else if tree.parent(id).is_none() { // We're at the root of our tree, so we need to move to the root of // the next tree. - if let Some(next_tree_id) = store.next_tree(tree.root()).await { + if let Some(next_tree_id) = store.next_tree_id(tree.root()).await { *tree = store.tree(&next_tree_id).await; *id = next_tree_id; true @@ -157,7 +157,7 @@ impl> InnerTreeViewState { pub async fn move_cursor_up(&mut self) { match &mut self.cursor { Cursor::Bottom => { - if let Some(last_tree_id) = self.store.last_tree().await { + if let Some(last_tree_id) = self.store.last_tree_id().await { let tree = self.store.tree(&last_tree_id).await; let mut id = last_tree_id; while Self::find_last_child(&tree, &mut id) {} @@ -186,8 +186,8 @@ impl> InnerTreeViewState { } pub async fn move_cursor_to_top(&mut self) { - if let Some(tree_id) = self.store.first_tree().await { - self.cursor = Cursor::Msg(tree_id); + if let Some(first_tree_id) = self.store.first_tree_id().await { + self.cursor = Cursor::Msg(first_tree_id); self.make_cursor_visible = true; } } diff --git a/src/ui/chat/tree/layout.rs b/src/ui/chat/tree/layout.rs index 4f6155f..2e5fc71 100644 --- a/src/ui/chat/tree/layout.rs +++ b/src/ui/chat/tree/layout.rs @@ -11,7 +11,7 @@ use super::{util, InnerTreeViewState}; impl> InnerTreeViewState { async fn cursor_path(&self, cursor: &Cursor) -> Path { match cursor { - Cursor::Bottom => match self.store.last_tree().await { + Cursor::Bottom => match self.store.last_tree_id().await { Some(id) => Path::new(vec![id]), None => Path::new(vec![M::last_possible_id()]), }, @@ -217,9 +217,9 @@ impl> InnerTreeViewState { async fn expand_blocks_up(&self, frame: &mut Frame, blocks: &mut Blocks) { while blocks.top_line > 0 { let tree_id = if let Some((root_top, _)) = &blocks.roots { - self.store.prev_tree(root_top).await + self.store.prev_tree_id(root_top).await } else { - self.store.last_tree().await + self.store.last_tree_id().await }; if let Some(tree_id) = tree_id { @@ -234,7 +234,7 @@ impl> InnerTreeViewState { async fn expand_blocks_down(&self, frame: &mut Frame, blocks: &mut Blocks) { while blocks.bottom_line < frame.size().height as i32 { let tree_id = if let Some((_, root_bot)) = &blocks.roots { - self.store.next_tree(root_bot).await + self.store.next_tree_id(root_bot).await } else { // We assume that a Blocks without roots is at the bottom of the // room's history. Therefore, there are no more messages below. diff --git a/src/vault/euph.rs b/src/vault/euph.rs index a8f02ed..5c286ce 100644 --- a/src/vault/euph.rs +++ b/src/vault/euph.rs @@ -88,7 +88,7 @@ impl Vault { pub async fn euph_rooms(&self) -> Vec { // TODO vault::Error let (tx, rx) = oneshot::channel(); - let request = EuphRequest::Rooms { result: tx }; + let request = EuphRequest::GetRooms { result: tx }; let _ = self.tx.send(request.into()); rx.await.unwrap() } @@ -139,7 +139,7 @@ impl EuphVault { pub async fn last_span(&self) -> Option<(Option, Option)> { // TODO vault::Error let (tx, rx) = oneshot::channel(); - let request = EuphRequest::LastSpan { + let request = EuphRequest::GetLastSpan { room: self.room.clone(), result: tx, }; @@ -153,7 +153,7 @@ impl MsgStore for EuphVault { async fn path(&self, id: &Snowflake) -> Path { // TODO vault::Error let (tx, rx) = oneshot::channel(); - let request = EuphRequest::Path { + let request = EuphRequest::GetPath { room: self.room.clone(), id: *id, result: tx, @@ -165,7 +165,7 @@ impl MsgStore for EuphVault { async fn tree(&self, root: &Snowflake) -> Tree { // TODO vault::Error let (tx, rx) = oneshot::channel(); - let request = EuphRequest::Tree { + let request = EuphRequest::GetTree { room: self.room.clone(), root: *root, result: tx, @@ -174,10 +174,10 @@ impl MsgStore for EuphVault { rx.await.unwrap() } - async fn prev_tree(&self, root: &Snowflake) -> Option { + async fn prev_tree_id(&self, root: &Snowflake) -> Option { // TODO vault::Error let (tx, rx) = oneshot::channel(); - let request = EuphRequest::PrevTree { + let request = EuphRequest::GetPrevTreeId { room: self.room.clone(), root: *root, result: tx, @@ -186,10 +186,10 @@ impl MsgStore for EuphVault { rx.await.unwrap() } - async fn next_tree(&self, root: &Snowflake) -> Option { + async fn next_tree_id(&self, root: &Snowflake) -> Option { // TODO vault::Error let (tx, rx) = oneshot::channel(); - let request = EuphRequest::NextTree { + let request = EuphRequest::GetNextTreeId { room: self.room.clone(), root: *root, result: tx, @@ -198,10 +198,10 @@ impl MsgStore for EuphVault { rx.await.unwrap() } - async fn first_tree(&self) -> Option { + async fn first_tree_id(&self) -> Option { // TODO vault::Error let (tx, rx) = oneshot::channel(); - let request = EuphRequest::FirstTree { + let request = EuphRequest::GetFirstTreeId { room: self.room.clone(), result: tx, }; @@ -209,10 +209,10 @@ impl MsgStore for EuphVault { rx.await.unwrap() } - async fn last_tree(&self) -> Option { + async fn last_tree_id(&self) -> Option { // TODO vault::Error let (tx, rx) = oneshot::channel(); - let request = EuphRequest::LastTree { + let request = EuphRequest::GetLastTreeId { room: self.room.clone(), result: tx, }; @@ -222,7 +222,10 @@ impl MsgStore for EuphVault { } pub(super) enum EuphRequest { - Rooms { + /////////// + // Rooms // + /////////// + GetRooms { result: oneshot::Sender>, }, Join { @@ -232,6 +235,10 @@ pub(super) enum EuphRequest { Delete { room: String, }, + + ////////////// + // Messages // + ////////////// AddMsg { room: String, msg: Message, @@ -242,35 +249,35 @@ pub(super) enum EuphRequest { msgs: Vec, next_msg: Option, }, - LastSpan { + GetLastSpan { room: String, result: oneshot::Sender, Option)>>, }, - Path { + GetPath { room: String, id: Snowflake, result: oneshot::Sender>, }, - Tree { + GetTree { room: String, root: Snowflake, result: oneshot::Sender>, }, - PrevTree { + GetPrevTreeId { room: String, root: Snowflake, result: oneshot::Sender>, }, - NextTree { + GetNextTreeId { room: String, root: Snowflake, result: oneshot::Sender>, }, - FirstTree { + GetFirstTreeId { room: String, result: oneshot::Sender>, }, - LastTree { + GetLastTreeId { room: String, result: oneshot::Sender>, }, @@ -279,7 +286,7 @@ pub(super) enum EuphRequest { impl EuphRequest { pub(super) fn perform(self, conn: &mut Connection) { let result = match self { - EuphRequest::Rooms { result } => Self::rooms(conn, result), + EuphRequest::GetRooms { result } => Self::get_rooms(conn, result), EuphRequest::Join { room, time } => Self::join(conn, room, time), EuphRequest::Delete { room } => Self::delete(conn, room), EuphRequest::AddMsg { @@ -292,17 +299,21 @@ impl EuphRequest { msgs, next_msg, } => Self::add_msgs(conn, room, msgs, next_msg), - EuphRequest::LastSpan { room, result } => Self::last_span(conn, room, result), - EuphRequest::Path { room, id, result } => Self::path(conn, room, id, result), - EuphRequest::Tree { room, root, result } => Self::tree(conn, room, root, result), - EuphRequest::PrevTree { room, root, result } => { - Self::prev_tree(conn, room, root, result) + EuphRequest::GetLastSpan { room, result } => Self::get_last_span(conn, room, result), + EuphRequest::GetPath { room, id, result } => Self::get_path(conn, room, id, result), + EuphRequest::GetTree { room, root, result } => Self::get_tree(conn, room, root, result), + EuphRequest::GetPrevTreeId { room, root, result } => { + Self::get_prev_tree_id(conn, room, root, result) } - EuphRequest::NextTree { room, root, result } => { - Self::next_tree(conn, room, root, result) + EuphRequest::GetNextTreeId { room, root, result } => { + Self::get_next_tree_id(conn, room, root, result) + } + EuphRequest::GetFirstTreeId { room, result } => { + Self::get_first_tree_id(conn, room, result) + } + EuphRequest::GetLastTreeId { room, result } => { + Self::get_last_tree_id(conn, room, result) } - EuphRequest::FirstTree { room, result } => Self::first_tree(conn, room, result), - EuphRequest::LastTree { room, result } => Self::last_tree(conn, room, result), }; if let Err(e) = result { // If an error occurs here, the rest of the UI will likely panic and @@ -313,7 +324,10 @@ impl EuphRequest { } } - fn rooms(conn: &mut Connection, result: oneshot::Sender>) -> rusqlite::Result<()> { + fn get_rooms( + conn: &mut Connection, + result: oneshot::Sender>, + ) -> rusqlite::Result<()> { let rooms = conn .prepare( " @@ -533,7 +547,7 @@ impl EuphRequest { Ok(()) } - fn last_span( + fn get_last_span( conn: &Connection, room: String, result: oneshot::Sender, Option)>>, @@ -554,7 +568,7 @@ impl EuphRequest { Ok(()) } - fn path( + fn get_path( conn: &Connection, room: String, id: Snowflake, @@ -584,7 +598,7 @@ impl EuphRequest { Ok(()) } - fn tree( + fn get_tree( conn: &Connection, room: String, root: Snowflake, @@ -624,7 +638,7 @@ impl EuphRequest { Ok(()) } - fn prev_tree( + fn get_prev_tree_id( conn: &Connection, room: String, root: Snowflake, @@ -647,7 +661,7 @@ impl EuphRequest { Ok(()) } - fn next_tree( + fn get_next_tree_id( conn: &Connection, room: String, root: Snowflake, @@ -670,7 +684,7 @@ impl EuphRequest { Ok(()) } - fn first_tree( + fn get_first_tree_id( conn: &Connection, room: String, result: oneshot::Sender>, @@ -691,7 +705,7 @@ impl EuphRequest { Ok(()) } - fn last_tree( + fn get_last_tree_id( conn: &Connection, room: String, result: oneshot::Sender>,