Skip to content

Commit

Permalink
Merge pull request #90 from tofubert/feature/mark_all_chats_read
Browse files Browse the repository at this point in the history
add 'M' for marking all rooms as read
  • Loading branch information
tofubert authored Jan 21, 2025
2 parents bf43e4b + 340abff commit f2d7d8f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/backend/nc_room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,11 @@ impl NCRoomInterface for NCRoom {
requester: Arc<Mutex<Requester>>,
) -> Result<(), Box<dyn std::error::Error>> {
use std::cmp::Ordering;

if let Some(last_internal_id) = self.get_last_room_level_message_id() {
if let Some(room) = data_option {
if room.unreadMessages != self.room_data.unreadMessages {
self.update(Some(room), requester).await?;
}
} else if let Some(last_internal_id) = self.get_last_room_level_message_id() {
match message_id.cmp(&last_internal_id) {
Ordering::Greater => {
log::info!(
Expand Down Expand Up @@ -503,6 +506,7 @@ impl NCRoomInterface for NCRoom {
Ordering::Equal => (),
}
}

Ok(())
}

Expand Down
10 changes: 10 additions & 0 deletions src/backend/nc_talk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ pub trait NCBackend: Debug + Send {
&self,
token: &Token,
) -> Result<(), Box<dyn std::error::Error>>;
/// Mark all rooms as read, goes over list of unread rooms.
async fn mark_all_rooms_as_read(&self) -> Result<(), Box<dyn std::error::Error>>;
/// Fetch a rooms full history.
async fn fetch_room_history(&mut self, token: &Token) -> Result<(), Box<dyn Error>>;
/// trigger for all threads to be killed.
Expand Down Expand Up @@ -490,6 +492,13 @@ impl<Requester: NCRequestInterface + 'static + std::marker::Sync> NCBackend for
.mark_as_read(Arc::clone(&self.requester))
.await
}
async fn mark_all_rooms_as_read(&self) -> Result<(), Box<dyn std::error::Error>> {
for token in self.get_unread_rooms() {
self.mark_current_room_as_read(&token).await?;
}
Ok(())
}

fn get_room(&self, token: &Token) -> &Self::Room {
&self.rooms[token]
}
Expand Down Expand Up @@ -531,6 +540,7 @@ mock! {
async fn select_room(&mut self, token: &Token) -> Result<Option<(String, usize)>, Box<dyn Error>>;
async fn update_rooms(& mut self, force_update: bool) -> Result<Vec<String>, Box<dyn Error>>;
async fn mark_current_room_as_read(&self, token: &Token) -> Result<(), Box<dyn std::error::Error>>;
async fn mark_all_rooms_as_read(&self) -> Result<(), Box<dyn std::error::Error>>;
async fn fetch_room_history(&mut self, token: &Token) -> Result<(), Box<dyn Error>>;
async fn shutdown(&self) -> Result<(), Box<dyn std::error::Error>>;
}
Expand Down
9 changes: 9 additions & 0 deletions src/ui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ impl<Backend: NCBackend> App<'_, Backend> {
Ok(())
}

pub async fn mark_all_as_read(&mut self) -> Result<(), Box<dyn std::error::Error>> {
self.backend.mark_all_rooms_as_read().await?;
self.notify
.maybe_notify_new_rooms(self.backend.update_rooms(true).await?)?;
self.update_ui()?;
Ok(())
}

fn update_ui(&mut self) -> Result<(), Box<dyn std::error::Error>> {
self.title
.update(self.current_screen, &self.backend, &self.current_room_token);
Expand Down Expand Up @@ -497,6 +505,7 @@ impl<Backend: NCBackend> App<'_, Backend> {
}
KeyCode::Char('k') | KeyCode::Up if key.kind == KeyEventKind::Press => self.scroll_up(),
KeyCode::Char('m') => self.mark_current_as_read().await?,
KeyCode::Char('M') => self.mark_all_as_read().await?,
KeyCode::Char('o') => self.switch_screen(CurrentScreen::Opening),
KeyCode::Char('L') => self.switch_screen(CurrentScreen::Logging),
KeyCode::Char('q') => self.popup = Some(Popup::Exit),
Expand Down
7 changes: 6 additions & 1 deletion src/ui/widget/help_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ impl Widget for &HelpBox {
"mark as read",
"mark current chat as read, when in reading mode.",
]),
Row::new([
"M",
"mark all as read",
"mark all chats as read, when in reading mode.",
]),
Row::new([
"(e|i)",
"edit",
Expand Down Expand Up @@ -117,10 +122,10 @@ mod tests {
"│ f fetch history Force a full │",
"│ ? help enter this he │",
"│ m mark as read mark current │",
"│ M mark all as read mark all chat │",
"│ (e|i) edit enter the edi │",
"│ (u|d) jump scroll scroll up or │",
"│ ESC leave Mode leave help, o │",
"│ Enter send/select Send Message, │",
"│ │",
"└────────────────────────────────────────────┘",
]);
Expand Down

0 comments on commit f2d7d8f

Please sign in to comment.