feat: user renaming
This commit is contained in:
		
							parent
							
								
									720cc0cffc
								
							
						
					
					
						commit
						4cc0a07092
					
				
					 5 changed files with 98 additions and 42 deletions
				
			
		|  | @ -38,7 +38,7 @@ use ruma_identifiers::{RoomId, UserId}; | |||
| use serde_json::json; | ||||
| use std::{ | ||||
|     collections::BTreeMap, | ||||
|     convert::TryInto, | ||||
|     convert::{TryFrom, TryInto}, | ||||
|     path::PathBuf, | ||||
|     time::{Duration, SystemTime}, | ||||
| }; | ||||
|  | @ -325,8 +325,7 @@ pub fn set_displayname_route( | |||
|         if displayname == "" { | ||||
|             data.displayname_remove(&user_id); | ||||
|         } else { | ||||
|             data.displayname_set(&user_id, body.displayname.clone()); | ||||
|             // TODO send a new m.room.member join event with the updated displayname
 | ||||
|             data.displayname_set(&user_id, displayname.clone()); | ||||
|             // TODO send a new m.presence event with the updated displayname
 | ||||
|         } | ||||
|     } | ||||
|  | @ -610,26 +609,34 @@ pub fn create_room_route( | |||
|     MatrixResult(Ok(create_room::Response { room_id })) | ||||
| } | ||||
| 
 | ||||
| #[get("/_matrix/client/r0/directory/room/<room_alias>")] | ||||
| pub fn get_alias_route(room_alias: String) -> MatrixResult<get_alias::Response> { | ||||
| #[get("/_matrix/client/r0/directory/room/<_room_alias>", data = "<body>")] | ||||
| pub fn get_alias_route( | ||||
|     data: State<Data>, | ||||
|     body: Ruma<get_alias::Request>, | ||||
|     _room_alias: String, | ||||
| ) -> MatrixResult<get_alias::Response> { | ||||
|     // TODO
 | ||||
|     let room_id = match &*room_alias { | ||||
|         "#room:localhost" => "!xclkjvdlfj:localhost", | ||||
|         _ => { | ||||
|             debug!("Room not found."); | ||||
|             return MatrixResult(Err(Error { | ||||
|                 kind: ErrorKind::NotFound, | ||||
|                 message: "Room not found.".to_owned(), | ||||
|                 status_code: http::StatusCode::NOT_FOUND, | ||||
|             })); | ||||
|     let room_id = if body.room_alias.server_name() == data.hostname() { | ||||
|         match body.room_alias.alias() { | ||||
|             "conduit" => "!lgOCCXQKtXOAPlAlG5:conduit.rs", | ||||
|             _ => { | ||||
|                 debug!("Room alias not found."); | ||||
|                 return MatrixResult(Err(Error { | ||||
|                     kind: ErrorKind::NotFound, | ||||
|                     message: "Room not found.".to_owned(), | ||||
|                     status_code: http::StatusCode::NOT_FOUND, | ||||
|                 })); | ||||
|             } | ||||
|         } | ||||
|     } else { | ||||
|         todo!("ask remote server"); | ||||
|     } | ||||
|     .try_into() | ||||
|     .unwrap(); | ||||
| 
 | ||||
|     MatrixResult(Ok(get_alias::Response { | ||||
|         room_id, | ||||
|         servers: vec!["localhost".to_owned()], | ||||
|         servers: vec!["conduit.rs".to_owned()], | ||||
|     })) | ||||
| } | ||||
| 
 | ||||
|  | @ -661,21 +668,19 @@ pub fn join_room_by_id_or_alias_route( | |||
|     body: Ruma<join_room_by_id_or_alias::Request>, | ||||
|     _room_id_or_alias: String, | ||||
| ) -> MatrixResult<join_room_by_id_or_alias::Response> { | ||||
|     let room_id = if body.room_id_or_alias.is_room_alias_id() { | ||||
|         match body.room_id_or_alias.as_ref() { | ||||
|             "#room:localhost" => "!xclkjvdlfj:localhost".try_into().unwrap(), | ||||
|             _ => { | ||||
|                 debug!("Room not found."); | ||||
|                 return MatrixResult(Err(Error { | ||||
|                     kind: ErrorKind::NotFound, | ||||
|                     message: "Room not found.".to_owned(), | ||||
|                     status_code: http::StatusCode::NOT_FOUND, | ||||
|                 })); | ||||
|             } | ||||
|     let room_id = match RoomId::try_from(body.room_id_or_alias.clone()) { | ||||
|         Ok(room_id) => room_id, | ||||
|         Err(room_alias) => if room_alias.server_name() == data.hostname() { | ||||
|             return MatrixResult(Err(Error { | ||||
|                 kind: ErrorKind::NotFound, | ||||
|                 message: "Room alias not found.".to_owned(), | ||||
|                 status_code: http::StatusCode::NOT_FOUND, | ||||
|             })); | ||||
|         } else { | ||||
|             // Ask creator server of the room to join TODO ask someone else when not available
 | ||||
|             //server_server::send_request(data, destination, request)
 | ||||
|             todo!(); | ||||
|         } | ||||
|     } else { | ||||
|         todo!(); | ||||
|         //body.room_id_or_alias.try_into().unwrap()
 | ||||
|     }; | ||||
| 
 | ||||
|     if data.room_join( | ||||
|  | @ -758,9 +763,9 @@ pub async fn get_public_rooms_filtered_route( | |||
|     chunk.extend_from_slice( | ||||
|         &server_server::send_request( | ||||
|             &data, | ||||
|             "chat.privacytools.io".to_owned(), | ||||
|             "privacytools.io".to_owned(), | ||||
|             ruma_federation_api::v1::get_public_rooms::Request { | ||||
|                 limit: None, | ||||
|                 limit: Some(20_u32.into()), | ||||
|                 since: None, | ||||
|                 include_all_networks: None, | ||||
|                 third_party_instance_id: None, | ||||
|  |  | |||
							
								
								
									
										37
									
								
								src/data.rs
									
									
									
									
									
								
							
							
						
						
									
										37
									
								
								src/data.rs
									
									
									
									
									
								
							|  | @ -91,11 +91,21 @@ impl Data { | |||
|     } | ||||
| 
 | ||||
|     /// Set a new displayname.
 | ||||
|     pub fn displayname_set(&self, user_id: &UserId, displayname: Option<String>) { | ||||
|     pub fn displayname_set(&self, user_id: &UserId, displayname: String) { | ||||
|         self.db | ||||
|             .userid_displayname | ||||
|             .insert(user_id.to_string(), &*displayname.unwrap_or_default()) | ||||
|             .insert(user_id.to_string(), &*displayname) | ||||
|             .unwrap(); | ||||
|         for room_id in self.rooms_joined(user_id) { | ||||
|             self.pdu_append( | ||||
|                 room_id.clone(), | ||||
|                 user_id.clone(), | ||||
|                 EventType::RoomMember, | ||||
|                 json!({"membership": "join", "displayname": displayname}), | ||||
|                 None, | ||||
|                 Some(user_id.to_string()), | ||||
|             ); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /// Get a the displayname of a user.
 | ||||
|  | @ -200,11 +210,19 @@ impl Data { | |||
|             room_id.to_string().as_bytes().into(), | ||||
|         ); | ||||
| 
 | ||||
|         let mut content = json!({"membership": "join"}); | ||||
|         if let Some(displayname) = self.displayname_get(user_id) { | ||||
|             content | ||||
|                 .as_object_mut() | ||||
|                 .unwrap() | ||||
|                 .insert("displayname".to_owned(), displayname.into()); | ||||
|         } | ||||
| 
 | ||||
|         self.pdu_append( | ||||
|             room_id.clone(), | ||||
|             user_id.clone(), | ||||
|             EventType::RoomMember, | ||||
|             json!({"membership": "join"}), | ||||
|             content, | ||||
|             None, | ||||
|             Some(user_id.to_string()), | ||||
|         ); | ||||
|  | @ -429,6 +447,17 @@ impl Data { | |||
|             .unwrap_or(0_u64) | ||||
|             + 1; | ||||
| 
 | ||||
|         let mut unsigned = unsigned.unwrap_or_default(); | ||||
|         // TODO: Optimize this to not load the whole room state?
 | ||||
|         if let Some(state_key) = &state_key { | ||||
|             if let Some(prev_pdu) = self | ||||
|                 .room_state(&room_id) | ||||
|                 .get(&(event_type.clone(), state_key.clone())) | ||||
|             { | ||||
|                 unsigned.insert("prev_content".to_owned(), prev_pdu.content.clone()); | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         let mut pdu = PduEvent { | ||||
|             event_id: EventId::try_from("$thiswillbefilledinlater").unwrap(), | ||||
|             room_id: room_id.clone(), | ||||
|  | @ -442,7 +471,7 @@ impl Data { | |||
|             depth: depth.try_into().unwrap(), | ||||
|             auth_events: Vec::new(), | ||||
|             redacts: None, | ||||
|             unsigned: unsigned.unwrap_or_default(), | ||||
|             unsigned, | ||||
|             hashes: ruma_federation_api::EventHash { | ||||
|                 sha256: "aaa".to_owned(), | ||||
|             }, | ||||
|  |  | |||
|  | @ -76,7 +76,7 @@ fn main() { | |||
|     } | ||||
|     pretty_env_logger::init(); | ||||
| 
 | ||||
|     let data = Data::load_or_create("matrixtesting.koesters.xyz:14004"); | ||||
|     let data = Data::load_or_create("conduit.rs"); | ||||
|     //data.debug();
 | ||||
| 
 | ||||
|     setup_rocket(data).launch().unwrap(); | ||||
|  |  | |||
|  | @ -164,3 +164,11 @@ where | |||
|         } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| impl<T, E> Deref for MatrixResult<T, E> { | ||||
|     type Target = Result<T, E>; | ||||
| 
 | ||||
|     fn deref(&self) -> &Self::Target { | ||||
|         &self.0 | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -12,6 +12,22 @@ use std::{ | |||
|     time::{Duration, SystemTime}, | ||||
| }; | ||||
| 
 | ||||
| pub async fn request_well_known(data: &crate::Data, destination: &str) -> Option<String> { | ||||
|     let body: serde_json::Value = serde_json::from_str(&data | ||||
|         .reqwest_client() | ||||
|         .get(&format!( | ||||
|             "https://{}/.well-known/matrix/server", | ||||
|             destination | ||||
|         )) | ||||
|         .send() | ||||
|         .await | ||||
|         .ok()? | ||||
|         .text() | ||||
|         .await | ||||
|         .ok()?).ok()?; | ||||
|     Some(body.get("m.server")?.as_str()?.to_owned()) | ||||
| } | ||||
| 
 | ||||
| pub async fn send_request<T: Endpoint>( | ||||
|     data: &crate::Data, | ||||
|     destination: String, | ||||
|  | @ -19,7 +35,8 @@ pub async fn send_request<T: Endpoint>( | |||
| ) -> Option<T::Response> { | ||||
|     let mut http_request: http::Request<_> = request.try_into().unwrap(); | ||||
| 
 | ||||
|     *http_request.uri_mut() = format!("https://{}:8448{}", &destination.clone(), T::METADATA.path) | ||||
|     let actual_destination = "https://".to_owned() + &request_well_known(data, &destination).await.unwrap_or(destination.clone() + ":8448"); | ||||
|     *http_request.uri_mut() = (actual_destination + T::METADATA.path) | ||||
|         .parse() | ||||
|         .unwrap(); | ||||
| 
 | ||||
|  | @ -35,7 +52,7 @@ pub async fn send_request<T: Endpoint>( | |||
|     request_map.insert("method".to_owned(), T::METADATA.method.to_string().into()); | ||||
|     request_map.insert("uri".to_owned(), T::METADATA.path.into()); | ||||
|     request_map.insert("origin".to_owned(), data.hostname().into()); | ||||
|     request_map.insert("destination".to_owned(), "privacytools.io".into()); | ||||
|     request_map.insert("destination".to_owned(), destination.into()); | ||||
| 
 | ||||
|     let mut request_json = request_map.into(); | ||||
|     ruma_signatures::sign_json(data.hostname(), data.keypair(), &mut request_json).unwrap(); | ||||
|  | @ -141,9 +158,6 @@ pub fn get_server_keys(data: State<Data>) -> Json<String> { | |||
| } | ||||
| 
 | ||||
| #[get("/_matrix/key/v2/server/<_key_id>")] | ||||
| pub fn get_server_keys_deprecated( | ||||
|     data: State<Data>, | ||||
|     _key_id: String, | ||||
| ) -> Json<String> { | ||||
| pub fn get_server_keys_deprecated(data: State<Data>, _key_id: String) -> Json<String> { | ||||
|     get_server_keys(data) | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue