Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add an "invite" order to the settings #395

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/iamb.5
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,26 @@ window.
Defaults to
.Sy ["power",\ "id"] .
.El

The available values are:
.Bl -tag -width Ds
.It Sy favorite
Put favorite rooms before other rooms.
.It Sy lowpriority
Put lowpriority rooms after other rooms.
.It Sy name
Sort rooms by alphabetically ascending room name.
.It Sy alias
Sort rooms by alphabetically ascending canonical room alias.
.It Sy id
Sort rooms by alphabetically ascending Matrix room identifier.
.It Sy unread
Put unread rooms before other rooms.
.It Sy recent
Sort rooms by most recent message timestamp.
.It Sy invite
Put invites before other rooms.
.El
.El

.Ss Example 1: Group room members by ther server first
Expand Down
4 changes: 4 additions & 0 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ pub enum SortFieldRoom {

/// Sort rooms by the timestamps of their most recent messages.
Recent,

/// Sort rooms by whether they are invites.
Invite,
}

/// Fields that users can be sorted by.
Expand Down Expand Up @@ -307,6 +310,7 @@ impl<'de> Visitor<'de> for SortRoomVisitor {
"name" => SortFieldRoom::Name,
"alias" => SortFieldRoom::Alias,
"id" => SortFieldRoom::RoomId,
"invite" => SortFieldRoom::Invite,
_ => {
let msg = format!("Unknown sort field: {value:?}");
return Err(E::custom(msg));
Expand Down
77 changes: 77 additions & 0 deletions src/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use matrix_sdk::{
RoomAliasId,
RoomId,
},
RoomState as MatrixRoomState,
};

use ratatui::{
Expand Down Expand Up @@ -195,6 +196,10 @@ fn room_cmp<T: RoomLikeItem>(a: &T, b: &T, field: &SortFieldRoom) -> Ordering {
// sort larger timestamps towards the top.
some_cmp(a.recent_ts(), b.recent_ts(), |a, b| b.cmp(a))
},
SortFieldRoom::Invite => {
// sort invites before other rooms.
b.is_invite().cmp(&a.is_invite())
},
}
}

Expand Down Expand Up @@ -273,6 +278,7 @@ trait RoomLikeItem {
fn recent_ts(&self) -> Option<&MessageTimeStamp>;
fn alias(&self) -> Option<&RoomAliasId>;
fn name(&self) -> &str;
fn is_invite(&self) -> bool;
}

#[inline]
Expand Down Expand Up @@ -914,6 +920,10 @@ impl RoomLikeItem for GenericChatItem {
fn is_unread(&self) -> bool {
self.unread.is_unread()
}

fn is_invite(&self) -> bool {
self.room().state() == MatrixRoomState::Invited
}
}

impl Display for GenericChatItem {
Expand Down Expand Up @@ -1024,6 +1034,10 @@ impl RoomLikeItem for RoomItem {
fn is_unread(&self) -> bool {
self.unread.is_unread()
}

fn is_invite(&self) -> bool {
self.room().state() == MatrixRoomState::Invited
}
}

impl Display for RoomItem {
Expand Down Expand Up @@ -1124,6 +1138,10 @@ impl RoomLikeItem for DirectItem {
fn is_unread(&self) -> bool {
self.unread.is_unread()
}

fn is_invite(&self) -> bool {
self.room().state() == MatrixRoomState::Invited
}
}

impl Display for DirectItem {
Expand Down Expand Up @@ -1223,6 +1241,10 @@ impl RoomLikeItem for SpaceItem {
// XXX: this needs to check whether the space contains rooms with unread messages
false
}

fn is_invite(&self) -> bool {
self.room().state() == MatrixRoomState::Invited
}
}

impl Display for SpaceItem {
Expand Down Expand Up @@ -1556,6 +1578,7 @@ mod tests {
alias: Option<OwnedRoomAliasId>,
name: &'static str,
unread: UnreadInfo,
invite: bool
}

impl RoomLikeItem for &TestRoomItem {
Expand All @@ -1582,6 +1605,10 @@ mod tests {
fn is_unread(&self) -> bool {
self.unread.is_unread()
}

fn is_invite(&self) -> bool {
self.invite
}
}

#[test]
Expand All @@ -1594,6 +1621,7 @@ mod tests {
alias: Some(room_alias_id!("#room1:example.com").to_owned()),
name: "Z",
unread: UnreadInfo::default(),
invite: false,
};

let room2 = TestRoomItem {
Expand All @@ -1602,6 +1630,7 @@ mod tests {
alias: Some(room_alias_id!("#a:example.com").to_owned()),
name: "Unnamed Room",
unread: UnreadInfo::default(),
invite: false,
};

let room3 = TestRoomItem {
Expand All @@ -1610,6 +1639,7 @@ mod tests {
alias: None,
name: "Cool Room",
unread: UnreadInfo::default(),
invite: false,
};

// Sort by Name ascending.
Expand Down Expand Up @@ -1655,6 +1685,7 @@ mod tests {
alias: None,
name: "Room 1",
unread: UnreadInfo { unread: false, latest: None },
invite: false,
};

let room2 = TestRoomItem {
Expand All @@ -1666,6 +1697,7 @@ mod tests {
unread: false,
latest: Some(MessageTimeStamp::OriginServer(40u32.into())),
},
invite: false,
};

let room3 = TestRoomItem {
Expand All @@ -1677,6 +1709,7 @@ mod tests {
unread: false,
latest: Some(MessageTimeStamp::OriginServer(20u32.into())),
},
invite: false,
};

// Sort by Recent ascending.
Expand All @@ -1691,4 +1724,48 @@ mod tests {
rooms.sort_by(|a, b| room_fields_cmp(a, b, fields));
assert_eq!(rooms, vec![&room1, &room3, &room2]);
}

#[test]
fn test_sort_room_invites() {
let server = server_name!("example.com");

let room1 = TestRoomItem {
room_id: RoomId::new(server).to_owned(),
tags: vec![],
alias: None,
name: "Old room 1",
unread: UnreadInfo::default(),
invite: false,
};

let room2 = TestRoomItem {
room_id: RoomId::new(server).to_owned(),
tags: vec![],
alias: None,
name: "Old room 2",
unread: UnreadInfo::default(),
invite: false,
};

let room3 = TestRoomItem {
room_id: RoomId::new(server).to_owned(),
tags: vec![],
alias: None,
name: "New Fancy Room",
unread: UnreadInfo::default(),
invite: true,
};

// Sort invites first
let mut rooms = vec![&room1, &room2, &room3];
let fields = &[SortColumn(SortFieldRoom::Invite, SortOrder::Ascending)];
rooms.sort_by(|a, b| room_fields_cmp(a, b, fields));
assert_eq!(rooms, vec![&room3, &room1, &room2]);

// Sort invites after
let mut rooms = vec![&room1, &room2, &room3];
let fields = &[SortColumn(SortFieldRoom::Invite, SortOrder::Descending)];
rooms.sort_by(|a, b| room_fields_cmp(a, b, fields));
assert_eq!(rooms, vec![&room1, &room2, &room3]);
}
}