From fbefbd57bec98e8a88e6d2f9d162c5f0b71d4d71 Mon Sep 17 00:00:00 2001 From: strawberry Date: Sun, 24 Mar 2024 12:53:32 -0400 Subject: [PATCH] implement `include_state` search criteria Signed-off-by: strawberry --- Cargo.lock | 42 +++++++++++------------ Cargo.toml | 2 +- src/api/client_server/search.rs | 59 ++++++++++++++++++++++++++++----- 3 files changed, 72 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c1a19027..906cb513 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -96,13 +96,13 @@ dependencies = [ [[package]] name = "async-trait" -version = "0.1.78" +version = "0.1.79" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "461abc97219de0eaaf81fe3ef974a540158f3d079c2ab200f891f1a2ef201e85" +checksum = "a507401cad91ec6a857ed5513a2073c82a9b9048762b885bb98655b306964681" dependencies = [ "proc-macro2", "quote", - "syn 2.0.53", + "syn 2.0.55", ] [[package]] @@ -256,7 +256,7 @@ dependencies = [ "regex", "rustc-hash", "shlex", - "syn 2.0.53", + "syn 2.0.55", ] [[package]] @@ -404,7 +404,7 @@ dependencies = [ "heck 0.5.0", "proc-macro2", "quote", - "syn 2.0.53", + "syn 2.0.55", ] [[package]] @@ -577,7 +577,7 @@ checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" dependencies = [ "proc-macro2", "quote", - "syn 2.0.53", + "syn 2.0.55", ] [[package]] @@ -674,7 +674,7 @@ dependencies = [ "heck 0.4.1", "proc-macro2", "quote", - "syn 2.0.53", + "syn 2.0.55", ] [[package]] @@ -799,7 +799,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn 2.0.53", + "syn 2.0.55", ] [[package]] @@ -1810,7 +1810,7 @@ dependencies = [ "proc-macro2", "proc-macro2-diagnostics", "quote", - "syn 2.0.53", + "syn 2.0.55", ] [[package]] @@ -1884,7 +1884,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" dependencies = [ "proc-macro2", "quote", - "syn 2.0.53", + "syn 2.0.55", ] [[package]] @@ -1979,7 +1979,7 @@ checksum = "af066a9c399a26e020ada66a034357a868728e72cd426f3adcd35f80d88d88c8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.53", + "syn 2.0.55", "version_check", "yansi", ] @@ -2294,7 +2294,7 @@ dependencies = [ "quote", "ruma-identifiers-validation", "serde", - "syn 2.0.53", + "syn 2.0.55", "toml", ] @@ -2532,7 +2532,7 @@ checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.53", + "syn 2.0.55", ] [[package]] @@ -2798,9 +2798,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.53" +version = "2.0.55" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7383cd0e49fff4b6b90ca5670bfd3e9d6a733b3f90c686605aa7eec8c4996032" +checksum = "002a1b3dbf967edfafc32655d0f377ab0bb7b994aa1d32c8cc7e9b8bf3ebb8f0" dependencies = [ "proc-macro2", "quote", @@ -2862,7 +2862,7 @@ checksum = "c61f3ba182994efc43764a46c018c347bc492c79f024e705f46567b418f6d4f7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.53", + "syn 2.0.55", ] [[package]] @@ -3000,7 +3000,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.53", + "syn 2.0.55", ] [[package]] @@ -3154,7 +3154,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.53", + "syn 2.0.55", ] [[package]] @@ -3380,7 +3380,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.53", + "syn 2.0.55", "wasm-bindgen-shared", ] @@ -3414,7 +3414,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.53", + "syn 2.0.55", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -3692,7 +3692,7 @@ checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.53", + "syn 2.0.55", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index fc545ee7..ddb443bc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -51,7 +51,7 @@ serde_html_form = "0.2.5" hmac = "0.12.1" sha-1 = "0.10.1" -async-trait = "0.1.78" +async-trait = "0.1.79" # used for checking if an IP is in specific subnets / CIDR ranges easier ipaddress = "0.1.3" diff --git a/src/api/client_server/search.rs b/src/api/client_server/search.rs index 0bb03df7..e8c324b9 100644 --- a/src/api/client_server/search.rs +++ b/src/api/client_server/search.rs @@ -1,12 +1,18 @@ use std::collections::BTreeMap; -use ruma::api::client::{ - error::ErrorKind, - search::search_events::{ - self, - v3::{EventContextResult, ResultCategories, ResultRoomEvents, SearchResult}, +use ruma::{ + api::client::{ + error::ErrorKind, + search::search_events::{ + self, + v3::{EventContextResult, ResultCategories, ResultRoomEvents, SearchResult}, + }, }, + events::AnyStateEvent, + serde::Raw, + OwnedRoomId, }; +use tracing::debug; use crate::{services, Error, Result, Ruma}; @@ -21,6 +27,7 @@ pub async fn search_events_route(body: Ruma) -> Resu let search_criteria = body.search_categories.room_events.as_ref().unwrap(); let filter = &search_criteria.filter; + let include_state = &search_criteria.include_state; let room_ids = filter .rooms @@ -30,17 +37,51 @@ pub async fn search_events_route(body: Ruma) -> Resu // Use limit or else 10, with maximum 100 let limit = filter.limit.map_or(10, u64::from).min(100) as usize; + let mut room_states: BTreeMap>> = BTreeMap::new(); + + if include_state.is_some_and(|include_state| include_state) { + for room_id in &room_ids { + if !services().rooms.state_cache.is_joined(sender_user, room_id)? { + return Err(Error::BadRequest( + ErrorKind::Forbidden, + "You don't have permission to view this room.", + )); + } + + // check if sender_user can see state events + if services().rooms.state_accessor.user_can_see_state_events(sender_user, room_id)? { + let room_state = services() + .rooms + .state_accessor + .room_state_full(room_id) + .await? + .values() + .map(|pdu| pdu.to_state_event()) + .collect::>(); + + debug!("Room state: {:?}", room_state); + + room_states.insert(room_id.clone(), room_state); + } else { + return Err(Error::BadRequest( + ErrorKind::Forbidden, + "You don't have permission to view this room.", + )); + } + } + } + let mut searches = Vec::new(); - for room_id in room_ids { - if !services().rooms.state_cache.is_joined(sender_user, &room_id)? { + for room_id in &room_ids { + if !services().rooms.state_cache.is_joined(sender_user, room_id)? { return Err(Error::BadRequest( ErrorKind::Forbidden, "You don't have permission to view this room.", )); } - if let Some(search) = services().rooms.search.search_pdus(&room_id, &search_criteria.search_term)? { + if let Some(search) = services().rooms.search.search_pdus(room_id, &search_criteria.search_term)? { searches.push(search.0.peekable()); } } @@ -114,7 +155,7 @@ pub async fn search_events_route(body: Ruma) -> Resu groups: BTreeMap::new(), // TODO next_batch, results, - state: BTreeMap::new(), // TODO + state: room_states, highlights: search_criteria .search_term .split_terminator(|c: char| !c.is_alphanumeric())