Commit 760fe154c3

760fe154c3c398a01a9b1ec32f12addd587538ce

parent: 7f5420850f

Unregistered key

vaw <git@nlih.de> · 2026-09-18 03:34 UTC
committer: <noreply@github.com>

Unify the the room list representation (#708)

Layout: unified · split

src/base.rs +10 −8
@@ -1783,22 +1783,22 @@ fn emoji_map() -> CompletionMap<String, &'static Emoji> {
17831783#[derive(Default)]
17841784pub struct SyncInfo {
17851785 /// Spaces that the user is a member of.
1786 pub spaces: Vec<Arc<(MatrixRoom, Option<Tags>)>>,
1786 pub spaces: Vec<MatrixRoom>,
17871787
17881788 /// Rooms that the user is a member of.
1789 pub rooms: Vec<Arc<(MatrixRoom, Option<Tags>)>>,
1789 pub rooms: Vec<MatrixRoom>,
17901790
17911791 /// DMs that the user is a member of.
1792 pub dms: Vec<Arc<(MatrixRoom, Option<Tags>)>>,
1792 pub dms: Vec<MatrixRoom>,
17931793}
17941794
17951795impl SyncInfo {
17961796 pub fn rooms(&self) -> impl Iterator<Item = &RoomId> {
1797 self.rooms.iter().map(|r| r.0.room_id())
1797 self.rooms.iter().map(|r| r.room_id())
17981798 }
17991799
18001800 pub fn dms(&self) -> impl Iterator<Item = &RoomId> {
1801 self.dms.iter().map(|r| r.0.room_id())
1801 self.dms.iter().map(|r| r.room_id())
18021802 }
18031803
18041804 pub fn chats(&self) -> impl Iterator<Item = &RoomId> {
@@ -1977,9 +1977,11 @@ impl ChatStore {
19771977 self.rooms.get_or_default(room_id)
19781978 }
19791979
1980 /// Set the name for a room.
1981 pub fn set_room_name(&mut self, room_id: &RoomId, name: &str) {
1982 self.rooms.get_or_default(room_id.to_owned()).name = name.to_string().into();
1980 /// Set the name and tags for a room.
1981 pub fn set_room_info(&mut self, room_id: OwnedRoomId, name: String, tags: Option<Tags>) {
1982 let info = self.rooms.get_or_default(room_id);
1983 info.name = name.into();
1984 info.tags = tags;
19831985 }
19841986}
19851987
src/windows/mod.rs +173 −551
@@ -12,8 +12,9 @@ use std::fmt::{self};
1212
1313use feruca::Collator;
1414use matrix_sdk::room::RoomMember;
15use matrix_sdk::ruma::RoomAliasId;
1615use matrix_sdk::ruma::events::room::member::MembershipState;
16use matrix_sdk::ruma::{RoomAliasId, assign};
17use modalkit::editing::completion::CompletionMap;
1718use modalkit_ratatui::Window;
1819use modalkit_ratatui::list::{List, ListCursor, ListItem, ListState};
1920
@@ -27,8 +28,6 @@ pub mod room;
2728pub mod verify;
2829pub mod welcome;
2930
30type MatrixRoomInfo = Arc<(MatrixRoom, Option<Tags>)>;
31
3231const MEMBER_FETCH_DEBOUNCE: Duration = Duration::from_secs(5);
3332
3433#[inline]
@@ -55,20 +54,10 @@ pub fn selected_style(selected: bool) -> Style {
5554 }
5655}
5756
58#[inline]
59fn selected_span(s: &str, selected: bool) -> Span<'_> {
60 Span::styled(s, selected_style(selected))
61}
62
63#[inline]
64fn selected_text(s: &str, selected: bool) -> Text<'_> {
65 Text::from(selected_span(s, selected))
66}
67
6857fn name_and_labels<'a>(
6958 name: &'a str,
7059 unread: &UnreadInfo,
71 room: &MatrixRoom,
60 room_membership: MatrixRoomState,
7261 style: Style,
7362) -> (Span<'a>, Vec<Vec<Span<'static>>>) {
7463 // TODO: use different colors for "mention", "notification", "muted room"
@@ -82,7 +71,7 @@ fn name_and_labels<'a>(
8271
8372 let mut labels = vec![];
8473
85 match room.state() {
74 match room_membership {
8675 MatrixRoomState::Joined => {},
8776 MatrixRoomState::Left => labels.push(vec![Span::styled("Left", style)]),
8877 MatrixRoomState::Banned => labels.push(vec![Span::styled("Banned", style)]),
@@ -263,6 +252,7 @@ trait RoomLikeItem {
263252 fn alias(&self) -> Option<&RoomAliasId>;
264253 fn name(&self) -> &str;
265254 fn is_invite(&self) -> bool;
255 fn has_mention(&self) -> bool;
266256}
267257
268258#[inline]
@@ -313,17 +303,17 @@ macro_rules! delegate {
313303}
314304
315305pub enum IambWindow {
316 DirectList(DirectListState),
306 DirectList(RoomListState),
317307 MemberList(MemberListState, OwnedRoomId, Option<Instant>),
318308 Room(RoomState),
319309 VerifyList(VerifyListState),
320310 RoomList(RoomListState),
321 SpaceList(SpaceListState),
311 SpaceList(RoomListState),
322312 Welcome(WelcomeState),
323 ChatList(ChatListState),
324 UnreadList(UnreadListState),
325 MentionsList(MentionsListState),
326 InvitesList(InvitesListState),
313 ChatList(RoomListState),
314 UnreadList(RoomListState),
315 MentionsList(RoomListState),
316 InvitesList(RoomListState),
327317}
328318
329319impl IambWindow {
@@ -402,22 +392,10 @@ impl IambWindow {
402392 }
403393}
404394
405pub type DirectListState = ListState<DirectItem, IambInfo>;
406395pub type MemberListState = ListState<MemberItem, IambInfo>;
407pub type RoomListState = ListState<RoomItem, IambInfo>;
408pub type ChatListState = ListState<GenericChatItem, IambInfo>;
409pub type UnreadListState = ListState<GenericChatItem, IambInfo>;
410pub type MentionsListState = ListState<GenericChatItem, IambInfo>;
411pub type InvitesListState = ListState<GenericChatItem, IambInfo>;
412pub type SpaceListState = ListState<SpaceItem, IambInfo>;
396pub type RoomListState = ListState<GenericRoomItem, IambInfo>;
413397pub type VerifyListState = ListState<VerifyItem, IambInfo>;
414398
415impl From<ChatListState> for IambWindow {
416 fn from(list: ChatListState) -> Self {
417 IambWindow::ChatList(list)
418 }
419}
420
421399impl From<RoomState> for IambWindow {
422400 fn from(room: RoomState) -> Self {
423401 IambWindow::Room(room)
@@ -430,24 +408,6 @@ impl From<VerifyListState> for IambWindow {
430408 }
431409}
432410
433impl From<DirectListState> for IambWindow {
434 fn from(list: DirectListState) -> Self {
435 IambWindow::DirectList(list)
436 }
437}
438
439impl From<RoomListState> for IambWindow {
440 fn from(list: RoomListState) -> Self {
441 IambWindow::RoomList(list)
442 }
443}
444
445impl From<SpaceListState> for IambWindow {
446 fn from(list: SpaceListState) -> Self {
447 IambWindow::SpaceList(list)
448 }
449}
450
451411impl From<WelcomeState> for IambWindow {
452412 fn from(win: WelcomeState) -> Self {
453413 IambWindow::Welcome(win)
@@ -511,23 +471,30 @@ impl TerminalCursor for IambWindow {
511471
512472impl WindowOps<IambInfo> for IambWindow {
513473 fn draw(&mut self, area: Rect, buf: &mut Buffer, focused: bool, store: &mut ProgramStore) {
474 let ChatStore {
475 collator,
476 names,
477 rooms,
478 settings,
479 sync_info,
480 verifications,
481 worker,
482 ..
483 } = &mut store.application;
484
514485 match self {
515486 IambWindow::Room(state) => state.draw(area, buf, focused, store),
516487 IambWindow::DirectList(state) => {
517 let mut items = store
518 .application
519 .sync_info
488 let mut items = sync_info
520489 .dms
521 .clone()
522 .into_iter()
523 .map(|room_info| DirectItem::new(room_info, store))
490 .iter()
491 .map(|room| GenericRoomItem::new_unspecified(room, rooms, names))
524492 .collect::<Vec<_>>();
525 let fields = &store.application.settings.tunables.sort.dms;
526 let collator = &mut store.application.collator;
493 let fields = &settings.tunables.sort.dms;
527494 items.sort_by(|a, b| room_fields_cmp(a, b, fields, collator));
528495
529496 state.set(items);
530 state.set_ignorecase(store.application.settings.tunables.ignorecase);
497 state.set_ignorecase(settings.tunables.ignorecase);
531498
532499 List::new(store)
533500 .empty_message("No direct messages yet!")
@@ -541,18 +508,18 @@ impl WindowOps<IambInfo> for IambWindow {
541508 None => true,
542509 };
543510
544 if need_fetch && let Ok(mems) = store.application.worker.members(room_id.clone()) {
511 if need_fetch && let Ok(mems) = worker.members(room_id.clone()) {
545512 let mut items = mems
546513 .into_iter()
547514 .map(|m| MemberItem::new(m, room_id.clone()))
548515 .collect::<Vec<_>>();
549 let fields = &store.application.settings.tunables.sort.members;
516 let fields = &settings.tunables.sort.members;
550517 items.sort_by(|a, b| user_fields_cmp(a, b, fields));
551518 state.set(items);
552519 *last_fetch = Some(Instant::now());
553520 }
554521
555 state.set_ignorecase(store.application.settings.tunables.ignorecase);
522 state.set_ignorecase(settings.tunables.ignorecase);
556523
557524 List::new(store)
558525 .empty_message("No users here yet!")
@@ -561,20 +528,16 @@ impl WindowOps<IambInfo> for IambWindow {
561528 .render(area, buf, state);
562529 },
563530 IambWindow::RoomList(state) => {
564 let mut items = store
565 .application
566 .sync_info
531 let mut items = sync_info
567532 .rooms
568 .clone()
569 .into_iter()
570 .map(|room_info| RoomItem::new(room_info, store))
533 .iter()
534 .map(|room| GenericRoomItem::new_unspecified(room, rooms, names))
571535 .collect::<Vec<_>>();
572 let fields = &store.application.settings.tunables.sort.rooms;
573 let collator = &mut store.application.collator;
536 let fields = &settings.tunables.sort.rooms;
574537 items.sort_by(|a, b| room_fields_cmp(a, b, fields, collator));
575538
576539 state.set(items);
577 state.set_ignorecase(store.application.settings.tunables.ignorecase);
540 state.set_ignorecase(settings.tunables.ignorecase);
578541
579542 List::new(store)
580543 .empty_message("You haven't joined any rooms yet")
@@ -583,31 +546,18 @@ impl WindowOps<IambInfo> for IambWindow {
583546 .render(area, buf, state);
584547 },
585548 IambWindow::ChatList(state) => {
586 let mut items = store
587 .application
588 .sync_info
549 let mut items = sync_info
589550 .rooms
590 .clone()
591 .into_iter()
592 .map(|room_info| GenericChatItem::new(room_info, store, false))
551 .iter()
552 .chain(sync_info.dms.iter())
553 .map(|room| GenericRoomItem::new(room, rooms, names))
593554 .collect::<Vec<_>>();
594555
595 let dms = store
596 .application
597 .sync_info
598 .dms
599 .clone()
600 .into_iter()
601 .map(|room_info| GenericChatItem::new(room_info, store, true));
602
603 items.extend(dms);
604
605 let fields = &store.application.settings.tunables.sort.chats;
606 let collator = &mut store.application.collator;
556 let fields = &settings.tunables.sort.chats;
607557 items.sort_by(|a, b| room_fields_cmp(a, b, fields, collator));
608558
609559 state.set(items);
610 state.set_ignorecase(store.application.settings.tunables.ignorecase);
560 state.set_ignorecase(settings.tunables.ignorecase);
611561
612562 List::new(store)
613563 .empty_message("You do not have rooms or dms yet")
@@ -616,33 +566,19 @@ impl WindowOps<IambInfo> for IambWindow {
616566 .render(area, buf, state);
617567 },
618568 IambWindow::UnreadList(state) => {
619 let mut items = store
620 .application
621 .sync_info
569 let mut items = sync_info
622570 .rooms
623 .clone()
624 .into_iter()
625 .map(|room_info| GenericChatItem::new(room_info, store, false))
571 .iter()
572 .chain(sync_info.dms.iter())
573 .map(|room| GenericRoomItem::new(room, rooms, names))
626574 .filter(RoomLikeItem::is_unread)
627575 .collect::<Vec<_>>();
628576
629 let dms = store
630 .application
631 .sync_info
632 .dms
633 .clone()
634 .into_iter()
635 .map(|room_info| GenericChatItem::new(room_info, store, true))
636 .filter(RoomLikeItem::is_unread);
637
638 items.extend(dms);
639
640 let fields = &store.application.settings.tunables.sort.chats;
641 let collator = &mut store.application.collator;
577 let fields = &settings.tunables.sort.chats;
642578 items.sort_by(|a, b| room_fields_cmp(a, b, fields, collator));
643579
644580 state.set(items);
645 state.set_ignorecase(store.application.settings.tunables.ignorecase);
581 state.set_ignorecase(settings.tunables.ignorecase);
646582
647583 List::new(store)
648584 .empty_message("You do not have any unreads yet")
@@ -651,33 +587,19 @@ impl WindowOps<IambInfo> for IambWindow {
651587 .render(area, buf, state);
652588 },
653589 IambWindow::MentionsList(state) => {
654 let mut items = store
655 .application
656 .sync_info
590 let mut items = sync_info
657591 .rooms
658 .clone()
659 .into_iter()
660 .map(|room_info| GenericChatItem::new(room_info, store, false))
661 .filter(GenericChatItem::has_mention)
592 .iter()
593 .chain(sync_info.dms.iter())
594 .map(|room| GenericRoomItem::new(room, rooms, names))
595 .filter(RoomLikeItem::has_mention)
662596 .collect::<Vec<_>>();
663597
664 let dms = store
665 .application
666 .sync_info
667 .dms
668 .clone()
669 .into_iter()
670 .map(|room_info| GenericChatItem::new(room_info, store, true))
671 .filter(GenericChatItem::has_mention);
672
673 items.extend(dms);
674
675 let fields = &store.application.settings.tunables.sort.chats;
676 let collator = &mut store.application.collator;
598 let fields = &settings.tunables.sort.chats;
677599 items.sort_by(|a, b| room_fields_cmp(a, b, fields, collator));
678600
679601 state.set(items);
680 state.set_ignorecase(store.application.settings.tunables.ignorecase);
602 state.set_ignorecase(settings.tunables.ignorecase);
681603
682604 List::new(store)
683605 .empty_message("You do not have any unread mentions yet")
@@ -686,33 +608,19 @@ impl WindowOps<IambInfo> for IambWindow {
686608 .render(area, buf, state);
687609 },
688610 IambWindow::InvitesList(state) => {
689 let mut items = store
690 .application
691 .sync_info
611 let mut items = sync_info
692612 .rooms
693 .clone()
694 .into_iter()
695 .map(|room_info| GenericChatItem::new(room_info, store, false))
696 .filter(GenericChatItem::is_invite)
613 .iter()
614 .chain(sync_info.dms.iter())
615 .map(|room| GenericRoomItem::new(room, rooms, names))
616 .filter(RoomLikeItem::is_invite)
697617 .collect::<Vec<_>>();
698618
699 let dms = store
700 .application
701 .sync_info
702 .dms
703 .clone()
704 .into_iter()
705 .map(|room_info| GenericChatItem::new(room_info, store, true))
706 .filter(GenericChatItem::is_invite);
707
708 items.extend(dms);
709
710 let fields = &store.application.settings.tunables.sort.chats;
711 let collator = &mut store.application.collator;
619 let fields = &settings.tunables.sort.chats;
712620 items.sort_by(|a, b| room_fields_cmp(a, b, fields, collator));
713621
714622 state.set(items);
715 state.set_ignorecase(store.application.settings.tunables.ignorecase);
623 state.set_ignorecase(settings.tunables.ignorecase);
716624
717625 List::new(store)
718626 .empty_message("You do not have any open invites")
@@ -721,20 +629,17 @@ impl WindowOps<IambInfo> for IambWindow {
721629 .render(area, buf, state);
722630 },
723631 IambWindow::SpaceList(state) => {
724 let mut items = store
725 .application
726 .sync_info
632 let mut items = sync_info
727633 .spaces
728 .clone()
729 .into_iter()
730 .map(|room| SpaceItem::new(room, store))
634 .iter()
635 .map(|room| GenericRoomItem::new_unspecified(room, rooms, names))
731636 .collect::<Vec<_>>();
732 let fields = &store.application.settings.tunables.sort.spaces;
733 let collator = &mut store.application.collator;
637
638 let fields = &settings.tunables.sort.spaces;
734639 items.sort_by(|a, b| room_fields_cmp(a, b, fields, collator));
735640
736641 state.set(items);
737 state.set_ignorecase(store.application.settings.tunables.ignorecase);
642 state.set_ignorecase(settings.tunables.ignorecase);
738643
739644 List::new(store)
740645 .empty_message("You haven't joined any spaces yet")
@@ -743,9 +648,7 @@ impl WindowOps<IambInfo> for IambWindow {
743648 .render(area, buf, state);
744649 },
745650 IambWindow::VerifyList(state) => {
746 let mut items = store
747 .application
748 .verifications
651 let mut items = verifications
749652 .iter()
750653 .map(|(_, req)| VerifyItem::new(req.to_owned()))
751654 .collect::<Vec<_>>();
@@ -758,7 +661,7 @@ impl WindowOps<IambInfo> for IambWindow {
758661 }
759662
760663 state.set(items);
761 state.set_ignorecase(store.application.settings.tunables.ignorecase);
664 state.set_ignorecase(settings.tunables.ignorecase);
762665
763666 List::new(store)
764667 .empty_message("No in-progress verifications")
@@ -773,18 +676,18 @@ impl WindowOps<IambInfo> for IambWindow {
773676 fn dup(&self, store: &mut ProgramStore) -> Self {
774677 match self {
775678 IambWindow::Room(w) => w.dup(store).into(),
776 IambWindow::DirectList(w) => w.dup(store).into(),
679 IambWindow::DirectList(w) => Self::DirectList(w.dup(store)),
777680 IambWindow::MemberList(w, room_id, last_fetch) => {
778681 IambWindow::MemberList(w.dup(store), room_id.clone(), *last_fetch)
779682 },
780 IambWindow::RoomList(w) => w.dup(store).into(),
781 IambWindow::SpaceList(w) => w.dup(store).into(),
683 IambWindow::RoomList(w) => Self::RoomList(w.dup(store)),
684 IambWindow::SpaceList(w) => Self::SpaceList(w.dup(store)),
782685 IambWindow::VerifyList(w) => w.dup(store).into(),
783686 IambWindow::Welcome(w) => w.dup(store).into(),
784 IambWindow::ChatList(w) => w.dup(store).into(),
785 IambWindow::UnreadList(w) => w.dup(store).into(),
786 IambWindow::MentionsList(w) => w.dup(store).into(),
787 IambWindow::InvitesList(w) => w.dup(store).into(),
687 IambWindow::ChatList(w) => Self::ChatList(w.dup(store)),
688 IambWindow::UnreadList(w) => Self::UnreadList(w.dup(store)),
689 IambWindow::MentionsList(w) => Self::MentionsList(w.dup(store)),
690 IambWindow::InvitesList(w) => Self::InvitesList(w.dup(store)),
788691 }
789692 }
790693
@@ -902,26 +805,26 @@ impl Window<IambInfo> for IambWindow {
902805 return Ok(RoomState::not_joined(name).into());
903806 },
904807 IambId::DirectList => {
905 let list = DirectListState::new(IambBufferId::DirectList, vec![]);
808 let list = RoomListState::new(IambBufferId::DirectList, vec![]);
906809
907 return Ok(list.into());
810 return Ok(Self::DirectList(list));
908811 },
909812 IambId::MemberList(room_id) => {
910813 let id = IambBufferId::MemberList(room_id.clone());
911814 let list = MemberListState::new(id, vec![]);
912 let win = IambWindow::MemberList(list, room_id, None);
815 let win = Self::MemberList(list, room_id, None);
913816
914817 return Ok(win);
915818 },
916819 IambId::RoomList => {
917820 let list = RoomListState::new(IambBufferId::RoomList, vec![]);
918821
919 return Ok(list.into());
822 return Ok(Self::RoomList(list));
920823 },
921824 IambId::SpaceList => {
922 let list = SpaceListState::new(IambBufferId::SpaceList, vec![]);
825 let list = RoomListState::new(IambBufferId::SpaceList, vec![]);
923826
924 return Ok(list.into());
827 return Ok(Self::SpaceList(list));
925828 },
926829 IambId::VerifyList => {
927830 let list = VerifyListState::new(IambBufferId::VerifyList, vec![]);
@@ -934,24 +837,24 @@ impl Window<IambInfo> for IambWindow {
934837 return Ok(win.into());
935838 },
936839 IambId::ChatList => {
937 let list = ChatListState::new(IambBufferId::ChatList, vec![]);
840 let list = RoomListState::new(IambBufferId::ChatList, vec![]);
938841
939 Ok(list.into())
842 return Ok(Self::ChatList(list));
940843 },
941844 IambId::UnreadList => {
942 let list = UnreadListState::new(IambBufferId::UnreadList, vec![]);
845 let list = RoomListState::new(IambBufferId::UnreadList, vec![]);
943846
944 Ok(IambWindow::UnreadList(list))
847 Ok(Self::UnreadList(list))
945848 },
946849 IambId::MentionsList => {
947 let list = MentionsListState::new(IambBufferId::MentionsList, vec![]);
850 let list = RoomListState::new(IambBufferId::MentionsList, vec![]);
948851
949 Ok(IambWindow::MentionsList(list))
852 Ok(Self::MentionsList(list))
950853 },
951854 IambId::InvitesList => {
952 let list = InvitesListState::new(IambBufferId::InvitesList, vec![]);
855 let list = RoomListState::new(IambBufferId::InvitesList, vec![]);
953856
954 Ok(IambWindow::InvitesList(list))
857 Ok(Self::InvitesList(list))
955858 },
956859 }
957860 }
@@ -978,317 +881,130 @@ impl Window<IambInfo> for IambWindow {
978881 }
979882}
980883
981#[derive(Clone)]
982pub struct GenericChatItem {
983 room_info: MatrixRoomInfo,
984 name: String,
985 alias: Option<OwnedRoomAliasId>,
986 unread: UnreadInfo,
987 is_dm: bool,
988}
989
990impl GenericChatItem {
991 fn new(room_info: MatrixRoomInfo, store: &mut ProgramStore, is_dm: bool) -> Self {
992 let room = &room_info.deref().0;
993 let room_id = room.room_id();
994
995 let info = store.application.rooms.get_or_default(room_id.to_owned());
996 let name = info.name.clone().unwrap_or_default();
997 let alias = room.canonical_alias();
998 let unread = info.unreads(room);
999 info.tags.clone_from(&room_info.deref().1);
1000
1001 if let Some(alias) = &alias {
1002 store.application.names.insert(alias.to_owned(), room_id.to_owned());
1003 }
1004
1005 GenericChatItem { room_info, name, alias, is_dm, unread }
1006 }
1007
1008 #[inline]
1009 fn room(&self) -> &MatrixRoom {
1010 &self.room_info.deref().0
1011 }
1012
1013 #[inline]
1014 fn tags(&self) -> &Option<Tags> {
1015 &self.room_info.deref().1
1016 }
1017
1018 #[inline]
1019 fn has_mention(&self) -> bool {
1020 self.unread.has_mention()
1021 }
1022}
1023
1024impl RoomLikeItem for GenericChatItem {
1025 fn name(&self) -> &str {
1026 self.name.as_str()
1027 }
1028
1029 fn alias(&self) -> Option<&RoomAliasId> {
1030 self.alias.as_deref()
1031 }
1032
1033 fn room_id(&self) -> &RoomId {
1034 self.room().room_id()
1035 }
1036
1037 fn has_tag(&self, tag: TagName) -> bool {
1038 if let Some(tags) = &self.room_info.deref().1 {
1039 tags.contains_key(&tag)
1040 } else {
1041 false
1042 }
1043 }
1044
1045 fn recent_ts(&self) -> Option<&MessageTimeStamp> {
1046 self.unread.latest()
1047 }
1048
1049 fn is_unread(&self) -> bool {
1050 self.unread.is_unread()
1051 }
1052
1053 fn is_invite(&self) -> bool {
1054 self.room().state() == MatrixRoomState::Invited
1055 }
1056}
884/// This is used to determine what tag to show on a [`GenericRoomItem`].
885#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
886enum RoomType {
887 DM,
888 Room,
889 Space,
1057890
1058impl Display for GenericChatItem {
1059 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1060 write!(f, "{}", self.name)
1061 }
891 /// Don't show a tag
892 Unspecified,
1062893}
1063894
1064impl ListItem<IambInfo> for GenericChatItem {
1065 fn show(
1066 &self,
1067 selected: bool,
1068 _: &ViewportContext<ListCursor>,
1069 _: &mut ProgramStore,
1070 ) -> Text<'_> {
1071 let style = selected_style(selected);
1072 let (name, mut labels) = name_and_labels(&self.name, &self.unread, self.room(), style);
1073 let mut spans = vec![name];
1074
1075 labels.push(if self.is_dm {
1076 vec![Span::styled("DM", style)]
1077 } else {
1078 vec![Span::styled("Room", style)]
1079 });
1080
1081 if let Some(tags) = &self.tags() {
1082 labels.extend(tags.keys().map(|t| tag_to_span(t, style)));
895impl RoomType {
896 fn text(self) -> Option<&'static str> {
897 match self {
898 RoomType::DM => Some("DM"),
899 RoomType::Room => Some("Room"),
900 RoomType::Space => Some("Space"),
901 RoomType::Unspecified => None,
1083902 }
1084
1085 append_tags(labels, &mut spans, style);
1086 Text::from(Line::from(spans))
1087 }
1088
1089 fn get_word(&self) -> Option<String> {
1090 self.room_id().to_string().into()
1091903 }
1092904}
1093905
1094impl Promptable<ProgramContext, ProgramStore, IambInfo> for GenericChatItem {
1095 fn prompt(
1096 &mut self,
1097 act: &PromptAction,
1098 ctx: &ProgramContext,
1099 _: &mut ProgramStore,
1100 ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
1101 room_prompt(self.room_id(), act, ctx)
1102 }
1103}
1104
1105#[derive(Clone)]
1106pub struct RoomItem {
1107 room_info: MatrixRoomInfo,
906#[derive(Debug, Clone)]
907pub struct GenericRoomItem {
908 room_id: OwnedRoomId,
1108909 name: String,
1109910 alias: Option<OwnedRoomAliasId>,
911 tags: Option<Tags>,
912 membership: MatrixRoomState,
1110913 unread: UnreadInfo,
914
915 room_type: RoomType,
1111916}
1112917
1113impl RoomItem {
1114 fn new(room_info: MatrixRoomInfo, store: &mut ProgramStore) -> Self {
1115 let room = &room_info.deref().0;
1116 let room_id = room.room_id();
918impl GenericRoomItem {
919 pub fn new_unspecified(
920 room: &MatrixRoom,
921 rooms: &mut CompletionMap<OwnedRoomId, RoomInfo>,
922 names: &mut CompletionMap<OwnedRoomAliasId, OwnedRoomId>,
923 ) -> Self {
924 let room_id = room.room_id().to_owned();
1117925
1118 let info = store.application.rooms.get_or_default(room_id.to_owned());
926 let info = rooms.get_or_default(room_id.to_owned());
1119927 let name = info.name.clone().unwrap_or_default();
1120928 let alias = room.canonical_alias();
1121929 let unread = info.unreads(room);
1122 info.tags.clone_from(&room_info.deref().1);
930 let tags = info.tags.clone();
1123931
1124932 if let Some(alias) = &alias {
1125 store.application.names.insert(alias.to_owned(), room_id.to_owned());
933 names.insert(alias.to_owned(), room_id.to_owned());
1126934 }
1127935
1128 RoomItem { room_info, name, alias, unread }
936 Self {
937 name,
938 room_id,
939 alias,
940 tags,
941 unread,
942 membership: room.state(),
943 room_type: RoomType::Unspecified,
944 }
1129945 }
1130946
1131 #[inline]
1132 fn room(&self) -> &MatrixRoom {
1133 &self.room_info.deref().0
1134 }
947 pub fn new(
948 room: &MatrixRoom,
949 rooms: &mut CompletionMap<OwnedRoomId, RoomInfo>,
950 names: &mut CompletionMap<OwnedRoomAliasId, OwnedRoomId>,
951 ) -> Self {
952 let room_type = if room.is_space() {
953 RoomType::Space
954 } else if room.is_dm() {
955 RoomType::DM
956 } else {
957 RoomType::Room
958 };
1135959
1136 #[inline]
1137 fn tags(&self) -> &Option<Tags> {
1138 &self.room_info.deref().1
960 assign!(Self::new_unspecified(room, rooms, names), { room_type })
1139961 }
1140962}
1141963
1142impl RoomLikeItem for RoomItem {
1143 fn name(&self) -> &str {
1144 self.name.as_str()
1145 }
1146
1147 fn alias(&self) -> Option<&RoomAliasId> {
1148 self.alias.as_deref()
1149 }
1150
964impl RoomLikeItem for GenericRoomItem {
1151965 fn room_id(&self) -> &RoomId {
1152 self.room().room_id()
966 &self.room_id
1153967 }
1154968
1155969 fn has_tag(&self, tag: TagName) -> bool {
1156 if let Some(tags) = &self.room_info.deref().1 {
1157 tags.contains_key(&tag)
1158 } else {
1159 false
1160 }
1161 }
1162
1163 fn recent_ts(&self) -> Option<&MessageTimeStamp> {
1164 self.unread.latest()
970 self.tags.as_ref().is_some_and(|tags| tags.contains_key(&tag))
1165971 }
1166972
1167973 fn is_unread(&self) -> bool {
974 // XXX: check space children for space
1168975 self.unread.is_unread()
1169976 }
1170977
1171 fn is_invite(&self) -> bool {
1172 self.room().state() == MatrixRoomState::Invited
1173 }
1174}
1175
1176impl Display for RoomItem {
1177 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1178 write!(f, "{}", self.name)
1179 }
1180}
1181
1182impl ListItem<IambInfo> for RoomItem {
1183 fn show(
1184 &self,
1185 selected: bool,
1186 _: &ViewportContext<ListCursor>,
1187 _: &mut ProgramStore,
1188 ) -> Text<'_> {
1189 let style = selected_style(selected);
1190 let (name, mut labels) = name_and_labels(&self.name, &self.unread, self.room(), style);
1191 let mut spans = vec![name];
1192
1193 if let Some(tags) = &self.tags() {
1194 labels.extend(tags.keys().map(|t| tag_to_span(t, style)));
1195 }
1196
1197 append_tags(labels, &mut spans, style);
1198
1199 Text::from(Line::from(spans))
1200 }
1201
1202 fn get_word(&self) -> Option<String> {
1203 self.room_id().to_string().into()
1204 }
1205}
1206
1207impl Promptable<ProgramContext, ProgramStore, IambInfo> for RoomItem {
1208 fn prompt(
1209 &mut self,
1210 act: &PromptAction,
1211 ctx: &ProgramContext,
1212 _: &mut ProgramStore,
1213 ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
1214 room_prompt(self.room_id(), act, ctx)
1215 }
1216}
1217
1218#[derive(Clone)]
1219pub struct DirectItem {
1220 room_info: MatrixRoomInfo,
1221 name: String,
1222 alias: Option<OwnedRoomAliasId>,
1223 unread: UnreadInfo,
1224}
1225
1226impl DirectItem {
1227 fn new(room_info: MatrixRoomInfo, store: &mut ProgramStore) -> Self {
1228 let room = &room_info.deref().0;
1229 let room_id = room_info.0.room_id().to_owned();
1230 let alias = room_info.0.canonical_alias();
1231
1232 let info = store.application.rooms.get_or_default(room_id);
1233 let name = info.name.clone().unwrap_or_default();
1234 let unread = info.unreads(room);
1235 info.tags.clone_from(&room_info.deref().1);
1236
1237 DirectItem { room_info, name, alias, unread }
1238 }
1239
1240 #[inline]
1241 fn room(&self) -> &MatrixRoom {
1242 &self.room_info.deref().0
1243 }
1244
1245 #[inline]
1246 fn tags(&self) -> &Option<Tags> {
1247 &self.room_info.deref().1
1248 }
1249}
1250
1251impl RoomLikeItem for DirectItem {
1252 fn name(&self) -> &str {
1253 self.name.as_str()
978 fn recent_ts(&self) -> Option<&MessageTimeStamp> {
979 // XXX: check space children for space
980 self.unread.latest()
1254981 }
1255982
1256983 fn alias(&self) -> Option<&RoomAliasId> {
1257984 self.alias.as_deref()
1258985 }
1259986
1260 fn has_tag(&self, tag: TagName) -> bool {
1261 if let Some(tags) = &self.room_info.deref().1 {
1262 tags.contains_key(&tag)
1263 } else {
1264 false
1265 }
1266 }
1267
1268 fn room_id(&self) -> &RoomId {
1269 self.room().room_id()
1270 }
1271
1272 fn recent_ts(&self) -> Option<&MessageTimeStamp> {
1273 self.unread.latest()
987 fn name(&self) -> &str {
988 &self.name
1274989 }
1275990
1276 fn is_unread(&self) -> bool {
1277 self.unread.is_unread()
991 fn is_invite(&self) -> bool {
992 self.membership == MatrixRoomState::Invited
1278993 }
1279994
1280 fn is_invite(&self) -> bool {
1281 self.room().state() == MatrixRoomState::Invited
995 fn has_mention(&self) -> bool {
996 // XXX: check space children for space
997 self.unread.has_mention()
1282998 }
1283999}
12841000
1285impl Display for DirectItem {
1001impl Display for GenericRoomItem {
12861002 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1287 write!(f, ":verify request {}", self.name)
1003 write!(f, "{}", self.name)
12881004 }
12891005}
12901006
1291impl ListItem<IambInfo> for DirectItem {
1007impl ListItem<IambInfo> for GenericRoomItem {
12921008 fn show(
12931009 &self,
12941010 selected: bool,
@@ -1296,121 +1012,23 @@ impl ListItem<IambInfo> for DirectItem {
12961012 _: &mut ProgramStore,
12971013 ) -> Text<'_> {
12981014 let style = selected_style(selected);
1299 let (name, mut labels) = name_and_labels(&self.name, &self.unread, self.room(), style);
1015 let (name, mut labels) = name_and_labels(&self.name, &self.unread, self.membership, style);
13001016 let mut spans = vec![name];
13011017
1302 if let Some(tags) = &self.tags() {
1018 if let Some(label) = self.room_type.text() {
1019 labels.push(vec![Span::styled(label, style)]);
1020 }
1021
1022 if let Some(tags) = &self.tags {
13031023 labels.extend(tags.keys().map(|t| tag_to_span(t, style)));
13041024 }
13051025
13061026 append_tags(labels, &mut spans, style);
1307
13081027 Text::from(Line::from(spans))
13091028 }
1310
1311 fn get_word(&self) -> Option<String> {
1312 self.room_id().to_string().into()
1313 }
1314}
1315
1316impl Promptable<ProgramContext, ProgramStore, IambInfo> for DirectItem {
1317 fn prompt(
1318 &mut self,
1319 act: &PromptAction,
1320 ctx: &ProgramContext,
1321 _: &mut ProgramStore,
1322 ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> {
1323 room_prompt(self.room_id(), act, ctx)
1324 }
1325}
1326
1327#[derive(Clone)]
1328pub struct SpaceItem {
1329 room_info: MatrixRoomInfo,
1330 name: String,
1331 alias: Option<OwnedRoomAliasId>,
1332}
1333
1334impl SpaceItem {
1335 fn new(room_info: MatrixRoomInfo, store: &mut ProgramStore) -> Self {
1336 let room_id = room_info.0.room_id();
1337 let name = store
1338 .application
1339 .get_room_info(room_id.to_owned())
1340 .name
1341 .clone()
1342 .unwrap_or_default();
1343 let alias = room_info.0.canonical_alias();
1344
1345 if let Some(alias) = &alias {
1346 store.application.names.insert(alias.to_owned(), room_id.to_owned());
1347 }
1348
1349 SpaceItem { room_info, name, alias }
1350 }
1351
1352 #[inline]
1353 fn room(&self) -> &MatrixRoom {
1354 &self.room_info.deref().0
1355 }
1356}
1357
1358impl RoomLikeItem for SpaceItem {
1359 fn name(&self) -> &str {
1360 self.name.as_str()
1361 }
1362
1363 fn room_id(&self) -> &RoomId {
1364 self.room().room_id()
1365 }
1366
1367 fn alias(&self) -> Option<&RoomAliasId> {
1368 self.alias.as_deref()
1369 }
1370
1371 fn has_tag(&self, _: TagName) -> bool {
1372 // I think that spaces can technically have tags, but afaik no client
1373 // exposes them, so we'll just always return false here for now.
1374 false
1375 }
1376
1377 fn recent_ts(&self) -> Option<&MessageTimeStamp> {
1378 // XXX: this needs to determine the room with most recent message and return its timestamp.
1379 None
1380 }
1381
1382 fn is_unread(&self) -> bool {
1383 // XXX: this needs to check whether the space contains rooms with unread messages
1384 false
1385 }
1386
1387 fn is_invite(&self) -> bool {
1388 self.room().state() == MatrixRoomState::Invited
1389 }
13901029}
13911030
1392impl Display for SpaceItem {
1393 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1394 write!(f, "{}", self.name)
1395 }
1396}
1397
1398impl ListItem<IambInfo> for SpaceItem {
1399 fn show(
1400 &self,
1401 selected: bool,
1402 _: &ViewportContext<ListCursor>,
1403 _: &mut ProgramStore,
1404 ) -> Text<'_> {
1405 selected_text(self.name.as_str(), selected)
1406 }
1407
1408 fn get_word(&self) -> Option<String> {
1409 self.room_id().to_string().into()
1410 }
1411}
1412
1413impl Promptable<ProgramContext, ProgramStore, IambInfo> for SpaceItem {
1031impl Promptable<ProgramContext, ProgramStore, IambInfo> for GenericRoomItem {
14141032 fn prompt(
14151033 &mut self,
14161034 act: &PromptAction,
@@ -1609,6 +1227,10 @@ mod tests {
16091227 fn is_invite(&self) -> bool {
16101228 self.invite
16111229 }
1230
1231 fn has_mention(&self) -> bool {
1232 false
1233 }
16121234 }
16131235
16141236 #[test]
src/windows/room/space.rs +10 −10
@@ -6,7 +6,7 @@ use matrix_sdk::ruma::events::space::child::SpaceChildEventContent;
66use modalkit_ratatui::list::{List, ListState};
77
88use crate::prelude::*;
9use crate::windows::{RoomItem, RoomLikeItem, room_fields_cmp};
9use crate::windows::{GenericRoomItem, RoomLikeItem, room_fields_cmp};
1010
1111const SPACE_HIERARCHY_DEBOUNCE: Duration = Duration::from_secs(5);
1212
@@ -14,7 +14,7 @@ const SPACE_HIERARCHY_DEBOUNCE: Duration = Duration::from_secs(5);
1414pub struct SpaceState {
1515 room_id: OwnedRoomId,
1616 room: MatrixRoom,
17 list: ListState<RoomItem, IambInfo>,
17 list: ListState<GenericRoomItem, IambInfo>,
1818 last_fetch: Option<Instant>,
1919}
2020
@@ -155,7 +155,7 @@ impl TerminalCursor for SpaceState {
155155}
156156
157157impl Deref for SpaceState {
158 type Target = ListState<RoomItem, IambInfo>;
158 type Target = ListState<GenericRoomItem, IambInfo>;
159159
160160 fn deref(&self) -> &Self::Target {
161161 &self.list
@@ -189,7 +189,9 @@ impl StatefulWidget for Space<'_> {
189189 type State = SpaceState;
190190
191191 fn render(self, area: Rect, buffer: &mut Buffer, state: &mut Self::State) {
192 state.set_ignorecase(self.store.application.settings.tunables.ignorecase);
192 let ChatStore { rooms, names, worker, settings, collator, .. } =
193 &mut self.store.application;
194 state.set_ignorecase(settings.tunables.ignorecase);
193195
194196 let mut empty_message = None;
195197 let need_fetch = match state.last_fetch {
@@ -198,26 +200,24 @@ impl StatefulWidget for Space<'_> {
198200 };
199201
200202 if need_fetch {
201 let res = self.store.application.worker.space_members(state.room_id.clone());
203 let res = worker.space_members(state.room_id.clone());
202204
203205 match res {
204206 Ok(members) => {
205207 let mut items = members
206208 .into_iter()
207209 .filter_map(|id| {
208 let (room, _, tags) =
209 self.store.application.worker.get_room(id.clone()).ok()?;
210 let (room, _, tags) = worker.get_room(id.clone()).ok()?;
210211 let room_info = std::sync::Arc::new((room, tags));
211212
212213 if id != state.room_id {
213 Some(RoomItem::new(room_info, self.store))
214 Some(GenericRoomItem::new(&room_info.0, rooms, names))
214215 } else {
215216 None
216217 }
217218 })
218219 .collect::<Vec<_>>();
219 let fields = &self.store.application.settings.tunables.sort.rooms;
220 let collator = &mut self.store.application.collator;
220 let fields = &settings.tunables.sort.rooms;
221221 items.sort_by(|a, b| room_fields_cmp(a, b, fields, collator));
222222
223223 state.list.set(items);
src/worker.rs +9 −9
@@ -397,7 +397,7 @@ async fn load_older_forever(client: &Client, store: &AsyncProgramStore) {
397397}
398398
399399async fn refresh_rooms(client: &Client, store: &AsyncProgramStore, first_sync: bool) {
400 let mut names = vec![];
400 let mut names_and_tags = vec![];
401401
402402 let mut spaces = vec![];
403403 let mut rooms = vec![];
@@ -422,14 +422,14 @@ async fn refresh_rooms(client: &Client, store: &AsyncProgramStore, first_sync: b
422422 let name = display.to_string();
423423 let tags = room.tags().await.unwrap_or_default();
424424
425 names.push((room.room_id().to_owned(), name));
425 names_and_tags.push((room.room_id().to_owned(), name, tags));
426426
427427 if room.is_direct().await.unwrap_or_default() {
428 dms.push(Arc::new((room, tags)));
428 dms.push(room);
429429 } else if room.is_space() {
430 spaces.push(Arc::new((room, tags)));
430 spaces.push(room);
431431 } else {
432 rooms.push(Arc::new((room, tags)));
432 rooms.push(room);
433433 }
434434 }
435435
@@ -438,8 +438,8 @@ async fn refresh_rooms(client: &Client, store: &AsyncProgramStore, first_sync: b
438438 locked.application.sync_info.rooms = rooms;
439439 locked.application.sync_info.dms = dms;
440440
441 for (room_id, name) in names {
442 locked.application.set_room_name(&room_id, &name);
441 for (room_id, name, tags) in names_and_tags {
442 locked.application.set_room_info(room_id, name, tags);
443443 }
444444}
445445
@@ -657,12 +657,12 @@ pub async fn do_first_sync(client: &Client, store: &AsyncProgramStore) -> Result
657657 let ChatStore { sync_info, need_load, .. } = &mut locked.application;
658658
659659 for room in sync_info.rooms.iter() {
660 let room_id = room.as_ref().0.room_id().to_owned();
660 let room_id = room.room_id().to_owned();
661661 need_load.need_messages(room_id);
662662 }
663663
664664 for room in sync_info.dms.iter() {
665 let room_id = room.as_ref().0.room_id().to_owned();
665 let room_id = room.room_id().to_owned();
666666 need_load.need_messages(room_id);
667667 }
668668