Skip to content

Commit

Permalink
Add "Favorite Chats" category to the chat selector screen
Browse files Browse the repository at this point in the history
Signed-off-by: jrichardsen <jrichardsen@emlix.com>
  • Loading branch information
jrichardsen committed Jan 9, 2025
1 parent a12870b commit 7328ace
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/backend/nc_room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ pub trait NCRoomInterface: Debug + Send + Display + Ord + Default {
fn get_messages(&self) -> &Vec<NCMessage>;
/// Get how many messages are unread.
fn get_unread(&self) -> usize;
/// Check if this Room is a favorite.
fn is_favorite(&self) -> bool;
/// Get the human readable display name of the room.
fn get_display_name(&self) -> &str;
/// Get the if of the last read messages.
Expand Down Expand Up @@ -231,6 +233,10 @@ impl NCRoomInterface for NCRoom {
self.room_data.unreadMessages.as_()
}

fn is_favorite(&self) -> bool {
self.room_data.isFavorite
}

fn get_display_name(&self) -> &str {
&self.room_data.displayName
}
Expand Down
12 changes: 12 additions & 0 deletions src/backend/nc_talk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub trait NCBackend: Debug + Send {
fn get_room(&self, token: &Token) -> &Self::Room;
/// Get a list of tokens of rooms with unread messages.
fn get_unread_rooms(&self) -> Vec<Token>;
/// Get a list of tokens of favorite rooms.
fn get_favorite_rooms(&self) -> Vec<Token>;
/// Get a room token by its Displayname.
fn get_room_by_displayname(&self, name: &str) -> Token;
/// Get a list of direct messages rooms as token, displayname pairs.
Expand Down Expand Up @@ -337,6 +339,15 @@ impl<Requester: NCRequestInterface + 'static + std::marker::Sync> NCBackend for
.collect::<Vec<Token>>()
}

fn get_favorite_rooms(&self) -> Vec<Token> {
self.rooms
.values()
.filter(|room| room.is_favorite())
.sorted()
.map(NCRoomInterface::to_token)
.collect()
}

fn get_room_by_displayname(&self, name: &str) -> Token {
for room in self.rooms.values() {
if room.to_string() == *name {
Expand Down Expand Up @@ -502,6 +513,7 @@ mock! {
fn write_to_log(&mut self) -> Result<(), std::io::Error>;
fn get_room(&self, token: &Token) -> &<MockNCTalk as NCBackend>::Room;
fn get_unread_rooms(&self) -> Vec<Token>;
fn get_favorite_rooms(&self) -> Vec<Token>;
fn get_room_by_displayname(&self, name: &str) -> Token;
fn get_dm_keys_display_name_mapping(&self) -> Vec<(Token, String)>;
fn get_group_keys_display_name_mapping(&self) -> Vec<(Token, String)>;
Expand Down
45 changes: 43 additions & 2 deletions src/ui/widget/chat_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ impl ChatSelector<'_> {
.collect_vec(),
)
.expect("unread duplicate"),
TreeItem::new::<String>(
"favorites".to_string(),
"Favorite Chats".to_string(),
backend
.get_favorite_rooms()
.iter()
.map(|token| {
TreeItem::new_leaf::<String>(
token.to_string(),
backend.get_room(token).get_display_name().into(),
)
})
.collect_vec(),
)
.expect("favorite room name duplicate"),
TreeItem::new::<String>(
"direct".to_string(),
"DMs".to_string(),
Expand Down Expand Up @@ -86,6 +101,20 @@ impl ChatSelector<'_> {
})
.collect_vec(),
)?,
TreeItem::new::<String>(
"favorites".to_string(),
"Favorite Chats".to_string(),
backend
.get_favorite_rooms()
.iter()
.map(|token| {
TreeItem::new_leaf::<String>(
token.to_string(),
backend.get_room(token).get_display_name().into(),
)
})
.collect_vec(),
)?,
TreeItem::new::<String>(
"direct".to_string(),
"DMs".to_string(),
Expand Down Expand Up @@ -161,6 +190,12 @@ mod tests {
.in_sequence(&mut seq)
.return_const(vec![]);

mock_nc_backend
.expect_get_favorite_rooms()
.once()
.in_sequence(&mut seq)
.return_const(vec![]);

mock_nc_backend
.expect_get_dm_keys_display_name_mapping()
.once()
Expand Down Expand Up @@ -191,6 +226,12 @@ mod tests {
.in_sequence(&mut seq)
.return_const(mock_room);

mock_nc_backend
.expect_get_favorite_rooms()
.once()
.in_sequence(&mut seq)
.return_const(vec![]);

mock_nc_backend
.expect_get_dm_keys_display_name_mapping()
.once()
Expand All @@ -215,13 +256,13 @@ mod tests {
let mut expected = Buffer::with_lines([
"┌Chat Section──────────────────────────┐",
"│ Unread Chats │",
"│ Favorite Chats │",
"│ DMs │",
"│ Group │",
"│ │",
"│ │",
"│ │",
"│ │",
"│ │",
"└──────────────────────────────────────┘",
]);
expected.set_style(Rect::new(0, 0, 40, 10), config.theme.default_style());
Expand All @@ -241,12 +282,12 @@ mod tests {
"┌Chat Section──────────────────────────┐",
"│>> ▼ Unread Chats │",
"│ General │",
"│ Favorite Chats │",
"│ ▶ DMs │",
"│ ▶ Group │",
"│ │",
"│ │",
"│ │",
"│ │",
"└──────────────────────────────────────┘",
]);
expected.set_style(Rect::new(0, 0, 40, 10), config.theme.default_style());
Expand Down

0 comments on commit 7328ace

Please sign in to comment.