Simplify deserialization and deconstruction for Responses
This commit is contained in:
		
							parent
							
								
									33bc666859
								
							
						
					
					
						commit
						84d47da392
					
				
					 2 changed files with 18 additions and 31 deletions
				
			
		|  | @ -594,27 +594,18 @@ pub fn get_global_account_data_route( | |||
| ) -> ConduitResult<get_global_account_data::Response> { | ||||
|     let user_id = body.user_id.as_ref().expect("user is authenticated"); | ||||
| 
 | ||||
|     let data = db | ||||
|         .account_data | ||||
|     db.account_data | ||||
|         .get( | ||||
|             None, | ||||
|             user_id, | ||||
|             &EventType::try_from(&body.event_type).expect("EventType::try_from can never fail"), | ||||
|         )? | ||||
|         .ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?; | ||||
| 
 | ||||
|     let data: AnyEvent = data | ||||
|         .deserialize() | ||||
|         .map_err(|_| Error::bad_database("Deserialization of account data failed"))?; | ||||
| 
 | ||||
|     if let AnyEvent::Basic(data) = data { | ||||
|         Ok(get_global_account_data::Response { | ||||
|             account_data: data.into(), | ||||
|         } | ||||
|         .into()) | ||||
|     } else { | ||||
|         Err(Error::bad_database("Encountered a non account data event.")) | ||||
|     } | ||||
|         .and_then(|ev| { | ||||
|             serde_json::from_str(ev.json().get()) | ||||
|                 .map(|data| get_global_account_data::Response { account_data: data }.into()) | ||||
|                 .ok() | ||||
|         }) | ||||
|         .ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found.")) | ||||
| } | ||||
| 
 | ||||
| #[put("/_matrix/client/r0/profile/<_user_id>/displayname", data = "<body>")] | ||||
|  | @ -2546,14 +2537,12 @@ pub fn sync_route( | |||
|                     .account_data | ||||
|                     .changes_since(Some(&room_id), &user_id, since)? | ||||
|                     .into_iter() | ||||
|                     .map(|(_, v)| { | ||||
|                         if let Ok(AnyEvent::Basic(account_event)) = v.deserialize() { | ||||
|                             Ok(EventJson::from(account_event)) | ||||
|                         } else { | ||||
|                             Err(Error::bad_database("found invalid event")) | ||||
|                         } | ||||
|                     .filter_map(|(_, v)| { | ||||
|                         serde_json::from_str(v.json().get()) | ||||
|                             .map_err(|_| Error::bad_database("Invalid account event in database.")) | ||||
|                             .ok() | ||||
|                     }) | ||||
|                     .collect::<Result<Vec<_>, _>>()?, | ||||
|                     .collect::<Vec<_>>(), | ||||
|             }, | ||||
|             summary: sync_events::RoomSummary { | ||||
|                 heroes, | ||||
|  | @ -2690,14 +2679,12 @@ pub fn sync_route( | |||
|                 .account_data | ||||
|                 .changes_since(None, &user_id, since)? | ||||
|                 .into_iter() | ||||
|                 .map(|(_, v)| { | ||||
|                     if let Ok(AnyEvent::Basic(account_event)) = v.deserialize() { | ||||
|                         Ok(EventJson::from(account_event)) | ||||
|                     } else { | ||||
|                         Err(Error::bad_database("found invalid event")) | ||||
|                     } | ||||
|                 .filter_map(|(_, v)| { | ||||
|                     serde_json::from_str(v.json().get()) | ||||
|                         .map_err(|_| Error::bad_database("Invalid account event in database.")) | ||||
|                         .ok() | ||||
|                 }) | ||||
|                 .collect::<Result<Vec<_>, _>>()?, | ||||
|                 .collect::<Vec<_>>(), | ||||
|         }, | ||||
|         device_lists: sync_events::DeviceLists { | ||||
|             changed: if since != 0 { | ||||
|  |  | |||
|  | @ -31,7 +31,7 @@ impl Globals { | |||
|                 .unwrap_or("localhost") | ||||
|                 .to_string() | ||||
|                 .try_into() | ||||
|                 .map_err(|_| Error::bad_database("Private or public keys are invalid."))?, | ||||
|                 .map_err(|_| Error::BadConfig("Invalid server name found."))?, | ||||
|             registration_disabled: config.get_bool("registration_disabled").unwrap_or(false), | ||||
|         }) | ||||
|     } | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue