Commit 7afd68c6b2

7afd68c6b2dcfcb47e79a4b767beefc1242bb2a9

parent: 061a16b03c

Unregistered key

vaw <git@nlih.de> · 2026-08-15 04:43 UTC
committer: <noreply@github.com>

Add `:mentions` window and hopefully finally fix unreads (#579)

Layout: unified · split

docs/iamb.1 +2
@@ -65,6 +65,8 @@ View a list of joined spaces.
6565View a list of unread rooms.
6666.It Sy ":unreads clear"
6767Mark all rooms as read.
68.It Sy ":mentions"
69View a list of rooms with mentions of the current user.
6870.It Sy ":welcome"
6971View the startup Welcome window.
7072.It Sy ":forget"
docs/iamb.5 +1 −1
@@ -223,7 +223,7 @@ Defines whether or not reactions should be shown.
223223.It Sy reaction_shortcode_display
224224Defines whether or not reactions should be shown as their respective shortcode.
225225.It Sy read_receipt_send
226Defines whether or not read confirmations are sent.
226Defines whether or not public read confirmations are sent.
227227.It Sy read_receipt_display
228228Defines whether or not read confirmations are displayed.
229229.It Sy request_timeout
src/base.rs +66 −63
@@ -869,13 +869,19 @@ impl EventLocation {
869869
870870#[derive(Clone, Debug, Default, Eq, PartialEq)]
871871pub struct UnreadInfo {
872 pub(crate) unread: bool,
872 pub(crate) unread_messages: u64,
873 pub(crate) unread_notifications: u64,
874 pub(crate) unread_mentions: u64,
873875 pub(crate) latest: Option<MessageTimeStamp>,
874876}
875877
876878impl UnreadInfo {
877879 pub fn is_unread(&self) -> bool {
878 self.unread
880 self.unread_notifications > 0 || self.unread_mentions > 0
881 }
882
883 pub fn has_mention(&self) -> bool {
884 self.unread_mentions > 0
879885 }
880886
881887 pub fn latest(&self) -> Option<&MessageTimeStamp> {
@@ -1271,52 +1277,18 @@ impl RoomInfo {
12711277 }
12721278
12731279 /// Indicates whether this room has unread messages.
1274 pub fn unreads(&self, room: &MatrixRoom, settings: &ApplicationSettings) -> UnreadInfo {
1275 let marked_unread = room.is_marked_unread();
1276
1277 let last_message = self.messages.last_key_value();
1278
1279 let last_receipt = self
1280 .user_receipts
1281 .get(&ReceiptThread::Main)
1282 .and_then(|receipts| receipts.get(&settings.profile.user_id));
1283 let last_receipt = last_receipt.as_ref().and_then(|event_id| {
1284 match &self.keys.get(*event_id)? {
1285 EventLocation::Message(_, key) |
1286 EventLocation::State(key) |
1287 EventLocation::Sticker(key) => Some(key),
1288 EventLocation::Reaction(_) => None,
1289 }
1290 });
1291
1292 let last_unthreaded = self
1293 .user_receipts
1294 .get(&ReceiptThread::Unthreaded)
1295 .and_then(|receipts| receipts.get(&settings.profile.user_id));
1296 let last_unthreaded = last_unthreaded.as_ref().and_then(|event_id| {
1297 match &self.keys.get(*event_id)? {
1298 EventLocation::Message(_, key) |
1299 EventLocation::State(key) |
1300 EventLocation::Sticker(key) => Some(key),
1301 EventLocation::Reaction(_) => None,
1302 }
1303 });
1304
1305 let last_receipt = std::cmp::max(last_receipt, last_unthreaded);
1306
1307 match (last_message, last_receipt) {
1308 (Some(((ts, _), _)), Some((read_ts, _))) => {
1309 UnreadInfo {
1310 unread: marked_unread | (ts > read_ts),
1311 latest: Some(*ts),
1312 }
1313 },
1314 (Some(((ts, _), _)), None) => {
1315 // If we've never loaded/generated a room's receipt (example,
1316 // a newly joined but never viewed room), show it as unread.
1317 UnreadInfo { unread: true, latest: Some(*ts) }
1318 },
1319 (None, _) => UnreadInfo { unread: marked_unread, latest: None },
1280 pub fn unreads(&self, room: &matrix_sdk::Room) -> UnreadInfo {
1281 let last_message = self
1282 .messages
1283 .iter()
1284 .rev()
1285 .find(|(_, msg)| !matches!(&msg.event, MessageEvent::State(..)));
1286
1287 UnreadInfo {
1288 unread_messages: room.num_unread_messages(),
1289 unread_notifications: room.num_unread_notifications(),
1290 unread_mentions: room.num_unread_mentions(),
1291 latest: last_message.map(|((ts, _), _)| ts.to_owned()),
13201292 }
13211293 }
13221294
@@ -1432,27 +1404,42 @@ impl RoomInfo {
14321404 self.user_receipts.entry(thread).or_default().insert(user_id, event_id);
14331405 }
14341406
1435 pub fn fully_read(&mut self, user_id: &UserId) {
1436 let Some(((_, event_id), _)) = self.messages.last_key_value() else {
1407 pub fn fully_read(&mut self, user_id: OwnedUserId, thread: ReceiptThread) {
1408 let Some(messages) = (match &thread {
1409 ReceiptThread::Main => self.get_thread(None),
1410 ReceiptThread::Thread(root) => self.get_thread(Some(root)),
1411 _ => None,
1412 }) else {
14371413 return;
14381414 };
14391415
1440 self.set_receipt(ReceiptThread::Main, user_id.to_owned(), event_id.clone());
1441
1442 let newest = self
1443 .threads
1416 let event_id = messages
14441417 .iter()
1445 .filter_map(|(thread_id, messages)| {
1446 let thread = ReceiptThread::Thread(thread_id.to_owned());
1447
1448 messages
1449 .last_key_value()
1450 .map(|((_, event_id), _)| (thread, event_id.to_owned()))
1418 .filter(|(_, msg)| msg.sender != user_id)
1419 .filter(|(_, msg)| {
1420 matches!(
1421 msg.event,
1422 MessageEvent::EncryptedOriginal(..) |
1423 MessageEvent::EncryptedRedacted(..) |
1424 MessageEvent::Original(..) |
1425 MessageEvent::Redacted(..)
1426 )
14511427 })
1452 .collect::<Vec<_>>();
1428 .map(|(_, msg)| msg.event.event_id().to_owned())
1429 .next_back();
1430
1431 if let Some(event_id) = event_id {
1432 self.set_receipt(thread, user_id, event_id);
1433 }
1434 }
1435
1436 pub fn fully_read_all(&mut self, user_id: &UserId) {
1437 self.fully_read(user_id.to_owned(), ReceiptThread::Main);
1438
1439 let threads: Vec<_> = self.threads.keys().map(|root| root.to_owned()).collect();
14531440
1454 for (thread, event_id) in newest.into_iter() {
1455 self.set_receipt(thread, user_id.to_owned(), event_id.clone());
1441 for thread in threads {
1442 self.fully_read(user_id.to_owned(), ReceiptThread::Thread(thread));
14561443 }
14571444 }
14581445
@@ -1845,6 +1832,9 @@ pub enum IambId {
18451832
18461833 /// The `:unreads` window.
18471834 UnreadList,
1835
1836 /// The `:mentions` window.
1837 MentionsList,
18481838}
18491839
18501840impl Display for IambId {
@@ -1866,6 +1856,7 @@ impl Display for IambId {
18661856 IambId::Welcome => f.write_str("iamb://welcome"),
18671857 IambId::ChatList => f.write_str("iamb://chats"),
18681858 IambId::UnreadList => f.write_str("iamb://unreads"),
1859 IambId::MentionsList => f.write_str("iamb://mentions"),
18691860 }
18701861 }
18711862}
@@ -2004,6 +1995,13 @@ impl Visitor<'_> for IambIdVisitor {
20041995
20051996 Ok(IambId::UnreadList)
20061997 },
1998 Some("mentions") => {
1999 if url.path() != "" {
2000 return Err(E::custom("iamb://mentions takes no path"));
2001 }
2002
2003 Ok(IambId::MentionsList)
2004 },
20072005 Some(s) => Err(E::custom(format!("{s:?} is not a valid window"))),
20082006 None => Err(E::custom("Invalid iamb window URL")),
20092007 }
@@ -2074,6 +2072,9 @@ pub enum IambBufferId {
20742072
20752073 /// The `:unreads` window.
20762074 UnreadList,
2075
2076 /// The `:mentions` window.
2077 MentionsList,
20772078}
20782079
20792080impl IambBufferId {
@@ -2090,6 +2091,7 @@ impl IambBufferId {
20902091 IambBufferId::Welcome => IambId::Welcome,
20912092 IambBufferId::ChatList => IambId::ChatList,
20922093 IambBufferId::UnreadList => IambId::UnreadList,
2094 IambBufferId::MentionsList => IambId::MentionsList,
20932095 };
20942096
20952097 Some(id)
@@ -2134,6 +2136,7 @@ impl Completer<IambInfo> for IambCompleter {
21342136 IambBufferId::Welcome => vec![],
21352137 IambBufferId::ChatList => vec![],
21362138 IambBufferId::UnreadList => vec![],
2139 IambBufferId::MentionsList => vec![],
21372140 }
21382141 }
21392142}
src/commands.rs +16
@@ -354,6 +354,17 @@ fn iamb_unreads(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
354354 }
355355}
356356
357fn iamb_mentions(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
358 if !desc.arg.text.is_empty() {
359 return Result::Err(CommandError::InvalidArgument);
360 }
361
362 let open = ctx.switch(OpenTarget::Application(IambId::MentionsList));
363 let step = CommandStep::Continue(open, ctx.context.clone());
364
365 return Ok(step);
366}
367
357368fn iamb_spaces(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
358369 if !desc.arg.text.is_empty() {
359370 return Result::Err(CommandError::InvalidArgument);
@@ -810,6 +821,11 @@ fn add_iamb_commands(cmds: &mut ProgramCommands) {
810821 aliases: vec![],
811822 f: iamb_unreads,
812823 });
824 cmds.add_command(ProgramCommand {
825 name: "mentions".into(),
826 aliases: vec![],
827 f: iamb_mentions,
828 });
813829 cmds.add_command(ProgramCommand {
814830 name: "unreact".into(),
815831 aliases: vec![],
src/main.rs +1 −1
@@ -569,7 +569,7 @@ impl Application {
569569
570570 for room_id in store.application.sync_info.chats() {
571571 if let Some(room) = store.application.rooms.get_mut(room_id) {
572 room.fully_read(user_id);
572 room.fully_read_all(user_id);
573573 }
574574 }
575575
src/windows/mod.rs +85 −19
@@ -125,19 +125,27 @@ fn selected_text(s: &str, selected: bool) -> Text<'_> {
125125 Text::from(selected_span(s, selected))
126126}
127127
128fn name_and_labels(name: &str, unread: bool, style: Style) -> (Span<'_>, Vec<Vec<Span<'_>>>) {
129 let name_style = if unread {
128fn name_and_labels<'a>(
129 name: &'a str,
130 unread: &UnreadInfo,
131 style: Style,
132) -> (Span<'a>, Vec<Vec<Span<'static>>>) {
133 // TODO: use different colors for "mention", "notification", "muted room"
134 let name_style = if unread.is_unread() {
130135 style.add_modifier(StyleModifier::BOLD)
131136 } else {
132137 style
133138 };
134139
135140 let name = Span::styled(name, name_style);
136 let labels = if unread {
137 vec![vec![Span::styled("Unread", style)]]
138 } else {
139 vec![]
140 };
141
142 let mut labels = vec![];
143
144 if unread.unread_mentions > 0 {
145 labels.push(vec![Span::styled("Unread Mention", style)]);
146 } else if unread.unread_notifications > 0 || unread.unread_messages > 0 {
147 labels.push(vec![Span::styled("Unread", style)]);
148 }
141149
142150 (name, labels)
143151}
@@ -330,6 +338,7 @@ macro_rules! delegate {
330338 IambWindow::Welcome($id) => $e,
331339 IambWindow::ChatList($id) => $e,
332340 IambWindow::UnreadList($id) => $e,
341 IambWindow::MentionsList($id) => $e,
333342 }
334343 };
335344}
@@ -344,6 +353,7 @@ pub enum IambWindow {
344353 Welcome(WelcomeState),
345354 ChatList(ChatListState),
346355 UnreadList(UnreadListState),
356 MentionsList(MentionsListState),
347357}
348358
349359impl IambWindow {
@@ -427,6 +437,7 @@ pub type MemberListState = ListState<MemberItem, IambInfo>;
427437pub type RoomListState = ListState<RoomItem, IambInfo>;
428438pub type ChatListState = ListState<GenericChatItem, IambInfo>;
429439pub type UnreadListState = ListState<GenericChatItem, IambInfo>;
440pub type MentionsListState = ListState<GenericChatItem, IambInfo>;
430441pub type SpaceListState = ListState<SpaceItem, IambInfo>;
431442pub type VerifyListState = ListState<VerifyItem, IambInfo>;
432443
@@ -660,6 +671,40 @@ impl WindowOps<IambInfo> for IambWindow {
660671 .focus(focused)
661672 .render(area, buf, state);
662673 },
674 IambWindow::MentionsList(state) => {
675 let mut items = store
676 .application
677 .sync_info
678 .rooms
679 .clone()
680 .into_iter()
681 .map(|room_info| GenericChatItem::new(room_info, store, false))
682 .filter(GenericChatItem::has_mention)
683 .collect::<Vec<_>>();
684
685 let dms = store
686 .application
687 .sync_info
688 .dms
689 .clone()
690 .into_iter()
691 .map(|room_info| GenericChatItem::new(room_info, store, true))
692 .filter(GenericChatItem::has_mention);
693
694 items.extend(dms);
695
696 let fields = &store.application.settings.tunables.sort.chats;
697 let collator = &mut store.application.collator;
698 items.sort_by(|a, b| room_fields_cmp(a, b, fields, collator));
699
700 state.set(items);
701
702 List::new(store)
703 .empty_message("You do not have any unread mentions yet")
704 .empty_alignment(Alignment::Center)
705 .focus(focused)
706 .render(area, buf, state);
707 },
663708 IambWindow::SpaceList(state) => {
664709 let mut items = store
665710 .application
@@ -713,6 +758,7 @@ impl WindowOps<IambInfo> for IambWindow {
713758 IambWindow::Welcome(w) => w.dup(store).into(),
714759 IambWindow::ChatList(w) => w.dup(store).into(),
715760 IambWindow::UnreadList(w) => w.dup(store).into(),
761 IambWindow::MentionsList(w) => w.dup(store).into(),
716762 }
717763 }
718764
@@ -754,6 +800,7 @@ impl Window<IambInfo> for IambWindow {
754800 IambWindow::Welcome(_) => IambId::Welcome,
755801 IambWindow::ChatList(_) => IambId::ChatList,
756802 IambWindow::UnreadList(_) => IambId::UnreadList,
803 IambWindow::MentionsList(_) => IambId::MentionsList,
757804 }
758805 }
759806
@@ -766,6 +813,7 @@ impl Window<IambInfo> for IambWindow {
766813 IambWindow::Welcome(_) => bold_spans("Welcome to iamb"),
767814 IambWindow::ChatList(_) => bold_spans("DMs & Rooms"),
768815 IambWindow::UnreadList(_) => bold_spans("Unread Messages"),
816 IambWindow::MentionsList(_) => bold_spans("Unread Mentions"),
769817
770818 IambWindow::Room(w) => {
771819 let title = store.application.get_room_title(w.id());
@@ -794,6 +842,7 @@ impl Window<IambInfo> for IambWindow {
794842 IambWindow::Welcome(_) => bold_spans("Welcome to iamb"),
795843 IambWindow::ChatList(_) => bold_spans("DMs & Rooms"),
796844 IambWindow::UnreadList(_) => bold_spans("Unread Messages"),
845 IambWindow::MentionsList(_) => bold_spans("Unread Mentions"),
797846
798847 IambWindow::Room(w) => w.get_title(store),
799848 IambWindow::MemberList(state, room_id, _) => {
@@ -860,6 +909,11 @@ impl Window<IambInfo> for IambWindow {
860909
861910 Ok(IambWindow::UnreadList(list))
862911 },
912 IambId::MentionsList => {
913 let list = MentionsListState::new(IambBufferId::MentionsList, vec![]);
914
915 Ok(IambWindow::MentionsList(list))
916 },
863917 }
864918 }
865919
@@ -911,7 +965,7 @@ impl GenericChatItem {
911965 let info = store.application.rooms.get_or_default(room_id.to_owned());
912966 let name = info.name.clone().unwrap_or_default();
913967 let alias = room.canonical_alias();
914 let unread = info.unreads(room, &store.application.settings);
968 let unread = info.unreads(room);
915969 info.tags.clone_from(&room_info.deref().1);
916970
917971 if let Some(alias) = &alias {
@@ -930,6 +984,11 @@ impl GenericChatItem {
930984 fn tags(&self) -> &Option<Tags> {
931985 &self.room_info.deref().1
932986 }
987
988 #[inline]
989 fn has_mention(&self) -> bool {
990 self.unread.has_mention()
991 }
933992}
934993
935994impl RoomLikeItem for GenericChatItem {
@@ -979,9 +1038,8 @@ impl ListItem<IambInfo> for GenericChatItem {
9791038 _: &ViewportContext<ListCursor>,
9801039 _: &mut ProgramStore,
9811040 ) -> Text<'_> {
982 let unread = self.unread.is_unread();
9831041 let style = selected_style(selected);
984 let (name, mut labels) = name_and_labels(&self.name, unread, style);
1042 let (name, mut labels) = name_and_labels(&self.name, &self.unread, style);
9851043 let mut spans = vec![name];
9861044
9871045 labels.push(if self.is_dm {
@@ -1030,7 +1088,7 @@ impl RoomItem {
10301088 let info = store.application.rooms.get_or_default(room_id.to_owned());
10311089 let name = info.name.clone().unwrap_or_default();
10321090 let alias = room.canonical_alias();
1033 let unread = info.unreads(room, &store.application.settings);
1091 let unread = info.unreads(room);
10341092 info.tags.clone_from(&room_info.deref().1);
10351093
10361094 if let Some(alias) = &alias {
@@ -1098,9 +1156,8 @@ impl ListItem<IambInfo> for RoomItem {
10981156 _: &ViewportContext<ListCursor>,
10991157 _: &mut ProgramStore,
11001158 ) -> Text<'_> {
1101 let unread = self.unread.is_unread();
11021159 let style = selected_style(selected);
1103 let (name, mut labels) = name_and_labels(&self.name, unread, style);
1160 let (name, mut labels) = name_and_labels(&self.name, &self.unread, style);
11041161 let mut spans = vec![name];
11051162
11061163 if let Some(tags) = &self.tags() {
@@ -1138,12 +1195,13 @@ pub struct DirectItem {
11381195
11391196impl DirectItem {
11401197 fn new(room_info: MatrixRoomInfo, store: &mut ProgramStore) -> Self {
1198 let room = &room_info.deref().0;
11411199 let room_id = room_info.0.room_id().to_owned();
11421200 let alias = room_info.0.canonical_alias();
11431201
11441202 let info = store.application.rooms.get_or_default(room_id);
11451203 let name = info.name.clone().unwrap_or_default();
1146 let unread = info.unreads(&room_info.0, &store.application.settings);
1204 let unread = info.unreads(room);
11471205 info.tags.clone_from(&room_info.deref().1);
11481206
11491207 DirectItem { room_info, name, alias, unread }
@@ -1207,9 +1265,8 @@ impl ListItem<IambInfo> for DirectItem {
12071265 _: &ViewportContext<ListCursor>,
12081266 _: &mut ProgramStore,
12091267 ) -> Text<'_> {
1210 let unread = self.unread.is_unread();
12111268 let style = selected_style(selected);
1212 let (name, mut labels) = name_and_labels(&self.name, unread, style);
1269 let (name, mut labels) = name_and_labels(&self.name, &self.unread, style);
12131270 let mut spans = vec![name];
12141271
12151272 if let Some(tags) = &self.tags() {
@@ -1757,7 +1814,12 @@ mod tests {
17571814 tags: vec![],
17581815 alias: None,
17591816 name: "Room 1",
1760 unread: UnreadInfo { unread: false, latest: None },
1817 unread: UnreadInfo {
1818 latest: None,
1819 unread_messages: 0,
1820 unread_notifications: 0,
1821 unread_mentions: 0,
1822 },
17611823 invite: false,
17621824 };
17631825
@@ -1767,8 +1829,10 @@ mod tests {
17671829 alias: None,
17681830 name: "Room 2",
17691831 unread: UnreadInfo {
1770 unread: false,
17711832 latest: Some(MessageTimeStamp::OriginServer(40u32.into())),
1833 unread_messages: 0,
1834 unread_notifications: 0,
1835 unread_mentions: 0,
17721836 },
17731837 invite: false,
17741838 };
@@ -1779,8 +1843,10 @@ mod tests {
17791843 alias: None,
17801844 name: "Room 3",
17811845 unread: UnreadInfo {
1782 unread: false,
17831846 latest: Some(MessageTimeStamp::OriginServer(20u32.into())),
1847 unread_messages: 0,
1848 unread_notifications: 0,
1849 unread_mentions: 0,
17841850 },
17851851 invite: false,
17861852 };
src/windows/room/scrollback.rs +2 −7
@@ -1455,14 +1455,9 @@ impl StatefulWidget for Scrollback<'_> {
14551455 }
14561456 }
14571457
1458 if self.room_focused &&
1459 settings.tunables.read_receipt_send &&
1460 state.cursor.timestamp.is_none()
1461 {
1458 if self.room_focused && state.cursor.timestamp.is_none() {
14621459 // If the cursor is at the last message, then update the read marker.
1463 if let Some((k, _)) = thread.last_key_value() {
1464 info.set_receipt(thread.1.clone(), settings.profile.user_id.clone(), k.1.clone());
1465 }
1460 info.fully_read(settings.profile.user_id.clone(), thread.1.clone());
14661461 }
14671462
14681463 // Check whether we should load older messages for this room.
src/worker.rs +14 −4
@@ -461,6 +461,8 @@ async fn refresh_rooms_forever(client: &Client, store: &AsyncProgramStore) {
461461}
462462
463463async fn send_receipts_forever(client: &Client, store: &AsyncProgramStore) {
464 use matrix_sdk::ruma::api::client::receipt::create_receipt::v3::ReceiptType;
465
464466 let mut interval = tokio::time::interval(Duration::from_secs(2));
465467 let mut sent: HashMap<OwnedRoomId, HashMap<ReceiptThread, OwnedEventId>> = Default::default();
466468
@@ -489,11 +491,15 @@ async fn send_receipts_forever(client: &Client, store: &AsyncProgramStore) {
489491
490492 updates.extend(changed);
491493 }
494
495 let receipt_type = if locked.application.settings.tunables.read_receipt_send {
496 ReceiptType::Read
497 } else {
498 ReceiptType::ReadPrivate
499 };
492500 drop(locked);
493501
494502 for (room_id, thread, new_receipt) in updates {
495 use matrix_sdk::ruma::api::client::receipt::create_receipt::v3::ReceiptType;
496
497503 let Some(room) = client.get_room(&room_id) else {
498504 continue;
499505 };
@@ -505,7 +511,7 @@ async fn send_receipts_forever(client: &Client, store: &AsyncProgramStore) {
505511 }
506512
507513 match room
508 .send_single_receipt(ReceiptType::Read, thread.to_owned(), new_receipt.clone())
514 .send_single_receipt(receipt_type.clone(), thread.to_owned(), new_receipt.clone())
509515 .await
510516 {
511517 Ok(()) => {
@@ -721,7 +727,11 @@ pub async fn create_client(settings: &ApplicationSettings) -> Client {
721727 res => res,
722728 };
723729
724 res.expect("Failed to instantiate client")
730 let client = res.expect("Failed to instantiate client");
731
732 client.event_cache().subscribe().expect("Failed to start event cache");
733
734 client
725735}
726736
727737#[derive(Clone)]