1
0
Fork 0
mirror of https://gitlab.com/famedly/conduit.git synced 2024-11-16 02:11:05 +01:00

Upgrade Ruma

Co-authored-by: Timo Kösters <timo@koesters.xyz>
This commit is contained in:
Jonas Platte 2021-10-13 10:16:45 +02:00
parent 47f3263396
commit 09895a20c8
No known key found for this signature in database
GPG key ID: 7D261D771D915378
21 changed files with 628 additions and 623 deletions

50
Cargo.lock generated
View file

@ -1968,7 +1968,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma" name = "ruma"
version = "0.4.0" version = "0.4.0"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"assign", "assign",
"js_int", "js_int",
@ -1988,8 +1988,8 @@ dependencies = [
[[package]] [[package]]
name = "ruma-api" name = "ruma-api"
version = "0.18.3" version = "0.18.5"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"bytes", "bytes",
"http", "http",
@ -2004,8 +2004,8 @@ dependencies = [
[[package]] [[package]]
name = "ruma-api-macros" name = "ruma-api-macros"
version = "0.18.3" version = "0.18.5"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"proc-macro-crate", "proc-macro-crate",
"proc-macro2", "proc-macro2",
@ -2016,7 +2016,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-appservice-api" name = "ruma-appservice-api"
version = "0.4.0" version = "0.4.0"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"ruma-api", "ruma-api",
"ruma-common", "ruma-common",
@ -2029,8 +2029,8 @@ dependencies = [
[[package]] [[package]]
name = "ruma-client-api" name = "ruma-client-api"
version = "0.12.2" version = "0.12.3"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"assign", "assign",
"bytes", "bytes",
@ -2050,7 +2050,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-common" name = "ruma-common"
version = "0.6.0" version = "0.6.0"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"indexmap", "indexmap",
"js_int", "js_int",
@ -2064,8 +2064,8 @@ dependencies = [
[[package]] [[package]]
name = "ruma-events" name = "ruma-events"
version = "0.24.5" version = "0.24.6"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"indoc", "indoc",
"js_int", "js_int",
@ -2080,8 +2080,8 @@ dependencies = [
[[package]] [[package]]
name = "ruma-events-macros" name = "ruma-events-macros"
version = "0.24.5" version = "0.24.6"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"proc-macro-crate", "proc-macro-crate",
"proc-macro2", "proc-macro2",
@ -2092,7 +2092,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-federation-api" name = "ruma-federation-api"
version = "0.3.1" version = "0.3.1"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"js_int", "js_int",
"ruma-api", "ruma-api",
@ -2107,9 +2107,10 @@ dependencies = [
[[package]] [[package]]
name = "ruma-identifiers" name = "ruma-identifiers"
version = "0.20.0" version = "0.20.0"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"paste", "paste",
"percent-encoding",
"rand 0.8.4", "rand 0.8.4",
"ruma-identifiers-macros", "ruma-identifiers-macros",
"ruma-identifiers-validation", "ruma-identifiers-validation",
@ -2121,7 +2122,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-identifiers-macros" name = "ruma-identifiers-macros"
version = "0.20.0" version = "0.20.0"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"quote", "quote",
"ruma-identifiers-validation", "ruma-identifiers-validation",
@ -2131,12 +2132,15 @@ dependencies = [
[[package]] [[package]]
name = "ruma-identifiers-validation" name = "ruma-identifiers-validation"
version = "0.5.0" version = "0.5.0"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [
"thiserror",
]
[[package]] [[package]]
name = "ruma-identity-service-api" name = "ruma-identity-service-api"
version = "0.3.0" version = "0.3.0"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"js_int", "js_int",
"ruma-api", "ruma-api",
@ -2149,7 +2153,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-push-gateway-api" name = "ruma-push-gateway-api"
version = "0.3.0" version = "0.3.0"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"js_int", "js_int",
"ruma-api", "ruma-api",
@ -2164,7 +2168,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-serde" name = "ruma-serde"
version = "0.5.0" version = "0.5.0"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"bytes", "bytes",
"form_urlencoded", "form_urlencoded",
@ -2178,7 +2182,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-serde-macros" name = "ruma-serde-macros"
version = "0.5.0" version = "0.5.0"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"proc-macro-crate", "proc-macro-crate",
"proc-macro2", "proc-macro2",
@ -2189,7 +2193,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-signatures" name = "ruma-signatures"
version = "0.9.0" version = "0.9.0"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"base64 0.13.0", "base64 0.13.0",
"ed25519-dalek", "ed25519-dalek",
@ -2206,7 +2210,7 @@ dependencies = [
[[package]] [[package]]
name = "ruma-state-res" name = "ruma-state-res"
version = "0.4.1" version = "0.4.1"
source = "git+https://github.com/ruma/ruma?rev=a6a1224652912a957b09f136ec5da2686be6e0e2#a6a1224652912a957b09f136ec5da2686be6e0e2" source = "git+https://github.com/ruma/ruma?rev=44cfd0adbc83303c19aef590ad0d71647e19f197#44cfd0adbc83303c19aef590ad0d71647e19f197"
dependencies = [ dependencies = [
"itertools 0.10.1", "itertools 0.10.1",
"js_int", "js_int",

View file

@ -19,7 +19,7 @@ rocket = { version = "0.5.0-rc.1", features = ["tls"] } # Used to handle request
# Used for matrix spec type definitions and helpers # Used for matrix spec type definitions and helpers
#ruma = { version = "0.4.0", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-pre-spec", "unstable-exhaustive-types"] } #ruma = { version = "0.4.0", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-pre-spec", "unstable-exhaustive-types"] }
ruma = { git = "https://github.com/ruma/ruma", rev = "a6a1224652912a957b09f136ec5da2686be6e0e2", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-pre-spec", "unstable-exhaustive-types"] } ruma = { git = "https://github.com/ruma/ruma", rev = "44cfd0adbc83303c19aef590ad0d71647e19f197", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-pre-spec", "unstable-exhaustive-types"] }
#ruma = { git = "https://github.com/timokoesters/ruma", rev = "50c1db7e0a3a21fc794b0cce3b64285a4c750c71", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-pre-spec", "unstable-exhaustive-types"] } #ruma = { git = "https://github.com/timokoesters/ruma", rev = "50c1db7e0a3a21fc794b0cce3b64285a4c750c71", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-pre-spec", "unstable-exhaustive-types"] }
#ruma = { path = "../ruma/crates/ruma", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-pre-spec", "unstable-exhaustive-types"] } #ruma = { path = "../ruma/crates/ruma", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-pre-spec", "unstable-exhaustive-types"] }

View file

@ -1 +1 @@
1.52 1.53

View file

@ -15,19 +15,28 @@ use ruma::{
ThirdPartyIdRemovalStatus, ThirdPartyIdRemovalStatus,
}, },
contact::get_contacts, contact::get_contacts,
uiaa::{AuthFlow, UiaaInfo}, uiaa::{AuthFlow, AuthType, UiaaInfo},
}, },
}, },
events::{ events::{
room::{ room::{
canonical_alias, guest_access, history_visibility, join_rules, member, message, name, canonical_alias::RoomCanonicalAliasEventContent,
topic, create::RoomCreateEventContent,
guest_access::{GuestAccess, RoomGuestAccessEventContent},
history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
join_rules::{JoinRule, RoomJoinRulesEventContent},
member::{MembershipState, RoomMemberEventContent},
message::RoomMessageEventContent,
name::RoomNameEventContent,
power_levels::RoomPowerLevelsEventContent,
topic::RoomTopicEventContent,
}, },
EventType, EventType,
}, },
identifiers::RoomName, identifiers::RoomName,
push, RoomAliasId, RoomId, RoomVersionId, UserId, push, RoomAliasId, RoomId, RoomVersionId, UserId,
}; };
use serde_json::value::to_raw_value;
use tracing::info; use tracing::info;
use register::RegistrationKind; use register::RegistrationKind;
@ -147,7 +156,7 @@ pub async fn register_route(
// UIAA // UIAA
let mut uiaainfo = UiaaInfo { let mut uiaainfo = UiaaInfo {
flows: vec![AuthFlow { flows: vec![AuthFlow {
stages: vec!["m.login.dummy".to_owned()], stages: vec![AuthType::Dummy],
}], }],
completed: Vec::new(), completed: Vec::new(),
params: Default::default(), params: Default::default(),
@ -270,7 +279,7 @@ pub async fn register_route(
); );
let state_lock = mutex_state.lock().await; let state_lock = mutex_state.lock().await;
let mut content = ruma::events::room::create::CreateEventContent::new(conduit_user.clone()); let mut content = RoomCreateEventContent::new(conduit_user.clone());
content.federate = true; content.federate = true;
content.predecessor = None; content.predecessor = None;
content.room_version = RoomVersionId::Version6; content.room_version = RoomVersionId::Version6;
@ -279,7 +288,7 @@ pub async fn register_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomCreate, event_type: EventType::RoomCreate,
content: serde_json::to_value(content).expect("event is valid, we just created it"), content: to_raw_value(&content).expect("event is valid, we just created it"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
redacts: None, redacts: None,
@ -294,8 +303,8 @@ pub async fn register_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: serde_json::to_value(member::MemberEventContent { content: to_raw_value(&RoomMemberEventContent {
membership: member::MembershipState::Join, membership: MembershipState::Join,
displayname: None, displayname: None,
avatar_url: None, avatar_url: None,
is_direct: None, is_direct: None,
@ -322,12 +331,10 @@ pub async fn register_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomPowerLevels, event_type: EventType::RoomPowerLevels,
content: serde_json::to_value( content: to_raw_value(&RoomPowerLevelsEventContent {
ruma::events::room::power_levels::PowerLevelsEventContent { users,
users, ..Default::default()
..Default::default() })
},
)
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
@ -343,10 +350,8 @@ pub async fn register_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomJoinRules, event_type: EventType::RoomJoinRules,
content: serde_json::to_value(join_rules::JoinRulesEventContent::new( content: to_raw_value(&RoomJoinRulesEventContent::new(JoinRule::Invite))
join_rules::JoinRule::Invite, .expect("event is valid, we just created it"),
))
.expect("event is valid, we just created it"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
redacts: None, redacts: None,
@ -361,11 +366,9 @@ pub async fn register_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomHistoryVisibility, event_type: EventType::RoomHistoryVisibility,
content: serde_json::to_value( content: to_raw_value(&RoomHistoryVisibilityEventContent::new(
history_visibility::HistoryVisibilityEventContent::new( HistoryVisibility::Shared,
history_visibility::HistoryVisibility::Shared, ))
),
)
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
@ -381,10 +384,8 @@ pub async fn register_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomGuestAccess, event_type: EventType::RoomGuestAccess,
content: serde_json::to_value(guest_access::GuestAccessEventContent::new( content: to_raw_value(&RoomGuestAccessEventContent::new(GuestAccess::Forbidden))
guest_access::GuestAccess::Forbidden, .expect("event is valid, we just created it"),
))
.expect("event is valid, we just created it"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
redacts: None, redacts: None,
@ -402,7 +403,7 @@ pub async fn register_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomName, event_type: EventType::RoomName,
content: serde_json::to_value(name::NameEventContent::new(Some(room_name))) content: to_raw_value(&RoomNameEventContent::new(Some(room_name)))
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
@ -417,7 +418,7 @@ pub async fn register_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomTopic, event_type: EventType::RoomTopic,
content: serde_json::to_value(topic::TopicEventContent { content: to_raw_value(&RoomTopicEventContent {
topic: format!("Manage {}", db.globals.server_name()), topic: format!("Manage {}", db.globals.server_name()),
}) })
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
@ -439,7 +440,7 @@ pub async fn register_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomCanonicalAlias, event_type: EventType::RoomCanonicalAlias,
content: serde_json::to_value(canonical_alias::CanonicalAliasEventContent { content: to_raw_value(&RoomCanonicalAliasEventContent {
alias: Some(alias.clone()), alias: Some(alias.clone()),
alt_aliases: Vec::new(), alt_aliases: Vec::new(),
}) })
@ -460,8 +461,8 @@ pub async fn register_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: serde_json::to_value(member::MemberEventContent { content: to_raw_value(&RoomMemberEventContent {
membership: member::MembershipState::Invite, membership: MembershipState::Invite,
displayname: None, displayname: None,
avatar_url: None, avatar_url: None,
is_direct: None, is_direct: None,
@ -482,8 +483,8 @@ pub async fn register_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: serde_json::to_value(member::MemberEventContent { content: to_raw_value(&RoomMemberEventContent {
membership: member::MembershipState::Join, membership: MembershipState::Join,
displayname: Some(displayname), displayname: Some(displayname),
avatar_url: None, avatar_url: None,
is_direct: None, is_direct: None,
@ -506,7 +507,7 @@ pub async fn register_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMessage, event_type: EventType::RoomMessage,
content: serde_json::to_value(message::MessageEventContent::text_html( content: to_raw_value(&RoomMessageEventContent::text_html(
"## Thank you for trying out Conduit!\n\nConduit is currently in Beta. This means you can join and participate in most Matrix rooms, but not all features are supported and you might run into bugs from time to time.\n\nHelpful links:\n> Website: https://conduit.rs\n> Git and Documentation: https://gitlab.com/famedly/conduit\n> Report issues: https://gitlab.com/famedly/conduit/-/issues\n\nHere are some rooms you can join (by typing the command):\n\nConduit room (Ask questions and get notified on updates):\n`/join #conduit:fachschaften.org`\n\nConduit lounge (Off-topic, only Conduit users are allowed to join)\n`/join #conduit-lounge:conduit.rs`".to_owned(), "## Thank you for trying out Conduit!\n\nConduit is currently in Beta. This means you can join and participate in most Matrix rooms, but not all features are supported and you might run into bugs from time to time.\n\nHelpful links:\n> Website: https://conduit.rs\n> Git and Documentation: https://gitlab.com/famedly/conduit\n> Report issues: https://gitlab.com/famedly/conduit/-/issues\n\nHere are some rooms you can join (by typing the command):\n\nConduit room (Ask questions and get notified on updates):\n`/join #conduit:fachschaften.org`\n\nConduit lounge (Off-topic, only Conduit users are allowed to join)\n`/join #conduit-lounge:conduit.rs`".to_owned(),
"<h2>Thank you for trying out Conduit!</h2>\n<p>Conduit is currently in Beta. This means you can join and participate in most Matrix rooms, but not all features are supported and you might run into bugs from time to time.</p>\n<p>Helpful links:</p>\n<blockquote>\n<p>Website: https://conduit.rs<br>Git and Documentation: https://gitlab.com/famedly/conduit<br>Report issues: https://gitlab.com/famedly/conduit/-/issues</p>\n</blockquote>\n<p>Here are some rooms you can join (by typing the command):</p>\n<p>Conduit room (Ask questions and get notified on updates):<br><code>/join #conduit:fachschaften.org</code></p>\n<p>Conduit lounge (Off-topic, only Conduit users are allowed to join)<br><code>/join #conduit-lounge:conduit.rs</code></p>\n".to_owned(), "<h2>Thank you for trying out Conduit!</h2>\n<p>Conduit is currently in Beta. This means you can join and participate in most Matrix rooms, but not all features are supported and you might run into bugs from time to time.</p>\n<p>Helpful links:</p>\n<blockquote>\n<p>Website: https://conduit.rs<br>Git and Documentation: https://gitlab.com/famedly/conduit<br>Report issues: https://gitlab.com/famedly/conduit/-/issues</p>\n</blockquote>\n<p>Here are some rooms you can join (by typing the command):</p>\n<p>Conduit room (Ask questions and get notified on updates):<br><code>/join #conduit:fachschaften.org</code></p>\n<p>Conduit lounge (Off-topic, only Conduit users are allowed to join)<br><code>/join #conduit-lounge:conduit.rs</code></p>\n".to_owned(),
)) ))
@ -562,7 +563,7 @@ pub async fn change_password_route(
let mut uiaainfo = UiaaInfo { let mut uiaainfo = UiaaInfo {
flows: vec![AuthFlow { flows: vec![AuthFlow {
stages: vec!["m.login.password".to_owned()], stages: vec![AuthType::Password],
}], }],
completed: Vec::new(), completed: Vec::new(),
params: Default::default(), params: Default::default(),
@ -654,7 +655,7 @@ pub async fn deactivate_route(
let mut uiaainfo = UiaaInfo { let mut uiaainfo = UiaaInfo {
flows: vec![AuthFlow { flows: vec![AuthFlow {
stages: vec!["m.login.password".to_owned()], stages: vec![AuthType::Password],
}], }],
completed: Vec::new(), completed: Vec::new(),
params: Default::default(), params: Default::default(),
@ -698,8 +699,8 @@ pub async fn deactivate_route(
for room_id in all_rooms { for room_id in all_rooms {
let room_id = room_id?; let room_id = room_id?;
let event = member::MemberEventContent { let event = RoomMemberEventContent {
membership: member::MembershipState::Leave, membership: MembershipState::Leave,
displayname: None, displayname: None,
avatar_url: None, avatar_url: None,
is_direct: None, is_direct: None,
@ -721,7 +722,7 @@ pub async fn deactivate_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: serde_json::to_value(event).expect("event is valid, we just created it"), content: to_raw_value(&event).expect("event is valid, we just created it"),
unsigned: None, unsigned: None,
state_key: Some(sender_user.to_string()), state_key: Some(sender_user.to_string()),
redacts: None, redacts: None,

View file

@ -3,7 +3,7 @@ use ruma::api::client::{
error::ErrorKind, error::ErrorKind,
r0::{ r0::{
device::{self, delete_device, delete_devices, get_device, get_devices, update_device}, device::{self, delete_device, delete_devices, get_device, get_devices, update_device},
uiaa::{AuthFlow, UiaaInfo}, uiaa::{AuthFlow, AuthType, UiaaInfo},
}, },
}; };
@ -109,7 +109,7 @@ pub async fn delete_device_route(
// UIAA // UIAA
let mut uiaainfo = UiaaInfo { let mut uiaainfo = UiaaInfo {
flows: vec![AuthFlow { flows: vec![AuthFlow {
stages: vec!["m.login.password".to_owned()], stages: vec![AuthType::Password],
}], }],
completed: Vec::new(), completed: Vec::new(),
params: Default::default(), params: Default::default(),
@ -172,7 +172,7 @@ pub async fn delete_devices_route(
// UIAA // UIAA
let mut uiaainfo = UiaaInfo { let mut uiaainfo = UiaaInfo {
flows: vec![AuthFlow { flows: vec![AuthFlow {
stages: vec!["m.login.password".to_owned()], stages: vec![AuthType::Password],
}], }],
completed: Vec::new(), completed: Vec::new(),
params: Default::default(), params: Default::default(),

View file

@ -17,10 +17,16 @@ use ruma::{
}, },
directory::{Filter, IncomingFilter, IncomingRoomNetwork, PublicRoomsChunk, RoomNetwork}, directory::{Filter, IncomingFilter, IncomingRoomNetwork, PublicRoomsChunk, RoomNetwork},
events::{ events::{
room::{avatar, canonical_alias, guest_access, history_visibility, name, topic}, room::{
avatar::RoomAvatarEventContent,
canonical_alias::RoomCanonicalAliasEventContent,
guest_access::{GuestAccess, RoomGuestAccessEventContent},
history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
name::RoomNameEventContent,
topic::RoomTopicEventContent,
},
EventType, EventType,
}, },
serde::Raw,
ServerName, UInt, ServerName, UInt,
}; };
use tracing::{info, warn}; use tracing::{info, warn};
@ -217,157 +223,143 @@ pub(crate) async fn get_public_rooms_filtered_helper(
} }
} }
let mut all_rooms = let mut all_rooms = db
db.rooms .rooms
.public_rooms() .public_rooms()
.map(|room_id| { .map(|room_id| {
let room_id = room_id?; let room_id = room_id?;
let chunk = PublicRoomsChunk { let chunk = PublicRoomsChunk {
aliases: Vec::new(), aliases: Vec::new(),
canonical_alias: db canonical_alias: db
.rooms .rooms
.room_state_get(&room_id, &EventType::RoomCanonicalAlias, "")? .room_state_get(&room_id, &EventType::RoomCanonicalAlias, "")?
.map_or(Ok::<_, Error>(None), |s| { .map_or(Ok::<_, Error>(None), |s| {
Ok(serde_json::from_value::< Ok(
Raw<canonical_alias::CanonicalAliasEventContent>, serde_json::from_str::<RoomCanonicalAliasEventContent>(s.content.get())
>(s.content.clone()) .map_err(|_| {
.expect("from_value::<Raw<..>> can never fail") Error::bad_database(
.deserialize() "Invalid canonical alias event in database.",
.map_err(|_| { )
Error::bad_database("Invalid canonical alias event in database.") })?
})? .alias,
.alias) )
})?, })?,
name: db name: db
.rooms .rooms
.room_state_get(&room_id, &EventType::RoomName, "")? .room_state_get(&room_id, &EventType::RoomName, "")?
.map_or(Ok::<_, Error>(None), |s| { .map_or(Ok::<_, Error>(None), |s| {
Ok(serde_json::from_value::<Raw<name::NameEventContent>>( Ok(
s.content.clone(), serde_json::from_str::<RoomNameEventContent>(s.content.get())
) .map_err(|_| {
.expect("from_value::<Raw<..>> can never fail") Error::bad_database("Invalid room name event in database.")
.deserialize() })?
.map_err(|_| { .name,
Error::bad_database("Invalid room name event in database.") )
})? })?,
.name) num_joined_members: db
})?, .rooms
num_joined_members: db .room_joined_count(&room_id)?
.rooms .unwrap_or_else(|| {
.room_joined_count(&room_id)? warn!("Room {} has no member count", room_id);
.unwrap_or_else(|| { 0
warn!("Room {} has no member count", room_id); })
0 .try_into()
}) .expect("user count should not be that big"),
.try_into() topic: db
.expect("user count should not be that big"), .rooms
topic: db .room_state_get(&room_id, &EventType::RoomTopic, "")?
.rooms .map_or(Ok::<_, Error>(None), |s| {
.room_state_get(&room_id, &EventType::RoomTopic, "")? Ok(Some(
.map_or(Ok::<_, Error>(None), |s| { serde_json::from_str::<RoomTopicEventContent>(s.content.get())
Ok(Some(
serde_json::from_value::<Raw<topic::TopicEventContent>>(
s.content.clone(),
)
.expect("from_value::<Raw<..>> can never fail")
.deserialize()
.map_err(|_| { .map_err(|_| {
Error::bad_database("Invalid room topic event in database.") Error::bad_database("Invalid room topic event in database.")
})? })?
.topic, .topic,
)) ))
})?, })?,
world_readable: db world_readable: db
.rooms .rooms
.room_state_get(&room_id, &EventType::RoomHistoryVisibility, "")? .room_state_get(&room_id, &EventType::RoomHistoryVisibility, "")?
.map_or(Ok::<_, Error>(false), |s| { .map_or(Ok::<_, Error>(false), |s| {
Ok(serde_json::from_value::< Ok(serde_json::from_str::<RoomHistoryVisibilityEventContent>(
Raw<history_visibility::HistoryVisibilityEventContent>, s.content.get(),
>(s.content.clone())
.expect("from_value::<Raw<..>> can never fail")
.deserialize()
.map_err(|_| {
Error::bad_database(
"Invalid room history visibility event in database.",
)
})?
.history_visibility
== history_visibility::HistoryVisibility::WorldReadable)
})?,
guest_can_join: db
.rooms
.room_state_get(&room_id, &EventType::RoomGuestAccess, "")?
.map_or(Ok::<_, Error>(false), |s| {
Ok(
serde_json::from_value::<Raw<guest_access::GuestAccessEventContent>>(
s.content.clone(),
)
.expect("from_value::<Raw<..>> can never fail")
.deserialize()
.map_err(|_| {
Error::bad_database("Invalid room guest access event in database.")
})?
.guest_access
== guest_access::GuestAccess::CanJoin,
) )
})?, .map_err(|_| {
avatar_url: db Error::bad_database(
.rooms "Invalid room history visibility event in database.",
.room_state_get(&room_id, &EventType::RoomAvatar, "")? )
.map(|s| { })?
Ok::<_, Error>( .history_visibility
serde_json::from_value::<Raw<avatar::AvatarEventContent>>( == HistoryVisibility::WorldReadable)
s.content.clone(), })?,
) guest_can_join: db
.expect("from_value::<Raw<..>> can never fail") .rooms
.deserialize() .room_state_get(&room_id, &EventType::RoomGuestAccess, "")?
.map_or(Ok::<_, Error>(false), |s| {
Ok(
serde_json::from_str::<RoomGuestAccessEventContent>(s.content.get())
.map_err(|_| {
Error::bad_database(
"Invalid room guest access event in database.",
)
})?
.guest_access
== GuestAccess::CanJoin,
)
})?,
avatar_url: db
.rooms
.room_state_get(&room_id, &EventType::RoomAvatar, "")?
.map(|s| {
Ok::<_, Error>(
serde_json::from_str::<RoomAvatarEventContent>(s.content.get())
.map_err(|_| { .map_err(|_| {
Error::bad_database("Invalid room avatar event in database.") Error::bad_database("Invalid room avatar event in database.")
})? })?
.url, .url,
) )
}) })
.transpose()? .transpose()?
// url is now an Option<String> so we must flatten // url is now an Option<String> so we must flatten
.flatten(), .flatten(),
room_id, room_id,
}; };
Ok(chunk) Ok(chunk)
}) })
.filter_map(|r: Result<_>| r.ok()) // Filter out buggy rooms .filter_map(|r: Result<_>| r.ok()) // Filter out buggy rooms
.filter(|chunk| { .filter(|chunk| {
if let Some(query) = filter if let Some(query) = filter
.generic_search_term .generic_search_term
.as_ref() .as_ref()
.map(|q| q.to_lowercase()) .map(|q| q.to_lowercase())
{ {
if let Some(name) = &chunk.name { if let Some(name) = &chunk.name {
if name.as_str().to_lowercase().contains(&query) { if name.as_str().to_lowercase().contains(&query) {
return true; return true;
}
} }
if let Some(topic) = &chunk.topic {
if topic.to_lowercase().contains(&query) {
return true;
}
}
if let Some(canonical_alias) = &chunk.canonical_alias {
if canonical_alias.as_str().to_lowercase().contains(&query) {
return true;
}
}
false
} else {
// No search term
true
} }
})
// We need to collect all, so we can sort by member count if let Some(topic) = &chunk.topic {
.collect::<Vec<_>>(); if topic.to_lowercase().contains(&query) {
return true;
}
}
if let Some(canonical_alias) = &chunk.canonical_alias {
if canonical_alias.as_str().to_lowercase().contains(&query) {
return true;
}
}
false
} else {
// No search term
true
}
})
// We need to collect all, so we can sort by member count
.collect::<Vec<_>>();
all_rooms.sort_by(|l, r| r.num_joined_members.cmp(&l.num_joined_members)); all_rooms.sort_by(|l, r| r.num_joined_members.cmp(&l.num_joined_members));

View file

@ -10,7 +10,7 @@ use ruma::{
claim_keys, get_key_changes, get_keys, upload_keys, upload_signatures, claim_keys, get_key_changes, get_keys, upload_keys, upload_signatures,
upload_signing_keys, upload_signing_keys,
}, },
uiaa::{AuthFlow, UiaaInfo}, uiaa::{AuthFlow, AuthType, UiaaInfo},
}, },
}, },
federation, federation,
@ -148,7 +148,7 @@ pub async fn upload_signing_keys_route(
// UIAA // UIAA
let mut uiaainfo = UiaaInfo { let mut uiaainfo = UiaaInfo {
flows: vec![AuthFlow { flows: vec![AuthFlow {
stages: vec!["m.login.password".to_owned()], stages: vec![AuthType::Password],
}], }],
completed: Vec::new(), completed: Vec::new(),
params: Default::default(), params: Default::default(),

View file

@ -1,10 +1,9 @@
use crate::{ use crate::{
client_server, client_server,
database::DatabaseGuard, database::DatabaseGuard,
pdu::{PduBuilder, PduEvent}, pdu::{EventHash, PduBuilder, PduEvent},
server_server, utils, ConduitResult, Database, Error, Result, Ruma, server_server, utils, ConduitResult, Database, Error, Result, Ruma,
}; };
use member::{MemberEventContent, MembershipState};
use ruma::{ use ruma::{
api::{ api::{
client::{ client::{
@ -18,14 +17,17 @@ use ruma::{
federation::{self, membership::create_invite}, federation::{self, membership::create_invite},
}, },
events::{ events::{
pdu::Pdu, room::{
room::{create::CreateEventContent, member}, create::RoomCreateEventContent,
member::{MembershipState, RoomMemberEventContent},
},
EventType, EventType,
}, },
serde::{to_canonical_value, CanonicalJsonObject, CanonicalJsonValue, Raw}, serde::{to_canonical_value, CanonicalJsonObject, CanonicalJsonValue},
state_res::{self, RoomVersion}, state_res::{self, RoomVersion},
uint, EventId, RoomId, RoomVersionId, ServerName, UserId, uint, EventId, RoomId, RoomVersionId, ServerName, UserId,
}; };
use serde_json::value::{to_raw_value, RawValue as RawJsonValue};
use std::{ use std::{
collections::{hash_map::Entry, BTreeMap, HashMap, HashSet}, collections::{hash_map::Entry, BTreeMap, HashMap, HashSet},
convert::{TryFrom, TryInto}, convert::{TryFrom, TryInto},
@ -204,7 +206,7 @@ pub async fn kick_user_route(
) -> ConduitResult<kick_user::Response> { ) -> ConduitResult<kick_user::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let mut event = serde_json::from_value::<Raw<ruma::events::room::member::MemberEventContent>>( let mut event: RoomMemberEventContent = serde_json::from_str(
db.rooms db.rooms
.room_state_get( .room_state_get(
&body.room_id, &body.room_id,
@ -216,13 +218,11 @@ pub async fn kick_user_route(
"Cannot kick member that's not in the room.", "Cannot kick member that's not in the room.",
))? ))?
.content .content
.clone(), .get(),
) )
.expect("Raw::from_value always works")
.deserialize()
.map_err(|_| Error::bad_database("Invalid member event in database."))?; .map_err(|_| Error::bad_database("Invalid member event in database."))?;
event.membership = ruma::events::room::member::MembershipState::Leave; event.membership = MembershipState::Leave;
// TODO: reason // TODO: reason
let mutex_state = Arc::clone( let mutex_state = Arc::clone(
@ -238,7 +238,7 @@ pub async fn kick_user_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: serde_json::to_value(event).expect("event is valid, we just created it"), content: to_raw_value(&event).expect("event is valid, we just created it"),
unsigned: None, unsigned: None,
state_key: Some(body.user_id.to_string()), state_key: Some(body.user_id.to_string()),
redacts: None, redacts: None,
@ -280,8 +280,8 @@ pub async fn ban_user_route(
&body.user_id.to_string(), &body.user_id.to_string(),
)? )?
.map_or( .map_or(
Ok::<_, Error>(member::MemberEventContent { Ok::<_, Error>(RoomMemberEventContent {
membership: member::MembershipState::Ban, membership: MembershipState::Ban,
displayname: db.users.displayname(&body.user_id)?, displayname: db.users.displayname(&body.user_id)?,
avatar_url: db.users.avatar_url(&body.user_id)?, avatar_url: db.users.avatar_url(&body.user_id)?,
is_direct: None, is_direct: None,
@ -290,13 +290,9 @@ pub async fn ban_user_route(
reason: None, reason: None,
}), }),
|event| { |event| {
let mut event = serde_json::from_value::<Raw<member::MemberEventContent>>( let mut event = serde_json::from_str::<RoomMemberEventContent>(event.content.get())
event.content.clone(), .map_err(|_| Error::bad_database("Invalid member event in database."))?;
) event.membership = MembershipState::Ban;
.expect("Raw::from_value always works")
.deserialize()
.map_err(|_| Error::bad_database("Invalid member event in database."))?;
event.membership = ruma::events::room::member::MembershipState::Ban;
Ok(event) Ok(event)
}, },
)?; )?;
@ -314,7 +310,7 @@ pub async fn ban_user_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: serde_json::to_value(event).expect("event is valid, we just created it"), content: to_raw_value(&event).expect("event is valid, we just created it"),
unsigned: None, unsigned: None,
state_key: Some(body.user_id.to_string()), state_key: Some(body.user_id.to_string()),
redacts: None, redacts: None,
@ -346,7 +342,7 @@ pub async fn unban_user_route(
) -> ConduitResult<unban_user::Response> { ) -> ConduitResult<unban_user::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let mut event = serde_json::from_value::<Raw<ruma::events::room::member::MemberEventContent>>( let mut event = serde_json::from_str::<RoomMemberEventContent>(
db.rooms db.rooms
.room_state_get( .room_state_get(
&body.room_id, &body.room_id,
@ -358,13 +354,11 @@ pub async fn unban_user_route(
"Cannot unban a user who is not banned.", "Cannot unban a user who is not banned.",
))? ))?
.content .content
.clone(), .get(),
) )
.expect("from_value::<Raw<..>> can never fail")
.deserialize()
.map_err(|_| Error::bad_database("Invalid member event in database."))?; .map_err(|_| Error::bad_database("Invalid member event in database."))?;
event.membership = ruma::events::room::member::MembershipState::Leave; event.membership = MembershipState::Leave;
let mutex_state = Arc::clone( let mutex_state = Arc::clone(
db.globals db.globals
@ -379,7 +373,7 @@ pub async fn unban_user_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: serde_json::to_value(event).expect("event is valid, we just created it"), content: to_raw_value(&event).expect("event is valid, we just created it"),
unsigned: None, unsigned: None,
state_key: Some(body.user_id.to_string()), state_key: Some(body.user_id.to_string()),
redacts: None, redacts: None,
@ -584,10 +578,9 @@ async fn join_room_by_id_helper(
}; };
let mut join_event_stub = let mut join_event_stub =
serde_json::from_str::<CanonicalJsonObject>(make_join_response.event.json().get()) serde_json::from_str::<CanonicalJsonObject>(make_join_response.event.get()).map_err(
.map_err(|_| { |_| Error::BadServerResponse("Invalid make_join event json received from server."),
Error::BadServerResponse("Invalid make_join event json received from server.") )?;
})?;
// TODO: Is origin needed? // TODO: Is origin needed?
join_event_stub.insert( join_event_stub.insert(
@ -604,8 +597,8 @@ async fn join_room_by_id_helper(
); );
join_event_stub.insert( join_event_stub.insert(
"content".to_owned(), "content".to_owned(),
to_canonical_value(member::MemberEventContent { to_canonical_value(RoomMemberEventContent {
membership: member::MembershipState::Join, membership: MembershipState::Join,
displayname: db.users.displayname(sender_user)?, displayname: db.users.displayname(sender_user)?,
avatar_url: db.users.avatar_url(sender_user)?, avatar_url: db.users.avatar_url(sender_user)?,
is_direct: None, is_direct: None,
@ -653,7 +646,7 @@ async fn join_room_by_id_helper(
federation::membership::create_join_event::v2::Request { federation::membership::create_join_event::v2::Request {
room_id, room_id,
event_id: &event_id, event_id: &event_id,
pdu: PduEvent::convert_to_outgoing_federation_event(join_event.clone()), pdu: &PduEvent::convert_to_outgoing_federation_event(join_event.clone()),
}, },
) )
.await?; .await?;
@ -756,8 +749,8 @@ async fn join_room_by_id_helper(
// where events in the current room state do not exist // where events in the current room state do not exist
db.rooms.set_room_state(room_id, statehashid)?; db.rooms.set_room_state(room_id, statehashid)?;
} else { } else {
let event = member::MemberEventContent { let event = RoomMemberEventContent {
membership: member::MembershipState::Join, membership: MembershipState::Join,
displayname: db.users.displayname(sender_user)?, displayname: db.users.displayname(sender_user)?,
avatar_url: db.users.avatar_url(sender_user)?, avatar_url: db.users.avatar_url(sender_user)?,
is_direct: None, is_direct: None,
@ -769,7 +762,7 @@ async fn join_room_by_id_helper(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: serde_json::to_value(event).expect("event is valid, we just created it"), content: to_raw_value(&event).expect("event is valid, we just created it"),
unsigned: None, unsigned: None,
state_key: Some(sender_user.to_string()), state_key: Some(sender_user.to_string()),
redacts: None, redacts: None,
@ -789,12 +782,12 @@ async fn join_room_by_id_helper(
} }
fn validate_and_add_event_id( fn validate_and_add_event_id(
pdu: &Raw<Pdu>, pdu: &RawJsonValue,
room_version: &RoomVersionId, room_version: &RoomVersionId,
pub_key_map: &RwLock<BTreeMap<String, BTreeMap<String, String>>>, pub_key_map: &RwLock<BTreeMap<String, BTreeMap<String, String>>>,
db: &Database, db: &Database,
) -> Result<(EventId, CanonicalJsonObject)> { ) -> Result<(EventId, CanonicalJsonObject)> {
let mut value = serde_json::from_str::<CanonicalJsonObject>(pdu.json().get()).map_err(|e| { let mut value = serde_json::from_str::<CanonicalJsonObject>(pdu.get()).map_err(|e| {
error!("Invalid PDU in server response: {:?}: {:?}", pdu, e); error!("Invalid PDU in server response: {:?}: {:?}", pdu, e);
Error::BadServerResponse("Invalid PDU in server response") Error::BadServerResponse("Invalid PDU in server response")
})?; })?;
@ -884,9 +877,7 @@ pub(crate) async fn invite_helper<'a>(
let create_event_content = create_event let create_event_content = create_event
.as_ref() .as_ref()
.map(|create_event| { .map(|create_event| {
serde_json::from_value::<Raw<CreateEventContent>>(create_event.content.clone()) serde_json::from_str::<RoomCreateEventContent>(create_event.content.get())
.expect("Raw::from_value always works.")
.deserialize()
.map_err(|e| { .map_err(|e| {
warn!("Invalid create event: {}", e); warn!("Invalid create event: {}", e);
Error::bad_database("Invalid create event in db.") Error::bad_database("Invalid create event in db.")
@ -910,7 +901,7 @@ pub(crate) async fn invite_helper<'a>(
let room_version = let room_version =
RoomVersion::new(&room_version_id).expect("room version is supported"); RoomVersion::new(&room_version_id).expect("room version is supported");
let content = serde_json::to_value(MemberEventContent { let content = to_raw_value(&RoomMemberEventContent {
avatar_url: None, avatar_url: None,
displayname: None, displayname: None,
is_direct: Some(is_direct), is_direct: Some(is_direct),
@ -946,7 +937,7 @@ pub(crate) async fn invite_helper<'a>(
unsigned.insert("prev_content".to_owned(), prev_pdu.content.clone()); unsigned.insert("prev_content".to_owned(), prev_pdu.content.clone());
unsigned.insert( unsigned.insert(
"prev_sender".to_owned(), "prev_sender".to_owned(),
serde_json::to_value(&prev_pdu.sender).expect("UserId::to_value always works"), serde_json::from_str(prev_pdu.sender.as_str()).expect("UserId is valid string"),
); );
} }
@ -967,11 +958,15 @@ pub(crate) async fn invite_helper<'a>(
.map(|(_, pdu)| pdu.event_id.clone()) .map(|(_, pdu)| pdu.event_id.clone())
.collect(), .collect(),
redacts: None, redacts: None,
unsigned, unsigned: if unsigned.is_empty() {
hashes: ruma::events::pdu::EventHash { None
} else {
Some(to_raw_value(&unsigned).expect("to_raw_value always works"))
},
hashes: EventHash {
sha256: "aaa".to_owned(), sha256: "aaa".to_owned(),
}, },
signatures: BTreeMap::new(), signatures: None,
}; };
let auth_check = state_res::auth_check( let auth_check = state_res::auth_check(
@ -1035,11 +1030,11 @@ pub(crate) async fn invite_helper<'a>(
&db.globals, &db.globals,
user_id.server_name(), user_id.server_name(),
create_invite::v2::Request { create_invite::v2::Request {
room_id: room_id.clone(), room_id,
event_id: expected_event_id.clone(), event_id: &expected_event_id,
room_version: room_version_id, room_version: &room_version_id,
event: PduEvent::convert_to_outgoing_federation_event(pdu_json.clone()), event: &PduEvent::convert_to_outgoing_federation_event(pdu_json.clone()),
invite_room_state, invite_room_state: &invite_room_state,
}, },
) )
.await?; .await?;
@ -1116,8 +1111,8 @@ pub(crate) async fn invite_helper<'a>(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: serde_json::to_value(member::MemberEventContent { content: to_raw_value(&RoomMemberEventContent {
membership: member::MembershipState::Invite, membership: MembershipState::Invite,
displayname: db.users.displayname(user_id)?, displayname: db.users.displayname(user_id)?,
avatar_url: db.users.avatar_url(user_id)?, avatar_url: db.users.avatar_url(user_id)?,
is_direct: Some(is_direct), is_direct: Some(is_direct),

View file

@ -9,9 +9,9 @@ use ruma::{
}, },
federation::{self, query::get_profile_information::v1::ProfileField}, federation::{self, query::get_profile_information::v1::ProfileField},
}, },
events::EventType, events::{room::member::RoomMemberEventContent, EventType},
serde::Raw,
}; };
use serde_json::value::to_raw_value;
use std::{convert::TryInto, sync::Arc}; use std::{convert::TryInto, sync::Arc};
#[cfg(feature = "conduit_bin")] #[cfg(feature = "conduit_bin")]
@ -45,9 +45,9 @@ pub async fn set_displayname_route(
Ok::<_, Error>(( Ok::<_, Error>((
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: serde_json::to_value(ruma::events::room::member::MemberEventContent { content: to_raw_value(&RoomMemberEventContent {
displayname: body.displayname.clone(), displayname: body.displayname.clone(),
..serde_json::from_value::<Raw<_>>( ..serde_json::from_str(
db.rooms db.rooms
.room_state_get( .room_state_get(
&room_id, &room_id,
@ -61,10 +61,8 @@ pub async fn set_displayname_route(
) )
})? })?
.content .content
.clone(), .get(),
) )
.expect("from_value::<Raw<..>> can never fail")
.deserialize()
.map_err(|_| Error::bad_database("Database contains invalid PDU."))? .map_err(|_| Error::bad_database("Database contains invalid PDU."))?
}) })
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
@ -190,9 +188,9 @@ pub async fn set_avatar_url_route(
Ok::<_, Error>(( Ok::<_, Error>((
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: serde_json::to_value(ruma::events::room::member::MemberEventContent { content: to_raw_value(&RoomMemberEventContent {
avatar_url: body.avatar_url.clone(), avatar_url: body.avatar_url.clone(),
..serde_json::from_value::<Raw<_>>( ..serde_json::from_str(
db.rooms db.rooms
.room_state_get( .room_state_get(
&room_id, &room_id,
@ -206,10 +204,8 @@ pub async fn set_avatar_url_route(
) )
})? })?
.content .content
.clone(), .get(),
) )
.expect("from_value::<Raw<..>> can never fail")
.deserialize()
.map_err(|_| Error::bad_database("Database contains invalid PDU."))? .map_err(|_| Error::bad_database("Database contains invalid PDU."))?
}) })
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),

View file

@ -3,11 +3,12 @@ use std::sync::Arc;
use crate::{database::DatabaseGuard, pdu::PduBuilder, ConduitResult, Ruma}; use crate::{database::DatabaseGuard, pdu::PduBuilder, ConduitResult, Ruma};
use ruma::{ use ruma::{
api::client::r0::redact::redact_event, api::client::r0::redact::redact_event,
events::{room::redaction, EventType}, events::{room::redaction::RoomRedactionEventContent, EventType},
}; };
#[cfg(feature = "conduit_bin")] #[cfg(feature = "conduit_bin")]
use rocket::put; use rocket::put;
use serde_json::value::to_raw_value;
/// # `PUT /_matrix/client/r0/rooms/{roomId}/redact/{eventId}/{txnId}` /// # `PUT /_matrix/client/r0/rooms/{roomId}/redact/{eventId}/{txnId}`
/// ///
@ -38,7 +39,7 @@ pub async fn redact_event_route(
let event_id = db.rooms.build_and_append_pdu( let event_id = db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomRedaction, event_type: EventType::RoomRedaction,
content: serde_json::to_value(redaction::RedactionEventContent { content: to_raw_value(&RoomRedactionEventContent {
reason: body.reason.clone(), reason: body.reason.clone(),
}) })
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),

View file

@ -8,12 +8,23 @@ use ruma::{
r0::room::{self, aliases, create_room, get_room_event, upgrade_room}, r0::room::{self, aliases, create_room, get_room_event, upgrade_room},
}, },
events::{ events::{
room::{guest_access, history_visibility, join_rules, member, name, topic}, room::{
canonical_alias::RoomCanonicalAliasEventContent,
create::RoomCreateEventContent,
guest_access::{GuestAccess, RoomGuestAccessEventContent},
history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
join_rules::{JoinRule, RoomJoinRulesEventContent},
member::{MembershipState, RoomMemberEventContent},
name::RoomNameEventContent,
power_levels::RoomPowerLevelsEventContent,
tombstone::RoomTombstoneEventContent,
topic::RoomTopicEventContent,
},
EventType, EventType,
}, },
serde::Raw,
RoomAliasId, RoomId, RoomVersionId, RoomAliasId, RoomId, RoomVersionId,
}; };
use serde_json::value::to_raw_value;
use std::{cmp::max, collections::BTreeMap, convert::TryFrom, sync::Arc}; use std::{cmp::max, collections::BTreeMap, convert::TryFrom, sync::Arc};
use tracing::{info, warn}; use tracing::{info, warn};
@ -80,7 +91,7 @@ pub async fn create_room_route(
} }
})?; })?;
let mut content = ruma::events::room::create::CreateEventContent::new(sender_user.clone()); let mut content = RoomCreateEventContent::new(sender_user.clone());
content.federate = body.creation_content.federate; content.federate = body.creation_content.federate;
content.predecessor = body.creation_content.predecessor.clone(); content.predecessor = body.creation_content.predecessor.clone();
content.room_version = match body.room_version.clone() { content.room_version = match body.room_version.clone() {
@ -101,7 +112,7 @@ pub async fn create_room_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomCreate, event_type: EventType::RoomCreate,
content: serde_json::to_value(content).expect("event is valid, we just created it"), content: to_raw_value(&content).expect("event is valid, we just created it"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
redacts: None, redacts: None,
@ -116,8 +127,8 @@ pub async fn create_room_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: serde_json::to_value(member::MemberEventContent { content: to_raw_value(&RoomMemberEventContent {
membership: member::MembershipState::Join, membership: MembershipState::Join,
displayname: db.users.displayname(sender_user)?, displayname: db.users.displayname(sender_user)?,
avatar_url: db.users.avatar_url(sender_user)?, avatar_url: db.users.avatar_url(sender_user)?,
is_direct: Some(body.is_direct), is_direct: Some(body.is_direct),
@ -157,12 +168,11 @@ pub async fn create_room_route(
} }
} }
let mut power_levels_content = let mut power_levels_content = serde_json::to_value(RoomPowerLevelsEventContent {
serde_json::to_value(ruma::events::room::power_levels::PowerLevelsEventContent { users,
users, ..Default::default()
..Default::default() })
}) .expect("event is valid, we just created it");
.expect("event is valid, we just created it");
if let Some(power_level_content_override) = &body.power_level_content_override { if let Some(power_level_content_override) = &body.power_level_content_override {
let json = serde_json::from_str::<serde_json::Map<String, serde_json::Value>>( let json = serde_json::from_str::<serde_json::Map<String, serde_json::Value>>(
@ -180,7 +190,8 @@ pub async fn create_room_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomPowerLevels, event_type: EventType::RoomPowerLevels,
content: power_levels_content, content: to_raw_value(&power_levels_content)
.expect("to_raw_value always works on serde_json::Value"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
redacts: None, redacts: None,
@ -196,12 +207,10 @@ pub async fn create_room_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomCanonicalAlias, event_type: EventType::RoomCanonicalAlias,
content: serde_json::to_value( content: to_raw_value(&RoomCanonicalAliasEventContent {
ruma::events::room::canonical_alias::CanonicalAliasEventContent { alias: Some(room_alias_id.clone()),
alias: Some(room_alias_id.clone()), alt_aliases: vec![],
alt_aliases: vec![], })
},
)
.expect("We checked that alias earlier, it must be fine"), .expect("We checked that alias earlier, it must be fine"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
@ -220,17 +229,12 @@ pub async fn create_room_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomJoinRules, event_type: EventType::RoomJoinRules,
content: match preset { content: to_raw_value(&RoomJoinRulesEventContent::new(match preset {
create_room::RoomPreset::PublicChat => serde_json::to_value( create_room::RoomPreset::PublicChat => JoinRule::Public,
join_rules::JoinRulesEventContent::new(join_rules::JoinRule::Public),
)
.expect("event is valid, we just created it"),
// according to spec "invite" is the default // according to spec "invite" is the default
_ => serde_json::to_value(join_rules::JoinRulesEventContent::new( _ => JoinRule::Invite,
join_rules::JoinRule::Invite, }))
)) .expect("event is valid, we just created it"),
.expect("event is valid, we just created it"),
},
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
redacts: None, redacts: None,
@ -245,8 +249,8 @@ pub async fn create_room_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomHistoryVisibility, event_type: EventType::RoomHistoryVisibility,
content: serde_json::to_value(history_visibility::HistoryVisibilityEventContent::new( content: to_raw_value(&RoomHistoryVisibilityEventContent::new(
history_visibility::HistoryVisibility::Shared, HistoryVisibility::Shared,
)) ))
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
unsigned: None, unsigned: None,
@ -263,18 +267,11 @@ pub async fn create_room_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomGuestAccess, event_type: EventType::RoomGuestAccess,
content: match preset { content: to_raw_value(&RoomGuestAccessEventContent::new(match preset {
create_room::RoomPreset::PublicChat => { create_room::RoomPreset::PublicChat => GuestAccess::Forbidden,
serde_json::to_value(guest_access::GuestAccessEventContent::new( _ => GuestAccess::CanJoin,
guest_access::GuestAccess::Forbidden, }))
)) .expect("event is valid, we just created it"),
.expect("event is valid, we just created it")
}
_ => serde_json::to_value(guest_access::GuestAccessEventContent::new(
guest_access::GuestAccess::CanJoin,
))
.expect("event is valid, we just created it"),
},
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
redacts: None, redacts: None,
@ -306,7 +303,7 @@ pub async fn create_room_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomName, event_type: EventType::RoomName,
content: serde_json::to_value(name::NameEventContent::new(Some(name.clone()))) content: to_raw_value(&RoomNameEventContent::new(Some(name.clone())))
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
@ -323,7 +320,7 @@ pub async fn create_room_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomTopic, event_type: EventType::RoomTopic,
content: serde_json::to_value(topic::TopicEventContent { content: to_raw_value(&RoomTopicEventContent {
topic: topic.clone(), topic: topic.clone(),
}) })
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
@ -477,7 +474,7 @@ pub async fn upgrade_room_route(
let tombstone_event_id = db.rooms.build_and_append_pdu( let tombstone_event_id = db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomTombstone, event_type: EventType::RoomTombstone,
content: serde_json::to_value(ruma::events::room::tombstone::TombstoneEventContent { content: to_raw_value(&RoomTombstoneEventContent {
body: "This room has been replaced".to_string(), body: "This room has been replaced".to_string(),
replacement_room: replacement_room.clone(), replacement_room: replacement_room.clone(),
}) })
@ -505,15 +502,13 @@ pub async fn upgrade_room_route(
let state_lock = mutex_state.lock().await; let state_lock = mutex_state.lock().await;
// Get the old room federations status // Get the old room federations status
let federate = serde_json::from_value::<Raw<ruma::events::room::create::CreateEventContent>>( let federate = serde_json::from_str::<RoomCreateEventContent>(
db.rooms db.rooms
.room_state_get(&body.room_id, &EventType::RoomCreate, "")? .room_state_get(&body.room_id, &EventType::RoomCreate, "")?
.ok_or_else(|| Error::bad_database("Found room without m.room.create event."))? .ok_or_else(|| Error::bad_database("Found room without m.room.create event."))?
.content .content
.clone(), .get(),
) )
.expect("Raw::from_value always works")
.deserialize()
.map_err(|_| Error::bad_database("Invalid room event in database."))? .map_err(|_| Error::bad_database("Invalid room event in database."))?
.federate; .federate;
@ -524,8 +519,7 @@ 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
let mut create_event_content = let mut create_event_content = RoomCreateEventContent::new(sender_user.clone());
ruma::events::room::create::CreateEventContent::new(sender_user.clone());
create_event_content.federate = federate; create_event_content.federate = federate;
create_event_content.room_version = body.new_version.clone(); create_event_content.room_version = body.new_version.clone();
create_event_content.predecessor = predecessor; create_event_content.predecessor = predecessor;
@ -533,7 +527,7 @@ pub async fn upgrade_room_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomCreate, event_type: EventType::RoomCreate,
content: serde_json::to_value(create_event_content) content: to_raw_value(&create_event_content)
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),
@ -549,8 +543,8 @@ pub async fn upgrade_room_route(
db.rooms.build_and_append_pdu( db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: serde_json::to_value(member::MemberEventContent { content: to_raw_value(&RoomMemberEventContent {
membership: member::MembershipState::Join, membership: MembershipState::Join,
displayname: db.users.displayname(sender_user)?, displayname: db.users.displayname(sender_user)?,
avatar_url: db.users.avatar_url(sender_user)?, avatar_url: db.users.avatar_url(sender_user)?,
is_direct: None, is_direct: None,
@ -611,17 +605,14 @@ pub async fn upgrade_room_route(
} }
// Get the old room power levels // Get the old room power levels
let mut power_levels_event_content = let mut power_levels_event_content = serde_json::from_str::<RoomPowerLevelsEventContent>(
serde_json::from_value::<Raw<ruma::events::room::power_levels::PowerLevelsEventContent>>( db.rooms
db.rooms .room_state_get(&body.room_id, &EventType::RoomPowerLevels, "")?
.room_state_get(&body.room_id, &EventType::RoomPowerLevels, "")? .ok_or_else(|| Error::bad_database("Found room without m.room.create event."))?
.ok_or_else(|| Error::bad_database("Found room without m.room.create event."))? .content
.content .get(),
.clone(), )
) .map_err(|_| Error::bad_database("Invalid room event in database."))?;
.expect("database contains invalid PDU")
.deserialize()
.map_err(|_| Error::bad_database("Invalid room event in database."))?;
// Setting events_default and invite to the greater of 50 and users_default + 1 // Setting events_default and invite to the greater of 50 and users_default + 1
let new_level = max( let new_level = max(
@ -635,7 +626,7 @@ pub async fn upgrade_room_route(
let _ = db.rooms.build_and_append_pdu( let _ = db.rooms.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomPowerLevels, event_type: EventType::RoomPowerLevels,
content: serde_json::to_value(power_levels_event_content) content: to_raw_value(&power_levels_event_content)
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
unsigned: None, unsigned: None,
state_key: Some("".to_owned()), state_key: Some("".to_owned()),

View file

@ -60,10 +60,10 @@ pub async fn login_route(
// Validate login method // Validate login method
// TODO: Other login methods // TODO: Other login methods
let user_id = match &body.login_info { let user_id = match &body.login_info {
login::IncomingLoginInfo::Password { login::IncomingLoginInfo::Password(login::IncomingPassword {
identifier, identifier,
password, password,
} => { }) => {
let username = if let IncomingUserIdentifier::MatrixId(matrix_id) = identifier { let username = if let IncomingUserIdentifier::MatrixId(matrix_id) = identifier {
matrix_id matrix_id
} else { } else {
@ -97,7 +97,7 @@ pub async fn login_route(
user_id user_id
} }
login::IncomingLoginInfo::Token { token } => { login::IncomingLoginInfo::Token(login::IncomingToken { token }) => {
if let Some(jwt_decoding_key) = db.globals.jwt_decoding_key() { if let Some(jwt_decoding_key) = db.globals.jwt_decoding_key() {
let token = jsonwebtoken::decode::<Claims>( let token = jsonwebtoken::decode::<Claims>(
token, token,
@ -116,6 +116,12 @@ pub async fn login_route(
)); ));
} }
} }
_ => {
return Err(Error::BadRequest(
ErrorKind::Unknown,
"Unsupported login type.",
));
}
}; };
// Generate new device id if the user didn't specify one // Generate new device id if the user didn't specify one

View file

@ -10,8 +10,8 @@ use ruma::{
}, },
events::{ events::{
room::{ room::{
canonical_alias::CanonicalAliasEventContent, canonical_alias::RoomCanonicalAliasEventContent,
history_visibility::{HistoryVisibility, HistoryVisibilityEventContent}, history_visibility::{HistoryVisibility, RoomHistoryVisibilityEventContent},
}, },
AnyStateEventContent, EventType, AnyStateEventContent, EventType,
}, },
@ -112,7 +112,7 @@ pub async fn get_state_events_route(
db.rooms db.rooms
.room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")? .room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")?
.map(|event| { .map(|event| {
serde_json::from_value::<HistoryVisibilityEventContent>(event.content.clone()) serde_json::from_str::<RoomHistoryVisibilityEventContent>(event.content.get())
.map_err(|_| { .map_err(|_| {
Error::bad_database( Error::bad_database(
"Invalid room history visibility event in database.", "Invalid room history visibility event in database.",
@ -164,7 +164,7 @@ pub async fn get_state_events_for_key_route(
db.rooms db.rooms
.room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")? .room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")?
.map(|event| { .map(|event| {
serde_json::from_value::<HistoryVisibilityEventContent>(event.content.clone()) serde_json::from_str::<RoomHistoryVisibilityEventContent>(event.content.get())
.map_err(|_| { .map_err(|_| {
Error::bad_database( Error::bad_database(
"Invalid room history visibility event in database.", "Invalid room history visibility event in database.",
@ -190,7 +190,7 @@ pub async fn get_state_events_for_key_route(
))?; ))?;
Ok(get_state_events_for_key::Response { Ok(get_state_events_for_key::Response {
content: serde_json::from_value(event.content.clone()) content: serde_json::from_str(event.content.get())
.map_err(|_| Error::bad_database("Invalid event content in database"))?, .map_err(|_| Error::bad_database("Invalid event content in database"))?,
} }
.into()) .into())
@ -220,7 +220,7 @@ pub async fn get_state_events_for_empty_key_route(
db.rooms db.rooms
.room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")? .room_state_get(&body.room_id, &EventType::RoomHistoryVisibility, "")?
.map(|event| { .map(|event| {
serde_json::from_value::<HistoryVisibilityEventContent>(event.content.clone()) serde_json::from_str::<RoomHistoryVisibilityEventContent>(event.content.get())
.map_err(|_| { .map_err(|_| {
Error::bad_database( Error::bad_database(
"Invalid room history visibility event in database.", "Invalid room history visibility event in database.",
@ -246,7 +246,7 @@ pub async fn get_state_events_for_empty_key_route(
))?; ))?;
Ok(get_state_events_for_key::Response { Ok(get_state_events_for_key::Response {
content: serde_json::from_value(event.content.clone()) content: serde_json::from_str(event.content.get())
.map_err(|_| Error::bad_database("Invalid event content in database"))?, .map_err(|_| Error::bad_database("Invalid event content in database"))?,
} }
.into()) .into())
@ -265,7 +265,7 @@ async fn send_state_event_for_key_helper(
// TODO: Review this check, error if event is unparsable, use event type, allow alias if it // TODO: Review this check, error if event is unparsable, use event type, allow alias if it
// previously existed // previously existed
if let Ok(canonical_alias) = if let Ok(canonical_alias) =
serde_json::from_str::<CanonicalAliasEventContent>(json.json().get()) serde_json::from_str::<RoomCanonicalAliasEventContent>(json.json().get())
{ {
let mut aliases = canonical_alias.alt_aliases.clone(); let mut aliases = canonical_alias.alt_aliases.clone();

View file

@ -1,7 +1,10 @@
use crate::{database::DatabaseGuard, ConduitResult, Database, Error, Result, Ruma, RumaResponse}; use crate::{database::DatabaseGuard, ConduitResult, Database, Error, Result, Ruma, RumaResponse};
use ruma::{ use ruma::{
api::client::r0::{sync::sync_events, uiaa::UiaaResponse}, api::client::r0::{sync::sync_events, uiaa::UiaaResponse},
events::{room::member::MembershipState, AnySyncEphemeralRoomEvent, EventType}, events::{
room::member::{MembershipState, RoomMemberEventContent},
AnySyncEphemeralRoomEvent, EventType,
},
serde::Raw, serde::Raw,
DeviceId, RoomId, UserId, DeviceId, RoomId, UserId,
}; };
@ -287,10 +290,11 @@ async fn sync_helper(
.filter_map(|pdu| pdu.ok()) // Ignore all broken pdus .filter_map(|pdu| pdu.ok()) // Ignore all broken pdus
.filter(|(_, pdu)| pdu.kind == EventType::RoomMember) .filter(|(_, pdu)| pdu.kind == EventType::RoomMember)
.map(|(_, pdu)| { .map(|(_, pdu)| {
let content = serde_json::from_value::< let content =
ruma::events::room::member::MemberEventContent, serde_json::from_str::<RoomMemberEventContent>(pdu.content.get())
>(pdu.content.clone()) .map_err(|_| {
.map_err(|_| Error::bad_database("Invalid member event in database."))?; Error::bad_database("Invalid member event in database.")
})?;
if let Some(state_key) = &pdu.state_key { if let Some(state_key) = &pdu.state_key {
let user_id = UserId::try_from(state_key.clone()).map_err(|_| { let user_id = UserId::try_from(state_key.clone()).map_err(|_| {
@ -371,13 +375,9 @@ async fn sync_helper(
sender_user.as_str(), sender_user.as_str(),
)? )?
.and_then(|pdu| { .and_then(|pdu| {
serde_json::from_value::<Raw<ruma::events::room::member::MemberEventContent>>( serde_json::from_str::<RoomMemberEventContent>(pdu.content.get())
pdu.content.clone(), .map_err(|_| Error::bad_database("Invalid PDU in database."))
) .ok()
.expect("Raw::from_value always works")
.deserialize()
.map_err(|_| Error::bad_database("Invalid PDU in database."))
.ok()
}); });
let joined_since_last_sync = since_sender_member let joined_since_last_sync = since_sender_member
@ -432,11 +432,9 @@ async fn sync_helper(
continue; continue;
} }
let new_membership = serde_json::from_value::< let new_membership = serde_json::from_str::<RoomMemberEventContent>(
Raw<ruma::events::room::member::MemberEventContent>, state_event.content.get(),
>(state_event.content.clone()) )
.expect("Raw::from_value always works")
.deserialize()
.map_err(|_| Error::bad_database("Invalid PDU in database."))? .map_err(|_| Error::bad_database("Invalid PDU in database."))?
.membership; .membership;
@ -739,7 +737,7 @@ async fn sync_helper(
presence: sync_events::Presence { presence: sync_events::Presence {
events: presence_updates events: presence_updates
.into_iter() .into_iter()
.map(|(_, v)| Raw::from(v)) .map(|(_, v)| Raw::new(&v).expect("PresenceEvent always serializes successfully"))
.collect(), .collect(),
}, },
account_data: sync_events::GlobalAccountData { account_data: sync_events::GlobalAccountData {

View file

@ -6,16 +6,17 @@ use std::{
use crate::{pdu::PduBuilder, Database}; use crate::{pdu::PduBuilder, Database};
use rocket::futures::{channel::mpsc, stream::StreamExt}; use rocket::futures::{channel::mpsc, stream::StreamExt};
use ruma::{ use ruma::{
events::{room::message, EventType}, events::{room::message::RoomMessageEventContent, EventType},
UserId, UserId,
}; };
use serde_json::value::to_raw_value;
use tokio::sync::{MutexGuard, RwLock, RwLockReadGuard}; use tokio::sync::{MutexGuard, RwLock, RwLockReadGuard};
use tracing::warn; use tracing::warn;
pub enum AdminCommand { pub enum AdminCommand {
RegisterAppservice(serde_yaml::Value), RegisterAppservice(serde_yaml::Value),
ListAppservices, ListAppservices,
SendMessage(message::MessageEventContent), SendMessage(RoomMessageEventContent),
} }
#[derive(Clone)] #[derive(Clone)]
@ -58,7 +59,7 @@ impl Admin {
drop(guard); drop(guard);
let send_message = |message: message::MessageEventContent, let send_message = |message: RoomMessageEventContent,
guard: RwLockReadGuard<'_, Database>, guard: RwLockReadGuard<'_, Database>,
mutex_lock: &MutexGuard<'_, ()>| { mutex_lock: &MutexGuard<'_, ()>| {
guard guard
@ -66,7 +67,7 @@ impl Admin {
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMessage, event_type: EventType::RoomMessage,
content: serde_json::to_value(message) content: to_raw_value(&message)
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
unsigned: None, unsigned: None,
state_key: None, state_key: None,
@ -106,9 +107,9 @@ impl Admin {
count, count,
appservices.into_iter().filter_map(|r| r.ok()).collect::<Vec<_>>().join(", ") appservices.into_iter().filter_map(|r| r.ok()).collect::<Vec<_>>().join(", ")
); );
send_message(message::MessageEventContent::text_plain(output), guard, &state_lock); send_message(RoomMessageEventContent::text_plain(output), guard, &state_lock);
} else { } else {
send_message(message::MessageEventContent::text_plain("Failed to get appservices."), guard, &state_lock); send_message(RoomMessageEventContent::text_plain("Failed to get appservices."), guard, &state_lock);
} }
} }
AdminCommand::SendMessage(message) => { AdminCommand::SendMessage(message) => {

View file

@ -9,8 +9,10 @@ use ruma::{
}, },
IncomingResponse, OutgoingRequest, SendAccessToken, IncomingResponse, OutgoingRequest, SendAccessToken,
}, },
events::{room::power_levels::PowerLevelsEventContent, AnySyncRoomEvent, EventType}, events::{
identifiers::RoomName, room::{name::RoomNameEventContent, power_levels::RoomPowerLevelsEventContent},
AnySyncRoomEvent, EventType,
},
push::{Action, PushConditionRoomCtx, PushFormat, Ruleset, Tweak}, push::{Action, PushConditionRoomCtx, PushFormat, Ruleset, Tweak},
serde::Raw, serde::Raw,
uint, RoomId, UInt, UserId, uint, RoomId, UInt, UserId,
@ -177,11 +179,11 @@ pub async fn send_push_notice(
let mut notify = None; let mut notify = None;
let mut tweaks = Vec::new(); let mut tweaks = Vec::new();
let power_levels: PowerLevelsEventContent = db let power_levels: RoomPowerLevelsEventContent = db
.rooms .rooms
.room_state_get(&pdu.room_id, &EventType::RoomPowerLevels, "")? .room_state_get(&pdu.room_id, &EventType::RoomPowerLevels, "")?
.map(|ev| { .map(|ev| {
serde_json::from_value(ev.content.clone()) serde_json::from_str(ev.content.get())
.map_err(|_| Error::bad_database("invalid m.room.power_levels event")) .map_err(|_| Error::bad_database("invalid m.room.power_levels event"))
}) })
.transpose()? .transpose()?
@ -226,7 +228,7 @@ pub async fn send_push_notice(
pub fn get_actions<'a>( pub fn get_actions<'a>(
user: &UserId, user: &UserId,
ruleset: &'a Ruleset, ruleset: &'a Ruleset,
power_levels: &PowerLevelsEventContent, power_levels: &RoomPowerLevelsEventContent,
pdu: &Raw<AnySyncRoomEvent>, pdu: &Raw<AnySyncRoomEvent>,
room_id: &RoomId, room_id: &RoomId,
db: &Database, db: &Database,
@ -318,16 +320,18 @@ async fn send_notice(
let user_name = db.users.displayname(&event.sender)?; let user_name = db.users.displayname(&event.sender)?;
notifi.sender_display_name = user_name.as_deref(); notifi.sender_display_name = user_name.as_deref();
let room_name = db
.rooms let room_name = if let Some(room_name_pdu) =
.room_state_get(&event.room_id, &EventType::RoomName, "")? db.rooms
.map(|pdu| match pdu.content.get("name") { .room_state_get(&event.room_id, &EventType::RoomName, "")?
Some(serde_json::Value::String(s)) => { {
Some(Box::<RoomName>::try_from(&**s).expect("room name is valid")) serde_json::from_str::<RoomNameEventContent>(room_name_pdu.content.get())
} .map_err(|_| Error::bad_database("Invalid room name event in database."))?
_ => None, .name
}) } else {
.flatten(); None
};
notifi.room_name = room_name.as_deref(); notifi.room_name = room_name.as_deref();
send_request( send_request(

View file

@ -1,9 +1,11 @@
mod edus; mod edus;
pub use edus::RoomEdus; pub use edus::RoomEdus;
use member::MembershipState;
use crate::{pdu::PduBuilder, server_server, utils, Database, Error, PduEvent, Result}; use crate::{
pdu::{EventHash, PduBuilder},
server_server, utils, Database, Error, PduEvent, Result,
};
use lru_cache::LruCache; use lru_cache::LruCache;
use regex::Regex; use regex::Regex;
use ring::digest; use ring::digest;
@ -13,16 +15,22 @@ use ruma::{
events::{ events::{
ignored_user_list, push_rules, ignored_user_list, push_rules,
room::{ room::{
create::CreateEventContent, member, message, power_levels::PowerLevelsEventContent, create::RoomCreateEventContent,
member::{MembershipState, RoomMemberEventContent},
message::RoomMessageEventContent,
power_levels::RoomPowerLevelsEventContent,
}, },
AnyStrippedStateEvent, AnySyncStateEvent, EventType, AnyStrippedStateEvent, AnySyncStateEvent, EventType,
}, },
push::{self, Action, Tweak}, push::{Action, Ruleset, Tweak},
serde::{CanonicalJsonObject, CanonicalJsonValue, Raw}, serde::{CanonicalJsonObject, CanonicalJsonValue, Raw},
state_res::{self, RoomVersion, StateMap}, state_res::{self, RoomVersion, StateMap},
uint, EventId, RoomAliasId, RoomId, RoomVersionId, ServerName, UserId, uint, EventId, RoomAliasId, RoomId, RoomVersionId, ServerName, UserId,
}; };
use serde::Deserialize;
use serde_json::value::to_raw_value;
use std::{ use std::{
borrow::Cow,
collections::{BTreeMap, HashMap, HashSet}, collections::{BTreeMap, HashMap, HashSet},
convert::{TryFrom, TryInto}, convert::{TryFrom, TryInto},
mem::size_of, mem::size_of,
@ -243,7 +251,7 @@ impl Rooms {
kind: &EventType, kind: &EventType,
sender: &UserId, sender: &UserId,
state_key: Option<&str>, state_key: Option<&str>,
content: &serde_json::Value, content: &serde_json::value::RawValue,
) -> Result<StateMap<Arc<PduEvent>>> { ) -> Result<StateMap<Arc<PduEvent>>> {
let shortstatehash = let shortstatehash =
if let Some(current_shortstatehash) = self.current_shortstatehash(room_id)? { if let Some(current_shortstatehash) = self.current_shortstatehash(room_id)? {
@ -252,7 +260,8 @@ impl Rooms {
return Ok(HashMap::new()); return Ok(HashMap::new());
}; };
let auth_events = state_res::auth_types_for_event(kind, sender, state_key, content); let auth_events = state_res::auth_types_for_event(kind, sender, state_key, content)
.expect("content is a valid JSON object");
let mut sauthevents = auth_events let mut sauthevents = auth_events
.into_iter() .into_iter()
@ -391,37 +400,43 @@ impl Rooms {
.ok() .ok()
.map(|(_, id)| id) .map(|(_, id)| id)
}) { }) {
if let Some(pdu) = self.get_pdu_json(&event_id)? { let pdu = match self.get_pdu_json(&event_id)? {
if pdu.get("type").and_then(|val| val.as_str()) == Some("m.room.member") { Some(pdu) => pdu,
if let Ok(pdu) = serde_json::from_value::<PduEvent>( None => continue,
serde_json::to_value(&pdu).expect("CanonicalJsonObj is a valid JsonValue"), };
) {
if let Some(membership) = if pdu.get("type").and_then(|val| val.as_str()) != Some("m.room.member") {
pdu.content.get("membership").and_then(|membership| { continue;
serde_json::from_value::<member::MembershipState>(
membership.clone(),
)
.ok()
})
{
if let Some(state_key) = pdu
.state_key
.and_then(|state_key| UserId::try_from(state_key).ok())
{
self.update_membership(
room_id,
&state_key,
membership,
&pdu.sender,
None,
db,
false,
)?;
}
}
}
}
} }
let pdu = match serde_json::from_str::<PduEvent>(
&serde_json::to_string(&pdu).expect("CanonicalJsonObj can be serialized to JSON"),
) {
Ok(pdu) => pdu,
Err(_) => continue,
};
#[derive(Deserialize)]
struct ExtractMembership {
membership: MembershipState,
}
let membership = match serde_json::from_str::<ExtractMembership>(pdu.content.get()) {
Ok(e) => e.membership,
Err(_) => continue,
};
let state_key = match pdu.state_key {
Some(k) => k,
None => continue,
};
let user_id = match UserId::try_from(state_key) {
Ok(id) => id,
Err(_) => continue,
};
self.update_membership(room_id, &user_id, membership, &pdu.sender, None, db, false)?;
} }
self.update_joined_count(room_id, db)?; self.update_joined_count(room_id, db)?;
@ -1325,11 +1340,11 @@ impl Rooms {
drop(insert_lock); drop(insert_lock);
// See if the event matches any known pushers // See if the event matches any known pushers
let power_levels: PowerLevelsEventContent = db let power_levels: RoomPowerLevelsEventContent = db
.rooms .rooms
.room_state_get(&pdu.room_id, &EventType::RoomPowerLevels, "")? .room_state_get(&pdu.room_id, &EventType::RoomPowerLevels, "")?
.map(|ev| { .map(|ev| {
serde_json::from_value(ev.content.clone()) serde_json::from_str(ev.content.get())
.map_err(|_| Error::bad_database("invalid m.room.power_levels event")) .map_err(|_| Error::bad_database("invalid m.room.power_levels event"))
}) })
.transpose()? .transpose()?
@ -1350,7 +1365,7 @@ impl Rooms {
.account_data .account_data
.get::<push_rules::PushRulesEvent>(None, user, EventType::PushRules)? .get::<push_rules::PushRulesEvent>(None, user, EventType::PushRules)?
.map(|ev| ev.content.global) .map(|ev| ev.content.global)
.unwrap_or_else(|| push::Ruleset::server_default(user)); .unwrap_or_else(|| Ruleset::server_default(user));
let mut highlight = false; let mut highlight = false;
let mut notify = false; let mut notify = false;
@ -1404,30 +1419,21 @@ impl Rooms {
} }
EventType::RoomMember => { EventType::RoomMember => {
if let Some(state_key) = &pdu.state_key { if let Some(state_key) = &pdu.state_key {
#[derive(Deserialize)]
struct ExtractMembership {
membership: MembershipState,
}
// if the state_key fails // if the state_key fails
let target_user_id = UserId::try_from(state_key.clone()) let target_user_id = UserId::try_from(state_key.clone())
.expect("This state_key was previously validated"); .expect("This state_key was previously validated");
let membership = serde_json::from_value::<member::MembershipState>( let content = serde_json::from_str::<ExtractMembership>(pdu.content.get())
pdu.content .map_err(|_| Error::bad_database("Invalid content in pdu."))?;
.get("membership")
.ok_or(Error::BadRequest(
ErrorKind::InvalidParam,
"Invalid member event content",
))?
.clone(),
)
.map_err(|_| {
Error::BadRequest(
ErrorKind::InvalidParam,
"Invalid membership state content.",
)
})?;
let invite_state = match membership { let invite_state = match content.membership {
member::MembershipState::Invite => { MembershipState::Invite => {
let state = self.calculate_invite_state(pdu)?; let state = self.calculate_invite_state(pdu)?;
Some(state) Some(state)
} }
_ => None, _ => None,
@ -1438,7 +1444,7 @@ impl Rooms {
self.update_membership( self.update_membership(
&pdu.room_id, &pdu.room_id,
&target_user_id, &target_user_id,
membership, content.membership,
&pdu.sender, &pdu.sender,
invite_state, invite_state,
db, db,
@ -1447,7 +1453,16 @@ impl Rooms {
} }
} }
EventType::RoomMessage => { EventType::RoomMessage => {
if let Some(body) = pdu.content.get("body").and_then(|b| b.as_str()) { #[derive(Deserialize)]
struct ExtractBody<'a> {
#[serde(borrow)]
body: Option<Cow<'a, str>>,
}
let content = serde_json::from_str::<ExtractBody<'_>>(pdu.content.get())
.map_err(|_| Error::bad_database("Invalid content in pdu."))?;
if let Some(body) = content.body {
let mut batch = body let mut batch = body
.split_terminator(|c: char| !c.is_alphanumeric()) .split_terminator(|c: char| !c.is_alphanumeric())
.filter(|s| !s.is_empty()) .filter(|s| !s.is_empty())
@ -1498,18 +1513,16 @@ impl Rooms {
} }
Err(e) => { Err(e) => {
db.admin.send(AdminCommand::SendMessage( db.admin.send(AdminCommand::SendMessage(
message::MessageEventContent::text_plain( RoomMessageEventContent::text_plain(format!(
format!( "Could not parse appservice config: {}",
"Could not parse appservice config: {}", e
e )),
),
),
)); ));
} }
} }
} else { } else {
db.admin.send(AdminCommand::SendMessage( db.admin.send(AdminCommand::SendMessage(
message::MessageEventContent::text_plain( RoomMessageEventContent::text_plain(
"Expected code block in command body.", "Expected code block in command body.",
), ),
)); ));
@ -1542,12 +1555,10 @@ impl Rooms {
.count(); .count();
let elapsed = start.elapsed(); let elapsed = start.elapsed();
db.admin.send(AdminCommand::SendMessage( db.admin.send(AdminCommand::SendMessage(
message::MessageEventContent::text_plain( RoomMessageEventContent::text_plain(format!(
format!( "Loaded auth chain with length {} in {:?}",
"Loaded auth chain with length {} in {:?}", count, elapsed
count, elapsed )),
),
),
)); ));
} }
} }
@ -1580,14 +1591,17 @@ impl Rooms {
) { ) {
Ok(pdu) => { Ok(pdu) => {
db.admin.send(AdminCommand::SendMessage( db.admin.send(AdminCommand::SendMessage(
message::MessageEventContent::text_plain( RoomMessageEventContent::text_plain(
format!("EventId: {:?}\n{:#?}", event_id, pdu), format!(
"EventId: {:?}\n{:#?}",
event_id, pdu
),
), ),
)); ));
} }
Err(e) => { Err(e) => {
db.admin.send(AdminCommand::SendMessage( db.admin.send(AdminCommand::SendMessage(
message::MessageEventContent::text_plain( RoomMessageEventContent::text_plain(
format!("EventId: {:?}\nCould not parse event: {}", event_id, e), format!("EventId: {:?}\nCould not parse event: {}", event_id, e),
), ),
)); ));
@ -1596,18 +1610,16 @@ impl Rooms {
} }
Err(e) => { Err(e) => {
db.admin.send(AdminCommand::SendMessage( db.admin.send(AdminCommand::SendMessage(
message::MessageEventContent::text_plain( RoomMessageEventContent::text_plain(format!(
format!( "Invalid json in command body: {}",
"Invalid json in command body: {}", e
e )),
),
),
)); ));
} }
} }
} else { } else {
db.admin.send(AdminCommand::SendMessage( db.admin.send(AdminCommand::SendMessage(
message::MessageEventContent::text_plain( RoomMessageEventContent::text_plain(
"Expected code block in command body.", "Expected code block in command body.",
), ),
)); ));
@ -1629,7 +1641,7 @@ impl Rooms {
serde_json::to_string_pretty(&json) serde_json::to_string_pretty(&json)
.expect("canonical json is valid json"); .expect("canonical json is valid json");
db.admin.send(AdminCommand::SendMessage( db.admin.send(AdminCommand::SendMessage(
message::MessageEventContent::text_html( RoomMessageEventContent::text_html(
format!("{}\n```json\n{}\n```", format!("{}\n```json\n{}\n```",
if outlier { if outlier {
"PDU is outlier" "PDU is outlier"
@ -1643,7 +1655,7 @@ impl Rooms {
} }
None => { None => {
db.admin.send(AdminCommand::SendMessage( db.admin.send(AdminCommand::SendMessage(
message::MessageEventContent::text_plain( RoomMessageEventContent::text_plain(
"PDU not found.", "PDU not found.",
), ),
)); ));
@ -1651,14 +1663,14 @@ impl Rooms {
} }
} else { } else {
db.admin.send(AdminCommand::SendMessage( db.admin.send(AdminCommand::SendMessage(
message::MessageEventContent::text_plain( RoomMessageEventContent::text_plain(
"Event ID could not be parsed.", "Event ID could not be parsed.",
), ),
)); ));
} }
} else { } else {
db.admin.send(AdminCommand::SendMessage( db.admin.send(AdminCommand::SendMessage(
message::MessageEventContent::text_plain( RoomMessageEventContent::text_plain(
"Usage: get_pdu <eventid>", "Usage: get_pdu <eventid>",
), ),
)); ));
@ -1666,7 +1678,7 @@ impl Rooms {
} }
_ => { _ => {
db.admin.send(AdminCommand::SendMessage( db.admin.send(AdminCommand::SendMessage(
message::MessageEventContent::text_plain(format!( RoomMessageEventContent::text_plain(format!(
"Unrecognized command: {}", "Unrecognized command: {}",
command command
)), )),
@ -1958,16 +1970,13 @@ impl Rooms {
let create_event = self.room_state_get(room_id, &EventType::RoomCreate, "")?; let create_event = self.room_state_get(room_id, &EventType::RoomCreate, "")?;
let create_event_content = create_event let create_event_content: Option<RoomCreateEventContent> = create_event
.as_ref() .as_ref()
.map(|create_event| { .map(|create_event| {
serde_json::from_value::<Raw<CreateEventContent>>(create_event.content.clone()) serde_json::from_str(create_event.content.get()).map_err(|e| {
.expect("Raw::from_value always works.") warn!("Invalid create event: {}", e);
.deserialize() Error::bad_database("Invalid create event in db.")
.map_err(|e| { })
warn!("Invalid create event: {}", e);
Error::bad_database("Invalid create event in db.")
})
}) })
.transpose()?; .transpose()?;
@ -2000,7 +2009,10 @@ impl Rooms {
let mut unsigned = unsigned.unwrap_or_default(); let mut unsigned = unsigned.unwrap_or_default();
if let Some(state_key) = &state_key { if let Some(state_key) = &state_key {
if let Some(prev_pdu) = self.room_state_get(room_id, &event_type, state_key)? { if let Some(prev_pdu) = self.room_state_get(room_id, &event_type, state_key)? {
unsigned.insert("prev_content".to_owned(), prev_pdu.content.clone()); unsigned.insert(
"prev_content".to_owned(),
serde_json::from_str(prev_pdu.content.get()).expect("string is valid json"),
);
unsigned.insert( unsigned.insert(
"prev_sender".to_owned(), "prev_sender".to_owned(),
serde_json::to_value(&prev_pdu.sender).expect("UserId::to_value always works"), serde_json::to_value(&prev_pdu.sender).expect("UserId::to_value always works"),
@ -2025,11 +2037,15 @@ impl Rooms {
.map(|(_, pdu)| pdu.event_id.clone()) .map(|(_, pdu)| pdu.event_id.clone())
.collect(), .collect(),
redacts, redacts,
unsigned, unsigned: if unsigned.is_empty() {
hashes: ruma::events::pdu::EventHash { None
} else {
Some(to_raw_value(&unsigned).expect("to_raw_value always works"))
},
hashes: EventHash {
sha256: "aaa".to_owned(), sha256: "aaa".to_owned(),
}, },
signatures: BTreeMap::new(), signatures: None,
}; };
let auth_check = state_res::auth_check( let auth_check = state_res::auth_check(
@ -2205,7 +2221,7 @@ impl Rooms {
let mut pdu = serde_json::from_slice::<PduEvent>(&v) let mut pdu = serde_json::from_slice::<PduEvent>(&v)
.map_err(|_| Error::bad_database("PDU in db is invalid."))?; .map_err(|_| Error::bad_database("PDU in db is invalid."))?;
if pdu.sender != user_id { if pdu.sender != user_id {
pdu.unsigned.remove("transaction_id"); pdu.remove_transaction_id()?;
} }
Ok((pdu_id, pdu)) Ok((pdu_id, pdu))
})) }))
@ -2242,7 +2258,7 @@ impl Rooms {
let mut pdu = serde_json::from_slice::<PduEvent>(&v) let mut pdu = serde_json::from_slice::<PduEvent>(&v)
.map_err(|_| Error::bad_database("PDU in db is invalid."))?; .map_err(|_| Error::bad_database("PDU in db is invalid."))?;
if pdu.sender != user_id { if pdu.sender != user_id {
pdu.unsigned.remove("transaction_id"); pdu.remove_transaction_id()?;
} }
Ok((pdu_id, pdu)) Ok((pdu_id, pdu))
})) }))
@ -2279,7 +2295,7 @@ impl Rooms {
let mut pdu = serde_json::from_slice::<PduEvent>(&v) let mut pdu = serde_json::from_slice::<PduEvent>(&v)
.map_err(|_| Error::bad_database("PDU in db is invalid."))?; .map_err(|_| Error::bad_database("PDU in db is invalid."))?;
if pdu.sender != user_id { if pdu.sender != user_id {
pdu.unsigned.remove("transaction_id"); pdu.remove_transaction_id()?;
} }
Ok((pdu_id, pdu)) Ok((pdu_id, pdu))
})) }))
@ -2309,7 +2325,7 @@ impl Rooms {
&self, &self,
room_id: &RoomId, room_id: &RoomId,
user_id: &UserId, user_id: &UserId,
membership: member::MembershipState, membership: MembershipState,
sender: &UserId, sender: &UserId,
last_state: Option<Vec<Raw<AnyStrippedStateEvent>>>, last_state: Option<Vec<Raw<AnyStrippedStateEvent>>>,
db: &Database, db: &Database,
@ -2338,7 +2354,7 @@ impl Rooms {
roomuser_id.extend_from_slice(user_id.as_bytes()); roomuser_id.extend_from_slice(user_id.as_bytes());
match &membership { match &membership {
member::MembershipState::Join => { MembershipState::Join => {
// Check if the user never joined this room // Check if the user never joined this room
if !self.once_joined(user_id, room_id)? { if !self.once_joined(user_id, room_id)? {
// Add the user ID to the join list then // Add the user ID to the join list then
@ -2348,12 +2364,8 @@ impl Rooms {
if let Some(predecessor) = self if let Some(predecessor) = self
.room_state_get(room_id, &EventType::RoomCreate, "")? .room_state_get(room_id, &EventType::RoomCreate, "")?
.and_then(|create| { .and_then(|create| {
serde_json::from_value::< serde_json::from_str::<RoomCreateEventContent>(create.content.get())
Raw<ruma::events::room::create::CreateEventContent>, .ok()
>(create.content.clone())
.expect("Raw::from_value always works")
.deserialize()
.ok()
}) })
.and_then(|content| content.predecessor) .and_then(|content| content.predecessor)
{ {
@ -2442,7 +2454,7 @@ impl Rooms {
self.userroomid_leftstate.remove(&userroom_id)?; self.userroomid_leftstate.remove(&userroom_id)?;
self.roomuserid_leftcount.remove(&roomuser_id)?; self.roomuserid_leftcount.remove(&roomuser_id)?;
} }
member::MembershipState::Invite => { MembershipState::Invite => {
// We want to know if the sender is ignored by the receiver // We want to know if the sender is ignored by the receiver
let is_ignored = db let is_ignored = db
.account_data .account_data
@ -2475,7 +2487,7 @@ impl Rooms {
self.userroomid_leftstate.remove(&userroom_id)?; self.userroomid_leftstate.remove(&userroom_id)?;
self.roomuserid_leftcount.remove(&roomuser_id)?; self.roomuserid_leftcount.remove(&roomuser_id)?;
} }
member::MembershipState::Leave | member::MembershipState::Ban => { MembershipState::Leave | MembershipState::Ban => {
if update_joined_count if update_joined_count
&& self && self
.room_members(room_id) .room_members(room_id)
@ -2700,26 +2712,23 @@ impl Rooms {
); );
let state_lock = mutex_state.lock().await; let state_lock = mutex_state.lock().await;
let mut event = serde_json::from_value::<Raw<member::MemberEventContent>>( let mut event = serde_json::from_str::<RoomMemberEventContent>(
self.room_state_get(room_id, &EventType::RoomMember, &user_id.to_string())? self.room_state_get(room_id, &EventType::RoomMember, &user_id.to_string())?
.ok_or(Error::BadRequest( .ok_or(Error::BadRequest(
ErrorKind::BadState, ErrorKind::BadState,
"Cannot leave a room you are not a member of.", "Cannot leave a room you are not a member of.",
))? ))?
.content .content
.clone(), .get(),
) )
.expect("from_value::<Raw<..>> can never fail")
.deserialize()
.map_err(|_| Error::bad_database("Invalid member event in database."))?; .map_err(|_| Error::bad_database("Invalid member event in database."))?;
event.membership = member::MembershipState::Leave; event.membership = MembershipState::Leave;
self.build_and_append_pdu( self.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: EventType::RoomMember, event_type: EventType::RoomMember,
content: serde_json::to_value(event) content: to_raw_value(&event).expect("event is valid, we just created it"),
.expect("event is valid, we just created it"),
unsigned: None, unsigned: None,
state_key: Some(user_id.to_string()), state_key: Some(user_id.to_string()),
redacts: None, redacts: None,
@ -2793,10 +2802,9 @@ impl Rooms {
}; };
let mut leave_event_stub = let mut leave_event_stub =
serde_json::from_str::<CanonicalJsonObject>(make_leave_response.event.json().get()) serde_json::from_str::<CanonicalJsonObject>(make_leave_response.event.get()).map_err(
.map_err(|_| { |_| Error::BadServerResponse("Invalid make_leave event json received from server."),
Error::BadServerResponse("Invalid make_leave event json received from server.") )?;
})?;
// TODO: Is origin needed? // TODO: Is origin needed?
leave_event_stub.insert( leave_event_stub.insert(
@ -2847,7 +2855,7 @@ impl Rooms {
federation::membership::create_leave_event::v2::Request { federation::membership::create_leave_event::v2::Request {
room_id, room_id,
event_id: &event_id, event_id: &event_id,
pdu: PduEvent::convert_to_outgoing_federation_event(leave_event.clone()), pdu: &PduEvent::convert_to_outgoing_federation_event(leave_event.clone()),
}, },
) )
.await?; .await?;

View file

@ -398,7 +398,7 @@ impl Sending {
let edu = Edu::DeviceListUpdate(DeviceListUpdateContent { let edu = Edu::DeviceListUpdate(DeviceListUpdateContent {
user_id, user_id,
device_id: device_id!("dummy"), device_id: device_id!("dummy"),
device_display_name: "Dummy".to_owned(), device_display_name: Some("Dummy".to_owned()),
stream_id: uint!(1), stream_id: uint!(1),
prev_id: Vec::new(), prev_id: Vec::new(),
deleted: None, deleted: None,
@ -573,8 +573,14 @@ impl Sending {
for pdu in pdus { for pdu in pdus {
// Redacted events are not notification targets (we don't send push for them) // Redacted events are not notification targets (we don't send push for them)
if pdu.unsigned.get("redacted_because").is_some() { if let Some(unsigned) = &pdu.unsigned {
continue; if let Ok(unsigned) =
serde_json::from_str::<serde_json::Value>(unsigned.get())
{
if unsigned.get("redacted_because").is_some() {
continue;
}
}
} }
let userid = let userid =

View file

@ -5,7 +5,8 @@ use ruma::{
api::client::{ api::client::{
error::ErrorKind, error::ErrorKind,
r0::uiaa::{ r0::uiaa::{
IncomingAuthData, IncomingPassword, IncomingUserIdentifier::MatrixId, UiaaInfo, AuthType, IncomingAuthData, IncomingPassword, IncomingUserIdentifier::MatrixId,
UiaaInfo,
}, },
}, },
signatures::CanonicalJsonValue, signatures::CanonicalJsonValue,
@ -99,10 +100,10 @@ impl Uiaa {
} }
// Password was correct! Let's add it to `completed` // Password was correct! Let's add it to `completed`
uiaainfo.completed.push("m.login.password".to_owned()); uiaainfo.completed.push(AuthType::Password);
} }
IncomingAuthData::Dummy(_) => { IncomingAuthData::Dummy(_) => {
uiaainfo.completed.push("m.login.dummy".to_owned()); uiaainfo.completed.push(AuthType::Dummy);
} }
k => error!("type not supported: {:?}", k), k => error!("type not supported: {:?}", k),
} }

View file

@ -1,19 +1,28 @@
use crate::Error; use crate::Error;
use ruma::{ use ruma::{
events::{ events::{
pdu::EventHash, room::member::MemberEventContent, AnyEphemeralRoomEvent, room::member::RoomMemberEventContent, AnyEphemeralRoomEvent, AnyInitialStateEvent,
AnyInitialStateEvent, AnyRoomEvent, AnyStateEvent, AnyStrippedStateEvent, AnySyncRoomEvent, AnyRoomEvent, AnyStateEvent, AnyStrippedStateEvent, AnySyncRoomEvent, AnySyncStateEvent,
AnySyncStateEvent, EventType, StateEvent, EventType, StateEvent,
}, },
serde::{CanonicalJsonObject, CanonicalJsonValue, Raw}, serde::{CanonicalJsonObject, CanonicalJsonValue, Raw},
state_res, EventId, MilliSecondsSinceUnixEpoch, RoomId, RoomVersionId, ServerName, state_res, EventId, MilliSecondsSinceUnixEpoch, RoomId, RoomVersionId, UInt, UserId,
ServerSigningKeyId, UInt, UserId,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::json; use serde_json::{
json,
value::{to_raw_value, RawValue as RawJsonValue},
};
use std::{cmp::Ordering, collections::BTreeMap, convert::TryFrom}; use std::{cmp::Ordering, collections::BTreeMap, convert::TryFrom};
use tracing::warn; use tracing::warn;
/// Content hashes of a PDU.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct EventHash {
/// The SHA-256 hash.
pub sha256: String,
}
#[derive(Clone, Deserialize, Serialize, Debug)] #[derive(Clone, Deserialize, Serialize, Debug)]
pub struct PduEvent { pub struct PduEvent {
pub event_id: EventId, pub event_id: EventId,
@ -22,7 +31,7 @@ pub struct PduEvent {
pub origin_server_ts: UInt, pub origin_server_ts: UInt,
#[serde(rename = "type")] #[serde(rename = "type")]
pub kind: EventType, pub kind: EventType,
pub content: serde_json::Value, pub content: Box<RawJsonValue>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub state_key: Option<String>, pub state_key: Option<String>,
pub prev_events: Vec<EventId>, pub prev_events: Vec<EventId>,
@ -30,16 +39,17 @@ pub struct PduEvent {
pub auth_events: Vec<EventId>, pub auth_events: Vec<EventId>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub redacts: Option<EventId>, pub redacts: Option<EventId>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")] #[serde(default, skip_serializing_if = "Option::is_none")]
pub unsigned: BTreeMap<String, serde_json::Value>, pub unsigned: Option<Box<RawJsonValue>>,
pub hashes: EventHash, pub hashes: EventHash,
pub signatures: BTreeMap<Box<ServerName>, BTreeMap<ServerSigningKeyId, String>>, #[serde(default, skip_serializing_if = "Option::is_none")]
pub signatures: Option<Box<RawJsonValue>>, // BTreeMap<Box<ServerName>, BTreeMap<ServerSigningKeyId, String>>
} }
impl PduEvent { impl PduEvent {
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub fn redact(&mut self, reason: &PduEvent) -> crate::Result<()> { pub fn redact(&mut self, reason: &PduEvent) -> crate::Result<()> {
self.unsigned.clear(); self.unsigned = None;
let allowed: &[&str] = match self.kind { let allowed: &[&str] = match self.kind {
EventType::RoomMember => &["membership"], EventType::RoomMember => &["membership"],
@ -59,10 +69,9 @@ impl PduEvent {
_ => &[], _ => &[],
}; };
let old_content = self let mut old_content =
.content serde_json::from_str::<BTreeMap<String, serde_json::Value>>(self.content.get())
.as_object_mut() .map_err(|_| Error::bad_database("PDU in db has invalid content."))?;
.ok_or_else(|| Error::bad_database("PDU in db has invalid content."))?;
let mut new_content = serde_json::Map::new(); let mut new_content = serde_json::Map::new();
@ -72,12 +81,23 @@ impl PduEvent {
} }
} }
self.unsigned.insert( self.unsigned = Some(to_raw_value(&json!({
"redacted_because".to_owned(), "redacted_because": serde_json::to_value(reason).expect("to_value(PduEvent) always works")
serde_json::to_value(reason).expect("to_value(PduEvent) always works"), })).expect("to string always works"));
);
self.content = new_content.into(); self.content = to_raw_value(&new_content).expect("to string always works");
Ok(())
}
pub fn remove_transaction_id(&mut self) -> crate::Result<()> {
if let Some(unsigned) = &self.unsigned {
let mut unsigned =
serde_json::from_str::<BTreeMap<String, Box<RawJsonValue>>>(unsigned.get())
.map_err(|_| Error::bad_database("Invalid unsigned in pdu event"))?;
unsigned.remove("transaction_id");
self.unsigned = Some(to_raw_value(&unsigned).expect("unsigned is valid"));
}
Ok(()) Ok(())
} }
@ -192,7 +212,7 @@ impl PduEvent {
} }
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub fn to_member_event(&self) -> Raw<StateEvent<MemberEventContent>> { pub fn to_member_event(&self) -> Raw<StateEvent<RoomMemberEventContent>> {
let json = json!({ let json = json!({
"content": self.content, "content": self.content,
"type": self.kind, "type": self.kind,
@ -212,7 +232,7 @@ impl PduEvent {
#[tracing::instrument] #[tracing::instrument]
pub fn convert_to_outgoing_federation_event( pub fn convert_to_outgoing_federation_event(
mut pdu_json: CanonicalJsonObject, mut pdu_json: CanonicalJsonObject,
) -> Raw<ruma::events::pdu::Pdu> { ) -> Box<RawJsonValue> {
if let Some(unsigned) = pdu_json if let Some(unsigned) = pdu_json
.get_mut("unsigned") .get_mut("unsigned")
.and_then(|val| val.as_object_mut()) .and_then(|val| val.as_object_mut())
@ -229,10 +249,7 @@ impl PduEvent {
// ) // )
// .expect("Raw::from_value always works") // .expect("Raw::from_value always works")
serde_json::from_value::<Raw<_>>( to_raw_value(&pdu_json).expect("CanonicalJson is valid serde_json::Value")
serde_json::to_value(pdu_json).expect("CanonicalJson is valid serde_json::Value"),
)
.expect("Raw::from_value always works")
} }
pub fn from_id_val( pub fn from_id_val(
@ -265,7 +282,7 @@ impl state_res::Event for PduEvent {
&self.kind &self.kind
} }
fn content(&self) -> &serde_json::Value { fn content(&self) -> &RawJsonValue {
&self.content &self.content
} }
@ -281,10 +298,6 @@ impl state_res::Event for PduEvent {
Box::new(self.prev_events.iter()) Box::new(self.prev_events.iter())
} }
fn depth(&self) -> &UInt {
&self.depth
}
fn auth_events(&self) -> Box<dyn DoubleEndedIterator<Item = &EventId> + '_> { fn auth_events(&self) -> Box<dyn DoubleEndedIterator<Item = &EventId> + '_> {
Box::new(self.auth_events.iter()) Box::new(self.auth_events.iter())
} }
@ -292,18 +305,6 @@ impl state_res::Event for PduEvent {
fn redacts(&self) -> Option<&EventId> { fn redacts(&self) -> Option<&EventId> {
self.redacts.as_ref() self.redacts.as_ref()
} }
fn hashes(&self) -> &EventHash {
&self.hashes
}
fn signatures(&self) -> BTreeMap<Box<ServerName>, BTreeMap<ruma::ServerSigningKeyId, String>> {
self.signatures.clone()
}
fn unsigned(&self) -> &BTreeMap<String, serde_json::Value> {
&self.unsigned
}
} }
// These impl's allow us to dedup state snapshots when resolving state // These impl's allow us to dedup state snapshots when resolving state
@ -329,9 +330,9 @@ impl Ord for PduEvent {
/// ///
/// Returns a tuple of the new `EventId` and the PDU as a `BTreeMap<String, CanonicalJsonValue>`. /// Returns a tuple of the new `EventId` and the PDU as a `BTreeMap<String, CanonicalJsonValue>`.
pub(crate) fn gen_event_id_canonical_json( pub(crate) fn gen_event_id_canonical_json(
pdu: &Raw<ruma::events::pdu::Pdu>, pdu: &RawJsonValue,
) -> crate::Result<(EventId, CanonicalJsonObject)> { ) -> crate::Result<(EventId, CanonicalJsonObject)> {
let value = serde_json::from_str(pdu.json().get()).map_err(|e| { let value = serde_json::from_str(pdu.get()).map_err(|e| {
warn!("Error parsing incoming event {:?}: {:?}", pdu, e); warn!("Error parsing incoming event {:?}: {:?}", pdu, e);
Error::BadServerResponse("Invalid PDU in server response") Error::BadServerResponse("Invalid PDU in server response")
})?; })?;
@ -352,7 +353,7 @@ pub(crate) fn gen_event_id_canonical_json(
pub struct PduBuilder { pub struct PduBuilder {
#[serde(rename = "type")] #[serde(rename = "type")]
pub event_type: EventType, pub event_type: EventType,
pub content: serde_json::Value, pub content: Box<RawJsonValue>,
pub unsigned: Option<BTreeMap<String, serde_json::Value>>, pub unsigned: Option<BTreeMap<String, serde_json::Value>>,
pub state_key: Option<String>, pub state_key: Option<String>,
pub redacts: Option<EventId>, pub redacts: Option<EventId>,
@ -363,7 +364,7 @@ impl From<AnyInitialStateEvent> for PduBuilder {
fn from(event: AnyInitialStateEvent) -> Self { fn from(event: AnyInitialStateEvent) -> Self {
Self { Self {
event_type: EventType::from(event.event_type()), event_type: EventType::from(event.event_type()),
content: serde_json::value::to_value(event.content()) content: to_raw_value(&event.content())
.expect("AnyStateEventContent came from JSON and can thus turn back into JSON."), .expect("AnyStateEventContent came from JSON and can thus turn back into JSON."),
unsigned: None, unsigned: None,
state_key: Some(event.state_key().to_owned()), state_key: Some(event.state_key().to_owned()),

View file

@ -1,6 +1,7 @@
use crate::{ use crate::{
client_server::{self, claim_keys_helper, get_keys_helper}, client_server::{self, claim_keys_helper, get_keys_helper},
database::{rooms::CompressedStateEvent, DatabaseGuard}, database::{rooms::CompressedStateEvent, DatabaseGuard},
pdu::EventHash,
utils, ConduitResult, Database, Error, PduEvent, Result, Ruma, utils, ConduitResult, Database, Error, PduEvent, Result, Ruma,
}; };
use get_profile_information::v1::ProfileField; use get_profile_information::v1::ProfileField;
@ -39,22 +40,22 @@ use ruma::{
}, },
directory::{IncomingFilter, IncomingRoomNetwork}, directory::{IncomingFilter, IncomingRoomNetwork},
events::{ events::{
pdu::Pdu,
receipt::{ReceiptEvent, ReceiptEventContent}, receipt::{ReceiptEvent, ReceiptEventContent},
room::{ room::{
create::CreateEventContent, create::RoomCreateEventContent,
member::{MemberEventContent, MembershipState}, member::{MembershipState, RoomMemberEventContent},
}, },
AnyEphemeralRoomEvent, EventType, AnyEphemeralRoomEvent, EventType,
}, },
int,
receipt::ReceiptType, receipt::ReceiptType,
serde::Raw,
signatures::{CanonicalJsonObject, CanonicalJsonValue}, signatures::{CanonicalJsonObject, CanonicalJsonValue},
state_res::{self, RoomVersion, StateMap}, state_res::{self, RoomVersion, StateMap},
to_device::DeviceIdOrAllDevices, to_device::DeviceIdOrAllDevices,
uint, EventId, MilliSecondsSinceUnixEpoch, RoomId, RoomVersionId, ServerName, uint, EventId, MilliSecondsSinceUnixEpoch, RoomId, RoomVersionId, ServerName,
ServerSigningKeyId, ServerSigningKeyId,
}; };
use serde_json::value::{to_raw_value, RawValue as RawJsonValue};
use std::{ use std::{
collections::{btree_map, hash_map, BTreeMap, BTreeSet, HashMap, HashSet}, collections::{btree_map, hash_map, BTreeMap, BTreeSet, HashMap, HashSet},
convert::{TryFrom, TryInto}, convert::{TryFrom, TryInto},
@ -1071,7 +1072,7 @@ pub(crate) async fn handle_incoming_pdu<'a>(
// and lexically by event_id. // and lexically by event_id.
println!("{}", event_id); println!("{}", event_id);
Ok(( Ok((
0, int!(0),
MilliSecondsSinceUnixEpoch( MilliSecondsSinceUnixEpoch(
eventid_info eventid_info
.get(event_id) .get(event_id)
@ -1153,14 +1154,13 @@ fn handle_outlier_pdu<'a>(
// 2. Check signatures, otherwise drop // 2. Check signatures, otherwise drop
// 3. check content hash, redact if doesn't match // 3. check content hash, redact if doesn't match
let create_event_content = let create_event_content = serde_json::from_str::<RoomCreateEventContent>(
serde_json::from_value::<Raw<CreateEventContent>>(create_event.content.clone()) create_event.content.get(),
.expect("Raw::from_value always works.") )
.deserialize() .map_err(|e| {
.map_err(|e| { warn!("Invalid create event: {}", e);
warn!("Invalid create event: {}", e); "Invalid create event in db.".to_owned()
"Invalid create event in db.".to_owned() })?;
})?;
let room_version_id = &create_event_content.room_version; let room_version_id = &create_event_content.room_version;
let room_version = RoomVersion::new(room_version_id).expect("room version is supported"); let room_version = RoomVersion::new(room_version_id).expect("room version is supported");
@ -1241,7 +1241,7 @@ fn handle_outlier_pdu<'a>(
.expect("all auth events have state keys"), .expect("all auth events have state keys"),
)) { )) {
hash_map::Entry::Vacant(v) => { hash_map::Entry::Vacant(v) => {
v.insert(auth_event.clone()); v.insert(auth_event);
} }
hash_map::Entry::Occupied(_) => { hash_map::Entry::Occupied(_) => {
return Err( return Err(
@ -1276,7 +1276,7 @@ fn handle_outlier_pdu<'a>(
if !state_res::event_auth::auth_check( if !state_res::event_auth::auth_check(
&room_version, &room_version,
&incoming_pdu, &incoming_pdu,
previous_create, previous_create.as_ref(),
None::<PduEvent>, // TODO: third party invite None::<PduEvent>, // TODO: third party invite
|k, s| auth_events.get(&(k.clone(), s.to_owned())), |k, s| auth_events.get(&(k.clone(), s.to_owned())),
) )
@ -1319,14 +1319,13 @@ async fn upgrade_outlier_to_timeline_pdu(
return Err("Event has been soft failed".into()); return Err("Event has been soft failed".into());
} }
let create_event_content = let create_event_content = serde_json::from_str::<RoomCreateEventContent>(
serde_json::from_value::<Raw<CreateEventContent>>(create_event.content.clone()) create_event.content.get(),
.expect("Raw::from_value always works.") )
.deserialize() .map_err(|e| {
.map_err(|e| { warn!("Invalid create event: {}", e);
warn!("Invalid create event: {}", e); "Invalid create event in db.".to_owned()
"Invalid create event in db.".to_owned() })?;
})?;
let room_version_id = &create_event_content.room_version; let room_version_id = &create_event_content.room_version;
let room_version = RoomVersion::new(room_version_id).expect("room version is supported"); let room_version = RoomVersion::new(room_version_id).expect("room version is supported");
@ -1562,7 +1561,7 @@ async fn upgrade_outlier_to_timeline_pdu(
let check_result = state_res::event_auth::auth_check( let check_result = state_res::event_auth::auth_check(
&room_version, &room_version,
&incoming_pdu, &incoming_pdu,
previous_create.as_deref(), previous_create.as_ref(),
None::<PduEvent>, // TODO: third party invite None::<PduEvent>, // TODO: third party invite
|k, s| { |k, s| {
db.rooms db.rooms
@ -1646,7 +1645,7 @@ async fn upgrade_outlier_to_timeline_pdu(
let soft_fail = !state_res::event_auth::auth_check( let soft_fail = !state_res::event_auth::auth_check(
&room_version, &room_version,
&incoming_pdu, &incoming_pdu,
previous_create.as_deref(), previous_create.as_ref(),
None::<PduEvent>, None::<PduEvent>,
|k, s| auth_events.get(&(k.clone(), s.to_owned())), |k, s| auth_events.get(&(k.clone(), s.to_owned())),
) )
@ -2669,13 +2668,12 @@ pub fn create_join_event_template_route(
let create_event_content = create_event let create_event_content = create_event
.as_ref() .as_ref()
.map(|create_event| { .map(|create_event| {
serde_json::from_value::<Raw<CreateEventContent>>(create_event.content.clone()) serde_json::from_str::<RoomCreateEventContent>(create_event.content.get()).map_err(
.expect("Raw::from_value always works.") |e| {
.deserialize()
.map_err(|e| {
warn!("Invalid create event: {}", e); warn!("Invalid create event: {}", e);
Error::bad_database("Invalid create event in db.") Error::bad_database("Invalid create event in db.")
}) },
)
}) })
.transpose()?; .transpose()?;
@ -2702,7 +2700,7 @@ pub fn create_join_event_template_route(
)); ));
} }
let content = serde_json::to_value(MemberEventContent { let content = to_raw_value(&RoomMemberEventContent {
avatar_url: None, avatar_url: None,
blurhash: None, blurhash: None,
displayname: None, displayname: None,
@ -2738,7 +2736,7 @@ pub fn create_join_event_template_route(
unsigned.insert("prev_content".to_owned(), prev_pdu.content.clone()); unsigned.insert("prev_content".to_owned(), prev_pdu.content.clone());
unsigned.insert( unsigned.insert(
"prev_sender".to_owned(), "prev_sender".to_owned(),
serde_json::to_value(&prev_pdu.sender).expect("UserId::to_value always works"), serde_json::from_str(prev_pdu.sender.as_str()).expect("UserId is valid string"),
); );
} }
@ -2759,17 +2757,21 @@ pub fn create_join_event_template_route(
.map(|(_, pdu)| pdu.event_id.clone()) .map(|(_, pdu)| pdu.event_id.clone())
.collect(), .collect(),
redacts: None, redacts: None,
unsigned, unsigned: if unsigned.is_empty() {
hashes: ruma::events::pdu::EventHash { None
} else {
Some(to_raw_value(&unsigned).expect("to_raw_value always works"))
},
hashes: EventHash {
sha256: "aaa".to_owned(), sha256: "aaa".to_owned(),
}, },
signatures: BTreeMap::new(), signatures: None,
}; };
let auth_check = state_res::auth_check( let auth_check = state_res::auth_check(
&room_version, &room_version,
&pdu, &pdu,
create_prev_event.as_deref(), create_prev_event,
None::<PduEvent>, // TODO: third_party_invite None::<PduEvent>, // TODO: third_party_invite
|k, s| auth_events.get(&(k.clone(), s.to_owned())), |k, s| auth_events.get(&(k.clone(), s.to_owned())),
) )
@ -2799,10 +2801,7 @@ pub fn create_join_event_template_route(
Ok(create_join_event_template::v1::Response { Ok(create_join_event_template::v1::Response {
room_version: Some(room_version_id), room_version: Some(room_version_id),
event: serde_json::from_value::<Raw<_>>( event: to_raw_value(&pdu_json).expect("CanonicalJson can be serialized to JSON"),
serde_json::to_value(pdu_json).expect("CanonicalJson is valid serde_json::Value"),
)
.expect("Raw::from_value always works"),
} }
.into()) .into())
} }
@ -2810,7 +2809,7 @@ pub fn create_join_event_template_route(
async fn create_join_event( async fn create_join_event(
db: &DatabaseGuard, db: &DatabaseGuard,
room_id: &RoomId, room_id: &RoomId,
pdu: &Raw<ruma::events::pdu::Pdu>, pdu: &RawJsonValue,
) -> Result<RoomState> { ) -> Result<RoomState> {
if !db.globals.allow_federation() { if !db.globals.allow_federation() {
return Err(Error::bad_config("Federation is disabled.")); return Err(Error::bad_config("Federation is disabled."));
@ -2947,7 +2946,7 @@ pub async fn create_join_event_v2_route(
#[tracing::instrument(skip(db, body))] #[tracing::instrument(skip(db, body))]
pub async fn create_invite_route( pub async fn create_invite_route(
db: DatabaseGuard, db: DatabaseGuard,
body: Ruma<create_invite::v2::Request>, body: Ruma<create_invite::v2::Request<'_>>,
) -> ConduitResult<create_invite::v2::Response> { ) -> ConduitResult<create_invite::v2::Response> {
if !db.globals.allow_federation() { if !db.globals.allow_federation() {
return Err(Error::bad_config("Federation is disabled.")); return Err(Error::bad_config("Federation is disabled."));
@ -3014,10 +3013,11 @@ pub async fn create_invite_route(
let mut invite_state = body.invite_room_state.clone(); let mut invite_state = body.invite_room_state.clone();
let mut event = serde_json::from_str::<serde_json::Map<String, serde_json::Value>>( let mut event =
&body.event.json().to_string(), serde_json::from_str::<serde_json::Map<String, serde_json::Value>>(body.event.get())
) .map_err(|_| {
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid invite event bytes."))?; Error::BadRequest(ErrorKind::InvalidParam, "Invalid invite event bytes.")
})?;
event.insert("event_id".to_owned(), "$dummy".into()); event.insert("event_id".to_owned(), "$dummy".into());
@ -3280,13 +3280,13 @@ pub(crate) async fn fetch_required_signing_keys(
// Gets a list of servers for which we don't have the signing key yet. We go over // Gets a list of servers for which we don't have the signing key yet. We go over
// the PDUs and either cache the key or add it to the list that needs to be retrieved. // the PDUs and either cache the key or add it to the list that needs to be retrieved.
fn get_server_keys_from_cache( fn get_server_keys_from_cache(
pdu: &Raw<Pdu>, pdu: &RawJsonValue,
servers: &mut BTreeMap<Box<ServerName>, BTreeMap<ServerSigningKeyId, QueryCriteria>>, servers: &mut BTreeMap<Box<ServerName>, BTreeMap<ServerSigningKeyId, QueryCriteria>>,
room_version: &RoomVersionId, room_version: &RoomVersionId,
pub_key_map: &mut RwLockWriteGuard<'_, BTreeMap<String, BTreeMap<String, String>>>, pub_key_map: &mut RwLockWriteGuard<'_, BTreeMap<String, BTreeMap<String, String>>>,
db: &Database, db: &Database,
) -> Result<()> { ) -> Result<()> {
let value = serde_json::from_str::<CanonicalJsonObject>(pdu.json().get()).map_err(|e| { let value = serde_json::from_str::<CanonicalJsonObject>(pdu.get()).map_err(|e| {
error!("Invalid PDU in server response: {:?}: {:?}", pdu, e); error!("Invalid PDU in server response: {:?}: {:?}", pdu, e);
Error::BadServerResponse("Invalid PDU in server response") Error::BadServerResponse("Invalid PDU in server response")
})?; })?;
@ -3385,10 +3385,10 @@ pub(crate) async fn fetch_join_signing_keys(
// Try to fetch keys, failure is okay // Try to fetch keys, failure is okay
// Servers we couldn't find in the cache will be added to `servers` // Servers we couldn't find in the cache will be added to `servers`
for pdu in &event.room_state.state { for pdu in &event.room_state.state {
let _ = get_server_keys_from_cache(pdu, &mut servers, room_version, &mut pkm, db); let _ = get_server_keys_from_cache(&pdu, &mut servers, room_version, &mut pkm, db);
} }
for pdu in &event.room_state.auth_chain { for pdu in &event.room_state.auth_chain {
let _ = get_server_keys_from_cache(pdu, &mut servers, room_version, &mut pkm, db); let _ = get_server_keys_from_cache(&pdu, &mut servers, room_version, &mut pkm, db);
} }
drop(pkm); drop(pkm);