Commit d8713141f2

d8713141f26dbb28f504ad46ea94a2b8bd63de14

parent: a6888bbc93

Unregistered key

Ulyssa <git@ulyssa.dev> · 2023-01-26 23:23 UTC

Display room tags in list of direct messages (#21)

Layout: unified · split

src/windows/mod.rs +98 −52
@@ -124,6 +124,57 @@ fn room_cmp(a: &MatrixRoom, b: &MatrixRoom) -> Ordering {
124124 ord.then_with(|| a.room_id().cmp(b.room_id()))
125125}
126126
127fn tag_cmp(a: &Option<Tags>, b: &Option<Tags>) -> Ordering {
128 let (fava, lowa) = a
129 .as_ref()
130 .map(|tags| {
131 (tags.contains_key(&TagName::Favorite), tags.contains_key(&TagName::LowPriority))
132 })
133 .unwrap_or((false, false));
134
135 let (favb, lowb) = b
136 .as_ref()
137 .map(|tags| {
138 (tags.contains_key(&TagName::Favorite), tags.contains_key(&TagName::LowPriority))
139 })
140 .unwrap_or((false, false));
141
142 // If a has Favorite and b doesn't, it should sort earlier in room list.
143 let cmpf = favb.cmp(&fava);
144
145 // If a has LowPriority and b doesn't, it should sort later in room list.
146 let cmpl = lowa.cmp(&lowb);
147
148 cmpl.then(cmpf)
149}
150
151fn append_tags<'a>(tags: &'a Tags, spans: &mut Vec<Span<'a>>, style: Style) {
152 if tags.is_empty() {
153 return;
154 }
155
156 spans.push(Span::styled(" (", style));
157
158 for (i, tag) in tags.keys().enumerate() {
159 if i > 0 {
160 spans.push(Span::styled(", ", style));
161 }
162
163 match tag {
164 TagName::Favorite => spans.push(Span::styled("Favorite", style)),
165 TagName::LowPriority => spans.push(Span::styled("Low Priority", style)),
166 TagName::ServerNotice => spans.push(Span::styled("Server Notice", style)),
167 TagName::User(tag) => {
168 spans.push(Span::styled("User Tag: ", style));
169 spans.push(Span::styled(tag.as_ref(), style));
170 },
171 tag => spans.push(Span::styled(format!("{:?}", tag), style)),
172 }
173 }
174
175 spans.push(Span::styled(")", style));
176}
177
127178#[inline]
128179fn room_prompt(
129180 room_id: &RoomId,
@@ -326,8 +377,13 @@ impl WindowOps<IambInfo> for IambWindow {
326377 IambWindow::Room(state) => state.draw(area, buf, focused, store),
327378 IambWindow::DirectList(state) => {
328379 let dms = store.application.worker.direct_messages();
329 let items = dms.into_iter().map(|(id, name)| DirectItem::new(id, name, store));
330 state.set(items.collect());
380 let mut items = dms
381 .into_iter()
382 .map(|(id, name, tags)| DirectItem::new(id, name, tags, store))
383 .collect::<Vec<_>>();
384 items.sort();
385
386 state.set(items);
331387
332388 List::new(store)
333389 .empty_message("No direct messages yet!")
@@ -583,29 +639,7 @@ impl Eq for RoomItem {}
583639
584640impl Ord for RoomItem {
585641 fn cmp(&self, other: &Self) -> Ordering {
586 let (fava, lowa) = self
587 .tags
588 .as_ref()
589 .map(|tags| {
590 (tags.contains_key(&TagName::Favorite), tags.contains_key(&TagName::LowPriority))
591 })
592 .unwrap_or((false, false));
593
594 let (favb, lowb) = other
595 .tags
596 .as_ref()
597 .map(|tags| {
598 (tags.contains_key(&TagName::Favorite), tags.contains_key(&TagName::LowPriority))
599 })
600 .unwrap_or((false, false));
601
602 // If self has Favorite and other doesn't, it should sort earlier in room list.
603 let cmpf = favb.cmp(&fava);
604
605 // If self has LowPriority and other doesn't, it should sort later in room list.
606 let cmpl = lowa.cmp(&lowb);
607
608 cmpl.then(cmpf).then_with(|| room_cmp(&self.room, &other.room))
642 tag_cmp(&self.tags, &other.tags).then_with(|| room_cmp(&self.room, &other.room))
609643 }
610644}
611645
@@ -627,30 +661,7 @@ impl ListItem<IambInfo> for RoomItem {
627661 let style = selected_style(selected);
628662 let mut spans = vec![Span::styled(self.name.as_str(), style)];
629663
630 if tags.is_empty() {
631 return Text::from(Spans(spans));
632 }
633
634 spans.push(Span::styled(" (", style));
635
636 for (i, tag) in tags.keys().enumerate() {
637 if i > 0 {
638 spans.push(Span::styled(", ", style));
639 }
640
641 match tag {
642 TagName::Favorite => spans.push(Span::styled("Favorite", style)),
643 TagName::LowPriority => spans.push(Span::styled("Low Priority", style)),
644 TagName::ServerNotice => spans.push(Span::styled("Server Notice", style)),
645 TagName::User(tag) => {
646 spans.push(Span::styled("User Tag: ", style));
647 spans.push(Span::styled(tag.as_ref(), style));
648 },
649 tag => spans.push(Span::styled(format!("{:?}", tag), style)),
650 }
651 }
652
653 spans.push(Span::styled(")", style));
664 append_tags(tags, &mut spans, style);
654665
655666 Text::from(Spans(spans))
656667 } else {
@@ -677,16 +688,22 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for RoomItem {
677688#[derive(Clone)]
678689pub struct DirectItem {
679690 room: MatrixRoom,
691 tags: Option<Tags>,
680692 name: String,
681693}
682694
683695impl DirectItem {
684 fn new(room: MatrixRoom, name: DisplayName, store: &mut ProgramStore) -> Self {
696 fn new(
697 room: MatrixRoom,
698 name: DisplayName,
699 tags: Option<Tags>,
700 store: &mut ProgramStore,
701 ) -> Self {
685702 let name = name.to_string();
686703
687704 store.application.set_room_name(room.room_id(), name.as_str());
688705
689 DirectItem { room, name }
706 DirectItem { room, tags, name }
690707 }
691708}
692709
@@ -698,7 +715,16 @@ impl ToString for DirectItem {
698715
699716impl ListItem<IambInfo> for DirectItem {
700717 fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text {
701 selected_text(self.name.as_str(), selected)
718 if let Some(tags) = &self.tags {
719 let style = selected_style(selected);
720 let mut spans = vec![Span::styled(self.name.as_str(), style)];
721
722 append_tags(tags, &mut spans, style);
723
724 Text::from(Spans(spans))
725 } else {
726 selected_text(self.name.as_str(), selected)
727 }
702728 }
703729
704730 fn get_word(&self) -> Option<String> {
@@ -706,6 +732,26 @@ impl ListItem<IambInfo> for DirectItem {
706732 }
707733}
708734
735impl PartialEq for DirectItem {
736 fn eq(&self, other: &Self) -> bool {
737 self.room.room_id() == other.room.room_id()
738 }
739}
740
741impl Eq for DirectItem {}
742
743impl Ord for DirectItem {
744 fn cmp(&self, other: &Self) -> Ordering {
745 tag_cmp(&self.tags, &other.tags).then_with(|| room_cmp(&self.room, &other.room))
746 }
747}
748
749impl PartialOrd for DirectItem {
750 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
751 self.cmp(other).into()
752 }
753}
754
709755impl Promptable<ProgramContext, ProgramStore, IambInfo> for DirectItem {
710756 fn prompt(
711757 &mut self,
src/worker.rs +17 −24
@@ -99,14 +99,16 @@ fn oneshot<T>() -> (ClientReply<T>, ClientResponse<T>) {
9999 return (reply, response);
100100}
101101
102pub type FetchedRoom = (MatrixRoom, DisplayName, Option<Tags>);
103
102104pub enum WorkerTask {
103 ActiveRooms(ClientReply<Vec<(MatrixRoom, DisplayName, Option<Tags>)>>),
104 DirectMessages(ClientReply<Vec<(MatrixRoom, DisplayName)>>),
105 ActiveRooms(ClientReply<Vec<FetchedRoom>>),
106 DirectMessages(ClientReply<Vec<FetchedRoom>>),
105107 Init(AsyncProgramStore, ClientReply<()>),
106108 LoadOlder(OwnedRoomId, Option<String>, u32, ClientReply<MessageFetchResult>),
107109 Login(LoginStyle, ClientReply<IambResult<EditInfo>>),
108110 GetInviter(Invited, ClientReply<IambResult<Option<RoomMember>>>),
109 GetRoom(OwnedRoomId, ClientReply<IambResult<(MatrixRoom, DisplayName, Option<Tags>)>>),
111 GetRoom(OwnedRoomId, ClientReply<IambResult<FetchedRoom>>),
110112 JoinRoom(String, ClientReply<IambResult<OwnedRoomId>>),
111113 Members(OwnedRoomId, ClientReply<IambResult<Vec<RoomMember>>>),
112114 SpaceMembers(OwnedRoomId, ClientReply<IambResult<Vec<OwnedRoomId>>>),
@@ -235,7 +237,7 @@ impl Requester {
235237 return response.recv();
236238 }
237239
238 pub fn direct_messages(&self) -> Vec<(MatrixRoom, DisplayName)> {
240 pub fn direct_messages(&self) -> Vec<FetchedRoom> {
239241 let (reply, response) = oneshot();
240242
241243 self.tx.send(WorkerTask::DirectMessages(reply)).unwrap();
@@ -251,10 +253,7 @@ impl Requester {
251253 return response.recv();
252254 }
253255
254 pub fn get_room(
255 &self,
256 room_id: OwnedRoomId,
257 ) -> IambResult<(MatrixRoom, DisplayName, Option<Tags>)> {
256 pub fn get_room(&self, room_id: OwnedRoomId) -> IambResult<FetchedRoom> {
258257 let (reply, response) = oneshot();
259258
260259 self.tx.send(WorkerTask::GetRoom(room_id, reply)).unwrap();
@@ -270,7 +269,7 @@ impl Requester {
270269 return response.recv();
271270 }
272271
273 pub fn active_rooms(&self) -> Vec<(MatrixRoom, DisplayName, Option<Tags>)> {
272 pub fn active_rooms(&self) -> Vec<FetchedRoom> {
274273 let (reply, response) = oneshot();
275274
276275 self.tx.send(WorkerTask::ActiveRooms(reply)).unwrap();
@@ -704,14 +703,9 @@ impl ClientWorker {
704703 Ok(Some(InfoMessage::from("Successfully logged in!")))
705704 }
706705
707 async fn direct_message(
708 &mut self,
709 user: OwnedUserId,
710 ) -> IambResult<(MatrixRoom, DisplayName, Option<Tags>)> {
711 for (room, name) in self.direct_messages().await {
706 async fn direct_message(&mut self, user: OwnedUserId) -> IambResult<FetchedRoom> {
707 for (room, name, tags) in self.direct_messages().await {
712708 if room.get_member(user.as_ref()).await.map_err(IambError::from)?.is_some() {
713 let tags = room.tags().await.map_err(IambError::from)?;
714
715709 return Ok((room, name, tags));
716710 }
717711 }
@@ -746,10 +740,7 @@ impl ClientWorker {
746740 Ok(details.inviter)
747741 }
748742
749 async fn get_room(
750 &mut self,
751 room_id: OwnedRoomId,
752 ) -> IambResult<(MatrixRoom, DisplayName, Option<Tags>)> {
743 async fn get_room(&mut self, room_id: OwnedRoomId) -> IambResult<FetchedRoom> {
753744 if let Some(room) = self.client.get_room(&room_id) {
754745 let name = room.display_name().await.map_err(IambError::from)?;
755746 let tags = room.tags().await.map_err(IambError::from)?;
@@ -783,7 +774,7 @@ impl ClientWorker {
783774 }
784775 }
785776
786 async fn direct_messages(&self) -> Vec<(MatrixRoom, DisplayName)> {
777 async fn direct_messages(&self) -> Vec<FetchedRoom> {
787778 let mut rooms = vec![];
788779
789780 for room in self.client.invited_rooms().into_iter() {
@@ -792,8 +783,9 @@ impl ClientWorker {
792783 }
793784
794785 let name = room.display_name().await.unwrap_or(DisplayName::Empty);
786 let tags = room.tags().await.unwrap_or_default();
795787
796 rooms.push((room.into(), name));
788 rooms.push((room.into(), name, tags));
797789 }
798790
799791 for room in self.client.joined_rooms().into_iter() {
@@ -802,14 +794,15 @@ impl ClientWorker {
802794 }
803795
804796 let name = room.display_name().await.unwrap_or(DisplayName::Empty);
797 let tags = room.tags().await.unwrap_or_default();
805798
806 rooms.push((room.into(), name));
799 rooms.push((room.into(), name, tags));
807800 }
808801
809802 return rooms;
810803 }
811804
812 async fn active_rooms(&self) -> Vec<(MatrixRoom, DisplayName, Option<Tags>)> {
805 async fn active_rooms(&self) -> Vec<FetchedRoom> {
813806 let mut rooms = vec![];
814807
815808 for room in self.client.invited_rooms().into_iter() {