main.rs (3092B)
1 #![feature(proc_macro_hygiene, decl_macro)] 2 3 #[macro_use] 4 extern crate rocket; 5 6 use rocket::State; 7 use ruma::{ 8 api::client::{ 9 error::{Error, ErrorKind}, 10 r0::{ 11 session::login, 12 sync::sync_events::{self, AccountData, DeviceLists, Presence, Rooms, ToDevice}, 13 to_device::send_event_to_device, 14 }, 15 }, 16 identifiers::UserId, 17 }; 18 19 use crate::data::Data; 20 use crate::ruma_wrapper::{MatrixResult, Ruma}; 21 use crate::tests::{Tests, TestsResult}; 22 use std::collections::BTreeMap; 23 use std::convert::TryFrom; 24 25 mod data; 26 mod error; 27 mod ruma_wrapper; 28 mod tests; 29 mod utils; 30 31 #[post("/set_test", data = "<next_test>")] 32 pub fn set_next_test(data: State<Data>, next_test: Tests) -> TestsResult { 33 data.set_current_test(next_test); 34 TestsResult(Ok(next_test)) 35 } 36 37 #[get("/_matrix/client/r0/sync", data = "<body>")] 38 pub fn sync_route( 39 data: State<Data>, 40 body: Ruma<sync_events::Request>, 41 ) -> MatrixResult<sync_events::Response> { 42 let user_id = body.user_id.as_ref().expect("user is authenticated"); 43 let device_id = body.device_id.as_ref().expect("user is authenticated"); 44 match *(data.current_test.lock().unwrap()) { 45 Tests::Start => MatrixResult(Ok(sync_events::Response { 46 next_batch: "1".to_string(), 47 rooms: Rooms { 48 leave: BTreeMap::new(), 49 join: BTreeMap::new(), 50 invite: BTreeMap::new(), 51 }, 52 presence: Presence { events: vec![] }, 53 account_data: AccountData { events: vec![] }, 54 to_device: ToDevice { events: vec![] }, 55 device_lists: DeviceLists { 56 changed: vec![], 57 left: vec![], 58 }, 59 device_one_time_keys_count: BTreeMap::new(), 60 })), 61 Tests::SyncTimeout => MatrixResult(Err(Error { 62 kind: ErrorKind::Unknown, 63 message: "Timeout".to_string(), 64 status_code: http::StatusCode::GATEWAY_TIMEOUT, 65 })), 66 _ => MatrixResult(Err(Error { 67 kind: ErrorKind::Unknown, 68 message: "Not Implemented".to_string(), 69 status_code: http::StatusCode::NOT_FOUND, 70 })), 71 } 72 } 73 74 #[post("/_matrix/client/r0/login", data = "<body>")] 75 pub fn login_route(db: State<Data>, body: Ruma<login::Request>) -> MatrixResult<login::Response> { 76 // TODO testing 77 MatrixResult(Ok(login::Response { 78 user_id: UserId::try_from("@carl:example.com").unwrap(), 79 access_token: "123456".to_string(), 80 home_server: None, 81 device_id: "KCZFUCGSLZ".to_string(), 82 well_known: None, 83 })) 84 } 85 86 #[options("/<_segments..>")] 87 pub fn options_route( 88 _segments: rocket::http::uri::Segments<'_>, 89 ) -> MatrixResult<send_event_to_device::Response> { 90 MatrixResult(Ok(send_event_to_device::Response)) 91 } 92 93 fn main() -> anyhow::Result<()> { 94 rocket::ignite() 95 .mount( 96 "/", 97 routes![sync_route, set_next_test, login_route, options_route,], 98 ) 99 .manage(Data::default()) 100 .launch()?; 101 Ok(()) 102 }