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