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