From f4146de17df151275ee361a148e6a951039de5c5 Mon Sep 17 00:00:00 2001 From: strawberry Date: Sat, 23 Mar 2024 23:13:40 -0400 Subject: [PATCH] add feature for smElement client hacks Signed-off-by: strawberry --- Cargo.toml | 7 +++- src/api/client_server/message.rs | 36 ++++++++++--------- src/api/client_server/sync.rs | 7 ++-- src/service/sending/mod.rs | 59 +++++++++++++++----------------- 4 files changed, 59 insertions(+), 50 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 11b7148f..fc545ee7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -277,7 +277,7 @@ hyperlocal = { git = "https://github.com/softprops/hyperlocal", rev = "2ee4d1496 [features] -default = ["conduit_bin", "backend_rocksdb", "systemd"] +default = ["conduit_bin", "backend_rocksdb", "systemd", "element_hacks"] conduit_bin = ["axum"] backend_sqlite = ["sqlite"] backend_rocksdb = ["rocksdb"] @@ -303,6 +303,11 @@ perf_measurements = [ "opentelemetry-jaeger", ] +# client/server interopability hacks +# +## element has various non-spec compliant behaviour +element_hacks = [] + [[bin]] name = "conduit" diff --git a/src/api/client_server/message.rs b/src/api/client_server/message.rs index 93d00d52..8c4219eb 100644 --- a/src/api/client_server/message.rs +++ b/src/api/client_server/message.rs @@ -189,18 +189,20 @@ pub async fn get_message_events_route( .collect(); for (_, event) in &events_after { - /* TODO: Remove this when these are resolved: + /* TODO: Remove the not "element_hacks" check when these are resolved: * https://github.com/vector-im/element-android/issues/3417 * https://github.com/vector-im/element-web/issues/21034 - if !services().rooms.lazy_loading.lazy_load_was_sent_before( - sender_user, - sender_device, - &body.room_id, - &event.sender, - )? { + */ + if !cfg!(features = "element_hacks") + && !services().rooms.lazy_loading.lazy_load_was_sent_before( + sender_user, + sender_device, + &body.room_id, + &event.sender, + )? { lazy_loaded.insert(event.sender.clone()); } - */ + lazy_loaded.insert(event.sender.clone()); } @@ -231,18 +233,20 @@ pub async fn get_message_events_route( .collect(); for (_, event) in &events_before { - /* TODO: Remove this when these are resolved: + /* TODO: Remove the not "element_hacks" check when these are resolved: * https://github.com/vector-im/element-android/issues/3417 * https://github.com/vector-im/element-web/issues/21034 - if !services().rooms.lazy_loading.lazy_load_was_sent_before( - sender_user, - sender_device, - &body.room_id, - &event.sender, - )? { + */ + if !cfg!(features = "element_hacks") + && !services().rooms.lazy_loading.lazy_load_was_sent_before( + sender_user, + sender_device, + &body.room_id, + &event.sender, + )? { lazy_loaded.insert(event.sender.clone()); } - */ + lazy_loaded.insert(event.sender.clone()); } diff --git a/src/api/client_server/sync.rs b/src/api/client_server/sync.rs index f108a0fd..ca155d5a 100644 --- a/src/api/client_server/sync.rs +++ b/src/api/client_server/sync.rs @@ -298,7 +298,7 @@ async fn sync_helper( || event_type != StateEventType::RoomMember || full_state // TODO: Delete the following line when this is resolved: https://github.com/vector-im/element-web/issues/22565 - || *sender_user == state_key + || (cfg!(features = "element_hacks") && *sender_user == state_key) { let Some(pdu) = services().rooms.timeline.get_pdu(&id)? else { error!("Pdu in state not found: {}", id); @@ -627,7 +627,7 @@ async fn load_joined_room( || full_state || timeline_users.contains(&state_key) // TODO: Delete the following line when this is resolved: https://github.com/vector-im/element-web/issues/22565 - || *sender_user == state_key + || (cfg!(features = "element_hacks") && *sender_user == state_key) { let pdu = match services().rooms.timeline.get_pdu(&id)? { Some(pdu) => pdu, @@ -966,6 +966,9 @@ fn share_encrypted_room(sender_user: &UserId, user_id: &UserId, ignore_room: &Ro .any(|encrypted| encrypted)) } +/// POST `/_matrix/client/unstable/org.matrix.msc3575/sync` +/// +/// Sliding Sync endpoint (future endpoint: `/_matrix/client/v4/sync`) pub async fn sync_events_v4_route( body: Ruma, ) -> Result> { diff --git a/src/service/sending/mod.rs b/src/service/sending/mod.rs index 45439010..3326a214 100644 --- a/src/service/sending/mod.rs +++ b/src/service/sending/mod.rs @@ -316,43 +316,40 @@ impl Service { continue; } - let event: AnySyncEphemeralRoomEvent = serde_json::from_str(read_receipt.json().get()) + let event = serde_json::from_str(read_receipt.json().get()) .map_err(|_| Error::bad_database("Invalid edu event in read_receipts."))?; - let federation_event = match event { - AnySyncEphemeralRoomEvent::Receipt(r) => { - let mut read = BTreeMap::new(); + let federation_event = if let AnySyncEphemeralRoomEvent::Receipt(r) = event { + let mut read = BTreeMap::new(); - let (event_id, mut receipt) = - r.content.0.into_iter().next().expect("we only use one event per read receipt"); - let receipt = receipt - .remove(&ReceiptType::Read) - .expect("our read receipts always set this") - .remove(&user_id) - .expect("our read receipts always have the user here"); + let (event_id, mut receipt) = + r.content.0.into_iter().next().expect("we only use one event per read receipt"); + let receipt = receipt + .remove(&ReceiptType::Read) + .expect("our read receipts always set this") + .remove(&user_id) + .expect("our read receipts always have the user here"); - read.insert( - user_id, - ReceiptData { - data: receipt.clone(), - event_ids: vec![event_id.clone()], - }, - ); + read.insert( + user_id, + ReceiptData { + data: receipt.clone(), + event_ids: vec![event_id.clone()], + }, + ); - let receipt_map = ReceiptMap { - read, - }; + let receipt_map = ReceiptMap { + read, + }; - let mut receipts = BTreeMap::new(); - receipts.insert(room_id.clone(), receipt_map); + let mut receipts = BTreeMap::new(); + receipts.insert(room_id.clone(), receipt_map); - Edu::Receipt(ReceiptContent { - receipts, - }) - }, - _ => { - Error::bad_database("Invalid event type in read_receipts"); - continue; - }, + Edu::Receipt(ReceiptContent { + receipts, + }) + } else { + Error::bad_database("Invalid event type in read_receipts"); + continue; }; events.push(serde_json::to_vec(&federation_event).expect("json can be serialized"));