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
6161View a list of joined rooms.
6262.It Sy ":spaces"
6363View a list of joined spaces.
64.It Sy ":unreads"
65View a list of unread rooms.
6466.It Sy ":welcome"
6567View the startup Welcome window.
6668.El
@@ -95,6 +97,8 @@ React to the selected message with an Emoji.
9597Redact the selected message.
9698.It Sy ":reply"
9799Reply to the selected message.
100.It Sy ":unreads clear"
101Mark all unread rooms as read.
98102.It Sy ":unreact [shortcode]"
99103Remove your reaction from the selected message.
100104When no arguments are given, remove all of your reactions from the message.
src/base.rs +45
@@ -510,6 +510,9 @@ pub enum IambAction {
510510
511511 /// Toggle the focus within the focused room.
512512 ToggleScrollbackFocus,
513
514 /// Clear all unread messages.
515 ClearUnreads,
513516}
514517
515518impl IambAction {
@@ -546,6 +549,7 @@ impl From<SendAction> for IambAction {
546549impl ApplicationAction for IambAction {
547550 fn is_edit_sequence(&self, _: &EditContext) -> SequenceStatus {
548551 match self {
552 IambAction::ClearUnreads => SequenceStatus::Break,
549553 IambAction::Homeserver(..) => SequenceStatus::Break,
550554 IambAction::Keys(..) => SequenceStatus::Break,
551555 IambAction::Message(..) => SequenceStatus::Break,
@@ -560,6 +564,7 @@ impl ApplicationAction for IambAction {
560564
561565 fn is_last_action(&self, _: &EditContext) -> SequenceStatus {
562566 match self {
567 IambAction::ClearUnreads => SequenceStatus::Atom,
563568 IambAction::Homeserver(..) => SequenceStatus::Atom,
564569 IambAction::Keys(..) => SequenceStatus::Atom,
565570 IambAction::Message(..) => SequenceStatus::Atom,
@@ -574,6 +579,7 @@ impl ApplicationAction for IambAction {
574579
575580 fn is_last_selection(&self, _: &EditContext) -> SequenceStatus {
576581 match self {
582 IambAction::ClearUnreads => SequenceStatus::Ignore,
577583 IambAction::Homeserver(..) => SequenceStatus::Ignore,
578584 IambAction::Keys(..) => SequenceStatus::Ignore,
579585 IambAction::Message(..) => SequenceStatus::Ignore,
@@ -588,6 +594,7 @@ impl ApplicationAction for IambAction {
588594
589595 fn is_switchable(&self, _: &EditContext) -> bool {
590596 match self {
597 IambAction::ClearUnreads => false,
591598 IambAction::Homeserver(..) => false,
592599 IambAction::Message(..) => false,
593600 IambAction::Room(..) => false,
@@ -1148,6 +1155,14 @@ impl RoomInfo {
11481155 self.user_receipts.insert(user_id, event_id);
11491156 }
11501157
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
11511166 pub fn get_receipt(&self, user_id: &UserId) -> Option<&OwnedEventId> {
11521167 self.user_receipts.get(user_id)
11531168 }
@@ -1309,6 +1324,20 @@ pub struct SyncInfo {
13091324 pub dms: Vec<Arc<(MatrixRoom, Option<Tags>)>>,
13101325}
13111326
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
13121341bitflags::bitflags! {
13131342 /// Load-needs
13141343 #[derive(Debug, Default, PartialEq)]
@@ -1480,6 +1509,9 @@ pub enum IambId {
14801509
14811510 /// The `:chats` window.
14821511 ChatList,
1512
1513 /// The `:unreads` window.
1514 UnreadList,
14831515}
14841516
14851517impl Display for IambId {
@@ -1500,6 +1532,7 @@ impl Display for IambId {
15001532 IambId::VerifyList => f.write_str("iamb://verify"),
15011533 IambId::Welcome => f.write_str("iamb://welcome"),
15021534 IambId::ChatList => f.write_str("iamb://chats"),
1535 IambId::UnreadList => f.write_str("iamb://unreads"),
15031536 }
15041537 }
15051538}
@@ -1631,6 +1664,13 @@ impl<'de> Visitor<'de> for IambIdVisitor {
16311664
16321665 Ok(IambId::ChatList)
16331666 },
1667 Some("unreads") => {
1668 if url.path() != "" {
1669 return Err(E::custom("iamb://unreads takes no path"));
1670 }
1671
1672 Ok(IambId::UnreadList)
1673 },
16341674 Some(s) => Err(E::custom(format!("{s:?} is not a valid window"))),
16351675 None => Err(E::custom("Invalid iamb window URL")),
16361676 }
@@ -1691,6 +1731,9 @@ pub enum IambBufferId {
16911731
16921732 /// The `:chats` window.
16931733 ChatList,
1734
1735 /// The `:unreads` window.
1736 UnreadList,
16941737}
16951738
16961739impl IambBufferId {
@@ -1706,6 +1749,7 @@ impl IambBufferId {
17061749 IambBufferId::VerifyList => IambId::VerifyList,
17071750 IambBufferId::Welcome => IambId::Welcome,
17081751 IambBufferId::ChatList => IambId::ChatList,
1752 IambBufferId::UnreadList => IambId::UnreadList,
17091753 };
17101754
17111755 Some(id)
@@ -1740,6 +1784,7 @@ impl ApplicationInfo for IambInfo {
17401784 IambBufferId::VerifyList => vec![],
17411785 IambBufferId::Welcome => vec![],
17421786 IambBufferId::ChatList => vec![],
1787 IambBufferId::UnreadList => vec![],
17431788 }
17441789 }
17451790
src/commands.rs +29
@@ -307,6 +307,30 @@ fn iamb_chats(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
307307 return Ok(step);
308308}
309309
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
310334fn iamb_spaces(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
311335 if !desc.arg.text.is_empty() {
312336 return Result::Err(CommandError::InvalidArgument);
@@ -648,6 +672,11 @@ fn add_iamb_commands(cmds: &mut ProgramCommands) {
648672 aliases: vec![],
649673 f: iamb_spaces,
650674 });
675 cmds.add_command(ProgramCommand {
676 name: "unreads".into(),
677 aliases: vec![],
678 f: iamb_unreads,
679 });
651680 cmds.add_command(ProgramCommand {
652681 name: "unreact".into(),
653682 aliases: vec![],
src/main.rs +12
@@ -529,6 +529,18 @@ impl Application {
529529 }
530530
531531 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
532544 IambAction::ToggleScrollbackFocus => {
533545 self.screen.current_window_mut()?.focus_toggle();
534546
src/windows/mod.rs +45
@@ -315,6 +315,7 @@ macro_rules! delegate {
315315 IambWindow::VerifyList($id) => $e,
316316 IambWindow::Welcome($id) => $e,
317317 IambWindow::ChatList($id) => $e,
318 IambWindow::UnreadList($id) => $e,
318319 }
319320 };
320321}
@@ -328,6 +329,7 @@ pub enum IambWindow {
328329 SpaceList(SpaceListState),
329330 Welcome(WelcomeState),
330331 ChatList(ChatListState),
332 UnreadList(UnreadListState),
331333}
332334
333335impl IambWindow {
@@ -383,6 +385,7 @@ pub type DirectListState = ListState<DirectItem, IambInfo>;
383385pub type MemberListState = ListState<MemberItem, IambInfo>;
384386pub type RoomListState = ListState<RoomItem, IambInfo>;
385387pub type ChatListState = ListState<GenericChatItem, IambInfo>;
388pub type UnreadListState = ListState<GenericChatItem, IambInfo>;
386389pub type SpaceListState = ListState<SpaceItem, IambInfo>;
387390pub type VerifyListState = ListState<VerifyItem, IambInfo>;
388391
@@ -579,6 +582,39 @@ impl WindowOps<IambInfo> for IambWindow {
579582 .focus(focused)
580583 .render(area, buf, state);
581584 },
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 },
582618 IambWindow::SpaceList(state) => {
583619 let mut items = store
584620 .application
@@ -630,6 +666,7 @@ impl WindowOps<IambInfo> for IambWindow {
630666 IambWindow::VerifyList(w) => w.dup(store).into(),
631667 IambWindow::Welcome(w) => w.dup(store).into(),
632668 IambWindow::ChatList(w) => w.dup(store).into(),
669 IambWindow::UnreadList(w) => w.dup(store).into(),
633670 }
634671 }
635672
@@ -670,6 +707,7 @@ impl Window<IambInfo> for IambWindow {
670707 IambWindow::VerifyList(_) => IambId::VerifyList,
671708 IambWindow::Welcome(_) => IambId::Welcome,
672709 IambWindow::ChatList(_) => IambId::ChatList,
710 IambWindow::UnreadList(_) => IambId::UnreadList,
673711 }
674712 }
675713
@@ -681,6 +719,7 @@ impl Window<IambInfo> for IambWindow {
681719 IambWindow::VerifyList(_) => bold_spans("Verifications"),
682720 IambWindow::Welcome(_) => bold_spans("Welcome to iamb"),
683721 IambWindow::ChatList(_) => bold_spans("DMs & Rooms"),
722 IambWindow::UnreadList(_) => bold_spans("Unread Messages"),
684723
685724 IambWindow::Room(w) => {
686725 let title = store.application.get_room_title(w.id());
@@ -708,6 +747,7 @@ impl Window<IambInfo> for IambWindow {
708747 IambWindow::VerifyList(_) => bold_spans("Verifications"),
709748 IambWindow::Welcome(_) => bold_spans("Welcome to iamb"),
710749 IambWindow::ChatList(_) => bold_spans("DMs & Rooms"),
750 IambWindow::UnreadList(_) => bold_spans("Unread Messages"),
711751
712752 IambWindow::Room(w) => w.get_title(store),
713753 IambWindow::MemberList(state, room_id, _) => {
@@ -769,6 +809,11 @@ impl Window<IambInfo> for IambWindow {
769809
770810 Ok(list.into())
771811 },
812 IambId::UnreadList => {
813 let list = UnreadListState::new(IambBufferId::UnreadList, vec![]);
814
815 Ok(IambWindow::UnreadList(list))
816 },
772817 }
773818 }
774819