From d3968c2fd1d901011e5aaf1dd14cecfed5af10bf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timo=20K=C3=B6sters?= <timo@koesters.xyz>
Date: Tue, 11 Oct 2022 21:51:20 +0200
Subject: [PATCH 1/2] fix: bump ruma again to fix state res problems

---
 Cargo.lock                           | 22 +++++++++++-----------
 Cargo.toml                           |  2 +-
 src/api/client_server/media.rs       |  9 ++++++++-
 src/api/client_server/message.rs     |  8 ++++----
 src/api/client_server/read_marker.rs |  4 +++-
 src/api/client_server/sync.rs        |  1 +
 6 files changed, 28 insertions(+), 18 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 760b4d90..9e58bccc 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2039,7 +2039,7 @@ dependencies = [
 [[package]]
 name = "ruma"
 version = "0.7.4"
-source = "git+https://github.com/ruma/ruma?rev=86b58cafb8abe29eecd0272d90b40bbb61a7919b#86b58cafb8abe29eecd0272d90b40bbb61a7919b"
+source = "git+https://github.com/ruma/ruma?rev=c2c45551335c443ede7fb9158284196899a0c696#c2c45551335c443ede7fb9158284196899a0c696"
 dependencies = [
  "assign",
  "js_int",
@@ -2057,7 +2057,7 @@ dependencies = [
 [[package]]
 name = "ruma-appservice-api"
 version = "0.7.0"
-source = "git+https://github.com/ruma/ruma?rev=86b58cafb8abe29eecd0272d90b40bbb61a7919b#86b58cafb8abe29eecd0272d90b40bbb61a7919b"
+source = "git+https://github.com/ruma/ruma?rev=c2c45551335c443ede7fb9158284196899a0c696#c2c45551335c443ede7fb9158284196899a0c696"
 dependencies = [
  "ruma-common",
  "serde",
@@ -2067,7 +2067,7 @@ dependencies = [
 [[package]]
 name = "ruma-client-api"
 version = "0.15.1"
-source = "git+https://github.com/ruma/ruma?rev=86b58cafb8abe29eecd0272d90b40bbb61a7919b#86b58cafb8abe29eecd0272d90b40bbb61a7919b"
+source = "git+https://github.com/ruma/ruma?rev=c2c45551335c443ede7fb9158284196899a0c696#c2c45551335c443ede7fb9158284196899a0c696"
 dependencies = [
  "assign",
  "bytes",
@@ -2083,7 +2083,7 @@ dependencies = [
 [[package]]
 name = "ruma-common"
 version = "0.10.3"
-source = "git+https://github.com/ruma/ruma?rev=86b58cafb8abe29eecd0272d90b40bbb61a7919b#86b58cafb8abe29eecd0272d90b40bbb61a7919b"
+source = "git+https://github.com/ruma/ruma?rev=c2c45551335c443ede7fb9158284196899a0c696#c2c45551335c443ede7fb9158284196899a0c696"
 dependencies = [
  "base64",
  "bytes",
@@ -2110,7 +2110,7 @@ dependencies = [
 [[package]]
 name = "ruma-federation-api"
 version = "0.6.0"
-source = "git+https://github.com/ruma/ruma?rev=86b58cafb8abe29eecd0272d90b40bbb61a7919b#86b58cafb8abe29eecd0272d90b40bbb61a7919b"
+source = "git+https://github.com/ruma/ruma?rev=c2c45551335c443ede7fb9158284196899a0c696#c2c45551335c443ede7fb9158284196899a0c696"
 dependencies = [
  "js_int",
  "ruma-common",
@@ -2121,7 +2121,7 @@ dependencies = [
 [[package]]
 name = "ruma-identifiers-validation"
 version = "0.9.0"
-source = "git+https://github.com/ruma/ruma?rev=86b58cafb8abe29eecd0272d90b40bbb61a7919b#86b58cafb8abe29eecd0272d90b40bbb61a7919b"
+source = "git+https://github.com/ruma/ruma?rev=c2c45551335c443ede7fb9158284196899a0c696#c2c45551335c443ede7fb9158284196899a0c696"
 dependencies = [
  "js_int",
  "thiserror",
@@ -2130,7 +2130,7 @@ dependencies = [
 [[package]]
 name = "ruma-identity-service-api"
 version = "0.6.0"
-source = "git+https://github.com/ruma/ruma?rev=86b58cafb8abe29eecd0272d90b40bbb61a7919b#86b58cafb8abe29eecd0272d90b40bbb61a7919b"
+source = "git+https://github.com/ruma/ruma?rev=c2c45551335c443ede7fb9158284196899a0c696#c2c45551335c443ede7fb9158284196899a0c696"
 dependencies = [
  "js_int",
  "ruma-common",
@@ -2140,7 +2140,7 @@ dependencies = [
 [[package]]
 name = "ruma-macros"
 version = "0.10.3"
-source = "git+https://github.com/ruma/ruma?rev=86b58cafb8abe29eecd0272d90b40bbb61a7919b#86b58cafb8abe29eecd0272d90b40bbb61a7919b"
+source = "git+https://github.com/ruma/ruma?rev=c2c45551335c443ede7fb9158284196899a0c696#c2c45551335c443ede7fb9158284196899a0c696"
 dependencies = [
  "once_cell",
  "proc-macro-crate",
@@ -2155,7 +2155,7 @@ dependencies = [
 [[package]]
 name = "ruma-push-gateway-api"
 version = "0.6.0"
-source = "git+https://github.com/ruma/ruma?rev=86b58cafb8abe29eecd0272d90b40bbb61a7919b#86b58cafb8abe29eecd0272d90b40bbb61a7919b"
+source = "git+https://github.com/ruma/ruma?rev=c2c45551335c443ede7fb9158284196899a0c696#c2c45551335c443ede7fb9158284196899a0c696"
 dependencies = [
  "js_int",
  "ruma-common",
@@ -2166,7 +2166,7 @@ dependencies = [
 [[package]]
 name = "ruma-signatures"
 version = "0.12.0"
-source = "git+https://github.com/ruma/ruma?rev=86b58cafb8abe29eecd0272d90b40bbb61a7919b#86b58cafb8abe29eecd0272d90b40bbb61a7919b"
+source = "git+https://github.com/ruma/ruma?rev=c2c45551335c443ede7fb9158284196899a0c696#c2c45551335c443ede7fb9158284196899a0c696"
 dependencies = [
  "base64",
  "ed25519-dalek",
@@ -2182,7 +2182,7 @@ dependencies = [
 [[package]]
 name = "ruma-state-res"
 version = "0.8.0"
-source = "git+https://github.com/ruma/ruma?rev=86b58cafb8abe29eecd0272d90b40bbb61a7919b#86b58cafb8abe29eecd0272d90b40bbb61a7919b"
+source = "git+https://github.com/ruma/ruma?rev=c2c45551335c443ede7fb9158284196899a0c696#c2c45551335c443ede7fb9158284196899a0c696"
 dependencies = [
  "itertools",
  "js_int",
diff --git a/Cargo.toml b/Cargo.toml
index 37b05294..0b3062da 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -21,7 +21,7 @@ tower-http = { version = "0.3.4", features = ["add-extension", "cors", "compress
 
 # 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 = { git = "https://github.com/ruma/ruma", rev = "86b58cafb8abe29eecd0272d90b40bbb61a7919b", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-msc2448", "unstable-exhaustive-types", "ring-compat", "unstable-unspecified" ] }
+ruma = { git = "https://github.com/ruma/ruma", rev = "c2c45551335c443ede7fb9158284196899a0c696", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-msc2448", "unstable-exhaustive-types", "ring-compat", "unstable-unspecified" ] }
 #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"] }
 
diff --git a/src/api/client_server/media.rs b/src/api/client_server/media.rs
index c1f5e1de..ae023c95 100644
--- a/src/api/client_server/media.rs
+++ b/src/api/client_server/media.rs
@@ -104,6 +104,7 @@ pub async fn get_content_route(
             file,
             content_type,
             content_disposition,
+            cross_origin_resource_policy: Some("cross-origin".to_owned()),
         })
     } else if &*body.server_name != services().globals.server_name() && body.allow_remote {
         let remote_content_response =
@@ -134,6 +135,7 @@ pub async fn get_content_as_filename_route(
             file,
             content_type,
             content_disposition: Some(format!("inline; filename={}", body.filename)),
+            cross_origin_resource_policy: Some("cross-origin".to_owned()),
         })
     } else if &*body.server_name != services().globals.server_name() && body.allow_remote {
         let remote_content_response =
@@ -143,6 +145,7 @@ pub async fn get_content_as_filename_route(
             content_disposition: Some(format!("inline: filename={}", body.filename)),
             content_type: remote_content_response.content_type,
             file: remote_content_response.file,
+            cross_origin_resource_policy: Some("cross-origin".to_owned()),
         })
     } else {
         Err(Error::BadRequest(ErrorKind::NotFound, "Media not found."))
@@ -174,7 +177,11 @@ pub async fn get_content_thumbnail_route(
         )
         .await?
     {
-        Ok(get_content_thumbnail::v3::Response { file, content_type })
+        Ok(get_content_thumbnail::v3::Response {
+            file,
+            content_type,
+            cross_origin_resource_policy: Some("cross-origin".to_owned()),
+        })
     } else if &*body.server_name != services().globals.server_name() && body.allow_remote {
         let get_thumbnail_response = services()
             .sending
diff --git a/src/api/client_server/message.rs b/src/api/client_server/message.rs
index 2b5bdf9d..b04c2626 100644
--- a/src/api/client_server/message.rs
+++ b/src/api/client_server/message.rs
@@ -127,8 +127,8 @@ pub async fn get_message_events_route(
             .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid `from` value."))?,
 
         None => match body.dir {
-            get_message_events::v3::Direction::Forward => 0,
-            get_message_events::v3::Direction::Backward => u64::MAX,
+            ruma::api::client::Direction::Forward => 0,
+            ruma::api::client::Direction::Backward => u64::MAX,
         },
     };
 
@@ -151,7 +151,7 @@ pub async fn get_message_events_route(
     let mut lazy_loaded = HashSet::new();
 
     match body.dir {
-        get_message_events::v3::Direction::Forward => {
+        ruma::api::client::Direction::Forward => {
             let events_after: Vec<_> = services()
                 .rooms
                 .timeline
@@ -196,7 +196,7 @@ pub async fn get_message_events_route(
             resp.end = next_token.map(|count| count.to_string());
             resp.chunk = events_after;
         }
-        get_message_events::v3::Direction::Backward => {
+        ruma::api::client::Direction::Backward => {
             let events_before: Vec<_> = services()
                 .rooms
                 .timeline
diff --git a/src/api/client_server/read_marker.rs b/src/api/client_server/read_marker.rs
index bdf467f9..48520fc9 100644
--- a/src/api/client_server/read_marker.rs
+++ b/src/api/client_server/read_marker.rs
@@ -1,7 +1,7 @@
 use crate::{services, Error, Result, Ruma};
 use ruma::{
     api::client::{error::ErrorKind, read_marker::set_read_marker, receipt::create_receipt},
-    events::{receipt::ReceiptType, RoomAccountDataEventType},
+    events::{receipt::{ReceiptType, ReceiptThread}, RoomAccountDataEventType},
     MilliSecondsSinceUnixEpoch,
 };
 use std::collections::BTreeMap;
@@ -59,6 +59,7 @@ pub async fn set_read_marker_route(
             sender_user.clone(),
             ruma::events::receipt::Receipt {
                 ts: Some(MilliSecondsSinceUnixEpoch::now()),
+                thread: ReceiptThread::Unthreaded,
             },
         );
 
@@ -119,6 +120,7 @@ pub async fn create_receipt_route(
                 sender_user.clone(),
                 ruma::events::receipt::Receipt {
                     ts: Some(MilliSecondsSinceUnixEpoch::now()),
+                    thread: ReceiptThread::Unthreaded,
                 },
             );
             let mut receipts = BTreeMap::new();
diff --git a/src/api/client_server/sync.rs b/src/api/client_server/sync.rs
index 4b732692..828ae19c 100644
--- a/src/api/client_server/sync.rs
+++ b/src/api/client_server/sync.rs
@@ -778,6 +778,7 @@ async fn sync_helper(
                     .collect(),
             },
             ephemeral: Ephemeral { events: edus },
+            unread_thread_notifications: BTreeMap::new(),
         };
 
         if !joined_room.is_empty() {

From 2b70d9604a2cb10830a6677c2380374836b4d990 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timo=20K=C3=B6sters?= <timo@koesters.xyz>
Date: Tue, 11 Oct 2022 22:37:14 +0200
Subject: [PATCH 2/2] fix: element gets stuck in /initialSync

---
 src/main.rs                            | 14 +++++++++++++-
 src/service/rooms/event_handler/mod.rs |  6 +++++-
 src/utils/error.rs                     |  2 +-
 3 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index bdbeaa6a..08368415 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -342,6 +342,14 @@ fn routes() -> Router {
         .ruma_route(server_server::get_profile_information_route)
         .ruma_route(server_server::get_keys_route)
         .ruma_route(server_server::claim_keys_route)
+        .route(
+            "/_matrix/client/r0/rooms/:room_id/initialSync",
+            get(initial_sync),
+        )
+        .route(
+            "/_matrix/client/v3/rooms/:room_id/initialSync",
+            get(initial_sync),
+        )
         .fallback(not_found.into_service())
 }
 
@@ -375,7 +383,11 @@ async fn shutdown_signal(handle: ServerHandle) {
 }
 
 async fn not_found(_uri: Uri) -> impl IntoResponse {
-    Error::BadRequest(ErrorKind::NotFound, "Unknown or unimplemented route")
+    Error::BadRequest(ErrorKind::Unrecognized, "Unrecognized request")
+}
+
+async fn initial_sync(_uri: Uri) -> impl IntoResponse {
+    Error::BadRequest(ErrorKind::GuestAccessForbidden, "Guest access not implemented")
 }
 
 trait RouterExt {
diff --git a/src/service/rooms/event_handler/mod.rs b/src/service/rooms/event_handler/mod.rs
index cd270c7c..477a9719 100644
--- a/src/service/rooms/event_handler/mod.rs
+++ b/src/service/rooms/event_handler/mod.rs
@@ -44,6 +44,7 @@ impl Service {
     /// When receiving an event one needs to:
     /// 0. Check the server is in the room
     /// 1. Skip the PDU if we already know about it
+    /// 1.1. Remove unsigned field
     /// 2. Check signatures, otherwise drop
     /// 3. Check content hash, redact if doesn't match
     /// 4. Fetch any missing auth events doing all checks listed here starting at 1. These are not
@@ -260,10 +261,13 @@ impl Service {
         create_event: &'a PduEvent,
         event_id: &'a EventId,
         room_id: &'a RoomId,
-        value: BTreeMap<String, CanonicalJsonValue>,
+        mut value: BTreeMap<String, CanonicalJsonValue>,
         pub_key_map: &'a RwLock<BTreeMap<String, BTreeMap<String, Base64>>>,
     ) -> AsyncRecursiveType<'a, Result<(Arc<PduEvent>, BTreeMap<String, CanonicalJsonValue>)>> {
         Box::pin(async move {
+            // 1.1. Remove unsigned field
+            value.remove("unsigned");
+
             // TODO: For RoomVersion6 we must check that Raw<..> is canonical do we anywhere?: https://matrix.org/docs/spec/rooms/v6#canonical-json
 
             // We go through all the signatures we see on the value and fetch the corresponding signing
diff --git a/src/utils/error.rs b/src/utils/error.rs
index 8967acb7..9c8617f9 100644
--- a/src/utils/error.rs
+++ b/src/utils/error.rs
@@ -117,7 +117,7 @@ impl Error {
                         StatusCode::FORBIDDEN
                     }
                     Unauthorized | UnknownToken { .. } | MissingToken => StatusCode::UNAUTHORIZED,
-                    NotFound => StatusCode::NOT_FOUND,
+                    NotFound | Unrecognized => StatusCode::NOT_FOUND,
                     LimitExceeded { .. } => StatusCode::TOO_MANY_REQUESTS,
                     UserDeactivated => StatusCode::FORBIDDEN,
                     TooLarge => StatusCode::PAYLOAD_TOO_LARGE,