matrix_sdk_ui/room_list_service/filters/
favourite.rs

1// Copyright 2024 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use super::{super::Room, Filter};
16
17struct FavouriteRoomMatcher<F>
18where
19    F: Fn(&Room) -> bool,
20{
21    is_favourite: F,
22}
23
24impl<F> FavouriteRoomMatcher<F>
25where
26    F: Fn(&Room) -> bool,
27{
28    fn matches(&self, room: &Room) -> bool {
29        (self.is_favourite)(room)
30    }
31}
32
33/// Create a new filter that will filter out rooms that are not marked as
34/// favourite (see [`matrix_sdk_base::Room::is_favourite`]).
35pub fn new_filter() -> impl Filter {
36    let matcher = FavouriteRoomMatcher { is_favourite: move |room| room.is_favourite() };
37
38    move |room| -> bool { matcher.matches(room) }
39}
40
41#[cfg(test)]
42mod tests {
43    use std::ops::Not;
44
45    use matrix_sdk::test_utils::logged_in_client_with_server;
46    use matrix_sdk_test::async_test;
47    use ruma::room_id;
48
49    use super::{super::new_rooms, *};
50
51    #[async_test]
52    async fn test_is_favourite() {
53        let (client, server) = logged_in_client_with_server().await;
54        let [room] = new_rooms([room_id!("!a:b.c")], &client, &server).await;
55
56        let matcher = FavouriteRoomMatcher { is_favourite: |_| true };
57
58        assert!(matcher.matches(&room));
59    }
60
61    #[async_test]
62    async fn test_is_not_favourite() {
63        let (client, server) = logged_in_client_with_server().await;
64        let [room] = new_rooms([room_id!("!a:b.c")], &client, &server).await;
65
66        let matcher = FavouriteRoomMatcher { is_favourite: |_| false };
67
68        assert!(matcher.matches(&room).not());
69    }
70}