mirror of
https://gitlab.com/famedly/conduit.git
synced 2024-09-12 01:49:09 +02:00
Cleanup room.rs; replace unwraps with map_err
This commit is contained in:
parent
d5d25fb064
commit
c4bce1d0c7
1 changed files with 50 additions and 36 deletions
|
@ -22,7 +22,7 @@ use ruma::{
|
||||||
},
|
},
|
||||||
EventType,
|
EventType,
|
||||||
},
|
},
|
||||||
serde::{CanonicalJsonObject, JsonObject, Raw},
|
serde::{CanonicalJsonObject, JsonObject},
|
||||||
RoomAliasId, RoomId, RoomVersionId,
|
RoomAliasId, RoomId, RoomVersionId,
|
||||||
};
|
};
|
||||||
use serde_json::{json, value::to_raw_value};
|
use serde_json::{json, value::to_raw_value};
|
||||||
|
@ -128,42 +128,48 @@ pub async fn create_room_route(
|
||||||
.expect("Invalid creation content");
|
.expect("Invalid creation content");
|
||||||
content.insert(
|
content.insert(
|
||||||
"creator".into(),
|
"creator".into(),
|
||||||
json!(sender_user.clone()).try_into().unwrap(),
|
json!(&sender_user).try_into().map_err(|_| {
|
||||||
|
Error::BadRequest(ErrorKind::BadJson, "Invalid creation content")
|
||||||
|
})?,
|
||||||
);
|
);
|
||||||
content.insert(
|
content.insert(
|
||||||
"room_version".into(),
|
"room_version".into(),
|
||||||
json!(room_version.as_str()).try_into().unwrap(),
|
json!(room_version.as_str()).try_into().map_err(|_| {
|
||||||
|
Error::BadRequest(ErrorKind::BadJson, "Invalid creation content")
|
||||||
|
})?,
|
||||||
);
|
);
|
||||||
content
|
content
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
let mut content = Raw::<CanonicalJsonObject>::from_json(
|
let mut content = serde_json::from_str::<CanonicalJsonObject>(
|
||||||
to_raw_value(&RoomCreateEventContent::new(sender_user.clone())).unwrap(),
|
to_raw_value(&RoomCreateEventContent::new(sender_user.clone()))
|
||||||
|
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Invalid creation content"))?
|
||||||
|
.get(),
|
||||||
)
|
)
|
||||||
.deserialize_as::<CanonicalJsonObject>()
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
content.insert(
|
content.insert(
|
||||||
"room_version".into(),
|
"room_version".into(),
|
||||||
json!(room_version.as_str()).try_into().unwrap(),
|
json!(room_version.as_str()).try_into().map_err(|_| {
|
||||||
|
Error::BadRequest(ErrorKind::BadJson, "Invalid creation content")
|
||||||
|
})?,
|
||||||
);
|
);
|
||||||
content
|
content
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Validate creation content
|
// Validate creation content
|
||||||
match Raw::<CanonicalJsonObject>::from_json(
|
let de_result = serde_json::from_str::<CanonicalJsonObject>(
|
||||||
to_raw_value(&content).expect("Invalid creation content"),
|
to_raw_value(&content)
|
||||||
)
|
.expect("Invalid creation content")
|
||||||
.deserialize_as::<RoomCreateEventContent>()
|
.get(),
|
||||||
{
|
);
|
||||||
Ok(_t) => {}
|
|
||||||
Err(_e) => {
|
if let Err(_) = de_result {
|
||||||
return Err(Error::BadRequest(
|
return Err(Error::BadRequest(
|
||||||
ErrorKind::BadJson,
|
ErrorKind::BadJson,
|
||||||
"Invalid creation content",
|
"Invalid creation content",
|
||||||
))
|
));
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
// 1. The room create event
|
// 1. The room create event
|
||||||
db.rooms.build_and_append_pdu(
|
db.rooms.build_and_append_pdu(
|
||||||
|
@ -575,28 +581,36 @@ pub async fn upgrade_room_route(
|
||||||
// Send a m.room.create event containing a predecessor field and the applicable room_version
|
// Send a m.room.create event containing a predecessor field and the applicable room_version
|
||||||
create_event_content.insert(
|
create_event_content.insert(
|
||||||
"creator".into(),
|
"creator".into(),
|
||||||
json!(sender_user.clone()).try_into().unwrap(),
|
json!(&sender_user)
|
||||||
|
.try_into()
|
||||||
|
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Error forming creation event"))?,
|
||||||
);
|
);
|
||||||
create_event_content.insert(
|
create_event_content.insert(
|
||||||
"room_version".into(),
|
"room_version".into(),
|
||||||
json!(body.new_version.clone()).try_into().unwrap(),
|
json!(&body.new_version)
|
||||||
|
.try_into()
|
||||||
|
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Error forming creation event"))?,
|
||||||
|
);
|
||||||
|
create_event_content.insert(
|
||||||
|
"predecessor".into(),
|
||||||
|
json!(predecessor)
|
||||||
|
.try_into()
|
||||||
|
.map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Error forming creation event"))?,
|
||||||
);
|
);
|
||||||
create_event_content.insert("predecessor".into(), json!(predecessor).try_into().unwrap());
|
|
||||||
|
|
||||||
// Validate creation event content
|
// Validate creation event content
|
||||||
match Raw::<CanonicalJsonObject>::from_json(
|
let de_result = serde_json::from_str::<CanonicalJsonObject>(
|
||||||
to_raw_value(&create_event_content).expect("Error forming creation event"),
|
to_raw_value(&create_event_content)
|
||||||
)
|
.expect("Error forming creation event")
|
||||||
.deserialize_as::<RoomCreateEventContent>()
|
.get(),
|
||||||
{
|
);
|
||||||
Ok(_t) => {}
|
|
||||||
Err(_e) => {
|
if let Err(_) = de_result {
|
||||||
return Err(Error::BadRequest(
|
return Err(Error::BadRequest(
|
||||||
ErrorKind::BadJson,
|
ErrorKind::BadJson,
|
||||||
"Error forming creation event",
|
"Error forming creation event",
|
||||||
))
|
));
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
db.rooms.build_and_append_pdu(
|
db.rooms.build_and_append_pdu(
|
||||||
PduBuilder {
|
PduBuilder {
|
||||||
|
|
Loading…
Reference in a new issue