Skip to content

Commit f5a136a

Browse files
author
Felix Van der Jeugt
committed
add an "invite" order to the settings
Adds a new order to the settings.sort configuration which puts invites before other rooms. This should counter missing out on invites when the order is set to e.g. "recent".
1 parent b023e38 commit f5a136a

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

docs/iamb.5

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,26 @@ window.
334334
Defaults to
335335
.Sy ["power",\ "id"] .
336336
.El
337+
338+
The available values are:
339+
.Bl -tag -width Ds
340+
.It Sy favorite
341+
Put favorite rooms before other rooms.
342+
.It Sy lowpriority
343+
Put lowpriority rooms after other rooms.
344+
.It Sy name
345+
Sort rooms by alphabetically ascending room name.
346+
.It Sy alias
347+
Sort rooms by alphabetically ascending canonical room alias.
348+
.It Sy id
349+
Sort rooms by alphabetically ascending Matrix room identifier.
350+
.It Sy unread
351+
Put unread rooms before other rooms.
352+
.It Sy recent
353+
Sort rooms by most recent message timestamp.
354+
.It Sy invite
355+
Put invites before other rooms.
356+
.El
337357
.El
338358

339359
.Ss Example 1: Group room members by ther server first

src/base.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ pub enum SortFieldRoom {
243243

244244
/// Sort rooms by the timestamps of their most recent messages.
245245
Recent,
246+
247+
/// Sort rooms by whether they are invites.
248+
Invite,
246249
}
247250

248251
/// Fields that users can be sorted by.
@@ -307,6 +310,7 @@ impl<'de> Visitor<'de> for SortRoomVisitor {
307310
"name" => SortFieldRoom::Name,
308311
"alias" => SortFieldRoom::Alias,
309312
"id" => SortFieldRoom::RoomId,
313+
"invite" => SortFieldRoom::Invite,
310314
_ => {
311315
let msg = format!("Unknown sort field: {value:?}");
312316
return Err(E::custom(msg));

src/windows/mod.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use matrix_sdk::{
2323
RoomAliasId,
2424
RoomId,
2525
},
26+
RoomState as MatrixRoomState,
2627
};
2728

2829
use ratatui::{
@@ -195,6 +196,10 @@ fn room_cmp<T: RoomLikeItem>(a: &T, b: &T, field: &SortFieldRoom) -> Ordering {
195196
// sort larger timestamps towards the top.
196197
some_cmp(a.recent_ts(), b.recent_ts(), |a, b| b.cmp(a))
197198
},
199+
SortFieldRoom::Invite => {
200+
// sort invites before other rooms.
201+
b.is_invite().cmp(&a.is_invite())
202+
},
198203
}
199204
}
200205

@@ -273,6 +278,7 @@ trait RoomLikeItem {
273278
fn recent_ts(&self) -> Option<&MessageTimeStamp>;
274279
fn alias(&self) -> Option<&RoomAliasId>;
275280
fn name(&self) -> &str;
281+
fn is_invite(&self) -> bool;
276282
}
277283

278284
#[inline]
@@ -914,6 +920,10 @@ impl RoomLikeItem for GenericChatItem {
914920
fn is_unread(&self) -> bool {
915921
self.unread.is_unread()
916922
}
923+
924+
fn is_invite(&self) -> bool {
925+
self.room().state() == MatrixRoomState::Invited
926+
}
917927
}
918928

919929
impl Display for GenericChatItem {
@@ -1024,6 +1034,10 @@ impl RoomLikeItem for RoomItem {
10241034
fn is_unread(&self) -> bool {
10251035
self.unread.is_unread()
10261036
}
1037+
1038+
fn is_invite(&self) -> bool {
1039+
self.room().state() == MatrixRoomState::Invited
1040+
}
10271041
}
10281042

10291043
impl Display for RoomItem {
@@ -1124,6 +1138,10 @@ impl RoomLikeItem for DirectItem {
11241138
fn is_unread(&self) -> bool {
11251139
self.unread.is_unread()
11261140
}
1141+
1142+
fn is_invite(&self) -> bool {
1143+
self.room().state() == MatrixRoomState::Invited
1144+
}
11271145
}
11281146

11291147
impl Display for DirectItem {
@@ -1223,6 +1241,10 @@ impl RoomLikeItem for SpaceItem {
12231241
// XXX: this needs to check whether the space contains rooms with unread messages
12241242
false
12251243
}
1244+
1245+
fn is_invite(&self) -> bool {
1246+
self.room().state() == MatrixRoomState::Invited
1247+
}
12261248
}
12271249

12281250
impl Display for SpaceItem {
@@ -1556,6 +1578,7 @@ mod tests {
15561578
alias: Option<OwnedRoomAliasId>,
15571579
name: &'static str,
15581580
unread: UnreadInfo,
1581+
invite: bool
15591582
}
15601583

15611584
impl RoomLikeItem for &TestRoomItem {
@@ -1582,6 +1605,10 @@ mod tests {
15821605
fn is_unread(&self) -> bool {
15831606
self.unread.is_unread()
15841607
}
1608+
1609+
fn is_invite(&self) -> bool {
1610+
self.invite
1611+
}
15851612
}
15861613

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

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

16071636
let room3 = TestRoomItem {
@@ -1610,6 +1639,7 @@ mod tests {
16101639
alias: None,
16111640
name: "Cool Room",
16121641
unread: UnreadInfo::default(),
1642+
invite: false,
16131643
};
16141644

16151645
// Sort by Name ascending.
@@ -1655,6 +1685,7 @@ mod tests {
16551685
alias: None,
16561686
name: "Room 1",
16571687
unread: UnreadInfo { unread: false, latest: None },
1688+
invite: false,
16581689
};
16591690

16601691
let room2 = TestRoomItem {
@@ -1666,6 +1697,7 @@ mod tests {
16661697
unread: false,
16671698
latest: Some(MessageTimeStamp::OriginServer(40u32.into())),
16681699
},
1700+
invite: false,
16691701
};
16701702

16711703
let room3 = TestRoomItem {
@@ -1677,6 +1709,7 @@ mod tests {
16771709
unread: false,
16781710
latest: Some(MessageTimeStamp::OriginServer(20u32.into())),
16791711
},
1712+
invite: false,
16801713
};
16811714

16821715
// Sort by Recent ascending.
@@ -1691,4 +1724,48 @@ mod tests {
16911724
rooms.sort_by(|a, b| room_fields_cmp(a, b, fields));
16921725
assert_eq!(rooms, vec![&room1, &room3, &room2]);
16931726
}
1727+
1728+
#[test]
1729+
fn test_sort_room_invites() {
1730+
let server = server_name!("example.com");
1731+
1732+
let room1 = TestRoomItem {
1733+
room_id: RoomId::new(server).to_owned(),
1734+
tags: vec![],
1735+
alias: None,
1736+
name: "Old room 1",
1737+
unread: UnreadInfo::default(),
1738+
invite: false,
1739+
};
1740+
1741+
let room2 = TestRoomItem {
1742+
room_id: RoomId::new(server).to_owned(),
1743+
tags: vec![],
1744+
alias: None,
1745+
name: "Old room 2",
1746+
unread: UnreadInfo::default(),
1747+
invite: false,
1748+
};
1749+
1750+
let room3 = TestRoomItem {
1751+
room_id: RoomId::new(server).to_owned(),
1752+
tags: vec![],
1753+
alias: None,
1754+
name: "New Fancy Room",
1755+
unread: UnreadInfo::default(),
1756+
invite: true,
1757+
};
1758+
1759+
// Sort invites first
1760+
let mut rooms = vec![&room1, &room2, &room3];
1761+
let fields = &[SortColumn(SortFieldRoom::Invite, SortOrder::Ascending)];
1762+
rooms.sort_by(|a, b| room_fields_cmp(a, b, fields));
1763+
assert_eq!(rooms, vec![&room3, &room1, &room2]);
1764+
1765+
// Sort invites after
1766+
let mut rooms = vec![&room1, &room2, &room3];
1767+
let fields = &[SortColumn(SortFieldRoom::Invite, SortOrder::Descending)];
1768+
rooms.sort_by(|a, b| room_fields_cmp(a, b, fields));
1769+
assert_eq!(rooms, vec![&room1, &room2, &room3]);
1770+
}
16941771
}

0 commit comments

Comments
 (0)