Our Personal Data Server from scratch!

feat(write): allow records with missing $type

Some clients (anisota) seem to miss the $type here. It's quite
inconvenient if they are not allowed to write their records - and we can
easily elide the $type anyway by what collection the record is going to!

a.starrysky.fyi fa8044a0 9c67bfb2

verified
+22 -2
+22 -2
crates/tranquil-pds/src/api/repo/record/write.rs
··· 71 71 controller_did: scope_proof.controller_did().map(|c| c.into_did()), 72 72 }) 73 73 } 74 + 75 + pub fn elide_record_type(mut record: serde_json::Value, collection: &Nsid) -> serde_json::Value { 76 + match record { 77 + serde_json::Value::Object(ref mut value) => { 78 + if value.contains_key("$type") { 79 + record 80 + } else { 81 + value.insert("$type".to_owned(), collection.as_str().into()); 82 + record 83 + } 84 + }, 85 + _ => record 86 + } 87 + } 88 + 74 89 #[derive(Deserialize)] 75 90 #[allow(dead_code)] 76 91 pub struct CreateRecordInput { ··· 102 117 pub async fn create_record( 103 118 State(state): State<AppState>, 104 119 auth: Auth<Active>, 105 - Json(input): Json<CreateRecordInput>, 120 + Json(mut input): Json<CreateRecordInput>, 106 121 ) -> Result<Response, crate::api::error::ApiError> { 107 122 let scope_proof = match auth.verify_repo_create(&input.collection) { 108 123 Ok(proof) => proof, ··· 126 141 { 127 142 return Ok(ApiError::InvalidSwap(Some("Repo has been modified".into())).into_response()); 128 143 } 144 + 145 + input.record = elide_record_type(input.record, &input.collection); 129 146 130 147 let validation_status = if input.validate.should_skip() { 131 148 None ··· 407 424 pub async fn put_record( 408 425 State(state): State<AppState>, 409 426 auth: Auth<Active>, 410 - Json(input): Json<PutRecordInput>, 427 + Json(mut input): Json<PutRecordInput>, 411 428 ) -> Result<Response, crate::api::error::ApiError> { 412 429 let upsert_proof = match auth.verify_repo_upsert(&input.collection) { 413 430 Ok(proof) => proof, ··· 450 467 }; 451 468 let mst = Mst::load(Arc::new(tracking_store.clone()), commit.data, None); 452 469 let key = format!("{}/{}", input.collection, input.rkey); 470 + 471 + input.record = elide_record_type(input.record, &input.collection); 472 + 453 473 let validation_status = if input.validate.should_skip() { 454 474 None 455 475 } else {