| @@ -17,7 +17,9 @@ use matrix_sdk::{ |
| 17 | 17 | ruma::{ |
| 18 | 18 | events::room::member::MembershipState, |
| 19 | 19 | events::tag::{TagName, Tags}, |
| 20 | OwnedRoomAliasId, |
| 20 | 21 | OwnedRoomId, |
| 22 | RoomAliasId, |
| 21 | 23 | RoomId, |
| 22 | 24 | }, |
| 23 | 25 | }; |
| @@ -80,6 +82,10 @@ use crate::base::{ |
| 80 | 82 | ProgramStore, |
| 81 | 83 | RoomAction, |
| 82 | 84 | SendAction, |
| 85 | SortColumn, |
| 86 | SortFieldRoom, |
| 87 | SortFieldUser, |
| 88 | SortOrder, |
| 83 | 89 | }; |
| 84 | 90 | |
| 85 | 91 | use self::{room::RoomState, welcome::WelcomeState}; |
| @@ -125,42 +131,87 @@ fn selected_text(s: &str, selected: bool) -> Text { |
| 125 | 131 | Text::from(selected_span(s, selected)) |
| 126 | 132 | } |
| 127 | 133 | |
| 128 | | fn room_cmp(a: &MatrixRoom, b: &MatrixRoom) -> Ordering { |
| 129 | | let ca1 = a.canonical_alias(); |
| 130 | | let ca2 = b.canonical_alias(); |
| 131 | | |
| 132 | | let ord = match (ca1, ca2) { |
| 134 | /// Sort `Some` to be less than `None` so that list items with values come before those without. |
| 135 | #[inline] |
| 136 | fn some_cmp<T: Ord>(a: Option<T>, b: Option<T>) -> Ordering { |
| 137 | match (a, b) { |
| 138 | (Some(a), Some(b)) => a.cmp(&b), |
| 133 | 139 | (None, None) => Ordering::Equal, |
| 134 | 140 | (None, Some(_)) => Ordering::Greater, |
| 135 | 141 | (Some(_), None) => Ordering::Less, |
| 136 | | (Some(ca1), Some(ca2)) => ca1.cmp(&ca2), |
| 137 | | }; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | fn user_cmp(a: &MemberItem, b: &MemberItem, field: &SortFieldUser) -> Ordering { |
| 146 | let a_id = a.member.user_id(); |
| 147 | let b_id = b.member.user_id(); |
| 138 | 148 | |
| 139 | | ord.then_with(|| a.room_id().cmp(b.room_id())) |
| 149 | match field { |
| 150 | SortFieldUser::UserId => a_id.cmp(b_id), |
| 151 | SortFieldUser::LocalPart => a_id.localpart().cmp(b_id.localpart()), |
| 152 | SortFieldUser::Server => a_id.server_name().cmp(b_id.server_name()), |
| 153 | SortFieldUser::PowerLevel => { |
| 154 | // Sort higher power levels towards the top of the list. |
| 155 | b.member.power_level().cmp(&a.member.power_level()) |
| 156 | }, |
| 157 | } |
| 140 | 158 | } |
| 141 | 159 | |
| 142 | | fn tag_cmp(a: &Option<Tags>, b: &Option<Tags>) -> Ordering { |
| 143 | | let (fava, lowa) = a |
| 144 | | .as_ref() |
| 145 | | .map(|tags| { |
| 146 | | (tags.contains_key(&TagName::Favorite), tags.contains_key(&TagName::LowPriority)) |
| 147 | | }) |
| 148 | | .unwrap_or((false, false)); |
| 160 | fn room_cmp<T: RoomLikeItem>(a: &T, b: &T, field: &SortFieldRoom) -> Ordering { |
| 161 | match field { |
| 162 | SortFieldRoom::Favorite => { |
| 163 | let fava = a.has_tag(TagName::Favorite); |
| 164 | let favb = b.has_tag(TagName::Favorite); |
| 149 | 165 | |
| 150 | | let (favb, lowb) = b |
| 151 | | .as_ref() |
| 152 | | .map(|tags| { |
| 153 | | (tags.contains_key(&TagName::Favorite), tags.contains_key(&TagName::LowPriority)) |
| 154 | | }) |
| 155 | | .unwrap_or((false, false)); |
| 166 | // If a has Favorite and b doesn't, it should sort earlier in room list. |
| 167 | favb.cmp(&fava) |
| 168 | }, |
| 169 | SortFieldRoom::LowPriority => { |
| 170 | let lowa = a.has_tag(TagName::LowPriority); |
| 171 | let lowb = b.has_tag(TagName::LowPriority); |
| 156 | 172 | |
| 157 | | // If a has Favorite and b doesn't, it should sort earlier in room list. |
| 158 | | let cmpf = favb.cmp(&fava); |
| 173 | // If a has LowPriority and b doesn't, it should sort later in room list. |
| 174 | lowa.cmp(&lowb) |
| 175 | }, |
| 176 | SortFieldRoom::Name => a.name().cmp(b.name()), |
| 177 | SortFieldRoom::Alias => some_cmp(a.alias(), b.alias()), |
| 178 | SortFieldRoom::RoomId => a.room_id().cmp(b.room_id()), |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /// Compare two rooms according the configured sort criteria. |
| 183 | fn room_fields_cmp<T: RoomLikeItem>( |
| 184 | a: &T, |
| 185 | b: &T, |
| 186 | fields: &[SortColumn<SortFieldRoom>], |
| 187 | ) -> Ordering { |
| 188 | for SortColumn(field, order) in fields { |
| 189 | match (room_cmp(a, b, field), order) { |
| 190 | (Ordering::Equal, _) => continue, |
| 191 | (o, SortOrder::Ascending) => return o, |
| 192 | (o, SortOrder::Descending) => return o.reverse(), |
| 193 | } |
| 194 | } |
| 159 | 195 | |
| 160 | | // If a has LowPriority and b doesn't, it should sort later in room list. |
| 161 | | let cmpl = lowa.cmp(&lowb); |
| 196 | // Break ties on ascending room id. |
| 197 | room_cmp(a, b, &SortFieldRoom::RoomId) |
| 198 | } |
| 162 | 199 | |
| 163 | | cmpl.then(cmpf) |
| 200 | fn user_fields_cmp( |
| 201 | a: &MemberItem, |
| 202 | b: &MemberItem, |
| 203 | fields: &[SortColumn<SortFieldUser>], |
| 204 | ) -> Ordering { |
| 205 | for SortColumn(field, order) in fields { |
| 206 | match (user_cmp(a, b, field), order) { |
| 207 | (Ordering::Equal, _) => continue, |
| 208 | (o, SortOrder::Ascending) => return o, |
| 209 | (o, SortOrder::Descending) => return o.reverse(), |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // Break ties on ascending user id. |
| 214 | user_cmp(a, b, &SortFieldUser::UserId) |
| 164 | 215 | } |
| 165 | 216 | |
| 166 | 217 | fn append_tags<'a>(tags: &'a Tags, spans: &mut Vec<Span<'a>>, style: Style) { |
| @@ -190,6 +241,13 @@ fn append_tags<'a>(tags: &'a Tags, spans: &mut Vec<Span<'a>>, style: Style) { |
| 190 | 241 | spans.push(Span::styled(")", style)); |
| 191 | 242 | } |
| 192 | 243 | |
| 244 | trait RoomLikeItem { |
| 245 | fn room_id(&self) -> &RoomId; |
| 246 | fn has_tag(&self, tag: TagName) -> bool; |
| 247 | fn alias(&self) -> Option<&RoomAliasId>; |
| 248 | fn name(&self) -> &str; |
| 249 | } |
| 250 | |
| 193 | 251 | #[inline] |
| 194 | 252 | fn room_prompt( |
| 195 | 253 | room_id: &RoomId, |
| @@ -399,7 +457,8 @@ impl WindowOps<IambInfo> for IambWindow { |
| 399 | 457 | .into_iter() |
| 400 | 458 | .map(|room_info| DirectItem::new(room_info, store)) |
| 401 | 459 | .collect::<Vec<_>>(); |
| 402 | | items.sort(); |
| 460 | let fields = &store.application.settings.tunables.sort.dms; |
| 461 | items.sort_by(|a, b| room_fields_cmp(a, b, fields)); |
| 403 | 462 | |
| 404 | 463 | state.set(items); |
| 405 | 464 | |
| @@ -417,8 +476,13 @@ impl WindowOps<IambInfo> for IambWindow { |
| 417 | 476 | |
| 418 | 477 | if need_fetch { |
| 419 | 478 | if let Ok(mems) = store.application.worker.members(room_id.clone()) { |
| 420 | | let items = mems.into_iter().map(|m| MemberItem::new(m, room_id.clone())); |
| 421 | | state.set(items.collect()); |
| 479 | let mut items = mems |
| 480 | .into_iter() |
| 481 | .map(|m| MemberItem::new(m, room_id.clone())) |
| 482 | .collect::<Vec<_>>(); |
| 483 | let fields = &store.application.settings.tunables.sort.members; |
| 484 | items.sort_by(|a, b| user_fields_cmp(a, b, fields)); |
| 485 | state.set(items); |
| 422 | 486 | *last_fetch = Some(Instant::now()); |
| 423 | 487 | } |
| 424 | 488 | } |
| @@ -438,7 +502,8 @@ impl WindowOps<IambInfo> for IambWindow { |
| 438 | 502 | .into_iter() |
| 439 | 503 | .map(|room_info| RoomItem::new(room_info, store)) |
| 440 | 504 | .collect::<Vec<_>>(); |
| 441 | | items.sort(); |
| 505 | let fields = &store.application.settings.tunables.sort.rooms; |
| 506 | items.sort_by(|a, b| room_fields_cmp(a, b, fields)); |
| 442 | 507 | |
| 443 | 508 | state.set(items); |
| 444 | 509 | |
| @@ -449,15 +514,18 @@ impl WindowOps<IambInfo> for IambWindow { |
| 449 | 514 | .render(area, buf, state); |
| 450 | 515 | }, |
| 451 | 516 | IambWindow::SpaceList(state) => { |
| 452 | | let items = store |
| 517 | let mut items = store |
| 453 | 518 | .application |
| 454 | 519 | .sync_info |
| 455 | 520 | .spaces |
| 456 | 521 | .clone() |
| 457 | 522 | .into_iter() |
| 458 | | .map(|room| SpaceItem::new(room, store)); |
| 459 | | state.set(items.collect()); |
| 460 | | state.draw(area, buf, focused, store); |
| 523 | .map(|room| SpaceItem::new(room, store)) |
| 524 | .collect::<Vec<_>>(); |
| 525 | let fields = &store.application.settings.tunables.sort.spaces; |
| 526 | items.sort_by(|a, b| room_fields_cmp(a, b, fields)); |
| 527 | |
| 528 | state.set(items); |
| 461 | 529 | |
| 462 | 530 | List::new(store) |
| 463 | 531 | .empty_message("You haven't joined any spaces yet") |
| @@ -662,6 +730,7 @@ impl Window<IambInfo> for IambWindow { |
| 662 | 730 | pub struct RoomItem { |
| 663 | 731 | room_info: MatrixRoomInfo, |
| 664 | 732 | name: String, |
| 733 | alias: Option<OwnedRoomAliasId>, |
| 665 | 734 | } |
| 666 | 735 | |
| 667 | 736 | impl RoomItem { |
| @@ -671,13 +740,14 @@ impl RoomItem { |
| 671 | 740 | |
| 672 | 741 | let info = store.application.get_room_info(room_id.to_owned()); |
| 673 | 742 | let name = info.name.clone().unwrap_or_default(); |
| 743 | let alias = room.canonical_alias(); |
| 674 | 744 | info.tags = room_info.deref().1.clone(); |
| 675 | 745 | |
| 676 | | if let Some(alias) = room.canonical_alias() { |
| 746 | if let Some(alias) = &alias { |
| 677 | 747 | store.application.names.insert(alias.to_string(), room_id.to_owned()); |
| 678 | 748 | } |
| 679 | 749 | |
| 680 | | RoomItem { room_info, name } |
| 750 | RoomItem { room_info, name, alias } |
| 681 | 751 | } |
| 682 | 752 | |
| 683 | 753 | #[inline] |
| @@ -685,34 +755,31 @@ impl RoomItem { |
| 685 | 755 | &self.room_info.deref().0 |
| 686 | 756 | } |
| 687 | 757 | |
| 688 | | #[inline] |
| 689 | | fn room_id(&self) -> &RoomId { |
| 690 | | self.room().room_id() |
| 691 | | } |
| 692 | | |
| 693 | 758 | #[inline] |
| 694 | 759 | fn tags(&self) -> &Option<Tags> { |
| 695 | 760 | &self.room_info.deref().1 |
| 696 | 761 | } |
| 697 | 762 | } |
| 698 | 763 | |
| 699 | | impl PartialEq for RoomItem { |
| 700 | | fn eq(&self, other: &Self) -> bool { |
| 701 | | self.room_id() == other.room_id() |
| 764 | impl RoomLikeItem for RoomItem { |
| 765 | fn name(&self) -> &str { |
| 766 | self.name.as_str() |
| 702 | 767 | } |
| 703 | | } |
| 704 | 768 | |
| 705 | | impl Eq for RoomItem {} |
| 769 | fn alias(&self) -> Option<&RoomAliasId> { |
| 770 | self.alias.as_deref() |
| 771 | } |
| 706 | 772 | |
| 707 | | impl Ord for RoomItem { |
| 708 | | fn cmp(&self, other: &Self) -> Ordering { |
| 709 | | tag_cmp(self.tags(), other.tags()).then_with(|| room_cmp(self.room(), other.room())) |
| 773 | fn room_id(&self) -> &RoomId { |
| 774 | self.room().room_id() |
| 710 | 775 | } |
| 711 | | } |
| 712 | 776 | |
| 713 | | impl PartialOrd for RoomItem { |
| 714 | | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 715 | | self.cmp(other).into() |
| 777 | fn has_tag(&self, tag: TagName) -> bool { |
| 778 | if let Some(tags) = &self.room_info.deref().1 { |
| 779 | tags.contains_key(&tag) |
| 780 | } else { |
| 781 | false |
| 782 | } |
| 716 | 783 | } |
| 717 | 784 | } |
| 718 | 785 | |
| @@ -756,14 +823,16 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for RoomItem { |
| 756 | 823 | pub struct DirectItem { |
| 757 | 824 | room_info: MatrixRoomInfo, |
| 758 | 825 | name: String, |
| 826 | alias: Option<OwnedRoomAliasId>, |
| 759 | 827 | } |
| 760 | 828 | |
| 761 | 829 | impl DirectItem { |
| 762 | 830 | fn new(room_info: MatrixRoomInfo, store: &mut ProgramStore) -> Self { |
| 763 | | let room_id = room_info.deref().0.room_id().to_owned(); |
| 831 | let room_id = room_info.0.room_id().to_owned(); |
| 764 | 832 | let name = store.application.get_room_info(room_id).name.clone().unwrap_or_default(); |
| 833 | let alias = room_info.0.canonical_alias(); |
| 765 | 834 | |
| 766 | | DirectItem { room_info, name } |
| 835 | DirectItem { room_info, name, alias } |
| 767 | 836 | } |
| 768 | 837 | |
| 769 | 838 | #[inline] |
| @@ -771,17 +840,34 @@ impl DirectItem { |
| 771 | 840 | &self.room_info.deref().0 |
| 772 | 841 | } |
| 773 | 842 | |
| 774 | | #[inline] |
| 775 | | fn room_id(&self) -> &RoomId { |
| 776 | | self.room().room_id() |
| 777 | | } |
| 778 | | |
| 779 | 843 | #[inline] |
| 780 | 844 | fn tags(&self) -> &Option<Tags> { |
| 781 | 845 | &self.room_info.deref().1 |
| 782 | 846 | } |
| 783 | 847 | } |
| 784 | 848 | |
| 849 | impl RoomLikeItem for DirectItem { |
| 850 | fn name(&self) -> &str { |
| 851 | self.name.as_str() |
| 852 | } |
| 853 | |
| 854 | fn alias(&self) -> Option<&RoomAliasId> { |
| 855 | self.alias.as_deref() |
| 856 | } |
| 857 | |
| 858 | fn has_tag(&self, tag: TagName) -> bool { |
| 859 | if let Some(tags) = &self.room_info.deref().1 { |
| 860 | tags.contains_key(&tag) |
| 861 | } else { |
| 862 | false |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | fn room_id(&self) -> &RoomId { |
| 867 | self.room().room_id() |
| 868 | } |
| 869 | } |
| 870 | |
| 785 | 871 | impl ToString for DirectItem { |
| 786 | 872 | fn to_string(&self) -> String { |
| 787 | 873 | return self.name.clone(); |
| @@ -807,26 +893,6 @@ impl ListItem<IambInfo> for DirectItem { |
| 807 | 893 | } |
| 808 | 894 | } |
| 809 | 895 | |
| 810 | | impl PartialEq for DirectItem { |
| 811 | | fn eq(&self, other: &Self) -> bool { |
| 812 | | self.room_id() == other.room_id() |
| 813 | | } |
| 814 | | } |
| 815 | | |
| 816 | | impl Eq for DirectItem {} |
| 817 | | |
| 818 | | impl Ord for DirectItem { |
| 819 | | fn cmp(&self, other: &Self) -> Ordering { |
| 820 | | tag_cmp(self.tags(), other.tags()).then_with(|| room_cmp(self.room(), other.room())) |
| 821 | | } |
| 822 | | } |
| 823 | | |
| 824 | | impl PartialOrd for DirectItem { |
| 825 | | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 826 | | self.cmp(other).into() |
| 827 | | } |
| 828 | | } |
| 829 | | |
| 830 | 896 | impl Promptable<ProgramContext, ProgramStore, IambInfo> for DirectItem { |
| 831 | 897 | fn prompt( |
| 832 | 898 | &mut self, |
| @@ -840,51 +906,58 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for DirectItem { |
| 840 | 906 | |
| 841 | 907 | #[derive(Clone)] |
| 842 | 908 | pub struct SpaceItem { |
| 843 | | room: MatrixRoom, |
| 909 | room_info: MatrixRoomInfo, |
| 844 | 910 | name: String, |
| 911 | alias: Option<OwnedRoomAliasId>, |
| 845 | 912 | } |
| 846 | 913 | |
| 847 | 914 | impl SpaceItem { |
| 848 | | fn new(room: MatrixRoom, store: &mut ProgramStore) -> Self { |
| 849 | | let room_id = room.room_id(); |
| 915 | fn new(room_info: MatrixRoomInfo, store: &mut ProgramStore) -> Self { |
| 916 | let room_id = room_info.0.room_id(); |
| 850 | 917 | let name = store |
| 851 | 918 | .application |
| 852 | 919 | .get_room_info(room_id.to_owned()) |
| 853 | 920 | .name |
| 854 | 921 | .clone() |
| 855 | 922 | .unwrap_or_default(); |
| 923 | let alias = room_info.0.canonical_alias(); |
| 856 | 924 | |
| 857 | | if let Some(alias) = room.canonical_alias() { |
| 925 | if let Some(alias) = &alias { |
| 858 | 926 | store.application.names.insert(alias.to_string(), room_id.to_owned()); |
| 859 | 927 | } |
| 860 | 928 | |
| 861 | | SpaceItem { room, name } |
| 929 | SpaceItem { room_info, name, alias } |
| 862 | 930 | } |
| 863 | | } |
| 864 | 931 | |
| 865 | | impl PartialEq for SpaceItem { |
| 866 | | fn eq(&self, other: &Self) -> bool { |
| 867 | | self.room.room_id() == other.room.room_id() |
| 932 | #[inline] |
| 933 | fn room(&self) -> &MatrixRoom { |
| 934 | &self.room_info.deref().0 |
| 868 | 935 | } |
| 869 | 936 | } |
| 870 | 937 | |
| 871 | | impl Eq for SpaceItem {} |
| 938 | impl RoomLikeItem for SpaceItem { |
| 939 | fn name(&self) -> &str { |
| 940 | self.name.as_str() |
| 941 | } |
| 872 | 942 | |
| 873 | | impl Ord for SpaceItem { |
| 874 | | fn cmp(&self, other: &Self) -> Ordering { |
| 875 | | room_cmp(&self.room, &other.room) |
| 943 | fn room_id(&self) -> &RoomId { |
| 944 | self.room().room_id() |
| 876 | 945 | } |
| 877 | | } |
| 878 | 946 | |
| 879 | | impl PartialOrd for SpaceItem { |
| 880 | | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 881 | | self.cmp(other).into() |
| 947 | fn alias(&self) -> Option<&RoomAliasId> { |
| 948 | self.alias.as_deref() |
| 949 | } |
| 950 | |
| 951 | fn has_tag(&self, _: TagName) -> bool { |
| 952 | // I think that spaces can technically have tags, but afaik no client |
| 953 | // exposes them, so we'll just always return false here for now. |
| 954 | false |
| 882 | 955 | } |
| 883 | 956 | } |
| 884 | 957 | |
| 885 | 958 | impl ToString for SpaceItem { |
| 886 | 959 | fn to_string(&self) -> String { |
| 887 | | return self.room.room_id().to_string(); |
| 960 | return self.room_id().to_string(); |
| 888 | 961 | } |
| 889 | 962 | } |
| 890 | 963 | |
| @@ -894,7 +967,7 @@ impl ListItem<IambInfo> for SpaceItem { |
| 894 | 967 | } |
| 895 | 968 | |
| 896 | 969 | fn get_word(&self) -> Option<String> { |
| 897 | | self.room.room_id().to_string().into() |
| 970 | self.room_id().to_string().into() |
| 898 | 971 | } |
| 899 | 972 | } |
| 900 | 973 | |
| @@ -905,7 +978,7 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for SpaceItem { |
| 905 | 978 | ctx: &ProgramContext, |
| 906 | 979 | _: &mut ProgramStore, |
| 907 | 980 | ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> { |
| 908 | | room_prompt(self.room.room_id(), act, ctx) |
| 981 | room_prompt(self.room_id(), act, ctx) |
| 909 | 982 | } |
| 910 | 983 | } |
| 911 | 984 | |
| @@ -1200,3 +1273,93 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for MemberItem { |
| 1200 | 1273 | } |
| 1201 | 1274 | } |
| 1202 | 1275 | } |
| 1276 | |
| 1277 | #[cfg(test)] |
| 1278 | mod tests { |
| 1279 | use super::*; |
| 1280 | use matrix_sdk::ruma::{room_alias_id, server_name}; |
| 1281 | |
| 1282 | #[derive(Debug, Eq, PartialEq)] |
| 1283 | struct TestRoomItem { |
| 1284 | room_id: OwnedRoomId, |
| 1285 | tags: Vec<TagName>, |
| 1286 | alias: Option<OwnedRoomAliasId>, |
| 1287 | name: &'static str, |
| 1288 | } |
| 1289 | |
| 1290 | impl RoomLikeItem for &TestRoomItem { |
| 1291 | fn room_id(&self) -> &RoomId { |
| 1292 | self.room_id.as_ref() |
| 1293 | } |
| 1294 | |
| 1295 | fn has_tag(&self, tag: TagName) -> bool { |
| 1296 | self.tags.contains(&tag) |
| 1297 | } |
| 1298 | |
| 1299 | fn alias(&self) -> Option<&RoomAliasId> { |
| 1300 | self.alias.as_deref() |
| 1301 | } |
| 1302 | |
| 1303 | fn name(&self) -> &str { |
| 1304 | self.name |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | #[test] |
| 1309 | fn test_sort_rooms() { |
| 1310 | let server = server_name!("example.com"); |
| 1311 | |
| 1312 | let room1 = TestRoomItem { |
| 1313 | room_id: RoomId::new(server).to_owned(), |
| 1314 | tags: vec![TagName::Favorite], |
| 1315 | alias: Some(room_alias_id!("#room1:example.com").to_owned()), |
| 1316 | name: "Z", |
| 1317 | }; |
| 1318 | |
| 1319 | let room2 = TestRoomItem { |
| 1320 | room_id: RoomId::new(server).to_owned(), |
| 1321 | tags: vec![], |
| 1322 | alias: Some(room_alias_id!("#a:example.com").to_owned()), |
| 1323 | name: "Unnamed Room", |
| 1324 | }; |
| 1325 | |
| 1326 | let room3 = TestRoomItem { |
| 1327 | room_id: RoomId::new(server).to_owned(), |
| 1328 | tags: vec![], |
| 1329 | alias: None, |
| 1330 | name: "Cool Room", |
| 1331 | }; |
| 1332 | |
| 1333 | // Sort by Name ascending. |
| 1334 | let mut rooms = vec![&room1, &room2, &room3]; |
| 1335 | let fields = &[SortColumn(SortFieldRoom::Name, SortOrder::Ascending)]; |
| 1336 | rooms.sort_by(|a, b| room_fields_cmp(a, b, fields)); |
| 1337 | assert_eq!(rooms, vec![&room3, &room2, &room1]); |
| 1338 | |
| 1339 | // Sort by Name descending. |
| 1340 | let mut rooms = vec![&room1, &room2, &room3]; |
| 1341 | let fields = &[SortColumn(SortFieldRoom::Name, SortOrder::Descending)]; |
| 1342 | rooms.sort_by(|a, b| room_fields_cmp(a, b, fields)); |
| 1343 | assert_eq!(rooms, vec![&room1, &room2, &room3]); |
| 1344 | |
| 1345 | // Sort by Favorite and Alias before Name to show order matters. |
| 1346 | let mut rooms = vec![&room1, &room2, &room3]; |
| 1347 | let fields = &[ |
| 1348 | SortColumn(SortFieldRoom::Favorite, SortOrder::Ascending), |
| 1349 | SortColumn(SortFieldRoom::Alias, SortOrder::Ascending), |
| 1350 | SortColumn(SortFieldRoom::Name, SortOrder::Ascending), |
| 1351 | ]; |
| 1352 | rooms.sort_by(|a, b| room_fields_cmp(a, b, fields)); |
| 1353 | assert_eq!(rooms, vec![&room1, &room2, &room3]); |
| 1354 | |
| 1355 | // Now flip order of Favorite with Descending |
| 1356 | let mut rooms = vec![&room1, &room2, &room3]; |
| 1357 | let fields = &[ |
| 1358 | SortColumn(SortFieldRoom::Favorite, SortOrder::Descending), |
| 1359 | SortColumn(SortFieldRoom::Alias, SortOrder::Ascending), |
| 1360 | SortColumn(SortFieldRoom::Name, SortOrder::Ascending), |
| 1361 | ]; |
| 1362 | rooms.sort_by(|a, b| room_fields_cmp(a, b, fields)); |
| 1363 | assert_eq!(rooms, vec![&room2, &room3, &room1]); |
| 1364 | } |
| 1365 | } |