Skip to content

Commit

Permalink
implement include_state search criteria
Browse files Browse the repository at this point in the history
Signed-off-by: strawberry <[email protected]>
  • Loading branch information
girlbossceo committed Mar 24, 2024
1 parent fd58ba6 commit e7fb8e7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 31 deletions.
42 changes: 21 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
59 changes: 50 additions & 9 deletions src/api/client_server/search.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand All @@ -21,6 +27,7 @@ pub async fn search_events_route(body: Ruma<search_events::v3::Request>) -> 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
Expand All @@ -30,17 +37,51 @@ pub async fn search_events_route(body: Ruma<search_events::v3::Request>) -> 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<OwnedRoomId, Vec<Raw<AnyStateEvent>>> = 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::<Vec<_>>();

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());
}
}
Expand Down Expand Up @@ -114,7 +155,7 @@ pub async fn search_events_route(body: Ruma<search_events::v3::Request>) -> 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())
Expand Down

0 comments on commit e7fb8e7

Please sign in to comment.