2021-07-14 07:07:08 +00:00
|
|
|
use crate::{database::DatabaseGuard, ConduitResult, Error, Ruma};
|
2020-07-30 18:14:47 +02:00
|
|
|
use ruma::api::client::{error::ErrorKind, r0::context::get_context};
|
2021-07-14 07:07:08 +00:00
|
|
|
use std::convert::TryFrom;
|
2020-07-30 18:14:47 +02:00
|
|
|
|
|
|
|
#[cfg(feature = "conduit_bin")]
|
|
|
|
use rocket::get;
|
|
|
|
|
2021-08-31 19:14:37 +02:00
|
|
|
/// # `GET /_matrix/client/r0/rooms/{roomId}/context`
|
|
|
|
///
|
|
|
|
/// Allows loading room history around an event.
|
|
|
|
///
|
|
|
|
/// - Only works if the user is joined (TODO: always allow, but only show events if the user was
|
|
|
|
/// joined, depending on history_visibility)
|
2020-07-30 18:14:47 +02:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
get("/_matrix/client/r0/rooms/<_>/context/<_>", data = "<body>")
|
|
|
|
)]
|
2021-02-28 12:41:03 +01:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-10-21 21:28:02 +02:00
|
|
|
pub async fn get_context_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-09-08 17:32:03 +02:00
|
|
|
body: Ruma<get_context::Request<'_>>,
|
2020-07-30 18:14:47 +02:00
|
|
|
) -> ConduitResult<get_context::Response> {
|
2020-10-18 20:33:12 +02:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2020-10-18 20:33:12 +02:00
|
|
|
if !db.rooms.is_joined(sender_user, &body.room_id)? {
|
2020-07-30 18:14:47 +02:00
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::Forbidden,
|
|
|
|
"You don't have permission to view this room.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2021-03-23 12:59:27 +01:00
|
|
|
let base_pdu_id = db
|
|
|
|
.rooms
|
|
|
|
.get_pdu_id(&body.event_id)?
|
|
|
|
.ok_or(Error::BadRequest(
|
|
|
|
ErrorKind::NotFound,
|
|
|
|
"Base event id not found.",
|
|
|
|
))?;
|
|
|
|
|
|
|
|
let base_token = db.rooms.pdu_count(&base_pdu_id)?;
|
|
|
|
|
2020-07-30 18:14:47 +02:00
|
|
|
let base_event = db
|
|
|
|
.rooms
|
2021-03-23 12:59:27 +01:00
|
|
|
.get_pdu_from_id(&base_pdu_id)?
|
2020-07-30 18:14:47 +02:00
|
|
|
.ok_or(Error::BadRequest(
|
|
|
|
ErrorKind::NotFound,
|
|
|
|
"Base event not found.",
|
|
|
|
))?
|
|
|
|
.to_room_event();
|
|
|
|
|
2021-10-13 11:51:30 +02:00
|
|
|
let events_before: Vec<_> = db
|
2020-07-30 18:14:47 +02:00
|
|
|
.rooms
|
2021-09-13 19:45:56 +02:00
|
|
|
.pdus_until(sender_user, &body.room_id, base_token)?
|
2020-07-30 18:14:47 +02:00
|
|
|
.take(
|
|
|
|
u32::try_from(body.limit).map_err(|_| {
|
|
|
|
Error::BadRequest(ErrorKind::InvalidParam, "Limit value is invalid.")
|
|
|
|
})? as usize
|
|
|
|
/ 2,
|
|
|
|
)
|
|
|
|
.filter_map(|r| r.ok()) // Remove buggy events
|
2021-10-13 11:51:30 +02:00
|
|
|
.collect();
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2020-09-17 14:44:47 +02:00
|
|
|
let start_token = events_before
|
|
|
|
.last()
|
|
|
|
.and_then(|(pdu_id, _)| db.rooms.pdu_count(pdu_id).ok())
|
|
|
|
.map(|count| count.to_string());
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2021-10-13 11:51:30 +02:00
|
|
|
let events_before: Vec<_> = events_before
|
2020-07-30 18:14:47 +02:00
|
|
|
.into_iter()
|
|
|
|
.map(|(_, pdu)| pdu.to_room_event())
|
2021-10-13 11:51:30 +02:00
|
|
|
.collect();
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2021-10-13 11:51:30 +02:00
|
|
|
let events_after: Vec<_> = db
|
2020-07-30 18:14:47 +02:00
|
|
|
.rooms
|
2021-09-13 19:45:56 +02:00
|
|
|
.pdus_after(sender_user, &body.room_id, base_token)?
|
2020-07-30 18:14:47 +02:00
|
|
|
.take(
|
|
|
|
u32::try_from(body.limit).map_err(|_| {
|
|
|
|
Error::BadRequest(ErrorKind::InvalidParam, "Limit value is invalid.")
|
|
|
|
})? as usize
|
|
|
|
/ 2,
|
|
|
|
)
|
|
|
|
.filter_map(|r| r.ok()) // Remove buggy events
|
2021-10-13 11:51:30 +02:00
|
|
|
.collect();
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2020-09-17 14:44:47 +02:00
|
|
|
let end_token = events_after
|
|
|
|
.last()
|
|
|
|
.and_then(|(pdu_id, _)| db.rooms.pdu_count(pdu_id).ok())
|
|
|
|
.map(|count| count.to_string());
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2021-10-13 11:51:30 +02:00
|
|
|
let events_after: Vec<_> = events_after
|
2020-07-30 18:14:47 +02:00
|
|
|
.into_iter()
|
|
|
|
.map(|(_, pdu)| pdu.to_room_event())
|
2021-10-13 11:51:30 +02:00
|
|
|
.collect();
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2022-01-13 11:48:18 +01:00
|
|
|
let resp = get_context::Response {
|
|
|
|
start: start_token,
|
|
|
|
end: end_token,
|
|
|
|
events_before,
|
|
|
|
event: Some(base_event),
|
|
|
|
events_after,
|
|
|
|
state: db // TODO: State at event
|
|
|
|
.rooms
|
|
|
|
.room_state_full(&body.room_id)?
|
|
|
|
.values()
|
|
|
|
.map(|pdu| pdu.to_state_event())
|
|
|
|
.collect(),
|
|
|
|
};
|
2020-08-06 08:29:59 -04:00
|
|
|
|
|
|
|
Ok(resp.into())
|
2020-07-30 18:14:47 +02:00
|
|
|
}
|