Commit d8713141f2
Unregistered key
Layout: unified · split
src/windows/mod.rs +98 −52
| @@ -124,6 +124,57 @@ fn room_cmp(a: &MatrixRoom, b: &MatrixRoom) -> Ordering { | |||
| 124 | ord.then_with(|| a.room_id().cmp(b.room_id())) | 124 | ord.then_with(|| a.room_id().cmp(b.room_id())) |
| 125 | } | 125 | } |
| 126 | 126 | ||
| 127 | fn 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 | |||
| 151 | fn 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 | |||
| 127 | #[inline] | 178 | #[inline] |
| 128 | fn room_prompt( | 179 | fn room_prompt( |
| 129 | room_id: &RoomId, | 180 | room_id: &RoomId, |
| @@ -326,8 +377,13 @@ impl WindowOps<IambInfo> for IambWindow { | |||
| 326 | IambWindow::Room(state) => state.draw(area, buf, focused, store), | 377 | IambWindow::Room(state) => state.draw(area, buf, focused, store), |
| 327 | IambWindow::DirectList(state) => { | 378 | IambWindow::DirectList(state) => { |
| 328 | let dms = store.application.worker.direct_messages(); | 379 | let dms = store.application.worker.direct_messages(); |
| 329 | let items = dms.into_iter().map(|(id, name)| DirectItem::new(id, name, store)); | 380 | let mut items = dms |
| 330 | state.set(items.collect()); | 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); | ||
| 331 | 387 | ||
| 332 | List::new(store) | 388 | List::new(store) |
| 333 | .empty_message("No direct messages yet!") | 389 | .empty_message("No direct messages yet!") |
| @@ -583,29 +639,7 @@ impl Eq for RoomItem {} | |||
| 583 | 639 | ||
| 584 | impl Ord for RoomItem { | 640 | impl Ord for RoomItem { |
| 585 | fn cmp(&self, other: &Self) -> Ordering { | 641 | fn cmp(&self, other: &Self) -> Ordering { |
| 586 | let (fava, lowa) = self | 642 | tag_cmp(&self.tags, &other.tags).then_with(|| room_cmp(&self.room, &other.room)) |
| 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)) | ||
| 609 | } | 643 | } |
| 610 | } | 644 | } |
| 611 | 645 | ||
| @@ -627,30 +661,7 @@ impl ListItem<IambInfo> for RoomItem { | |||
| 627 | let style = selected_style(selected); | 661 | let style = selected_style(selected); |
| 628 | let mut spans = vec![Span::styled(self.name.as_str(), style)]; | 662 | let mut spans = vec![Span::styled(self.name.as_str(), style)]; |
| 629 | 663 | ||
| 630 | if tags.is_empty() { | 664 | append_tags(tags, &mut spans, style); |
| 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)); | ||
| 654 | 665 | ||
| 655 | Text::from(Spans(spans)) | 666 | Text::from(Spans(spans)) |
| 656 | } else { | 667 | } else { |
| @@ -677,16 +688,22 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for RoomItem { | |||
| 677 | #[derive(Clone)] | 688 | #[derive(Clone)] |
| 678 | pub struct DirectItem { | 689 | pub struct DirectItem { |
| 679 | room: MatrixRoom, | 690 | room: MatrixRoom, |
| 691 | tags: Option<Tags>, | ||
| 680 | name: String, | 692 | name: String, |
| 681 | } | 693 | } |
| 682 | 694 | ||
| 683 | impl DirectItem { | 695 | impl 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 { | ||
| 685 | let name = name.to_string(); | 702 | let name = name.to_string(); |
| 686 | 703 | ||
| 687 | store.application.set_room_name(room.room_id(), name.as_str()); | 704 | store.application.set_room_name(room.room_id(), name.as_str()); |
| 688 | 705 | ||
| 689 | DirectItem { room, name } | 706 | DirectItem { room, tags, name } |
| 690 | } | 707 | } |
| 691 | } | 708 | } |
| 692 | 709 | ||
| @@ -698,7 +715,16 @@ impl ToString for DirectItem { | |||
| 698 | 715 | ||
| 699 | impl ListItem<IambInfo> for DirectItem { | 716 | impl ListItem<IambInfo> for DirectItem { |
| 700 | fn show(&self, selected: bool, _: &ViewportContext<ListCursor>, _: &mut ProgramStore) -> Text { | 717 | 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 | } | ||
| 702 | } | 728 | } |
| 703 | 729 | ||
| 704 | fn get_word(&self) -> Option<String> { | 730 | fn get_word(&self) -> Option<String> { |
| @@ -706,6 +732,26 @@ impl ListItem<IambInfo> for DirectItem { | |||
| 706 | } | 732 | } |
| 707 | } | 733 | } |
| 708 | 734 | ||
| 735 | impl PartialEq for DirectItem { | ||
| 736 | fn eq(&self, other: &Self) -> bool { | ||
| 737 | self.room.room_id() == other.room.room_id() | ||
| 738 | } | ||
| 739 | } | ||
| 740 | |||
| 741 | impl Eq for DirectItem {} | ||
| 742 | |||
| 743 | impl 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 | |||
| 749 | impl PartialOrd for DirectItem { | ||
| 750 | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
| 751 | self.cmp(other).into() | ||
| 752 | } | ||
| 753 | } | ||
| 754 | |||
| 709 | impl Promptable<ProgramContext, ProgramStore, IambInfo> for DirectItem { | 755 | impl Promptable<ProgramContext, ProgramStore, IambInfo> for DirectItem { |
| 710 | fn prompt( | 756 | fn prompt( |
| 711 | &mut self, | 757 | &mut self, |
src/worker.rs +17 −24
| @@ -99,14 +99,16 @@ fn oneshot<T>() -> (ClientReply<T>, ClientResponse<T>) { | |||
| 99 | return (reply, response); | 99 | return (reply, response); |
| 100 | } | 100 | } |
| 101 | 101 | ||
| 102 | pub type FetchedRoom = (MatrixRoom, DisplayName, Option<Tags>); | ||
| 103 | |||
| 102 | pub enum WorkerTask { | 104 | pub enum WorkerTask { |
| 103 | ActiveRooms(ClientReply<Vec<(MatrixRoom, DisplayName, Option<Tags>)>>), | 105 | ActiveRooms(ClientReply<Vec<FetchedRoom>>), |
| 104 | DirectMessages(ClientReply<Vec<(MatrixRoom, DisplayName)>>), | 106 | DirectMessages(ClientReply<Vec<FetchedRoom>>), |
| 105 | Init(AsyncProgramStore, ClientReply<()>), | 107 | Init(AsyncProgramStore, ClientReply<()>), |
| 106 | LoadOlder(OwnedRoomId, Option<String>, u32, ClientReply<MessageFetchResult>), | 108 | LoadOlder(OwnedRoomId, Option<String>, u32, ClientReply<MessageFetchResult>), |
| 107 | Login(LoginStyle, ClientReply<IambResult<EditInfo>>), | 109 | Login(LoginStyle, ClientReply<IambResult<EditInfo>>), |
| 108 | GetInviter(Invited, ClientReply<IambResult<Option<RoomMember>>>), | 110 | GetInviter(Invited, ClientReply<IambResult<Option<RoomMember>>>), |
| 109 | GetRoom(OwnedRoomId, ClientReply<IambResult<(MatrixRoom, DisplayName, Option<Tags>)>>), | 111 | GetRoom(OwnedRoomId, ClientReply<IambResult<FetchedRoom>>), |
| 110 | JoinRoom(String, ClientReply<IambResult<OwnedRoomId>>), | 112 | JoinRoom(String, ClientReply<IambResult<OwnedRoomId>>), |
| 111 | Members(OwnedRoomId, ClientReply<IambResult<Vec<RoomMember>>>), | 113 | Members(OwnedRoomId, ClientReply<IambResult<Vec<RoomMember>>>), |
| 112 | SpaceMembers(OwnedRoomId, ClientReply<IambResult<Vec<OwnedRoomId>>>), | 114 | SpaceMembers(OwnedRoomId, ClientReply<IambResult<Vec<OwnedRoomId>>>), |
| @@ -235,7 +237,7 @@ impl Requester { | |||
| 235 | return response.recv(); | 237 | return response.recv(); |
| 236 | } | 238 | } |
| 237 | 239 | ||
| 238 | pub fn direct_messages(&self) -> Vec<(MatrixRoom, DisplayName)> { | 240 | pub fn direct_messages(&self) -> Vec<FetchedRoom> { |
| 239 | let (reply, response) = oneshot(); | 241 | let (reply, response) = oneshot(); |
| 240 | 242 | ||
| 241 | self.tx.send(WorkerTask::DirectMessages(reply)).unwrap(); | 243 | self.tx.send(WorkerTask::DirectMessages(reply)).unwrap(); |
| @@ -251,10 +253,7 @@ impl Requester { | |||
| 251 | return response.recv(); | 253 | return response.recv(); |
| 252 | } | 254 | } |
| 253 | 255 | ||
| 254 | pub fn get_room( | 256 | pub fn get_room(&self, room_id: OwnedRoomId) -> IambResult<FetchedRoom> { |
| 255 | &self, | ||
| 256 | room_id: OwnedRoomId, | ||
| 257 | ) -> IambResult<(MatrixRoom, DisplayName, Option<Tags>)> { | ||
| 258 | let (reply, response) = oneshot(); | 257 | let (reply, response) = oneshot(); |
| 259 | 258 | ||
| 260 | self.tx.send(WorkerTask::GetRoom(room_id, reply)).unwrap(); | 259 | self.tx.send(WorkerTask::GetRoom(room_id, reply)).unwrap(); |
| @@ -270,7 +269,7 @@ impl Requester { | |||
| 270 | return response.recv(); | 269 | return response.recv(); |
| 271 | } | 270 | } |
| 272 | 271 | ||
| 273 | pub fn active_rooms(&self) -> Vec<(MatrixRoom, DisplayName, Option<Tags>)> { | 272 | pub fn active_rooms(&self) -> Vec<FetchedRoom> { |
| 274 | let (reply, response) = oneshot(); | 273 | let (reply, response) = oneshot(); |
| 275 | 274 | ||
| 276 | self.tx.send(WorkerTask::ActiveRooms(reply)).unwrap(); | 275 | self.tx.send(WorkerTask::ActiveRooms(reply)).unwrap(); |
| @@ -704,14 +703,9 @@ impl ClientWorker { | |||
| 704 | Ok(Some(InfoMessage::from("Successfully logged in!"))) | 703 | Ok(Some(InfoMessage::from("Successfully logged in!"))) |
| 705 | } | 704 | } |
| 706 | 705 | ||
| 707 | async fn direct_message( | 706 | async fn direct_message(&mut self, user: OwnedUserId) -> IambResult<FetchedRoom> { |
| 708 | &mut self, | 707 | for (room, name, tags) in self.direct_messages().await { |
| 709 | user: OwnedUserId, | ||
| 710 | ) -> IambResult<(MatrixRoom, DisplayName, Option<Tags>)> { | ||
| 711 | for (room, name) in self.direct_messages().await { | ||
| 712 | if room.get_member(user.as_ref()).await.map_err(IambError::from)?.is_some() { | 708 | 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 | |||
| 715 | return Ok((room, name, tags)); | 709 | return Ok((room, name, tags)); |
| 716 | } | 710 | } |
| 717 | } | 711 | } |
| @@ -746,10 +740,7 @@ impl ClientWorker { | |||
| 746 | Ok(details.inviter) | 740 | Ok(details.inviter) |
| 747 | } | 741 | } |
| 748 | 742 | ||
| 749 | async fn get_room( | 743 | async fn get_room(&mut self, room_id: OwnedRoomId) -> IambResult<FetchedRoom> { |
| 750 | &mut self, | ||
| 751 | room_id: OwnedRoomId, | ||
| 752 | ) -> IambResult<(MatrixRoom, DisplayName, Option<Tags>)> { | ||
| 753 | if let Some(room) = self.client.get_room(&room_id) { | 744 | if let Some(room) = self.client.get_room(&room_id) { |
| 754 | let name = room.display_name().await.map_err(IambError::from)?; | 745 | let name = room.display_name().await.map_err(IambError::from)?; |
| 755 | let tags = room.tags().await.map_err(IambError::from)?; | 746 | let tags = room.tags().await.map_err(IambError::from)?; |
| @@ -783,7 +774,7 @@ impl ClientWorker { | |||
| 783 | } | 774 | } |
| 784 | } | 775 | } |
| 785 | 776 | ||
| 786 | async fn direct_messages(&self) -> Vec<(MatrixRoom, DisplayName)> { | 777 | async fn direct_messages(&self) -> Vec<FetchedRoom> { |
| 787 | let mut rooms = vec![]; | 778 | let mut rooms = vec![]; |
| 788 | 779 | ||
| 789 | for room in self.client.invited_rooms().into_iter() { | 780 | for room in self.client.invited_rooms().into_iter() { |
| @@ -792,8 +783,9 @@ impl ClientWorker { | |||
| 792 | } | 783 | } |
| 793 | 784 | ||
| 794 | let name = room.display_name().await.unwrap_or(DisplayName::Empty); | 785 | let name = room.display_name().await.unwrap_or(DisplayName::Empty); |
| 786 | let tags = room.tags().await.unwrap_or_default(); | ||
| 795 | 787 | ||
| 796 | rooms.push((room.into(), name)); | 788 | rooms.push((room.into(), name, tags)); |
| 797 | } | 789 | } |
| 798 | 790 | ||
| 799 | for room in self.client.joined_rooms().into_iter() { | 791 | for room in self.client.joined_rooms().into_iter() { |
| @@ -802,14 +794,15 @@ impl ClientWorker { | |||
| 802 | } | 794 | } |
| 803 | 795 | ||
| 804 | let name = room.display_name().await.unwrap_or(DisplayName::Empty); | 796 | let name = room.display_name().await.unwrap_or(DisplayName::Empty); |
| 797 | let tags = room.tags().await.unwrap_or_default(); | ||
| 805 | 798 | ||
| 806 | rooms.push((room.into(), name)); | 799 | rooms.push((room.into(), name, tags)); |
| 807 | } | 800 | } |
| 808 | 801 | ||
| 809 | return rooms; | 802 | return rooms; |
| 810 | } | 803 | } |
| 811 | 804 | ||
| 812 | async fn active_rooms(&self) -> Vec<(MatrixRoom, DisplayName, Option<Tags>)> { | 805 | async fn active_rooms(&self) -> Vec<FetchedRoom> { |
| 813 | let mut rooms = vec![]; | 806 | let mut rooms = vec![]; |
| 814 | 807 | ||
| 815 | for room in self.client.invited_rooms().into_iter() { | 808 | for room in self.client.invited_rooms().into_iter() { |