Skip to main content

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