Commit 480888a1fc

480888a1fc911b394969087278f1886690400509

parent: 4fc05c7b40

Unregistered key

Ulyssa <git@ulyssa.dev> · 2024-08-21 02:33 UTC
committer: <noreply@github.com>

Add commands for viewing and clearing unreads (#332)

Layout: unified · split

docs/iamb.1 +4
@@ -61,6 +61,8 @@ Log out of
61View a list of joined rooms. 61View a list of joined rooms.
62.It Sy ":spaces" 62.It Sy ":spaces"
63View a list of joined spaces. 63View a list of joined spaces.
64.It Sy ":unreads"
65View a list of unread rooms.
64.It Sy ":welcome" 66.It Sy ":welcome"
65View the startup Welcome window. 67View the startup Welcome window.
66.El 68.El
@@ -95,6 +97,8 @@ React to the selected message with an Emoji.
95Redact the selected message. 97Redact the selected message.
96.It Sy ":reply" 98.It Sy ":reply"
97Reply to the selected message. 99Reply to the selected message.
100.It Sy ":unreads clear"
101Mark all unread rooms as read.
98.It Sy ":unreact [shortcode]" 102.It Sy ":unreact [shortcode]"
99Remove your reaction from the selected message. 103Remove your reaction from the selected message.
100When no arguments are given, remove all of your reactions from the message. 104When no arguments are given, remove all of your reactions from the message.
src/base.rs +45
@@ -510,6 +510,9 @@ pub enum IambAction {
510 510
511 /// Toggle the focus within the focused room. 511 /// Toggle the focus within the focused room.
512 ToggleScrollbackFocus, 512 ToggleScrollbackFocus,
513
514 /// Clear all unread messages.
515 ClearUnreads,
513} 516}
514 517
515impl IambAction { 518impl IambAction {
@@ -546,6 +549,7 @@ impl From<SendAction> for IambAction {
546impl ApplicationAction for IambAction { 549impl ApplicationAction for IambAction {
547 fn is_edit_sequence(&self, _: &EditContext) -> SequenceStatus { 550 fn is_edit_sequence(&self, _: &EditContext) -> SequenceStatus {
548 match self { 551 match self {
552 IambAction::ClearUnreads => SequenceStatus::Break,
549 IambAction::Homeserver(..) => SequenceStatus::Break, 553 IambAction::Homeserver(..) => SequenceStatus::Break,
550 IambAction::Keys(..) => SequenceStatus::Break, 554 IambAction::Keys(..) => SequenceStatus::Break,
551 IambAction::Message(..) => SequenceStatus::Break, 555 IambAction::Message(..) => SequenceStatus::Break,
@@ -560,6 +564,7 @@ impl ApplicationAction for IambAction {
560 564
561 fn is_last_action(&self, _: &EditContext) -> SequenceStatus { 565 fn is_last_action(&self, _: &EditContext) -> SequenceStatus {
562 match self { 566 match self {
567 IambAction::ClearUnreads => SequenceStatus::Atom,
563 IambAction::Homeserver(..) => SequenceStatus::Atom, 568 IambAction::Homeserver(..) => SequenceStatus::Atom,
564 IambAction::Keys(..) => SequenceStatus::Atom, 569 IambAction::Keys(..) => SequenceStatus::Atom,
565 IambAction::Message(..) => SequenceStatus::Atom, 570 IambAction::Message(..) => SequenceStatus::Atom,
@@ -574,6 +579,7 @@ impl ApplicationAction for IambAction {
574 579
575 fn is_last_selection(&self, _: &EditContext) -> SequenceStatus { 580 fn is_last_selection(&self, _: &EditContext) -> SequenceStatus {
576 match self { 581 match self {
582 IambAction::ClearUnreads => SequenceStatus::Ignore,
577 IambAction::Homeserver(..) => SequenceStatus::Ignore, 583 IambAction::Homeserver(..) => SequenceStatus::Ignore,
578 IambAction::Keys(..) => SequenceStatus::Ignore, 584 IambAction::Keys(..) => SequenceStatus::Ignore,
579 IambAction::Message(..) => SequenceStatus::Ignore, 585 IambAction::Message(..) => SequenceStatus::Ignore,
@@ -588,6 +594,7 @@ impl ApplicationAction for IambAction {
588 594
589 fn is_switchable(&self, _: &EditContext) -> bool { 595 fn is_switchable(&self, _: &EditContext) -> bool {
590 match self { 596 match self {
597 IambAction::ClearUnreads => false,
591 IambAction::Homeserver(..) => false, 598 IambAction::Homeserver(..) => false,
592 IambAction::Message(..) => false, 599 IambAction::Message(..) => false,
593 IambAction::Room(..) => false, 600 IambAction::Room(..) => false,
@@ -1148,6 +1155,14 @@ impl RoomInfo {
1148 self.user_receipts.insert(user_id, event_id); 1155 self.user_receipts.insert(user_id, event_id);
1149 } 1156 }
1150 1157
1158 pub fn fully_read(&mut self, user_id: OwnedUserId) {
1159 let Some(((_, event_id), _)) = self.messages.last_key_value() else {
1160 return;
1161 };
1162
1163 self.set_receipt(user_id, event_id.clone());
1164 }
1165
1151 pub fn get_receipt(&self, user_id: &UserId) -> Option<&OwnedEventId> { 1166 pub fn get_receipt(&self, user_id: &UserId) -> Option<&OwnedEventId> {
1152 self.user_receipts.get(user_id) 1167 self.user_receipts.get(user_id)
1153 } 1168 }
@@ -1309,6 +1324,20 @@ pub struct SyncInfo {
1309 pub dms: Vec<Arc<(MatrixRoom, Option<Tags>)>>, 1324 pub dms: Vec<Arc<(MatrixRoom, Option<Tags>)>>,
1310} 1325}
1311 1326
1327impl SyncInfo {
1328 pub fn rooms(&self) -> impl Iterator<Item = &RoomId> {
1329 self.rooms.iter().map(|r| r.0.room_id())
1330 }
1331
1332 pub fn dms(&self) -> impl Iterator<Item = &RoomId> {
1333 self.dms.iter().map(|r| r.0.room_id())
1334 }
1335
1336 pub fn chats(&self) -> impl Iterator<Item = &RoomId> {
1337 self.rooms().chain(self.dms())
1338 }
1339}
1340
1312bitflags::bitflags! { 1341bitflags::bitflags! {
1313 /// Load-needs 1342 /// Load-needs
1314 #[derive(Debug, Default, PartialEq)] 1343 #[derive(Debug, Default, PartialEq)]
@@ -1480,6 +1509,9 @@ pub enum IambId {
1480 1509
1481 /// The `:chats` window. 1510 /// The `:chats` window.
1482 ChatList, 1511 ChatList,
1512
1513 /// The `:unreads` window.
1514 UnreadList,
1483} 1515}
1484 1516
1485impl Display for IambId { 1517impl Display for IambId {
@@ -1500,6 +1532,7 @@ impl Display for IambId {
1500 IambId::VerifyList => f.write_str("iamb://verify"), 1532 IambId::VerifyList => f.write_str("iamb://verify"),
1501 IambId::Welcome => f.write_str("iamb://welcome"), 1533 IambId::Welcome => f.write_str("iamb://welcome"),
1502 IambId::ChatList => f.write_str("iamb://chats"), 1534 IambId::ChatList => f.write_str("iamb://chats"),
1535 IambId::UnreadList => f.write_str("iamb://unreads"),
1503 } 1536 }
1504 } 1537 }
1505} 1538}
@@ -1631,6 +1664,13 @@ impl<'de> Visitor<'de> for IambIdVisitor {
1631 1664
1632 Ok(IambId::ChatList) 1665 Ok(IambId::ChatList)
1633 }, 1666 },
1667 Some("unreads") => {
1668 if url.path() != "" {
1669 return Err(E::custom("iamb://unreads takes no path"));
1670 }
1671
1672 Ok(IambId::UnreadList)
1673 },
1634 Some(s) => Err(E::custom(format!("{s:?} is not a valid window"))), 1674 Some(s) => Err(E::custom(format!("{s:?} is not a valid window"))),
1635 None => Err(E::custom("Invalid iamb window URL")), 1675 None => Err(E::custom("Invalid iamb window URL")),
1636 } 1676 }
@@ -1691,6 +1731,9 @@ pub enum IambBufferId {
1691 1731
1692 /// The `:chats` window. 1732 /// The `:chats` window.
1693 ChatList, 1733 ChatList,
1734
1735 /// The `:unreads` window.
1736 UnreadList,
1694} 1737}
1695 1738
1696impl IambBufferId { 1739impl IambBufferId {
@@ -1706,6 +1749,7 @@ impl IambBufferId {
1706 IambBufferId::VerifyList => IambId::VerifyList, 1749 IambBufferId::VerifyList => IambId::VerifyList,
1707 IambBufferId::Welcome => IambId::Welcome, 1750 IambBufferId::Welcome => IambId::Welcome,
1708 IambBufferId::ChatList => IambId::ChatList, 1751 IambBufferId::ChatList => IambId::ChatList,
1752 IambBufferId::UnreadList => IambId::UnreadList,
1709 }; 1753 };
1710 1754
1711 Some(id) 1755 Some(id)
@@ -1740,6 +1784,7 @@ impl ApplicationInfo for IambInfo {
1740 IambBufferId::VerifyList => vec![], 1784 IambBufferId::VerifyList => vec![],
1741 IambBufferId::Welcome => vec![], 1785 IambBufferId::Welcome => vec![],
1742 IambBufferId::ChatList => vec![], 1786 IambBufferId::ChatList => vec![],
1787 IambBufferId::UnreadList => vec![],
1743 } 1788 }
1744 } 1789 }
1745 1790
src/commands.rs +29
@@ -307,6 +307,30 @@ fn iamb_chats(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
307 return Ok(step); 307 return Ok(step);
308} 308}
309 309
310fn iamb_unreads(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
311 let mut args = desc.arg.strings()?;
312
313 if args.len() > 1 {
314 return Result::Err(CommandError::InvalidArgument);
315 }
316
317 match args.pop().as_deref() {
318 Some("clear") => {
319 let clear = IambAction::ClearUnreads;
320 let step = CommandStep::Continue(clear.into(), ctx.context.clone());
321
322 return Ok(step);
323 },
324 Some(_) => return Result::Err(CommandError::InvalidArgument),
325 None => {
326 let open = ctx.switch(OpenTarget::Application(IambId::UnreadList));
327 let step = CommandStep::Continue(open, ctx.context.clone());
328
329 return Ok(step);
330 },
331 }
332}
333
310fn iamb_spaces(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult { 334fn iamb_spaces(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
311 if !desc.arg.text.is_empty() { 335 if !desc.arg.text.is_empty() {
312 return Result::Err(CommandError::InvalidArgument); 336 return Result::Err(CommandError::InvalidArgument);
@@ -648,6 +672,11 @@ fn add_iamb_commands(cmds: &mut ProgramCommands) {
648 aliases: vec![], 672 aliases: vec![],
649 f: iamb_spaces, 673 f: iamb_spaces,
650 }); 674 });
675 cmds.add_command(ProgramCommand {
676 name: "unreads".into(),
677 aliases: vec![],
678 f: iamb_unreads,
679 });
651 cmds.add_command(ProgramCommand { 680 cmds.add_command(ProgramCommand {
652 name: "unreact".into(), 681 name: "unreact".into(),
653 aliases: vec![], 682 aliases: vec![],
src/main.rs +12
@@ -529,6 +529,18 @@ impl Application {
529 } 529 }
530 530
531 let info = match action { 531 let info = match action {
532 IambAction::ClearUnreads => {
533 let user_id = &store.application.settings.profile.user_id;
534
535 for room_id in store.application.sync_info.chats() {
536 if let Some(room) = store.application.rooms.get_mut(room_id) {
537 room.fully_read(user_id.clone());
538 }
539 }
540
541 None
542 },
543
532 IambAction::ToggleScrollbackFocus => { 544 IambAction::ToggleScrollbackFocus => {
533 self.screen.current_window_mut()?.focus_toggle(); 545 self.screen.current_window_mut()?.focus_toggle();
534 546
src/windows/mod.rs +45
@@ -315,6 +315,7 @@ macro_rules! delegate {
315 IambWindow::VerifyList($id) => $e, 315 IambWindow::VerifyList($id) => $e,
316 IambWindow::Welcome($id) => $e, 316 IambWindow::Welcome($id) => $e,
317 IambWindow::ChatList($id) => $e, 317 IambWindow::ChatList($id) => $e,
318 IambWindow::UnreadList($id) => $e,
318 } 319 }
319 }; 320 };
320} 321}
@@ -328,6 +329,7 @@ pub enum IambWindow {
328 SpaceList(SpaceListState), 329 SpaceList(SpaceListState),
329 Welcome(WelcomeState), 330 Welcome(WelcomeState),
330 ChatList(ChatListState), 331 ChatList(ChatListState),
332 UnreadList(UnreadListState),
331} 333}
332 334
333impl IambWindow { 335impl IambWindow {
@@ -383,6 +385,7 @@ pub type DirectListState = ListState<DirectItem, IambInfo>;
383pub type MemberListState = ListState<MemberItem, IambInfo>; 385pub type MemberListState = ListState<MemberItem, IambInfo>;
384pub type RoomListState = ListState<RoomItem, IambInfo>; 386pub type RoomListState = ListState<RoomItem, IambInfo>;
385pub type ChatListState = ListState<GenericChatItem, IambInfo>; 387pub type ChatListState = ListState<GenericChatItem, IambInfo>;
388pub type UnreadListState = ListState<GenericChatItem, IambInfo>;
386pub type SpaceListState = ListState<SpaceItem, IambInfo>; 389pub type SpaceListState = ListState<SpaceItem, IambInfo>;
387pub type VerifyListState = ListState<VerifyItem, IambInfo>; 390pub type VerifyListState = ListState<VerifyItem, IambInfo>;
388 391
@@ -579,6 +582,39 @@ impl WindowOps<IambInfo> for IambWindow {
579 .focus(focused) 582 .focus(focused)
580 .render(area, buf, state); 583 .render(area, buf, state);
581 }, 584 },
585 IambWindow::UnreadList(state) => {
586 let mut items = store
587 .application
588 .sync_info
589 .rooms
590 .clone()
591 .into_iter()
592 .map(|room_info| GenericChatItem::new(room_info, store, false))
593 .filter(RoomLikeItem::is_unread)
594 .collect::<Vec<_>>();
595
596 let dms = store
597 .application
598 .sync_info
599 .dms
600 .clone()
601 .into_iter()
602 .map(|room_info| GenericChatItem::new(room_info, store, true))
603 .filter(RoomLikeItem::is_unread);
604
605 items.extend(dms);
606
607 let fields = &store.application.settings.tunables.sort.chats;
608 items.sort_by(|a, b| room_fields_cmp(a, b, fields));
609
610 state.set(items);
611
612 List::new(store)
613 .empty_message("You do not have rooms or dms yet")
614 .empty_alignment(Alignment::Center)
615 .focus(focused)
616 .render(area, buf, state);
617 },
582 IambWindow::SpaceList(state) => { 618 IambWindow::SpaceList(state) => {
583 let mut items = store 619 let mut items = store
584 .application 620 .application
@@ -630,6 +666,7 @@ impl WindowOps<IambInfo> for IambWindow {
630 IambWindow::VerifyList(w) => w.dup(store).into(), 666 IambWindow::VerifyList(w) => w.dup(store).into(),
631 IambWindow::Welcome(w) => w.dup(store).into(), 667 IambWindow::Welcome(w) => w.dup(store).into(),
632 IambWindow::ChatList(w) => w.dup(store).into(), 668 IambWindow::ChatList(w) => w.dup(store).into(),
669 IambWindow::UnreadList(w) => w.dup(store).into(),
633 } 670 }
634 } 671 }
635 672
@@ -670,6 +707,7 @@ impl Window<IambInfo> for IambWindow {
670 IambWindow::VerifyList(_) => IambId::VerifyList, 707 IambWindow::VerifyList(_) => IambId::VerifyList,
671 IambWindow::Welcome(_) => IambId::Welcome, 708 IambWindow::Welcome(_) => IambId::Welcome,
672 IambWindow::ChatList(_) => IambId::ChatList, 709 IambWindow::ChatList(_) => IambId::ChatList,
710 IambWindow::UnreadList(_) => IambId::UnreadList,
673 } 711 }
674 } 712 }
675 713
@@ -681,6 +719,7 @@ impl Window<IambInfo> for IambWindow {
681 IambWindow::VerifyList(_) => bold_spans("Verifications"), 719 IambWindow::VerifyList(_) => bold_spans("Verifications"),
682 IambWindow::Welcome(_) => bold_spans("Welcome to iamb"), 720 IambWindow::Welcome(_) => bold_spans("Welcome to iamb"),
683 IambWindow::ChatList(_) => bold_spans("DMs & Rooms"), 721 IambWindow::ChatList(_) => bold_spans("DMs & Rooms"),
722 IambWindow::UnreadList(_) => bold_spans("Unread Messages"),
684 723
685 IambWindow::Room(w) => { 724 IambWindow::Room(w) => {
686 let title = store.application.get_room_title(w.id()); 725 let title = store.application.get_room_title(w.id());
@@ -708,6 +747,7 @@ impl Window<IambInfo> for IambWindow {
708 IambWindow::VerifyList(_) => bold_spans("Verifications"), 747 IambWindow::VerifyList(_) => bold_spans("Verifications"),
709 IambWindow::Welcome(_) => bold_spans("Welcome to iamb"), 748 IambWindow::Welcome(_) => bold_spans("Welcome to iamb"),
710 IambWindow::ChatList(_) => bold_spans("DMs & Rooms"), 749 IambWindow::ChatList(_) => bold_spans("DMs & Rooms"),
750 IambWindow::UnreadList(_) => bold_spans("Unread Messages"),
711 751
712 IambWindow::Room(w) => w.get_title(store), 752 IambWindow::Room(w) => w.get_title(store),
713 IambWindow::MemberList(state, room_id, _) => { 753 IambWindow::MemberList(state, room_id, _) => {
@@ -769,6 +809,11 @@ impl Window<IambInfo> for IambWindow {
769 809
770 Ok(list.into()) 810 Ok(list.into())
771 }, 811 },
812 IambId::UnreadList => {
813 let list = UnreadListState::new(IambBufferId::UnreadList, vec![]);
814
815 Ok(IambWindow::UnreadList(list))
816 },
772 } 817 }
773 } 818 }
774 819