2021-07-14 07:07:08 +00:00
|
|
|
use crate::{database::DatabaseGuard, ConduitResult, Error, Ruma};
|
2020-08-18 10:15:27 +00:00
|
|
|
use ruma::api::client::{error::ErrorKind, r0::search::search_events};
|
|
|
|
|
|
|
|
#[cfg(feature = "conduit_bin")]
|
|
|
|
use rocket::post;
|
2020-09-08 15:32:03 +00:00
|
|
|
use search_events::{EventContextResult, ResultCategories, ResultRoomEvents, SearchResult};
|
2020-08-18 10:15:27 +00:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
post("/_matrix/client/r0/search", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-10-21 19:28:02 +00:00
|
|
|
pub async fn search_events_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<search_events::Request<'_>>,
|
2020-08-18 10:15:27 +00:00
|
|
|
) -> ConduitResult<search_events::Response> {
|
2020-10-18 18:33:12 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-08-18 10:15:27 +00:00
|
|
|
|
|
|
|
let search_criteria = body.search_categories.room_events.as_ref().unwrap();
|
2021-06-21 13:20:37 +00:00
|
|
|
let filter = search_criteria.filter.clone().unwrap_or_default();
|
2020-08-21 19:22:59 +00:00
|
|
|
|
2021-06-21 13:20:37 +00:00
|
|
|
let room_ids = filter.rooms.clone().unwrap_or_else(|| {
|
|
|
|
db.rooms
|
|
|
|
.rooms_joined(&sender_user)
|
|
|
|
.filter_map(|r| r.ok())
|
|
|
|
.collect()
|
|
|
|
});
|
2020-08-18 10:15:27 +00:00
|
|
|
|
|
|
|
let limit = filter.limit.map_or(10, |l| u64::from(l) as usize);
|
|
|
|
|
2021-06-21 13:20:37 +00:00
|
|
|
let mut searches = Vec::new();
|
|
|
|
|
|
|
|
for room_id in room_ids {
|
|
|
|
if !db.rooms.is_joined(sender_user, &room_id)? {
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::Forbidden,
|
|
|
|
"You don't have permission to view this room.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let search = db
|
|
|
|
.rooms
|
|
|
|
.search_pdus(&room_id, &search_criteria.search_term)?;
|
|
|
|
|
|
|
|
searches.push(search.0.peekable());
|
2020-08-18 10:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let skip = match body.next_batch.as_ref().map(|s| s.parse()) {
|
|
|
|
Some(Ok(s)) => s,
|
|
|
|
Some(Err(_)) => {
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::InvalidParam,
|
|
|
|
"Invalid next_batch token.",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
None => 0, // Default to the start
|
|
|
|
};
|
|
|
|
|
2021-06-21 13:20:37 +00:00
|
|
|
let mut results = Vec::new();
|
|
|
|
for _ in 0..skip + limit {
|
|
|
|
if let Some(s) = searches
|
|
|
|
.iter_mut()
|
|
|
|
.map(|s| (s.peek().cloned(), s))
|
|
|
|
.max_by_key(|(peek, _)| peek.clone())
|
|
|
|
.and_then(|(_, i)| i.next())
|
|
|
|
{
|
|
|
|
results.push(s);
|
|
|
|
}
|
|
|
|
}
|
2020-08-18 10:15:27 +00:00
|
|
|
|
2021-06-21 13:20:37 +00:00
|
|
|
let results = results
|
|
|
|
.iter()
|
2020-08-18 10:15:27 +00:00
|
|
|
.map(|result| {
|
|
|
|
Ok::<_, Error>(SearchResult {
|
2020-09-08 15:32:03 +00:00
|
|
|
context: EventContextResult {
|
|
|
|
end: None,
|
|
|
|
events_after: Vec::new(),
|
|
|
|
events_before: Vec::new(),
|
|
|
|
profile_info: BTreeMap::new(),
|
|
|
|
start: None,
|
|
|
|
},
|
2020-08-18 10:15:27 +00:00
|
|
|
rank: None,
|
2020-08-19 16:26:39 +00:00
|
|
|
result: db
|
2020-08-18 10:15:27 +00:00
|
|
|
.rooms
|
2020-08-19 16:26:39 +00:00
|
|
|
.get_pdu_from_id(&result)?
|
2020-09-08 15:32:03 +00:00
|
|
|
.map(|pdu| pdu.to_room_event()),
|
2020-08-18 10:15:27 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
.filter_map(|r| r.ok())
|
|
|
|
.skip(skip)
|
|
|
|
.take(limit)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let next_batch = if results.len() < limit as usize {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some((skip + limit).to_string())
|
|
|
|
};
|
|
|
|
|
2020-08-19 16:26:39 +00:00
|
|
|
Ok(search_events::Response::new(ResultCategories {
|
2020-09-08 15:32:03 +00:00
|
|
|
room_events: ResultRoomEvents {
|
2020-12-31 20:07:05 +00:00
|
|
|
count: Some((results.len() as u32).into()), // TODO: set this to none. Element shouldn't depend on it
|
|
|
|
groups: BTreeMap::new(), // TODO
|
2020-08-19 16:26:39 +00:00
|
|
|
next_batch,
|
|
|
|
results,
|
|
|
|
state: BTreeMap::new(), // TODO
|
2021-06-21 13:20:37 +00:00
|
|
|
highlights: search_criteria
|
|
|
|
.search_term
|
|
|
|
.split_terminator(|c: char| !c.is_alphanumeric())
|
|
|
|
.map(str::to_lowercase)
|
|
|
|
.collect::<Vec<_>>(),
|
2020-09-08 15:32:03 +00:00
|
|
|
},
|
2020-08-19 16:26:39 +00:00
|
|
|
})
|
2020-08-18 10:15:27 +00:00
|
|
|
.into())
|
|
|
|
}
|