diff options
author | Timo Kösters <timo@koesters.xyz> | 2023-06-25 19:31:40 +0200 |
---|---|---|
committer | Timo Kösters <timo@koesters.xyz> | 2023-06-25 19:40:33 +0200 |
commit | c7e0ea525a3c6f66072c3518bb8c533c87f1e3db (patch) | |
tree | 49632e0ab354a704c1e59bee220e3a9569a1a6c3 /src/api | |
parent | def079267d3a4255df2c3dd38ed317ca65df5416 (diff) | |
download | conduit-c7e0ea525a3c6f66072c3518bb8c533c87f1e3db.zip |
feat: WIP relationships and threads
Diffstat (limited to 'src/api')
-rw-r--r-- | src/api/client_server/context.rs | 19 | ||||
-rw-r--r-- | src/api/client_server/media.rs | 6 | ||||
-rw-r--r-- | src/api/client_server/message.rs | 8 | ||||
-rw-r--r-- | src/api/client_server/mod.rs | 2 | ||||
-rw-r--r-- | src/api/client_server/relations.rs | 10 | ||||
-rw-r--r-- | src/api/client_server/threads.rs | 49 | ||||
-rw-r--r-- | src/api/client_server/unversioned.rs | 2 | ||||
-rw-r--r-- | src/api/server_server.rs | 4 |
8 files changed, 86 insertions, 14 deletions
diff --git a/src/api/client_server/context.rs b/src/api/client_server/context.rs index 5a3013b..a824ea0 100644 --- a/src/api/client_server/context.rs +++ b/src/api/client_server/context.rs @@ -69,18 +69,18 @@ pub async fn get_context_route( lazy_loaded.insert(base_event.sender.as_str().to_owned()); } + // Use limit with maximum 100 + let limit = usize::try_from(body.limit) + .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Limit value is invalid."))? + .min(100); + let base_event = base_event.to_room_event(); let events_before: Vec<_> = services() .rooms .timeline .pdus_until(sender_user, &room_id, base_token)? - .take( - u32::try_from(body.limit).map_err(|_| { - Error::BadRequest(ErrorKind::InvalidParam, "Limit value is invalid.") - })? as usize - / 2, - ) + .take(limit / 2) .filter_map(|r| r.ok()) // Remove buggy events .filter(|(_, pdu)| { services() @@ -114,12 +114,7 @@ pub async fn get_context_route( .rooms .timeline .pdus_after(sender_user, &room_id, base_token)? - .take( - u32::try_from(body.limit).map_err(|_| { - Error::BadRequest(ErrorKind::InvalidParam, "Limit value is invalid.") - })? as usize - / 2, - ) + .take(limit / 2) .filter_map(|r| r.ok()) // Remove buggy events .filter(|(_, pdu)| { services() diff --git a/src/api/client_server/media.rs b/src/api/client_server/media.rs index 3410cc0..75f8e15 100644 --- a/src/api/client_server/media.rs +++ b/src/api/client_server/media.rs @@ -1,3 +1,5 @@ +use std::time::Duration; + use crate::{service::media::FileMeta, services, utils, Error, Result, Ruma}; use ruma::api::client::{ error::ErrorKind, @@ -67,6 +69,8 @@ pub async fn get_remote_content( allow_remote: false, server_name: server_name.to_owned(), media_id, + timeout_ms: Duration::from_secs(20), + allow_redirect: false, }, ) .await?; @@ -194,6 +198,8 @@ pub async fn get_content_thumbnail_route( method: body.method.clone(), server_name: body.server_name.clone(), media_id: body.media_id.clone(), + timeout_ms: Duration::from_secs(20), + allow_redirect: false, }, ) .await?; diff --git a/src/api/client_server/message.rs b/src/api/client_server/message.rs index faf178d..dc2d994 100644 --- a/src/api/client_server/message.rs +++ b/src/api/client_server/message.rs @@ -133,8 +133,12 @@ pub async fn get_message_events_route( from, )?; - // Use limit or else 10 - let limit = body.limit.try_into().map_or(10_usize, |l: u32| l as usize); + // Use limit or else 10, with maximum 100 + let limit = body + .limit + .try_into() + .map_or(10_usize, |l: u32| l as usize) + .min(100); let next_token; diff --git a/src/api/client_server/mod.rs b/src/api/client_server/mod.rs index 6ed17e7..4a77f23 100644 --- a/src/api/client_server/mod.rs +++ b/src/api/client_server/mod.rs @@ -24,6 +24,7 @@ mod state; mod sync; mod tag; mod thirdparty; +mod threads; mod to_device; mod typing; mod unversioned; @@ -56,6 +57,7 @@ pub use state::*; pub use sync::*; pub use tag::*; pub use thirdparty::*; +pub use threads::*; pub use to_device::*; pub use typing::*; pub use unversioned::*; diff --git a/src/api/client_server/relations.rs b/src/api/client_server/relations.rs new file mode 100644 index 0000000..4d2af47 --- /dev/null +++ b/src/api/client_server/relations.rs @@ -0,0 +1,10 @@ +use crate::{services, Result, Ruma}; +use std::time::{Duration, SystemTime}; + +/// # `GET /_matrix/client/r0/todo` +pub async fn get_relating_events_route( + body: Ruma<get_turn_server_info::v3::Request>, +) -> Result<get_turn_server_info::v3::Response> { + let sender_user = body.sender_user.as_ref().expect("user is authenticated"); + todo!(); +} diff --git a/src/api/client_server/threads.rs b/src/api/client_server/threads.rs new file mode 100644 index 0000000..a095b42 --- /dev/null +++ b/src/api/client_server/threads.rs @@ -0,0 +1,49 @@ +use ruma::api::client::{error::ErrorKind, threads::get_threads}; + +use crate::{services, Error, Result, Ruma}; + +/// # `GET /_matrix/client/r0/rooms/{roomId}/threads` +pub async fn get_threads_route( + body: Ruma<get_threads::v1::Request>, +) -> Result<get_threads::v1::Response> { + let sender_user = body.sender_user.as_ref().expect("user is authenticated"); + + // Use limit or else 10, with maximum 100 + let limit = body + .limit + .and_then(|l| l.try_into().ok()) + .unwrap_or(10) + .min(100); + + let from = if let Some(from) = &body.from { + from.parse() + .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, ""))? + } else { + u64::MAX + }; + + let threads = services() + .rooms + .threads + .threads_until(sender_user, &body.room_id, from, &body.include)? + .take(limit) + .filter_map(|r| r.ok()) + .filter(|(_, pdu)| { + services() + .rooms + .state_accessor + .user_can_see_event(sender_user, &body.room_id, &pdu.event_id) + .unwrap_or(false) + }) + .collect::<Vec<_>>(); + + let next_batch = threads.last().map(|(count, _)| count.to_string()); + + Ok(get_threads::v1::Response { + chunk: threads + .into_iter() + .map(|(_, pdu)| pdu.to_room_event()) + .collect(), + next_batch, + }) +} diff --git a/src/api/client_server/unversioned.rs b/src/api/client_server/unversioned.rs index 526598b..b4f03f4 100644 --- a/src/api/client_server/unversioned.rs +++ b/src/api/client_server/unversioned.rs @@ -23,6 +23,8 @@ pub async fn get_supported_versions_route( "r0.6.0".to_owned(), "v1.1".to_owned(), "v1.2".to_owned(), + "v1.3".to_owned(), + "v1.4".to_owned(), ], unstable_features: BTreeMap::from_iter([("org.matrix.e2e_cross_signing".to_owned(), true)]), }; diff --git a/src/api/server_server.rs b/src/api/server_server.rs index 961b658..c1c23a5 100644 --- a/src/api/server_server.rs +++ b/src/api/server_server.rs @@ -1,3 +1,4 @@ +#[allow(deprecated)] use crate::{ api::client_server::{self, claim_keys_helper, get_keys_helper}, service::pdu::{gen_event_id_canonical_json, PduBuilder}, @@ -497,6 +498,9 @@ async fn request_well_known(destination: &str) -> Option<String> { .send() .await; debug!("Got well known response"); + if let Err(e) = &response { + error!("Well known error: {e:?}"); + } let text = response.ok()?.text().await; debug!("Got well known response text"); let body: serde_json::Value = serde_json::from_str(&text.ok()?).ok()?; |