|
| 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 | + |
| 15 | +use super::{super::Room, Filter}; |
| 16 | + |
| 17 | +struct LowPriorityRoomMatcher<F> |
| 18 | +where |
| 19 | + F: Fn(&Room) -> bool, |
| 20 | +{ |
| 21 | + is_low_priority: F, |
| 22 | +} |
| 23 | + |
| 24 | +impl<F> LowPriorityRoomMatcher<F> |
| 25 | +where |
| 26 | + F: Fn(&Room) -> bool, |
| 27 | +{ |
| 28 | + fn matches(&self, room: &Room) -> bool { |
| 29 | + (self.is_low_priority)(room) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/// Create a new filter that will filter out rooms that are not marked as |
| 34 | +/// low priority (see [`matrix_sdk_base::Room::is_low_priority`]). |
| 35 | +pub fn new_filter() -> impl Filter { |
| 36 | + let matcher = LowPriorityRoomMatcher { is_low_priority: move |room| room.is_low_priority() }; |
| 37 | + |
| 38 | + move |room| -> bool { matcher.matches(room) } |
| 39 | +} |
| 40 | + |
| 41 | +#[cfg(test)] |
| 42 | +mod 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_low_priority() { |
| 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 = LowPriorityRoomMatcher { is_low_priority: |_| true }; |
| 57 | + |
| 58 | + assert!(matcher.matches(&room)); |
| 59 | + } |
| 60 | + |
| 61 | + #[async_test] |
| 62 | + async fn test_is_not_low_priority() { |
| 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 = LowPriorityRoomMatcher { is_low_priority: |_| false }; |
| 67 | + |
| 68 | + assert!(matcher.matches(&room).not()); |
| 69 | + } |
| 70 | +} |
0 commit comments