matrix_sdk_ui/room_list_service/filters/
joined.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 matrix_sdk_base::RoomState;
16
17use super::{super::Room, Filter};
18
19struct JoinedRoomMatcher<F>
20where
21    F: Fn(&Room) -> RoomState,
22{
23    state: F,
24}
25
26impl<F> JoinedRoomMatcher<F>
27where
28    F: Fn(&Room) -> RoomState,
29{
30    fn matches(&self, room: &Room) -> bool {
31        (self.state)(room) == RoomState::Joined
32    }
33}
34
35/// Create a new filter that will filter out rooms that are not joined (see
36/// [`matrix_sdk_base::RoomState::Joined`]).
37pub fn new_filter() -> impl Filter {
38    let matcher = JoinedRoomMatcher { state: move |room| room.state() };
39
40    move |room| -> bool { matcher.matches(room) }
41}
42
43#[cfg(test)]
44mod tests {
45    use matrix_sdk::test_utils::logged_in_client_with_server;
46    use matrix_sdk_base::RoomState;
47    use matrix_sdk_test::async_test;
48    use ruma::room_id;
49
50    use super::{super::new_rooms, *};
51
52    #[async_test]
53    async fn test_all_joined_kind() {
54        let (client, server) = logged_in_client_with_server().await;
55        let [room] = new_rooms([room_id!("!a:b.c")], &client, &server).await;
56
57        // When a room has been left, it doesn't match.
58        let matcher = JoinedRoomMatcher { state: |_| RoomState::Left };
59        assert!(!matcher.matches(&room));
60
61        // When a room is an invite, it doesn't match.
62        let matcher = JoinedRoomMatcher { state: |_| RoomState::Invited };
63        assert!(!matcher.matches(&room));
64
65        // When a room has been joined, it does match (unless it's empty).
66        let matcher = JoinedRoomMatcher { state: |_| RoomState::Joined };
67        assert!(matcher.matches(&room));
68    }
69}