From 64314ad080ddc46901e10e0097481f70ab54a4cc Mon Sep 17 00:00:00 2001 From: Mo Rajabi Date: Sat, 14 Nov 2026 16:59:00 +0400 Subject: [PATCH 1/3] adapter: expose source contracts and enforce protocol v5 --- Cargo.lock | 192 +++++++++++- crates/adapter/src/http.rs | 540 +++++++++++++++++++++++++++++++-- crates/adapter/src/protocol.rs | 28 +- 3 files changed, 727 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 83c9b68..575843f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,23 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + +[[package]] +name = "aes" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b169f7a6d4742236a0a00c541b845991d0ac43e546831af1249753ab4c3aa3a0" +dependencies = [ + "cfg-if", + "cipher", + "cpufeatures 0.2.17", +] + [[package]] name = "aho-corasick" version = "1.1.4" @@ -73,6 +90,12 @@ version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" +[[package]] +name = "autocfg" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" + [[package]] name = "axum" version = "0.8.9" @@ -201,6 +224,16 @@ dependencies = [ "rand_core 0.10.1", ] +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common", + "inout", +] + [[package]] name = "colorchoice" version = "1.0.5" @@ -241,6 +274,15 @@ dependencies = [ "libc", ] +[[package]] +name = "crc32fast" +version = "1.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b929dd2a135771f40afabd1073f85eed59c800b7c61901e5c254270497dad64" +dependencies = [ + "cfg-if", +] + [[package]] name = "crypto-common" version = "0.1.7" @@ -393,6 +435,17 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" +[[package]] +name = "flate2" +version = "1.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "626d4928326b646e70f65ed36fc7cf9f206bdb68a0019ee7c6b4709da4e084c4" +dependencies = [ + "crc32fast", + "miniz_oxide", + "zlib-rs", +] + [[package]] name = "foldhash" version = "0.2.0" @@ -550,6 +603,12 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "hex-literal" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fe2267d4ed49bc07b63801559be28c718ea06c4738b7a03c94df7386d2cde46" + [[package]] name = "hostname" version = "0.4.2" @@ -640,7 +699,7 @@ dependencies = [ "tokio", "tokio-rustls", "tower-service", - "webpki-roots", + "webpki-roots 1.0.8", ] [[package]] @@ -784,6 +843,7 @@ name = "inline-client" version = "0.6.3" dependencies = [ "futures-util", + "getrandom 0.3.4", "inline-sdk", "log", "prost", @@ -796,18 +856,28 @@ dependencies = [ [[package]] name = "inline-protocol" -version = "0.6.2" +version = "0.7.25" dependencies = [ + "aes", + "cipher", + "hex-literal", + "num-bigint", "prost", "prost-build", "serde", + "sha1", + "sha2", + "subtle", ] [[package]] name = "inline-sdk" -version = "0.6.2" +version = "0.7.25" dependencies = [ + "base64", + "flate2", "futures-util", + "getrandom 0.3.4", "hostname", "inline-protocol", "log", @@ -815,10 +885,21 @@ dependencies = [ "reqwest", "serde", "serde_json", + "sha2", "thiserror 1.0.69", "tokio", "tokio-tungstenite", "url", + "uuid", +] + +[[package]] +name = "inout" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879f10e63c20629ecabbb64a8010319738c66a5cd0c29b02d63d272b03751d01" +dependencies = [ + "generic-array", ] [[package]] @@ -896,6 +977,7 @@ version = "0.38.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f6c19a05435c21ac299d71b6a9c13db3e3f47c520517d58990a462a1397a61db" dependencies = [ + "cc", "pkg-config", "vcpkg", ] @@ -968,6 +1050,16 @@ dependencies = [ "unicase", ] +[[package]] +name = "miniz_oxide" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b63fbc4a50860e98e7b2aa7804ded1db5cbc3aff9193adaff57a6931bf7c4b4c" +dependencies = [ + "adler2", + "simd-adler32", +] + [[package]] name = "mio" version = "1.2.1" @@ -1002,6 +1094,34 @@ version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d87ecb2933e8aeadb3e3a02b828fed80a7528047e68b4f424523a0981a3a084" +[[package]] +name = "num-bigint" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c89e69e7e0f03bea5ef08013795c25018e101932225a656383bd384495ecc367" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ce2d95d4b3734dc35aa2f45e1aa22cd416814592a4f9d9205e11affd5b8e10b" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "once_cell" version = "1.21.4" @@ -1353,7 +1473,7 @@ dependencies = [ "wasm-bindgen-futures", "wasm-streams", "web-sys", - "webpki-roots", + "webpki-roots 1.0.8", ] [[package]] @@ -1582,12 +1702,45 @@ dependencies = [ "digest", ] +[[package]] +name = "sha1_smol" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbfa15b3dddfee50a0fff136974b3e1bde555604ba463834a7eb7deb6417705d" + +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures 0.2.17", + "digest", +] + [[package]] name = "shlex" version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" +[[package]] +name = "signal-hook-registry" +version = "1.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" +dependencies = [ + "errno", + "libc", +] + +[[package]] +name = "simd-adler32" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a219298ac11a56ea9a6d2120044824d6f01aeb034955e7af7bc16858527deea" + [[package]] name = "slab" version = "0.4.12" @@ -1678,7 +1831,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" dependencies = [ "fastrand", - "getrandom 0.3.4", + "getrandom 0.4.3", "once_cell", "rustix", "windows-sys 0.61.2", @@ -1759,6 +1912,7 @@ dependencies = [ "libc", "mio", "pin-project-lite", + "signal-hook-registry", "socket2", "tokio-macros", "windows-sys 0.61.2", @@ -1799,6 +1953,7 @@ dependencies = [ "tokio", "tokio-rustls", "tungstenite", + "webpki-roots 0.26.11", ] [[package]] @@ -1952,6 +2107,18 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "uuid" +version = "1.29.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bed99139f7dac9b5cfe5f17be4d66dc7079a34dee1d74bcc77217119240e267c" +dependencies = [ + "getrandom 0.4.3", + "js-sys", + "sha1_smol", + "wasm-bindgen", +] + [[package]] name = "vcpkg" version = "0.2.15" @@ -2076,6 +2243,15 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "webpki-roots" +version = "0.26.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "521bc38abb08001b01866da9f51eb7c5d647a19260e00054a8c7fd5f9e57f7a9" +dependencies = [ + "webpki-roots 1.0.8", +] + [[package]] name = "webpki-roots" version = "1.0.8" @@ -2288,6 +2464,12 @@ dependencies = [ "syn", ] +[[package]] +name = "zlib-rs" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b268e58e7c693d7c271f93ffc4ba3b380412554231c85bf61ca7af91042a4112" + [[package]] name = "zmij" version = "1.0.21" diff --git a/crates/adapter/src/http.rs b/crates/adapter/src/http.rs index 96a3024..cc77e03 100644 --- a/crates/adapter/src/http.rs +++ b/crates/adapter/src/http.rs @@ -14,11 +14,13 @@ use std::{ use axum::{ Json, Router, + extract::Request, extract::{ DefaultBodyLimit, FromRequestParts, Multipart, Query, State, ws::{Message, WebSocket, WebSocketUpgrade}, }, http::{HeaderMap, StatusCode, request::Parts}, + middleware::{self, Next}, response::{IntoResponse, Response}, routing::{get, post}, }; @@ -26,8 +28,9 @@ use inline_client::{ AuthStartRequest, AuthVerifyRequest, ChatParticipantsRequest, ClientCommandError, ClientErrorCategory, ClientRequestError, ClientStatus, ConnectRequest, CreateDmRequest, CreateReplyThreadRequest, CreateThreadRequest, DeleteMessageRequest, DialogsRequest, - EditMessageRequest, HistoryRequest, InlineClient, ReactRequest, ReadRequest, SendTextRequest, - TypingRequest, UploadRequest, + EditMessageRequest, HistoryRequest, InlineClient, ReactRequest, ReadRequest, + SearchUsersRequest, SendTextRequest, SpaceMembersRequest, TypingRequest, UploadRequest, + UsersRequest, }; use serde::{Deserialize, Serialize}; use tokio::{ @@ -345,9 +348,8 @@ struct AdapterRequestClient(InlineClient); #[derive(Clone, Debug)] struct AdapterRequestNamespace(String); -async fn request_namespace(parts: &Parts, state: &AdapterHttpState) -> Result { - let namespace = parts - .headers +fn request_namespace(headers: &HeaderMap, state: &AdapterHttpState) -> Result { + let namespace = headers .get(SESSION_NAMESPACE_HEADER) .and_then(|value| value.to_str().ok()) .map(str::trim) @@ -373,7 +375,7 @@ impl FromRequestParts for AdapterRequestClient { parts: &mut Parts, state: &AdapterHttpState, ) -> Result { - let namespace = request_namespace(parts, state).await?; + let namespace = request_namespace(&parts.headers, state)?; state .client_for_namespace(&namespace) .await @@ -389,7 +391,7 @@ impl FromRequestParts for AdapterRequestNamespace { parts: &mut Parts, state: &AdapterHttpState, ) -> Result { - let namespace = request_namespace(parts, state).await?; + let namespace = request_namespace(&parts.headers, state)?; state .client_for_namespace(&namespace) .await @@ -418,6 +420,13 @@ pub fn sidecar_router_with_state(state: AdapterHttpState) -> Router { .route("/rpc/dialogs", post(rpc_dialogs)) .route("/rpc/state/dialogs", post(rpc_cached_dialogs)) .route("/rpc/state/account", get(rpc_account_state)) + .route( + "/rpc/state/account/refresh", + post(rpc_refresh_account_state), + ) + .route("/rpc/users", post(rpc_users)) + .route("/rpc/users/search", post(rpc_search_users)) + .route("/rpc/spaces/members", post(rpc_space_members)) .route("/rpc/state/chat", post(rpc_chat_state)) .route("/rpc/state/chat/history", post(rpc_cached_history)) .route("/rpc/history", post(rpc_history)) @@ -450,9 +459,36 @@ pub fn sidecar_router_with_state(state: AdapterHttpState) -> Router { .route("/rpc/events/ack", post(rpc_events_ack)) .route("/rpc/upload", post(rpc_upload)) .layer(DefaultBodyLimit::max(SIDECAR_UPLOAD_BODY_LIMIT_BYTES)) + .layer(middleware::from_fn(require_rpc_protocol)) .with_state(state) } +// Bare routes lack the versioned command envelope, so require an explicit +// version before namespace selection, body decoding, or native client work. +async fn require_rpc_protocol(request: Request, next: Next) -> Response { + if request.uri().path().starts_with("/rpc/") { + let version = request + .headers() + .get("x-inline-protocol-version") + .and_then(|value| value.to_str().ok()) + .and_then(|value| value.parse::().ok()); + if version != Some(PROTOCOL_VERSION) { + return ( + StatusCode::UPGRADE_REQUIRED, + Json(SidecarResponse::error( + SidecarRequestId::try_new("http-protocol").expect("static request ID"), + SidecarError::new( + ClientErrorCategory::ProtocolMismatch, + format!("x-inline-protocol-version must be {PROTOCOL_VERSION}"), + ), + )), + ) + .into_response(); + } + } + next.run(request).await +} + /// Serves the localhost adapter router on an existing TCP listener. pub async fn serve_adapter_http( listener: TcpListener, @@ -541,10 +577,23 @@ async fn status( } async fn rpc( - AdapterRequestClient(client): AdapterRequestClient, State(state): State, + headers: HeaderMap, Json(request): Json, ) -> Response { + // Resolve the envelope before lazily creating/resuming an account runtime. + // An unsupported command version must never begin native session work. + if request.protocol_version != PROTOCOL_VERSION { + return protocol_mismatch_response(request.id, request.protocol_version); + } + let namespace = match request_namespace(&headers, &state) { + Ok(namespace) => namespace, + Err(response) => return response, + }; + let client = match state.client_for_namespace(&namespace).await { + Ok(client) => client, + Err(error) => return (StatusCode::BAD_REQUEST, error).into_response(), + }; dispatch_request(state, client, request).await } @@ -617,6 +666,37 @@ async fn rpc_account_state( dispatch_command(state, client, SidecarCommand::AccountState).await } +async fn rpc_refresh_account_state( + State(state): State, + AdapterRequestClient(client): AdapterRequestClient, +) -> Response { + dispatch_command(state, client, SidecarCommand::RefreshAccountState).await +} + +async fn rpc_users( + State(state): State, + AdapterRequestClient(client): AdapterRequestClient, + Json(request): Json, +) -> Response { + dispatch_command(state, client, SidecarCommand::Users(request)).await +} + +async fn rpc_search_users( + State(state): State, + AdapterRequestClient(client): AdapterRequestClient, + Json(request): Json, +) -> Response { + dispatch_command(state, client, SidecarCommand::SearchUsers(request)).await +} + +async fn rpc_space_members( + State(state): State, + AdapterRequestClient(client): AdapterRequestClient, + Json(request): Json, +) -> Response { + dispatch_command(state, client, SidecarCommand::SpaceMembers(request)).await +} + async fn rpc_chat_state( AdapterRequestClient(client): AdapterRequestClient, State(state): State, @@ -878,22 +958,7 @@ async fn dispatch_request( ) -> Response { let request_id = request.id.clone(); if request.protocol_version != PROTOCOL_VERSION { - log::warn!( - "adapter protocol mismatch: got {}, expected {}", - request.protocol_version, - PROTOCOL_VERSION - ); - return Json(SidecarResponse::error( - request_id, - SidecarError::new( - ClientErrorCategory::ProtocolMismatch, - format!( - "unsupported adapter protocol version {}; expected {}", - request.protocol_version, PROTOCOL_VERSION - ), - ), - )) - .into_response(); + return protocol_mismatch_response(request_id, request.protocol_version); } log::debug!("handling adapter command: {}", request.command.kind()); @@ -903,6 +968,18 @@ async fn dispatch_request( } } +fn protocol_mismatch_response(request_id: SidecarRequestId, version: u16) -> Response { + log::warn!("adapter protocol mismatch: got {version}, expected {PROTOCOL_VERSION}"); + Json(SidecarResponse::error( + request_id, + SidecarError::new( + ClientErrorCategory::ProtocolMismatch, + format!("unsupported adapter protocol version {version}; expected {PROTOCOL_VERSION}"), + ), + )) + .into_response() +} + async fn handle_command( client: &InlineClient, command: SidecarCommand, @@ -952,6 +1029,18 @@ async fn handle_command( .account_state() .await .map(SidecarResult::AccountState), + SidecarCommand::RefreshAccountState => client + .refresh_account_state() + .await + .map(SidecarResult::AccountState), + SidecarCommand::Users(request) => client.users(request).await.map(SidecarResult::Users), + SidecarCommand::SearchUsers(request) => { + client.search_users(request).await.map(SidecarResult::Users) + } + SidecarCommand::SpaceMembers(request) => client + .space_members(request) + .await + .map(SidecarResult::SpaceMembers), SidecarCommand::ChatState(request) => client .chat_state(request.chat_id) .await @@ -1367,6 +1456,172 @@ mod tests { serde_json::from_slice(&bytes).unwrap() } + #[tokio::test] + async fn bare_rpc_rejects_missing_old_and_future_protocol_before_native_work() { + let creations = Arc::new(AtomicUsize::new(0)); + let factory_creations = creations.clone(); + let factory: AdapterClientFactory = Arc::new(move |_| { + factory_creations.fetch_add(1, AtomicOrdering::SeqCst); + Box::pin(async { Err("unsupported versions must not create a native client".into()) }) + }); + let app = sidecar_router_with_state(AdapterHttpState::with_client_factory( + AdapterEventStore::open_in_memory("unused").unwrap(), + factory, + )); + let routes = [ + "/rpc/auth/start", + "/rpc/auth/verify", + "/rpc/resume", + "/rpc/connect", + "/rpc/logout", + "/rpc/dialogs", + "/rpc/state/dialogs", + "/rpc/state/account", + "/rpc/state/account/refresh", + "/rpc/users", + "/rpc/users/search", + "/rpc/spaces/members", + "/rpc/state/chat", + "/rpc/state/chat/history", + "/rpc/history", + "/rpc/chat/participants", + "/rpc/chat/participants/add", + "/rpc/chat/participants/remove", + "/rpc/chat/info", + "/rpc/chat/delete", + "/rpc/chat/create-dm", + "/rpc/chat/create-thread", + "/rpc/chat/create-reply-thread", + "/rpc/send", + "/rpc/send_text", + "/rpc/edit", + "/rpc/delete", + "/rpc/react", + "/rpc/read", + "/rpc/marked-unread", + "/rpc/dialog/notifications", + "/rpc/typing", + "/rpc/events/ack", + "/rpc/upload", + ]; + for route in routes { + for version in [None, Some("4"), Some("6"), Some("invalid")] { + let method = if route == "/rpc/state/account" { + "GET" + } else { + "POST" + }; + let mut request = Request::builder() + .method(method) + .uri(route) + .header(SESSION_NAMESPACE_HEADER, "account-1") + .header("content-type", "application/json"); + if let Some(version) = version { + request = request.header("x-inline-protocol-version", version); + } + let response = app + .clone() + .oneshot(request.body(Body::from("{}")).unwrap()) + .await + .unwrap(); + assert_eq!( + response.status(), + StatusCode::UPGRADE_REQUIRED, + "{route}: {version:?}" + ); + let response: SidecarResponse = json_response(response).await; + assert!( + matches!(response.outcome, SidecarOutcome::Error(error) if error.category == ClientErrorCategory::ProtocolMismatch) + ); + } + } + assert_eq!(creations.load(AtomicOrdering::SeqCst), 0); + let health = app + .oneshot( + Request::builder() + .uri("/health") + .body(Body::empty()) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!(health.status(), StatusCode::OK); + } + + #[tokio::test] + async fn generic_rpc_validates_body_version_before_creating_native_client() { + let creations = Arc::new(AtomicUsize::new(0)); + let factory_creations = creations.clone(); + let factory: AdapterClientFactory = Arc::new(move |_| { + factory_creations.fetch_add(1, AtomicOrdering::SeqCst); + Box::pin(async { Err("current version reached native client creation".into()) }) + }); + let app = sidecar_router_with_state(AdapterHttpState::with_client_factory( + AdapterEventStore::open_in_memory("unused").unwrap(), + factory, + )); + for version in [PROTOCOL_VERSION - 1, PROTOCOL_VERSION + 1] { + let request = SidecarRequest { + protocol_version: version, + id: SidecarRequestId::try_new("generic-version-test").unwrap(), + command: SidecarCommand::Status, + }; + let response = app + .clone() + .oneshot( + Request::builder() + .method("POST") + .uri("/rpc") + .header("content-type", "application/json") + .header("x-inline-protocol-version", PROTOCOL_VERSION.to_string()) + .header(SESSION_NAMESPACE_HEADER, "account-1") + .body(Body::from(serde_json::to_vec(&request).unwrap())) + .unwrap(), + ) + .await + .unwrap(); + let response: SidecarResponse = json_response(response).await; + assert!( + matches!(response.outcome, SidecarOutcome::Error(error) if error.category == ClientErrorCategory::ProtocolMismatch) + ); + } + let response = app + .clone() + .oneshot( + Request::builder() + .method("POST") + .uri("/rpc") + .header("content-type", "application/json") + .header(SESSION_NAMESPACE_HEADER, "account-1") + .body(Body::from( + r#"{"id":"missing-version","command":{"type":"status"}}"#, + )) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY); + assert_eq!(creations.load(AtomicOrdering::SeqCst), 0); + let request = SidecarRequest::current( + SidecarRequestId::try_new("current-version").unwrap(), + SidecarCommand::Status, + ); + let response = app + .oneshot( + Request::builder() + .method("POST") + .uri("/rpc") + .header("content-type", "application/json") + .header(SESSION_NAMESPACE_HEADER, "account-1") + .body(Body::from(serde_json::to_vec(&request).unwrap())) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!(response.status(), StatusCode::BAD_REQUEST); + assert_eq!(creations.load(AtomicOrdering::SeqCst), 1); + } + #[tokio::test] async fn health_endpoint_reports_protocol_and_status() { let client = InlineClient::builder() @@ -1378,6 +1633,7 @@ mod tests { let response = app .oneshot( Request::builder() + .header("x-inline-protocol-version", PROTOCOL_VERSION.to_string()) .uri("/health") .body(Body::empty()) .unwrap(), @@ -1454,6 +1710,7 @@ mod tests { let response = app .oneshot( Request::builder() + .header("x-inline-protocol-version", PROTOCOL_VERSION.to_string()) .method("POST") .uri("/rpc/events/ack") .header("content-type", "application/json") @@ -1488,6 +1745,7 @@ mod tests { let response = app .oneshot( Request::builder() + .header("x-inline-protocol-version", PROTOCOL_VERSION.to_string()) .method("POST") .uri("/rpc/events/ack") .header(SESSION_NAMESPACE_HEADER, "test") @@ -1524,6 +1782,7 @@ mod tests { let response = app .oneshot( Request::builder() + .header("x-inline-protocol-version", PROTOCOL_VERSION.to_string()) .uri("/status") .body(Body::empty()) .unwrap(), @@ -1579,6 +1838,7 @@ mod tests { .clone() .oneshot( Request::builder() + .header("x-inline-protocol-version", PROTOCOL_VERSION.to_string()) .uri("/status") .header(SESSION_NAMESPACE_HEADER, namespace) .body(Body::empty()) @@ -1600,6 +1860,7 @@ mod tests { let missing = app .oneshot( Request::builder() + .header("x-inline-protocol-version", PROTOCOL_VERSION.to_string()) .uri("/status") .body(Body::empty()) .unwrap(), @@ -1626,6 +1887,7 @@ mod tests { let response = app .oneshot( Request::builder() + .header("x-inline-protocol-version", PROTOCOL_VERSION.to_string()) .uri("/status") .header(SESSION_NAMESPACE_HEADER, "account-1") .body(Body::empty()) @@ -1669,6 +1931,7 @@ mod tests { .clone() .oneshot( Request::builder() + .header("x-inline-protocol-version", PROTOCOL_VERSION.to_string()) .uri("/status") .header(SESSION_NAMESPACE_HEADER, "account-1") .body(Body::empty()) @@ -1682,6 +1945,7 @@ mod tests { .clone() .oneshot( Request::builder() + .header("x-inline-protocol-version", PROTOCOL_VERSION.to_string()) .method("POST") .uri("/rpc/logout") .header(SESSION_NAMESPACE_HEADER, "account-1") @@ -1695,6 +1959,7 @@ mod tests { let recreated = app .oneshot( Request::builder() + .header("x-inline-protocol-version", PROTOCOL_VERSION.to_string()) .uri("/status") .header(SESSION_NAMESPACE_HEADER, "account-1") .body(Body::empty()) @@ -1717,6 +1982,7 @@ mod tests { let response = app .oneshot( Request::builder() + .header("x-inline-protocol-version", PROTOCOL_VERSION.to_string()) .method("POST") .uri("/rpc/resume") .body(Body::empty()) @@ -1748,6 +2014,7 @@ mod tests { app.clone() .oneshot( Request::builder() + .header("x-inline-protocol-version", PROTOCOL_VERSION.to_string()) .method("POST") .uri("/rpc/connect") .header("content-type", "application/json") @@ -1767,6 +2034,7 @@ mod tests { let response = app .oneshot( Request::builder() + .header("x-inline-protocol-version", PROTOCOL_VERSION.to_string()) .method("POST") .uri("/rpc/send") .header("content-type", "application/json") @@ -1791,6 +2059,226 @@ mod tests { } } + #[tokio::test] + async fn http_send_edit_and_multipart_caption_preserve_explicit_utf16_entities() { + let client = InlineClient::builder().build().spawn(); + client + .connect(ConnectRequest::new(AuthCredential::AccessToken { + token: AuthToken::try_new("test-token").unwrap(), + })) + .await + .unwrap(); + let app = sidecar_router(client.clone()); + let entity = inline_client::MessageEntityRecord { + kind: "TYPE_BOLD".into(), + offset: 3, + length: 4, + user_id: None, + agent_id: None, + group_id: None, + chat_id: None, + value: None, + }; + let mut send = SendTextRequest::new( + PeerRef::Chat { + chat_id: InlineId::new(7), + }, + "😀 text", + ); + send.entities = vec![entity.clone()]; + let edit = EditMessageRequest { + chat_id: InlineId::new(7), + message_id: InlineId::new(1), + text: "😀 edit".into(), + entities: vec![entity.clone()], + external_id: None, + parse_markdown: false, + }; + for (route, body) in [ + ("/rpc/send", serde_json::to_vec(&send).unwrap()), + ("/rpc/edit", serde_json::to_vec(&edit).unwrap()), + ] { + let response = app + .clone() + .oneshot( + Request::builder() + .method("POST") + .uri(route) + .header("x-inline-protocol-version", PROTOCOL_VERSION.to_string()) + .header("content-type", "application/json") + .body(Body::from(body)) + .unwrap(), + ) + .await + .unwrap(); + let response: SidecarResponse = json_response(response).await; + assert!( + matches!(response.outcome, SidecarOutcome::Ok(_)), + "{route}: {:?}", + response.outcome + ); + } + let upload = UploadRequest { + peer: send.peer, + kind: inline_client::MediaKind::Document, + file_name: Some("test.txt".into()), + mime_type: Some("text/plain".into()), + size_bytes: Some(4), + caption: Some("😀 file".into()), + entities: vec![entity.clone()], + width: None, + height: None, + duration_ms: None, + external_id: None, + random_id: None, + reply_to_message_id: None, + }; + let metadata = serde_json::to_string(&upload).unwrap(); + let multipart = format!( + "--boundary\r\nContent-Disposition: form-data; name=\"metadata\"\r\nContent-Type: application/json\r\n\r\n{metadata}\r\n--boundary\r\nContent-Disposition: form-data; name=\"file\"; filename=\"test.txt\"\r\nContent-Type: text/plain\r\n\r\nfile\r\n--boundary--\r\n" + ); + let response = app + .clone() + .oneshot( + Request::builder() + .method("POST") + .uri("/rpc/upload") + .header("x-inline-protocol-version", PROTOCOL_VERSION.to_string()) + .header("content-type", "multipart/form-data; boundary=boundary") + .body(Body::from(multipart)) + .unwrap(), + ) + .await + .unwrap(); + let response: SidecarResponse = json_response(response).await; + assert!( + matches!( + response.outcome, + SidecarOutcome::Ok(SidecarResult::Message(_)) + ), + "{:?}", + response.outcome + ); + let history = client + .history(HistoryRequest { + chat_id: InlineId::new(7), + limit: Some(10), + before_message_id: None, + after_message_id: None, + }) + .await + .unwrap(); + assert_eq!(history.messages.len(), 2); + assert_eq!(history.messages[0].metadata.entities, vec![entity.clone()]); + assert_eq!( + history.messages[0].content, + inline_client::MessageContent::Text { + text: "😀 edit".into() + } + ); + assert_eq!(history.messages[1].metadata.entities, vec![entity]); + assert!( + matches!(&history.messages[1].content, inline_client::MessageContent::Media { caption: Some(caption), .. } if caption == "😀 file") + ); + let media_before = history.messages[1].content.clone(); + let clear = EditMessageRequest { + chat_id: InlineId::new(7), + message_id: InlineId::new(2), + text: String::new(), + entities: Vec::new(), + external_id: None, + parse_markdown: false, + }; + let response = app + .oneshot( + Request::builder() + .method("POST") + .uri("/rpc/edit") + .header("x-inline-protocol-version", PROTOCOL_VERSION.to_string()) + .header("content-type", "application/json") + .body(Body::from(serde_json::to_vec(&clear).unwrap())) + .unwrap(), + ) + .await + .unwrap(); + let response: SidecarResponse = json_response(response).await; + assert!(matches!(response.outcome, SidecarOutcome::Ok(_))); + let history = client + .history(HistoryRequest { + chat_id: InlineId::new(7), + limit: Some(10), + before_message_id: None, + after_message_id: None, + }) + .await + .unwrap(); + assert!(history.messages[1].metadata.entities.is_empty()); + let mut expected = media_before; + if let inline_client::MessageContent::Media { caption, .. } = &mut expected { + *caption = Some(String::new()); + } + assert_eq!( + history.messages[1].content, expected, + "clearing a caption retains the original media identity" + ); + } + + #[tokio::test] + async fn invalid_entity_http_requests_fail_before_connection_or_upload_work() { + let app = sidecar_router(InlineClient::builder().build().spawn()); + let entity = serde_json::json!({"kind":"TYPE_BOLD", "offset":1, "length":1}); + let bodies = [ + ( + "/rpc/send", + serde_json::json!({"peer":{"type":"chat","chat_id":7},"text":"😀","entities":[entity.clone()]}), + ), + ( + "/rpc/edit", + serde_json::json!({"chat_id":7,"message_id":1,"text":"😀","entities":[entity.clone()]}), + ), + ]; + for (route, body) in bodies { + let response = app + .clone() + .oneshot( + Request::builder() + .method("POST") + .uri(route) + .header("x-inline-protocol-version", PROTOCOL_VERSION.to_string()) + .header("content-type", "application/json") + .body(Body::from(serde_json::to_vec(&body).unwrap())) + .unwrap(), + ) + .await + .unwrap(); + let response: SidecarResponse = json_response(response).await; + assert!( + matches!(response.outcome, SidecarOutcome::Error(error) if error.category == ClientErrorCategory::InvalidInput), + "{route}" + ); + } + let metadata = serde_json::json!({"peer":{"type":"chat","chat_id":7},"kind":"document","caption":"😀","entities":[entity]}); + let multipart = format!( + "--boundary\r\nContent-Disposition: form-data; name=\"metadata\"\r\n\r\n{metadata}\r\n--boundary\r\nContent-Disposition: form-data; name=\"file\"; filename=\"test.txt\"\r\n\r\nfile\r\n--boundary--\r\n" + ); + let response = app + .oneshot( + Request::builder() + .method("POST") + .uri("/rpc/upload") + .header("x-inline-protocol-version", PROTOCOL_VERSION.to_string()) + .header("content-type", "multipart/form-data; boundary=boundary") + .body(Body::from(multipart)) + .unwrap(), + ) + .await + .unwrap(); + let response: SidecarResponse = json_response(response).await; + assert!( + matches!(response.outcome, SidecarOutcome::Error(error) if error.category == ClientErrorCategory::InvalidInput) + ); + } + #[tokio::test] async fn chat_participants_route_dispatches_to_native_client() { let backend = InMemoryBackend::new(); @@ -1811,6 +2299,7 @@ mod tests { app.clone() .oneshot( Request::builder() + .header("x-inline-protocol-version", PROTOCOL_VERSION.to_string()) .method("POST") .uri("/rpc/connect") .header("content-type", "application/json") @@ -1827,6 +2316,7 @@ mod tests { let response = app .oneshot( Request::builder() + .header("x-inline-protocol-version", PROTOCOL_VERSION.to_string()) .method("POST") .uri("/rpc/chat/participants") .header("content-type", "application/json") @@ -1861,6 +2351,7 @@ mod tests { app.clone() .oneshot( Request::builder() + .header("x-inline-protocol-version", PROTOCOL_VERSION.to_string()) .method("POST") .uri("/rpc/connect") .header("content-type", "application/json") @@ -1926,6 +2417,7 @@ mod tests { .clone() .oneshot( Request::builder() + .header("x-inline-protocol-version", PROTOCOL_VERSION.to_string()) .method("POST") .uri(uri) .header("content-type", "application/json") diff --git a/crates/adapter/src/protocol.rs b/crates/adapter/src/protocol.rs index 3c52570..46a6b39 100644 --- a/crates/adapter/src/protocol.rs +++ b/crates/adapter/src/protocol.rs @@ -9,14 +9,15 @@ use inline_client::{ ConnectRequest, CreateDmRequest, CreateReplyThreadRequest, CreateThreadRequest, CreatedChat, DeleteChatRequest, DeleteMessageRequest, DialogsPage, DialogsRequest, EditMessageRequest, EventReliability, HistoryPage, HistoryRequest, InlineId, MessageMutation, ReactRequest, - ReadRequest, RemoveChatParticipantRequest, SendTextRequest, SetMarkedUnreadRequest, - TypingRequest, UpdateChatInfoRequest, UpdateDialogNotificationsRequest, UploadHandle, - UploadRequest, + ReadRequest, RemoveChatParticipantRequest, SearchUsersRequest, SendTextRequest, + SetMarkedUnreadRequest, SpaceMembersPage, SpaceMembersRequest, TypingRequest, + UpdateChatInfoRequest, UpdateDialogNotificationsRequest, UploadHandle, UploadRequest, + UsersPage, UsersRequest, }; use serde::{Deserialize, Serialize}; /// Current bridge adapter command/event protocol version. -pub const PROTOCOL_VERSION: u16 = 4; +pub const PROTOCOL_VERSION: u16 = 5; /// Version metadata returned by health/status endpoints. #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -197,6 +198,14 @@ pub enum SidecarCommand { CachedDialogs(DialogsRequest), /// Fetch account-level durable reconciliation state. AccountState, + /// Refresh accessible workspace navigation from the source. + RefreshAccountState, + /// Look up public source profiles by known IDs. + Users(UsersRequest), + /// Search under the authenticated source discovery policy. + SearchUsers(SearchUsersRequest), + /// Fetch a bounded member view for an accessible workspace. + SpaceMembers(SpaceMembersRequest), /// Fetch durable reconciliation state for one chat. ChatState(ChatStateRequest), /// Fetch history for a chat. @@ -252,6 +261,10 @@ impl SidecarCommand { Self::Dialogs(_) => "dialogs", Self::CachedDialogs(_) => "cached_dialogs", Self::AccountState => "account_state", + Self::RefreshAccountState => "refresh_account_state", + Self::Users(_) => "users", + Self::SearchUsers(_) => "search_users", + Self::SpaceMembers(_) => "space_members", Self::ChatState(_) => "chat_state", Self::History(_) => "history", Self::CachedHistory(_) => "cached_history", @@ -334,6 +347,10 @@ pub enum SidecarResult { Dialogs(DialogsPage), /// Account-level durable reconciliation state. AccountState(AccountStateSnapshot), + /// Source public profile results. + Users(UsersPage), + /// Source workspace member page. + SpaceMembers(SpaceMembersPage), /// Durable reconciliation state for one chat. ChatState(Box), /// History page. @@ -503,6 +520,7 @@ mod tests { let req = SidecarRequest::current( SidecarRequestId::try_new("req-1").unwrap(), SidecarCommand::SendText(SendTextRequest { + entities: Vec::new(), peer: PeerRef::Chat { chat_id: InlineId::new(7), }, @@ -510,6 +528,8 @@ mod tests { external_id: Some(ExternalId::try_new("matrix-event", "$event").unwrap()), random_id: Some(RandomId::new(99)), reply_to_message_id: None, + parse_markdown: false, + notification_mode: Default::default(), }), ); From 677b3f4f40d81d562cac23ab07affc6500f69b38 Mon Sep 17 00:00:00 2001 From: Mo Rajabi Date: Sat, 14 Nov 2026 16:59:28 +0400 Subject: [PATCH 2/3] bridge: preserve conversation, message and delivery semantics --- .github/workflows/ci.yml | 2 +- .github/workflows/docker.yml | 2 +- Dockerfile | 1 + Dockerfile.dockerignore | 2 + README.md | 18 +- cmd/matrix-inline/main.go | 3 + docs/bridge-semantics.md | 116 +++++ docs/operations.md | 12 +- docs/smoke-test.md | 33 +- go.mod | 6 +- pkg/connector/client.go | 676 +++++++++++++------------ pkg/connector/client_test.go | 49 +- pkg/connector/commands.go | 2 +- pkg/connector/connector.go | 65 ++- pkg/connector/creation.go | 101 ++++ pkg/connector/delivery.go | 26 + pkg/connector/delivery_test.go | 346 +++++++++++++ pkg/connector/directory.go | 123 +++++ pkg/connector/identity.go | 197 +++++++ pkg/connector/identity_retry.go | 111 ++++ pkg/connector/identity_test.go | 393 ++++++++++++++ pkg/connector/matrix_media.go | 97 ++++ pkg/connector/message_fidelity.go | 192 +++++++ pkg/connector/message_fidelity_test.go | 352 +++++++++++++ pkg/connector/navigation.go | 429 ++++++++++++++++ pkg/connector/read_projection.go | 322 ++++++++++++ pkg/connector/repair.go | 107 ++++ pkg/connector/review_test.go | 392 ++++++++++++++ pkg/connector/richtext.go | 407 +++++++++++++++ pkg/sidecar/client.go | 35 ++ pkg/sidecar/contract_v5_test.go | 131 +++++ pkg/sidecar/contracts_test.go | 78 +++ pkg/sidecar/types.go | 235 +++++++-- scripts/check.sh | 12 +- scripts/e2e-local.sh | 6 +- scripts/e2efixture/main.go | 62 ++- scripts/e2efixture/main_test.go | 58 +++ scripts/smoke-local.sh | 2 +- 38 files changed, 4753 insertions(+), 448 deletions(-) create mode 100644 docs/bridge-semantics.md create mode 100644 pkg/connector/creation.go create mode 100644 pkg/connector/delivery.go create mode 100644 pkg/connector/delivery_test.go create mode 100644 pkg/connector/directory.go create mode 100644 pkg/connector/identity.go create mode 100644 pkg/connector/identity_retry.go create mode 100644 pkg/connector/identity_test.go create mode 100644 pkg/connector/matrix_media.go create mode 100644 pkg/connector/message_fidelity.go create mode 100644 pkg/connector/message_fidelity_test.go create mode 100644 pkg/connector/navigation.go create mode 100644 pkg/connector/read_projection.go create mode 100644 pkg/connector/repair.go create mode 100644 pkg/connector/review_test.go create mode 100644 pkg/connector/richtext.go create mode 100644 pkg/sidecar/contract_v5_test.go create mode 100644 pkg/sidecar/contracts_test.go create mode 100644 scripts/e2efixture/main_test.go diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7f3d5cb..360728e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,7 +18,7 @@ jobs: uses: actions/checkout@v4 with: repository: inline-chat/inline - ref: 4d39dab03a39fc661a8a00d366e4debb8c0e969c + ref: ef8ebd51ac5e5d884a11dbaaa74b2c117c47acc9 path: inline-public - name: Install system dependencies diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 225aa1f..6cf1bef 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -41,7 +41,7 @@ jobs: uses: actions/checkout@v4 with: repository: inline-chat/inline - ref: 4d39dab03a39fc661a8a00d366e4debb8c0e969c + ref: ef8ebd51ac5e5d884a11dbaaa74b2c117c47acc9 path: inline-public - name: Set build time diff --git a/Dockerfile b/Dockerfile index 5604fe3..fb92f76 100644 --- a/Dockerfile +++ b/Dockerfile @@ -40,6 +40,7 @@ ENV CARGO_HOME=/cargo \ COPY inline-public/Cargo.toml inline-public/Cargo.lock inline-public/rust-toolchain.toml ./inline-public/ COPY inline-public/cli ./inline-public/cli COPY inline-public/crates ./inline-public/crates +COPY inline-public/vendor ./inline-public/vendor COPY matrix-inline/Cargo.toml matrix-inline/Cargo.lock matrix-inline/rust-toolchain.toml ./matrix-inline/ COPY matrix-inline/crates ./matrix-inline/crates WORKDIR /build/matrix-inline diff --git a/Dockerfile.dockerignore b/Dockerfile.dockerignore index 630b8eb..ec234cf 100644 --- a/Dockerfile.dockerignore +++ b/Dockerfile.dockerignore @@ -28,3 +28,5 @@ inline-public/cli/target/ inline-public/cli/target/** !inline-public/crates/ !inline-public/crates/** +!inline-public/vendor/ +!inline-public/vendor/** diff --git a/README.md b/README.md index a52f029..fedd179 100644 --- a/README.md +++ b/README.md @@ -17,30 +17,34 @@ chats into Matrix rooms, and send messages between Matrix/Beeper and Inline. - [x] Message edits - [x] Message deletes/redactions - [x] Reactions -- [x] Read receipts and marked-unread state +- [x] Conservative private read projection and Matrix-to-Inline marked-unread changes - [x] Permanent per-room mute/unmute - [x] Typing notifications - [x] Images, videos, files, audio, and voice/audio files - [x] Inline member list sync for bridged rooms - [x] Matrix ghosts with Inline display names and avatars when available - [x] Configurable bridge bot and network profile metadata -- [x] DM creation with a numeric Inline user ID -- [x] Basic group/thread creation from Matrix +- [x] DM creation by source-verified Inline ID or canonical username +- [x] Private chats and public/private workspace channel creation from Matrix - [x] Add/remove Inline thread participants from Matrix membership changes - [x] Rename Inline threads from Matrix - [x] Explicit Inline thread deletion from Beeper/Matrix - [x] Management commands for status and reconnect - [ ] New Inline account signup or invite-code onboarding - [ ] Matrix-native thread UI for Inline reply-thread chats -- [ ] Contact search for DM creation -- [ ] Formatted text, rich entities, and Matrix mention mapping +- [x] Bounded source-account people search for DM creation +- [x] Supported rich text, verified user mentions, and attachment caption edits - [ ] Pins and Inline link-preview projection - [ ] Room avatars - [ ] Room topic/description editing -- [ ] Presence and richer workspace/space representation +- [x] Account-scoped workspace Spaces and separate child-chat context +- [ ] Presence - [ ] Timed mute expiration (permanent mute is supported) - [ ] Calls +See [bridge semantics and upgrade notes](docs/bridge-semantics.md) for supported +formatting, read-state limits, workspace access and repairing existing channel rooms. + ## Requirements - An existing Inline account @@ -48,7 +52,7 @@ chats into Matrix rooms, and send messages between Matrix/Beeper and Inline. - Docker and Docker Compose, or Go 1.25 plus Rust 1.96 for native builds - A published container image from `ghcr.io/inline-chat/matrix-inline` -Source builds require a local checkout of `inline-chat/inline-public` next to this repo +Source builds require a local checkout of `inline-chat/inline` named `inline-public` next to this repo because the bridge builds against the Rust Inline client crates: ```text diff --git a/cmd/matrix-inline/main.go b/cmd/matrix-inline/main.go index b3e7647..cd41758 100644 --- a/cmd/matrix-inline/main.go +++ b/cmd/matrix-inline/main.go @@ -22,6 +22,9 @@ func main() { // mautrix environment variable, but the connector will reject non-zero // values at startup to preserve this delivery invariant. bridgev2.PortalEventBuffer = 0 + // A slow synchronous handler must never be acknowledged while it is still + // running. Transport cancellation/timeouts remain responsible for failures. + bridgev2.EventHandlingTimeoutTicks = int(^uint(0) >> 1) m := mxmain.BridgeMain{ Name: "matrix-inline", diff --git a/docs/bridge-semantics.md b/docs/bridge-semantics.md new file mode 100644 index 0000000..4116947 --- /dev/null +++ b/docs/bridge-semantics.md @@ -0,0 +1,116 @@ +# Bridge semantics and upgrade notes + +## Upgrading + +Upgrade the Go bridge and Rust adapter together: this source uses adapter +protocol **5**. Bare `/rpc/*` requests require the +`X-Inline-Protocol-Version: 5` header; `/rpc` validates its versioned envelope. +`/health` and `/status` remain usable for diagnostics. Build the native client revision pinned in the CI workflows. +Back up both databases before upgrading. Native client structure and directory +state use additive SQLite changes; preserve the backup if rolling back binaries. +The bridge retains existing Matrix room IDs and message mappings. + +For local validation without Docker, use `SKIP_DOCKER_CHECKS=1 scripts/check.sh`. +This explicitly skips Compose validation; container validation remains a separate +check. Do not interpret the skipped check as a tested container build. + +## Conversations and workspace navigation + +New channels are ordinary Matrix rooms; direct messages remain DMs. Unknown +legacy source structure is refreshed before assigning a type. A child +conversation keeps a separate room and its own history and audience. Its topic +can show parent context and a backlink when the parent is accessible and already +mapped for that login. Private child details are never written into the parent. +Hidden dialogs remain excluded by default. + +User-authored Matrix topics are preserved. Pending child context and workspace +links retry in bounded passes even when the room is idle. Workspace Spaces are +scoped to the source account. Confirmed workspace removal removes navigation; +a temporary refresh failure retains known navigation. Neither cached membership +nor a Matrix Space grants source permissions or causes independently accessible +chat history to be deleted. + +Workspace detail refreshes rotate through at most eight workspaces per pass +within a ten-second budget. Partial successes are retained, and an incomplete +pass does not erase previously proven membership. Larger accounts may need +multiple passes before every workspace has current detail. + +Existing rooms previously classified as group DMs require an explicit repair: + +```text +inline-repair-room 123 +inline-repair-room 123 apply +``` + +The first command previews. Apply updates bridge room classification in place and +saves the previous classification in bridge metadata. It preserves room/message +IDs, user topics and notification preferences. Run repairs without concurrent +administrators editing room presentation; Matrix state writes do not provide an +atomic compare-and-swap against those edits. + +## People and chat creation + +Lookup accepts positive source user IDs or canonical usernames and uses the +current login's source directory. Search is bounded; an incomplete result is not +a complete global contact list. Ambiguous usernames produce an error. Phone and +email directory searches are not supported. + +Creation types are `thread` (ordinary private chat), `private_channel`, and +`public_channel`. Workspace channel types require this login's Inline workspace +as the parent. Source permissions decide creation and participant access. +Native child creation, Matrix thread routing, and true group-DM creation are not +advertised; unsupported thread-history requests return an error. + +## Rich messages, edits and media + +Supported formatting includes bold, italic, underline, strikethrough, code, +preformatted text and safe links. Supported user mentions retain verified +identity; unknown identities and group mentions remain readable without +broadcast notifications. Unicode ranges use checked UTF-16 coordinates. + +Source revisions prevent stale edits from overwriting accepted content, including +formatting-only changes. Attachment caption edits reuse the saved Matrix media +object and encryption envelope across restart. Replacing an attachment through +an edit is unsupported. Legacy attachments require retrieval of their original +Matrix event before a caption change can be validated. + +Matrix uploads use the configured authenticated media transport with a 100 MiB +limit on declared and actual bytes while reading. Encrypted attachment hashes +are verified before plaintext is uploaded. Invalid/oversized attachments fail +explicitly; transient network errors remain retryable. An unsupported Matrix +backend without bounded transport fails explicitly rather than downloading +without a limit. Ordinary audio uses a file with its audio MIME type; explicit +voice-note metadata is retained where supported. Invalid video metadata uses a +file fallback. Dedicated sticker/GIF semantics are not advertised. + +## Delivery and read state + +The bridge waits for completed framework handling and verifies durable message +mappings. A queued/backgrounded response cannot acknowledge lossless source +work. History coverage is separate from live arrivals; a newer live message does +not skip an older history page. Failed backfill conversion returns an error so +its cursor does not silently advance. Backfill itself does not mark the room read. + +Source read intents are stored durably per login and account namespace. With the +supported double-puppet transport, the bridge sends private read/fully-read +markers only for a verified prefix of the Matrix timeline. An unread or unmapped +event is a barrier, including unknown message types. Inspection is limited to +20 pages of 500 events and a five-second deadline; unproven work remains pending +and retries during dialog scans. Large or partially mapped timelines can delay +read projection. + +Verified plaintext edits by the original sender can be traversed. Encrypted or +unverifiable edits, and redacted events whose durable mapping has been removed, +remain barriers. + +In the other direction, an explicit public Matrix receipt needs a real mapped +event, completed source history coverage through that message, and verified +Matrix timeline order. A lower source message delivered later must not be read +by an earlier Matrix receipt. Opening/viewing a room alone performs no source +read mutation. Unverifiable timelines withhold receipts rather than guessing. + +Automatic source-driven marked-unread and mute/tag writes are not performed: +they can overwrite later Matrix user choices. Private marker writes do not +clear the Matrix marked-unread flag. Explicit Matrix user unread/mute changes +still reach Inline. The pinned framework forwards public Matrix receipts but +not private user receipts; that direction remains a framework limitation. diff --git a/docs/operations.md b/docs/operations.md index 3dcecde..59a1c63 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -93,18 +93,24 @@ Adapter event cursors are `(generation, sequence)` pairs. If the adapter SQLite database is restored behind the Matrix database, the adapter rotates to a new generation and returns a structured reset. The Go bridge then performs authoritative reconciliation and atomically replaces its saved cursor before -resuming replay. Protocol v4 requires the Go bridge and Rust sidecar to be +resuming replay. Protocol v5 requires the Go bridge and Rust sidecar to be upgraded together. Dialog reconciliation uses stable chat-ID keyset pagination and persists its next cursor in login metadata. Activity reordering cannot skip rows, and large accounts resume from the last completed page after restart or RPC-budget yield. -Delivered message IDs are also persisted monotonically in portal metadata. +History coverage advances only after every message in a source-ID page has a +durable Matrix mapping. Live arrivals do not advance this frontier. On the first +upgrade, legacy greatest-ID checkpoints are rechecked from the oldest source +page; existing mappings retain their room/event IDs. Permanent media projection failures (invalid schemes, reviewed terminal 4xx, or the configured size ceiling) produce a stable Matrix unavailable notice and allow the lossless cursor to continue. Timeouts, 429, and 5xx remain retryable. +See [bridge semantics](bridge-semantics.md) for workspace navigation, room repair, +rich messages, read projection and protocol-v5 upgrade details. + ### Hidden Inline dialogs The network config defaults `hidden_dialogs` to `exclude`. Dialogs whose Inline @@ -256,5 +262,5 @@ ireconnect - Run: ```sh -curl -fsS -X POST http://127.0.0.1:29342/rpc/resume | jq . +curl -fsS -H "X-Inline-Protocol-Version: 5" -X POST http://127.0.0.1:29342/rpc/resume | jq . ``` diff --git a/docs/smoke-test.md b/docs/smoke-test.md index 5c136cc..7458428 100644 --- a/docs/smoke-test.md +++ b/docs/smoke-test.md @@ -23,7 +23,7 @@ For an installed bridge: ```sh curl -fsS http://127.0.0.1:29342/health | jq . curl -fsS http://127.0.0.1:29342/status | jq . -curl -fsS -X POST http://127.0.0.1:29342/rpc/resume | jq . +curl -fsS -H "X-Inline-Protocol-Version: 5" -X POST http://127.0.0.1:29342/rpc/resume | jq . ``` Expected adapter states: @@ -59,7 +59,13 @@ Verify both directions: 7. Matrix redactions delete or unsend the Inline message. 8. Reactions bridge in both directions. 9. Typing notifications appear where supported. -10. Opening a Matrix room marks the Inline chat read. +10. Opening a Matrix room alone does not mark Inline messages read. A public + receipt for a delivered message advances Inline only after history and + Matrix timeline order are verified. +11. Read in Inline and confirm a private Matrix marker stops before any unread + or unmapped event. A manually marked-unread Matrix room stays marked unread. +12. Send nested formatting with emoji, then make a formatting-only edit. Confirm + formatting and verified user mentions survive in both directions. ## Media @@ -68,13 +74,27 @@ Verify both directions: 3. Send an image or video from Inline and confirm it appears in Matrix. 4. Send a voice or audio file and confirm it appears as playable media or a clear unavailable notice. +5. Edit and clear an attachment caption before and after restarting the bridge. + Confirm the Matrix media URI/encryption metadata stays unchanged. +6. Confirm oversized attachments fail explicitly and a transient download error + can be retried. ## Rooms and Members 1. Confirm the Matrix member list includes current Inline members. 2. Confirm ghost display names match Inline users where available. -3. Create a DM using a numeric Inline user ID. -4. Create a basic Matrix group/thread and confirm an Inline thread is created. +3. Create a DM using a numeric Inline user ID and a canonical username. An + inaccessible or ambiguous lookup must not choose an arbitrary person. +4. Create a private chat and public/private workspace channels. Confirm the + workspace, visibility and participants match the requested source intent. +5. Confirm channels are ordinary rooms and DMs remain DMs. Preview an old + group-DM-classified channel with `inline-repair-room `, then apply + explicitly and verify the room ID, message IDs and user topic are preserved. +6. Confirm accessible workspaces appear as account-scoped Matrix Spaces. A + temporary refresh failure preserves navigation; source revocation removes + the link without deleting independently accessible history. +7. Confirm child chats keep separate rooms and audiences. Parent context appears + only inside an authorized child, without overwriting a user topic. ## Restart @@ -93,10 +113,13 @@ has not been invited to or joined to those rooms. - Login requires an existing Inline account. - Invite-code signup is not handled inside the bridge. -- DM creation currently requires a numeric Inline user ID. +- DM lookup supports source IDs and canonical usernames, not phone/email search. - Inline reply-thread chats are represented as Matrix rooms, not Matrix-native thread UI. - Room avatars and Matrix-to-Inline topic/description editing are not yet supported. - Permanent room mute/unmute is supported; timed mute expiration is not. - Calls are not bridged. + +See [bridge semantics](bridge-semantics.md) for protocol-v5 upgrade requirements, +bounded read inspection, and the supported creation/formatting/media subset. diff --git a/go.mod b/go.mod index 563ca42..30de51d 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,10 @@ go 1.25.0 require ( github.com/coder/websocket v1.8.14 + github.com/mattn/go-sqlite3 v1.14.44 + github.com/rs/zerolog v1.35.1 go.mau.fi/util v0.9.9 + golang.org/x/net v0.55.0 gopkg.in/yaml.v3 v3.0.1 maunium.net/go/mautrix v0.28.0 ) @@ -15,10 +18,8 @@ require ( github.com/lib/pq v1.12.3 // indirect github.com/mattn/go-colorable v0.1.14 // indirect github.com/mattn/go-isatty v0.0.20 // indirect - github.com/mattn/go-sqlite3 v1.14.44 // indirect github.com/petermattis/goid v0.0.0-20260330135022-df67b199bc81 // indirect github.com/rs/xid v1.6.0 // indirect - github.com/rs/zerolog v1.35.1 // indirect github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect github.com/tidwall/gjson v1.19.0 // indirect github.com/tidwall/match v1.1.1 // indirect @@ -28,7 +29,6 @@ require ( go.mau.fi/zeroconfig v0.2.0 // indirect golang.org/x/crypto v0.52.0 // indirect golang.org/x/exp v0.0.0-20260508232706-74f9aab9d74a // indirect - golang.org/x/net v0.55.0 // indirect golang.org/x/sync v0.20.0 // indirect golang.org/x/sys v0.45.0 // indirect golang.org/x/text v0.37.0 // indirect diff --git a/pkg/connector/client.go b/pkg/connector/client.go index 5853447..36a0204 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -91,6 +91,12 @@ type InlineClient struct { hiddenDialogsDefault hiddenDialogsPolicy hiddenDialogsOverride hiddenDialogsPolicy metadataSaveMu sync.Mutex + workspaceStateMu sync.Mutex + navigationMu sync.Mutex + identityRetryCursor int64 + readProjectionMu sync.Mutex + readProjectionCursor int64 + readProjectionAttempts map[int64]time.Time runCancel context.CancelFunc wg sync.WaitGroup @@ -217,6 +223,9 @@ func (ic *InlineClient) GetUserID() networkid.UserID { } func (ic *InlineClient) GetChatInfo(ctx context.Context, portal *bridgev2.Portal) (*bridgev2.ChatInfo, error) { + if info, handled, err := ic.workspacePortalInfo(portal); handled { + return info, err + } chatID, err := chatIDFromPortal(portal) if err != nil { return nil, err @@ -227,17 +236,17 @@ func (ic *InlineClient) GetChatInfo(ctx context.Context, portal *bridgev2.Portal name = optionalString(displayName) } if isDMDialog(dialog) { - return ic.chatInfoForDialog(ctx, dialog, name), nil + return preserveExistingRoomClassification(portal, ic.chatInfoForDialog(ctx, dialog, name)), nil } } info, err := ic.chatInfoForChat(ctx, chatID, name) if err != nil { if dialog, ok := ic.cachedDialog(chatID); ok { - return ic.chatInfoForDialog(ctx, dialog, name), nil + return preserveExistingRoomClassification(portal, ic.chatInfoForDialog(ctx, dialog, name)), nil } return nil, err } - return info, nil + return preserveExistingRoomClassification(portal, info), nil } func (ic *InlineClient) GetUserInfo(ctx context.Context, ghost *bridgev2.Ghost) (*bridgev2.UserInfo, error) { @@ -253,6 +262,9 @@ func (ic *InlineClient) GetUserInfo(ctx context.Context, ghost *bridgev2.Ghost) func (ic *InlineClient) GetCapabilities(ctx context.Context, portal *bridgev2.Portal) *event.RoomFeatures { features := &event.RoomFeatures{ MaxTextLength: 5000, + Formatting: event.FormattingFeatureMap{ + event.FmtBold: event.CapLevelFullySupported, event.FmtItalic: event.CapLevelFullySupported, event.FmtUnderline: event.CapLevelFullySupported, event.FmtStrikethrough: event.CapLevelFullySupported, event.FmtInlineCode: event.CapLevelFullySupported, event.FmtCodeBlock: event.CapLevelFullySupported, event.FmtSyntaxHighlighting: event.CapLevelFullySupported, event.FmtInlineLink: event.CapLevelFullySupported, event.FmtUserLink: event.CapLevelPartialSupport, + }, File: event.FileFeatureMap{ event.MsgImage: { MimeTypes: map[string]event.CapabilitySupportLevel{ @@ -313,22 +325,7 @@ func (ic *InlineClient) GetCapabilities(ctx context.Context, portal *bridgev2.Po } func (ic *InlineClient) ResolveIdentifier(ctx context.Context, identifier string, createChat bool) (*bridgev2.ResolveIdentifierResponse, error) { - userID, err := parseInlineUserID(identifier) - if err != nil { - return nil, err - } - resp := &bridgev2.ResolveIdentifierResponse{ - UserID: makeUserID(strconv.FormatInt(userID, 10)), - UserInfo: ic.userInfoForID(userID), - } - if createChat { - chat, err := ic.createDMWithUserID(ctx, userID) - if err != nil { - return nil, err - } - resp.Chat = chat - } - return resp, nil + return ic.resolveInlineIdentifier(ctx, identifier, createChat) } func (ic *InlineClient) CreateChatWithGhost(ctx context.Context, ghost *bridgev2.Ghost) (*bridgev2.CreateChatResponse, error) { @@ -343,49 +340,20 @@ func (ic *InlineClient) CreateChatWithGhost(ctx context.Context, ghost *bridgev2 } func (ic *InlineClient) CreateGroup(ctx context.Context, params *bridgev2.GroupCreateParams) (*bridgev2.CreateChatResponse, error) { - if params == nil { - return nil, fmt.Errorf("missing Inline group create params") - } - var title *string - if params.Name != nil { - title = optionalString(params.Name.Name) - } - var description *string - if params.Topic != nil { - description = optionalString(params.Topic.Topic) - } - participants := make([]sidecar.ChatCreateParticipant, 0, len(params.Participants)) - participantIDs := make([]networkid.UserID, 0, len(params.Participants)+1) - for _, participant := range params.Participants { - userID, err := parseInlineUserID(string(participant)) - if err != nil { - return nil, fmt.Errorf("invalid Inline participant %q: %w", participant, err) - } - participants = append(participants, sidecar.ChatCreateParticipant{UserID: userID}) - participantIDs = append(participantIDs, makeUserID(strconv.FormatInt(userID, 10))) - } - chat, err := ic.Sidecar.CreateThread(ctx, sidecar.CreateThreadRequest{ - Title: title, - Description: description, - IsPublic: false, - Participants: participants, - }) - if err != nil { - return nil, fmt.Errorf("Inline sidecar create thread failed: %w", err) - } - return ic.createChatResponse(ctx, chat, ic.chatInfoForCreatedChat(ctx, chat, participantIDs)), nil + return ic.createInlineGroup(ctx, params) } func (ic *InlineClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.MatrixMessage) (*bridgev2.MatrixMessageResponse, error) { - chatID, err := strconv.ParseInt(string(msg.Portal.ID), 10, 64) + if msg == nil || msg.Content == nil { + return nil, unsupportedMatrixOperation(fmt.Errorf("missing Matrix message")) + } + chatID, err := ic.validateMessagePortal(msg.Portal) if err != nil { - return nil, bridgev2.WrapErrorInStatus(fmt.Errorf("Inline portal ID %q is not a numeric chat ID yet", msg.Portal.ID)). - WithErrorAsMessage(). - WithIsCertain(true). - WithSendNotice(true). - WithErrorReason(event.MessageStatusUnsupported) + return nil, unsupportedMatrixOperation(err) + } + if _, err := ic.validatedReply(msg.Portal, msg.ReplyTo); err != nil { + return nil, unsupportedMatrixOperation(err) } - switch effectiveMatrixMessageType(msg.Content) { case event.MsgText: return ic.handleMatrixTextMessage(ctx, msg, chatID) @@ -397,83 +365,44 @@ func (ic *InlineClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.M } func (ic *InlineClient) handleMatrixTextMessage(ctx context.Context, msg *bridgev2.MatrixMessage, chatID int64) (*bridgev2.MatrixMessageResponse, error) { - request := sidecar.SendTextRequest{ - Peer: sidecar.ChatPeer(chatID), - Text: msg.Content.Body, - ExternalID: matrixExternalID(msg), - RandomID: matrixRandomID(msg), + text, entities, err := matrixTextToInline(msg.Content, false, ic.matrixMentionResolver) + if err != nil { + return nil, unsupportedMatrixOperation(err) } - if msg.ReplyTo != nil { - _, replyToMessageID, err := parseMessageID(msg.ReplyTo.ID) - if err == nil { - request.ReplyToMessageID = &replyToMessageID - } + reply, err := ic.validatedReply(msg.Portal, msg.ReplyTo) + if err != nil { + return nil, unsupportedMatrixOperation(err) } - + request := sidecar.SendTextRequest{Peer: sidecar.ChatPeer(chatID), Text: text, Entities: entities, ExternalID: matrixExternalID(msg), RandomID: matrixRandomID(msg), ReplyToMessageID: reply} mutation, err := ic.Sidecar.SendText(ctx, request) if err != nil { - return nil, bridgev2.WrapErrorInStatus(fmt.Errorf("Inline sidecar send failed: %w", err)). - WithErrorAsMessage(). - WithIsCertain(sidecarErrorIsCertain(err)). - WithSendNotice(true). - WithErrorReason(event.MessageStatusGenericError) + return nil, bridgev2.WrapErrorInStatus(fmt.Errorf("Inline sidecar send failed: %w", err)).WithErrorAsMessage().WithIsCertain(sidecarErrorIsCertain(err)).WithSendNotice(true).WithErrorReason(event.MessageStatusGenericError) } return ic.matrixSendResponse(msg, chatID, mutation) } func (ic *InlineClient) handleMatrixMediaMessage(ctx context.Context, msg *bridgev2.MatrixMessage, chatID int64) (*bridgev2.MatrixMessageResponse, error) { - data, err := ic.UserLogin.Bridge.Bot.DownloadMedia(ctx, msg.Content.URL, msg.Content.File) + caption, entities, err := matrixTextToInline(msg.Content, true, ic.matrixMentionResolver) if err != nil { - return nil, bridgev2.WrapErrorInStatus(fmt.Errorf("failed to download Matrix media: %w", err)). - WithErrorAsMessage(). - WithIsCertain(true). - WithSendNotice(true). - WithErrorReason(event.MessageStatusUnsupported) - } - if len(data) == 0 { - return nil, bridgev2.WrapErrorInStatus(fmt.Errorf("Matrix media download returned no bytes")). - WithErrorAsMessage(). - WithIsCertain(true). - WithSendNotice(true). - WithErrorReason(event.MessageStatusUnsupported) + return nil, unsupportedMatrixOperation(err) } - if len(data) > maxInlineMediaDownload { - return nil, bridgev2.WrapErrorInStatus(fmt.Errorf("Matrix media is too large for current Inline bridge upload limit")). - WithErrorAsMessage(). - WithIsCertain(true). - WithSendNotice(true). - WithErrorReason(event.MessageStatusUnsupported) + reply, err := ic.validatedReply(msg.Portal, msg.ReplyTo) + if err != nil { + return nil, unsupportedMatrixOperation(err) } - - request := sidecar.UploadRequest{ - Peer: sidecar.ChatPeer(chatID), - Kind: sidecarUploadKindForMatrix(msg.Content), - FileName: optionalString(sanitizeMatrixFileName(msg.Content.GetFileName())), - MimeType: optionalString(matrixMediaMimeType(msg.Content)), - SizeBytes: uint64Ptr(uint64(len(data))), - Caption: optionalString(msg.Content.GetCaption()), - ExternalID: matrixExternalID(msg), - RandomID: matrixRandomID(msg), + data, err := ic.downloadMatrixMedia(ctx, msg.Content) + if err != nil { + return nil, bridgev2.WrapErrorInStatus(fmt.Errorf("failed to download Matrix media: %w", err)).WithErrorAsMessage().WithIsCertain(isPermanentMediaFailure(err)).WithSendNotice(true).WithErrorReason(event.MessageStatusGenericError) } + request := sidecar.UploadRequest{Peer: sidecar.ChatPeer(chatID), Kind: sidecarUploadKindForMatrix(msg.Content), FileName: optionalString(sanitizeMatrixFileName(msg.Content.GetFileName())), MimeType: optionalString(matrixMediaMimeType(msg.Content)), SizeBytes: uint64Ptr(uint64(len(data))), Caption: nonemptyCaption(caption), Entities: entities, ExternalID: matrixExternalID(msg), RandomID: matrixRandomID(msg), ReplyToMessageID: reply} if msg.Content.Info != nil { request.Width = uint32PtrFromInt(msg.Content.Info.Width) request.Height = uint32PtrFromInt(msg.Content.Info.Height) request.DurationMS = uint64PtrFromInt(msg.Content.Info.Duration) } - if msg.ReplyTo != nil { - _, replyToMessageID, err := parseMessageID(msg.ReplyTo.ID) - if err == nil { - request.ReplyToMessageID = &replyToMessageID - } - } - mutation, err := ic.Sidecar.Upload(ctx, request, data) if err != nil { - return nil, bridgev2.WrapErrorInStatus(fmt.Errorf("Inline sidecar media send failed: %w", err)). - WithErrorAsMessage(). - WithIsCertain(sidecarErrorIsCertain(err)). - WithSendNotice(true). - WithErrorReason(event.MessageStatusGenericError) + return nil, bridgev2.WrapErrorInStatus(fmt.Errorf("Inline sidecar media send failed: %w", err)).WithErrorAsMessage().WithIsCertain(sidecarErrorIsCertain(err)).WithSendNotice(true).WithErrorReason(event.MessageStatusGenericError) } return ic.matrixSendResponse(msg, chatID, mutation) } @@ -531,6 +460,7 @@ func (ic *InlineClient) matrixSendResponse(msg *bridgev2.MatrixMessage, chatID i return &bridgev2.MatrixMessageResponse{ DB: &database.Message{ ID: makeMessageID(chatID, *mutation.MessageID), + Metadata: &MessageMetadata{MatrixContent: cloneMatrixContent(msg.Content), MediaHandled: effectiveMatrixMessageType(msg.Content) != event.MsgText}, SenderID: ic.GetUserID(), Timestamp: time.Now(), }, @@ -539,35 +469,53 @@ func (ic *InlineClient) matrixSendResponse(msg *bridgev2.MatrixMessage, chatID i } func (ic *InlineClient) HandleMatrixEdit(ctx context.Context, msg *bridgev2.MatrixEdit) error { - if msg.Content.MsgType != event.MsgText { + if msg == nil || msg.Content == nil { + return unsupportedMatrixOperation(fmt.Errorf("missing Matrix edit")) + } + chatID, messageID, err := ic.validatedTarget(msg.Portal, msg.EditTarget) + if err != nil { + return unsupportedMatrixOperation(err) + } + metadata, _ := msg.EditTarget.Metadata.(*MessageMetadata) + original, err := ic.originalEditContent(ctx, msg, metadata) + if err != nil { + return bridgev2.WrapErrorInStatus(err).WithErrorAsMessage().WithIsCertain(false).WithSendNotice(true).WithErrorReason(event.MessageStatusGenericError) + } + caption := false + switch effectiveMatrixMessageType(msg.Content) { + case event.MsgText: + if original.URL != "" || original.File != nil { + return unsupportedMatrixOperation(fmt.Errorf("an attachment must be edited as an attachment caption")) + } + case event.MsgImage, event.MsgVideo, event.MsgAudio, event.MsgFile: + if err := validateCaptionEdit(original, msg.Content); err != nil { + return unsupportedMatrixOperation(err) + } + caption = true + default: return bridgev2.ErrUnsupportedMessageType } - chatID, messageID, err := parseMessageID(msg.EditTarget.ID) + text, entities, err := matrixTextToInline(msg.Content, caption, ic.matrixMentionResolver) if err != nil { - return bridgev2.WrapErrorInStatus(fmt.Errorf("Inline edit target is invalid: %w", err)). - WithErrorAsMessage(). - WithIsCertain(true). - WithSendNotice(true). - WithErrorReason(event.MessageStatusUnsupported) + return unsupportedMatrixOperation(err) } - if err := ic.Sidecar.EditMessage(ctx, sidecar.EditMessageRequest{ - ChatID: chatID, - MessageID: messageID, - Text: msg.Content.Body, - ExternalID: matrixEventExternalID(msg.Event), - }); err != nil { - return bridgev2.WrapErrorInStatus(fmt.Errorf("Inline sidecar edit failed: %w", err)). - WithErrorAsMessage(). - WithIsCertain(true). - WithSendNotice(true). - WithErrorReason(event.MessageStatusGenericError) + if err := ic.Sidecar.EditMessage(ctx, sidecar.EditMessageRequest{ChatID: chatID, MessageID: messageID, Text: text, Entities: entities, ExternalID: matrixEventExternalID(msg.Event)}); err != nil { + return bridgev2.WrapErrorInStatus(fmt.Errorf("Inline sidecar edit failed: %w", err)).WithErrorAsMessage().WithIsCertain(sidecarErrorIsCertain(err)).WithSendNotice(true).WithErrorReason(event.MessageStatusGenericError) } + if metadata == nil { + metadata = &MessageMetadata{} + msg.EditTarget.Metadata = metadata + } + metadata.MatrixContent = cloneMatrixContent(msg.Content) msg.EditTarget.EditCount++ return nil } func (ic *InlineClient) HandleMatrixMessageRemove(ctx context.Context, msg *bridgev2.MatrixMessageRemove) error { - chatID, messageID, err := parseMessageID(msg.TargetMessage.ID) + if msg == nil { + return unsupportedMatrixOperation(errors.New("missing Inline redaction target")) + } + chatID, messageID, err := ic.validatedTarget(msg.Portal, msg.TargetMessage) if err != nil { return bridgev2.WrapErrorInStatus(fmt.Errorf("Inline redaction target is invalid: %w", err)). WithErrorAsMessage(). @@ -607,7 +555,10 @@ func (ic *InlineClient) PreHandleMatrixReaction(ctx context.Context, msg *bridge } func (ic *InlineClient) HandleMatrixReaction(ctx context.Context, msg *bridgev2.MatrixReaction) (*database.Reaction, error) { - chatID, messageID, err := parseMessageID(msg.TargetMessage.ID) + if msg == nil || msg.Content == nil { + return nil, unsupportedMatrixOperation(errors.New("missing Inline reaction target")) + } + chatID, messageID, err := ic.validatedTarget(msg.Portal, msg.TargetMessage) if err != nil { return nil, bridgev2.WrapErrorInStatus(fmt.Errorf("Inline reaction target is invalid: %w", err)). WithErrorAsMessage(). @@ -633,7 +584,10 @@ func (ic *InlineClient) HandleMatrixReaction(ctx context.Context, msg *bridgev2. } func (ic *InlineClient) HandleMatrixReactionRemove(ctx context.Context, msg *bridgev2.MatrixReactionRemove) error { - chatID, messageID, err := parseMessageID(msg.TargetReaction.MessageID) + if msg == nil || msg.TargetReaction == nil { + return unsupportedMatrixOperation(errors.New("missing Inline reaction removal target")) + } + chatID, messageID, err := ic.validatedTarget(msg.Portal, &database.Message{ID: msg.TargetReaction.MessageID, Room: msg.TargetReaction.Room}) if err != nil { return bridgev2.WrapErrorInStatus(fmt.Errorf("Inline reaction remove target is invalid: %w", err)). WithErrorAsMessage(). @@ -669,21 +623,20 @@ func (ic *InlineClient) HandleMatrixReactionRemove(ctx context.Context, msg *bri } func (ic *InlineClient) HandleMatrixReadReceipt(ctx context.Context, msg *bridgev2.MatrixReadReceipt) error { - chatID, err := strconv.ParseInt(string(msg.Portal.ID), 10, 64) + if msg == nil { + return errors.New("missing Inline read receipt") + } + if msg.ExactMessage == nil { + return nil + } // An unknown receipt must not mean read everything. + chatID, messageID, err := ic.validatedTarget(msg.Portal, msg.ExactMessage) if err != nil { - return fmt.Errorf("Inline portal ID %q is not a numeric chat ID yet: %w", msg.Portal.ID, err) + return fmt.Errorf("invalid Inline read receipt target: %w", err) } - var maxMessageID *int64 - if msg.ExactMessage != nil { - _, parsedMessageID, err := parseMessageID(msg.ExactMessage.ID) - if err == nil { - maxMessageID = &parsedMessageID - } + if err := ic.verifySourceReadReceipt(ctx, msg, chatID, messageID); err != nil { + return err } - if err := ic.Sidecar.Read(ctx, sidecar.ReadRequest{ - ChatID: chatID, - MaxMessageID: maxMessageID, - }); err != nil { + if err := ic.Sidecar.Read(ctx, sidecar.ReadRequest{ChatID: chatID, MaxMessageID: &messageID}); err != nil { return fmt.Errorf("Inline sidecar read failed: %w", err) } return nil @@ -801,17 +754,10 @@ func (ic *InlineClient) HandleMatrixDeleteChat(ctx context.Context, msg *bridgev return nil } -func (ic *InlineClient) HandleMatrixViewingChat(ctx context.Context, msg *bridgev2.MatrixViewingChat) error { - if msg.Portal == nil { - return nil - } - chatID, err := chatIDFromPortal(msg.Portal) - if err != nil { - return err - } - if err := ic.Sidecar.Read(ctx, sidecar.ReadRequest{ChatID: chatID}); err != nil { - return fmt.Errorf("Inline sidecar read failed: %w", err) - } +func (ic *InlineClient) HandleMatrixViewingChat(_ context.Context, _ *bridgev2.MatrixViewingChat) error { + // Opening a room does not prove that any particular message was displayed. + // The source may contain newer messages or undelivered history, so only an + // explicit receipt with a verified message mapping may advance source reads. return nil } @@ -850,10 +796,19 @@ func (ic *InlineClient) startSidecarLoops(ctx context.Context) { func (ic *InlineClient) syncDialogLoop(ctx context.Context) { defer ic.wg.Done() var lastLiveRefresh time.Time + var lastWorkspaceRefresh time.Time for { recoveringUpgrade := ic.needsBridgeStateRecovery() refreshLive := recoveringUpgrade || lastLiveRefresh.IsZero() || time.Since(lastLiveRefresh) >= dialogLiveRefreshInterval + if lastWorkspaceRefresh.IsZero() || time.Since(lastWorkspaceRefresh) >= dialogLiveRefreshInterval { + if navErr := ic.refreshWorkspaceNavigation(ctx); navErr != nil { + ic.UserLogin.Bridge.Log.Warn().Err(navErr).Msg("Inline workspace refresh failed; keeping known navigation") + } else { + lastWorkspaceRefresh = time.Now() + } + } + ic.retryIdentityProjection(ctx) var err error if recoveringUpgrade { err = ic.recoverBridgeState(ctx) @@ -1132,6 +1087,11 @@ func (ic *InlineClient) syncDialogs(ctx context.Context, refreshLive bool) error pages := 0 dialogsSeen := 0 resyncsQueued := 0 + readRetries := 4 + ic.readProjectionMu.Lock() + readAfter := ic.readProjectionCursor + ic.readProjectionMu.Unlock() + readVisited := false for { if remainingRPCs <= 0 { @@ -1159,6 +1119,14 @@ func (ic *InlineClient) syncDialogs(ctx context.Context, refreshLive bool) error return err } changed := ic.rememberDialog(dialog) + if ic.shouldProjectDialog(dialog) && readRetries > 0 && dialog.ChatID > readAfter { + ic.retryReadProjection(ctx, dialog.ChatID) + ic.readProjectionMu.Lock() + ic.readProjectionCursor = dialog.ChatID + ic.readProjectionMu.Unlock() + readVisited = true + readRetries-- + } if !ic.shouldProjectDialog(dialog) { continue } @@ -1192,6 +1160,11 @@ func (ic *InlineClient) syncDialogs(ctx context.Context, refreshLive bool) error return err } } + if !readVisited { + ic.readProjectionMu.Lock() + ic.readProjectionCursor = 0 + ic.readProjectionMu.Unlock() + } err := ic.syncDeferredDialogWork(ctx, deferredWork, remainingRPCs) if ic.UserLogin != nil && ic.UserLogin.Bridge != nil { ic.UserLogin.Bridge.Log.Debug(). @@ -1289,28 +1262,39 @@ func (ic *InlineClient) syncDialogDetails(ctx context.Context, dialog sidecar.Di if err != nil { return err } - ic.UserLogin.Bridge.QueueRemoteEvent(ic.UserLogin, &simplevent.ChatResync{ + result := ic.UserLogin.Bridge.QueueRemoteEvent(ic.UserLogin, &simplevent.ChatResync{ EventMeta: simplevent.EventMeta{ Type: bridgev2.RemoteEventChatResync, PortalKey: ic.portalKeyForChat(dialog.ChatID), Timestamp: time.Now(), CreatePortal: true, }, - ChatInfo: info, + ChatInfo: info, + GetChatInfoFunc: guardedChatInfo(info), }) - return nil + return eventHandlingResultError("dialog details", result) } func (ic *InlineClient) syncRecentHistory(ctx context.Context, dialog sidecar.DialogRecord) error { if !ic.shouldProjectDialog(dialog) || !ic.needsHistoryDelivery(dialog) { return nil } - request := historyRequestForDelivery(dialog, ic.historyCheckpoint(dialog.ChatID), startupHistoryLimit) + frontier := ic.historyCheckpoint(dialog.ChatID) + request := historyRequestForDelivery(dialog, frontier, startupHistoryLimit) page, err := ic.Sidecar.History(ctx, request) if err != nil { return err } ic.cacheUsers(page.Users) + pageMax, err := cachedHistoryPageMax(page.Messages, frontier) + if err != nil { + return err + } + for _, msg := range page.Messages { + if msg.ChatID != dialog.ChatID { + return errors.New("history returned a foreign chat") + } + } sort.SliceStable(page.Messages, func(i, j int) bool { left, right := page.Messages[i], page.Messages[j] @@ -1328,6 +1312,19 @@ func (ic *InlineClient) syncRecentHistory(ctx context.Context, dialog sidecar.Di return err } } + if len(page.Messages) == 0 { + if page.HasMore { + return errors.New("history returned an empty nonterminal page") + } + // A terminal empty source page can follow a deleted latest message. + pageMax = max(frontier, lastMessageIDValue(dialog)) + } + if pageMax > frontier { + if err := ic.persistHistoryCheckpoint(ctx, dialog.ChatID, pageMax); err != nil { + return err + } + ic.rememberHistoryDelivered(dialog.ChatID, pageMax) + } return nil } @@ -1336,9 +1333,8 @@ func historyRequestForDelivery(dialog sidecar.DialogRecord, checkpoint int64, li ChatID: dialog.ChatID, Limit: &limit, } - if checkpoint > 0 { - request.AfterMessageID = &checkpoint - } + // An explicit zero requests the oldest source page, not the latest window. + request.AfterMessageID = &checkpoint return request } @@ -1395,22 +1391,15 @@ func (ic *InlineClient) loadHistoryCheckpoint(ctx context.Context, chatID int64) if err != nil { return 0, fmt.Errorf("load Inline portal history checkpoint: %w", err) } - if metadata, ok := portal.Metadata.(*PortalMetadata); ok && metadata != nil && metadata.HistoryCheckpoint > 0 { + if metadata, ok := portal.Metadata.(*PortalMetadata); ok && metadata != nil && metadata.HistoryCoverageVersion == 1 && metadata.HistoryCheckpoint > 0 { checkpoint = metadata.HistoryCheckpoint ic.rememberHistoryDelivered(chatID, checkpoint) return checkpoint, nil } - messages, err := ic.UserLogin.Bridge.DB.Message.GetLastNInPortal(ctx, ic.portalKeyForChat(chatID), 16) - if err != nil { - return 0, err - } - checkpoint = maxInlineMessageIDForChat(messages, chatID) - if checkpoint > 0 { - if err := ic.persistHistoryCheckpoint(ctx, chatID, checkpoint); err != nil { - return 0, err - } - } + // Legacy checkpoints tracked the greatest observed ID, not contiguous history. + // Rewalk from zero once; existing Matrix mappings make this idempotent. + checkpoint = 0 ic.mu.Lock() if ic.history == nil { @@ -1509,7 +1498,7 @@ func rateLimitRetryDelay(err error, fallback time.Duration) time.Duration { func (ic *InlineClient) FetchMessages(ctx context.Context, params bridgev2.FetchMessagesParams) (*bridgev2.FetchMessagesResponse, error) { if params.ThreadRoot != "" { - return &bridgev2.FetchMessagesResponse{HasMore: false}, nil + return nil, errors.New("Inline child chats are separate rooms; Matrix thread history is unsupported") } request, err := historyRequestForBackfill(params) @@ -1522,14 +1511,20 @@ func (ic *InlineClient) FetchMessages(ctx context.Context, params bridgev2.Fetch } ic.cacheUsers(page.Users) - messages, cursor := ic.backfillMessagesFromHistory(ctx, params.Portal, page.Messages) + messages, cursor, err := ic.backfillMessagesFromHistory(ctx, params.Portal, page.Messages) + if err != nil { + return nil, err + } + if page.HasMore && cursor == "" { + return nil, errors.New("Inline backfill returned an empty nonterminal page") + } hasMore := page.HasMore && cursor != "" return &bridgev2.FetchMessagesResponse{ Messages: messages, Cursor: cursor, HasMore: hasMore, Forward: params.Forward, - MarkRead: true, + MarkRead: false, }, nil } @@ -1591,13 +1586,22 @@ func backfillLimit(count int) *uint32 { return &limit } -func (ic *InlineClient) backfillMessagesFromHistory(ctx context.Context, portal *bridgev2.Portal, records []sidecar.MessageRecord) ([]*bridgev2.BackfillMessage, networkid.PaginationCursor) { +func (ic *InlineClient) backfillMessagesFromHistory(ctx context.Context, portal *bridgev2.Portal, records []sidecar.MessageRecord) ([]*bridgev2.BackfillMessage, networkid.PaginationCursor, error) { var cursor networkid.PaginationCursor var cursorChatID, minimumMessageID int64 + seen := make(map[networkid.MessageID]struct{}, len(records)) for _, record := range records { if record.ChatID <= 0 || record.MessageID <= 0 { - continue + return nil, "", errors.New("invalid Inline message identity in backfill") } + if (portal != nil && portal.ID != networkid.PortalID(strconv.FormatInt(record.ChatID, 10))) || (cursorChatID != 0 && record.ChatID != cursorChatID) { + return nil, "", errors.New("backfill returned a foreign chat") + } + messageID := makeMessageID(record.ChatID, record.MessageID) + if _, duplicate := seen[messageID]; duplicate { + return nil, "", errors.New("backfill returned a duplicate message identity") + } + seen[messageID] = struct{}{} if minimumMessageID == 0 || record.MessageID < minimumMessageID { cursorChatID = record.ChatID minimumMessageID = record.MessageID @@ -1618,8 +1622,10 @@ func (ic *InlineClient) backfillMessagesFromHistory(ctx context.Context, portal backfillMessages := make([]*bridgev2.BackfillMessage, 0, len(records)) for _, record := range records { if record.ChatID <= 0 || record.MessageID <= 0 { - ic.logBackfillConversionError(errors.New("invalid Inline message identity"), record) - continue + return nil, "", errors.New("invalid Inline message identity in backfill") + } + if portal != nil && portal.ID != networkid.PortalID(strconv.FormatInt(record.ChatID, 10)) { + return nil, "", errors.New("backfill returned a foreign chat") } messageID := makeMessageID(record.ChatID, record.MessageID) @@ -1632,16 +1638,15 @@ func (ic *InlineClient) backfillMessagesFromHistory(ctx context.Context, portal var ok bool intent, ok = portal.GetIntentFor(ctx, sender, ic.UserLogin, bridgev2.RemoteEventMessage) if !ok { - continue + return nil, "", errors.New("backfill sender intent is unavailable") } } converted, err := convertInlineMessage(ctx, portal, intent, record) if err != nil { - ic.logBackfillConversionError(err, record) - continue + return nil, "", fmt.Errorf("convert backfill message %d: %w", record.MessageID, err) } if converted == nil { - continue + return nil, "", errors.New("backfill conversion returned no message") } backfillMessages = append(backfillMessages, &bridgev2.BackfillMessage{ ConvertedMessage: converted, @@ -1651,7 +1656,7 @@ func (ic *InlineClient) backfillMessagesFromHistory(ctx context.Context, portal Timestamp: inlineTimestamp(record.Timestamp), }) } - return backfillMessages, cursor + return backfillMessages, cursor, nil } func (ic *InlineClient) logBackfillConversionError(err error, record sidecar.MessageRecord) { @@ -1885,7 +1890,7 @@ func (ic *InlineClient) reconcileBridgeStateSlice(ctx context.Context, afterChat } func (ic *InlineClient) reconcileAccountDeletes(ctx context.Context) error { - accountState, err := ic.Sidecar.AccountState(ctx) + accountState, err := ic.fetchWorkspaceNavigation(ctx, false) if err != nil { return fmt.Errorf("fetch Inline account reconciliation state: %w", err) } @@ -1938,7 +1943,8 @@ func (ic *InlineClient) reconcileDialogState(ctx context.Context, dialog sidecar Timestamp: time.Now(), CreatePortal: true, }, - ChatInfo: info, + ChatInfo: info, + GetChatInfoFunc: guardedChatInfo(info), }) if err := eventHandlingResultError("reconciled chat resync", result); err != nil { return err @@ -2025,6 +2031,11 @@ func (ic *InlineClient) reconcileCachedHistory(ctx context.Context, chatID int64 if err != nil { return fmt.Errorf("validate cached Inline history for chat %d: %w", chatID, err) } + for _, message := range page.Messages { + if message.ChatID != chatID { + return errors.New("cached history returned a foreign chat") + } + } for _, message := range page.Messages { if err := eventHandlingResultError("reconciled message upsert", ic.queueInlineMessage(ctx, message)); err != nil { return err @@ -2121,40 +2132,6 @@ func (ic *InlineClient) syncReadState(ctx context.Context, chatID int64) error { return ic.queueReadState(state.ReadState) } -func (ic *InlineClient) queueReadState(state *sidecar.ReadStateRecord) error { - if state == nil { - return nil - } - sender := bridgev2.EventSender{ - Sender: makeUserID(ic.AccountID), - IsFromMe: true, - } - if state.ReadMaxID != nil && *state.ReadMaxID > 0 { - result := ic.UserLogin.Bridge.QueueRemoteEvent(ic.UserLogin, &simplevent.Receipt{ - EventMeta: simplevent.EventMeta{ - Type: bridgev2.RemoteEventReadReceipt, - PortalKey: ic.portalKeyForChat(state.ChatID), - Sender: sender, - Timestamp: time.Now(), - }, - LastTarget: makeMessageID(state.ChatID, *state.ReadMaxID), - }) - if err := eventHandlingResultError("read receipt", result); err != nil { - return err - } - } - result := ic.UserLogin.Bridge.QueueRemoteEvent(ic.UserLogin, &simplevent.MarkUnread{ - EventMeta: simplevent.EventMeta{ - Type: bridgev2.RemoteEventMarkUnread, - PortalKey: ic.portalKeyForChat(state.ChatID), - Sender: sender, - Timestamp: time.Now(), - }, - Unread: state.MarkedUnread, - }) - return eventHandlingResultError("mark unread", result) -} - func (ic *InlineClient) handleSequencedSidecarEvent(ctx context.Context, envelope *sidecar.EventEnvelope) error { if envelope == nil { return nil @@ -2455,7 +2432,7 @@ func (ic *InlineClient) syncMessageUpsert(ctx context.Context, chatID, messageID } ic.cacheUsers(page.Users) for _, msg := range page.Messages { - if msg.MessageID == messageID { + if msg.ChatID == chatID && msg.MessageID == messageID { return eventHandlingResultError("message upsert", ic.queueInlineMessage(ctx, msg)) } } @@ -2497,6 +2474,9 @@ func (ic *InlineClient) handleStatusChanged(evt *sidecar.StatusChangedEvent) { } func (ic *InlineClient) queueDialogResync(ctx context.Context, dialog sidecar.DialogRecord) bridgev2.EventHandlingResult { + if cached, ok := ic.cachedDialog(dialog.ChatID); ok { + dialog = cached + } if !ic.shouldProjectDialog(dialog) { return bridgev2.EventHandlingResultIgnored } @@ -2508,7 +2488,8 @@ func (ic *InlineClient) queueDialogResync(ctx context.Context, dialog sidecar.Di Timestamp: time.Now(), CreatePortal: true, }, - ChatInfo: chatInfo, + ChatInfo: chatInfo, + GetChatInfoFunc: guardedChatInfo(chatInfo), }) } @@ -2543,6 +2524,9 @@ func (ic *InlineClient) queueChatResyncByID(ctx context.Context, chatID int64) b } func (ic *InlineClient) queueSpaceDialogResync(ctx context.Context, spaceID int64) error { + if err := ic.refreshWorkspaceNavigation(ctx); err != nil { + return err + } ic.mu.RLock() dialogs := make([]sidecar.DialogRecord, 0) for _, dialog := range ic.dialogs { @@ -2564,6 +2548,13 @@ func (ic *InlineClient) queueSpaceDialogResync(ctx context.Context, spaceID int6 } func (ic *InlineClient) queueChatDelete(chatID int64) bridgev2.EventHandlingResult { + ic.mu.Lock() + delete(ic.dialogs, chatID) + delete(ic.details, chatID) + ic.mu.Unlock() + if err := ic.removeChatWorkspaceLinks(ic.UserLogin.Bridge.BackgroundCtx, chatID); err != nil { + return bridgev2.EventHandlingResultFailed.WithError(fmt.Errorf("remove deleted Inline chat navigation: %w", err)) + } return ic.UserLogin.Bridge.QueueRemoteEvent(ic.UserLogin, &simplevent.ChatDelete{ EventMeta: simplevent.EventMeta{ Type: bridgev2.RemoteEventChatDelete, @@ -2601,6 +2592,9 @@ func (ic *InlineClient) rememberDialog(dialog sidecar.DialogRecord) bool { ic.dialogs = make(map[int64]sidecar.DialogRecord) } previous, ok := ic.dialogs[dialog.ChatID] + if ok { + dialog = mergeDialogStructure(previous, dialog) + } ic.dialogs[dialog.ChatID] = dialog changed := !ok || !sameDialogRecord(previous, dialog) if !ok || !int64PtrEqual(previous.PeerUserID, dialog.PeerUserID) { @@ -2617,7 +2611,7 @@ func (ic *InlineClient) cachedDialog(chatID int64) (sidecar.DialogRecord, bool) } func sameDialogRecord(left, right sidecar.DialogRecord) bool { - return left.ChatID == right.ChatID && + return sameDialogStructure(left, right) && left.ChatID == right.ChatID && int64PtrEqual(left.PeerUserID, right.PeerUserID) && stringPtrEqual(left.Title, right.Title) && int64PtrEqual(left.LastMessageID, right.LastMessageID) && @@ -2631,7 +2625,7 @@ func boolPtrEqual(left, right *bool) bool { } func isDMDialog(dialog sidecar.DialogRecord) bool { - return dialog.PeerUserID != nil && *dialog.PeerUserID > 0 + return dialog.Kind != "chat" && dialog.PeerUserID != nil && *dialog.PeerUserID > 0 } func (ic *InlineClient) needsDialogDetailsSync(chatID int64) bool { @@ -2673,38 +2667,59 @@ func (ic *InlineClient) cachedUser(userID int64) (sidecar.UserRecord, bool) { } func (ic *InlineClient) chatInfoForChat(ctx context.Context, chatID int64, name *string) (*bridgev2.ChatInfo, error) { - if dialog, ok := ic.cachedDialog(chatID); ok && isDMDialog(dialog) { + dialog, known := ic.cachedDialog(chatID) + if known && isDMDialog(dialog) { return ic.chatInfoForDialog(ctx, dialog, name), nil } + if !known || dialog.Kind == "" || dialog.Kind == "unknown" { + state, err := ic.Sidecar.ChatState(ctx, sidecar.ChatStateRequest{ChatID: chatID}) + if err != nil { + return nil, fmt.Errorf("failed to fetch Inline conversation identity: %w", err) + } + if state.Deleted { + return nil, fmt.Errorf("Inline chat %d is no longer accessible", chatID) + } + if state.Dialog != nil && state.Dialog.ChatID == chatID { + ic.rememberDialog(*state.Dialog) + dialog, known = ic.cachedDialog(chatID) + if isDMDialog(dialog) { + return ic.chatInfoForDialog(ctx, dialog, name), nil + } + } + } page, err := ic.Sidecar.ChatParticipants(ctx, sidecar.ChatParticipantsRequest{ChatID: chatID}) if err != nil { return nil, fmt.Errorf("failed to fetch Inline chat participants: %w", err) } ic.cacheUsers(page.Users) - roomType := database.RoomTypeGroupDM - return &bridgev2.ChatInfo{ - Type: &roomType, - Name: name, - Members: ic.chatMemberListFromParticipants(ctx, page), - CanBackfill: true, - }, nil + if !known { + dialog = sidecar.DialogRecord{ChatID: chatID} + } + info := ic.chatInfoForDialog(ctx, dialog, name) + info.Members = ic.chatMemberListFromParticipants(ctx, page) + return info, nil } func (ic *InlineClient) chatInfoForDialog(ctx context.Context, dialog sidecar.DialogRecord, name *string) *bridgev2.ChatInfo { - info := &bridgev2.ChatInfo{ - Name: name, - CanBackfill: true, - } - if dialog.PeerUserID != nil && *dialog.PeerUserID > 0 { + info := &bridgev2.ChatInfo{Name: name, CanBackfill: true} + if isDMDialog(dialog) { roomType := database.RoomTypeDM - info.Type = &roomType - info.Name = nil + info.Type, info.Name = &roomType, nil info.Members = ic.chatMemberListForDM(ctx, *dialog.PeerUserID) } else { - roomType := database.RoomTypeGroupDM - info.Type = &roomType + if dialog.Kind == "chat" { + roomType := database.RoomTypeDefault + info.Type = &roomType + } info.Members = ic.chatMemberListForKnownSelf(ctx) } + info.ExtraUpdates = func(updateCtx context.Context, portal *bridgev2.Portal) bool { + changed := ic.updateChildContext(updateCtx, portal, dialog) + if err := ic.syncDialogWorkspaceLink(updateCtx, portal, dialog); err != nil && ic.UserLogin != nil && ic.UserLogin.Bridge != nil { + ic.UserLogin.Bridge.Log.Warn().Err(err).Int64("chat_id", dialog.ChatID).Msg("Failed to update Inline workspace navigation") + } + return changed + } return info } @@ -2792,6 +2807,7 @@ func (ic *InlineClient) createDMWithUserID(ctx context.Context, userID int64) (* if chat != nil { ic.rememberDialog(sidecar.DialogRecord{ ChatID: chat.ChatID, + Kind: "direct_message", PeerUserID: &userID, Title: chat.Title, }) @@ -2834,21 +2850,21 @@ func (ic *InlineClient) chatInfoForCreatedChat(ctx context.Context, chat *sideca } } memberList := &bridgev2.ChatMemberList{ - IsFull: true, + IsFull: false, CheckAllLogins: true, ExcludeChangesFromTimeline: true, TotalMemberCount: len(members), MemberMap: members, } - if len(members) == 2 { - for userID := range members { - if !ic.IsThisUser(ctx, userID) { - memberList.OtherUserID = userID - break - } + roomType := database.RoomTypeDefault + if chat != nil { + if dialog, ok := ic.cachedDialog(chat.ChatID); ok && isDMDialog(dialog) { + return ic.chatInfoForDialog(ctx, dialog, nil) } } + return &bridgev2.ChatInfo{ + Type: &roomType, Name: name, Members: memberList, CanBackfill: true, @@ -2878,7 +2894,7 @@ func (ic *InlineClient) userInfoForID(userID int64) *bridgev2.UserInfo { } func eventHandlingResultError(kind string, result bridgev2.EventHandlingResult) error { - if result.Success { + if result.Success && !result.Queued && result.Error == nil { return nil } if result.Error != nil { @@ -2888,15 +2904,19 @@ func eventHandlingResultError(kind string, result bridgev2.EventHandlingResult) } func (ic *InlineClient) queueInlineMessage(ctx context.Context, msg sidecar.MessageRecord) bridgev2.EventHandlingResult { + if msg.ChatID <= 0 || msg.MessageID <= 0 { + return bridgev2.EventHandlingResultFailed.WithError(errors.New("invalid Inline message identity")) + } result := ic.UserLogin.Bridge.QueueRemoteEvent(ic.UserLogin, ic.remoteMessageEvent(msg)) - if historyDeliveryAccepted(result) { - if err := ic.persistHistoryCheckpoint(ctx, msg.ChatID, msg.MessageID); err != nil { - result.Success = false - result.Error = err - return result - } - ic.rememberHistoryDelivered(msg.ChatID, msg.MessageID) + if !historyDeliveryAccepted(result) { + return result + } + // Completion must have produced a durable, same-portal mapping. In particular, + // an ignored event can mean either an existing message or a missing room. + if err := ic.verifyMessageDelivery(ctx, msg.ChatID, msg.MessageID); err != nil { + return bridgev2.EventHandlingResultFailed.WithError(err) } + // Live delivery never advances the historical coverage frontier. return result } @@ -2912,20 +2932,22 @@ func (ic *InlineClient) persistHistoryCheckpoint(ctx context.Context, chatID, me if !ok || metadata == nil { return errors.New("Inline portal metadata is unavailable") } - if messageID <= metadata.HistoryCheckpoint { + if metadata.HistoryCoverageVersion == 1 && messageID <= metadata.HistoryCheckpoint { return nil } - previous := metadata.HistoryCheckpoint + previous, previousVersion := metadata.HistoryCheckpoint, metadata.HistoryCoverageVersion metadata.HistoryCheckpoint = messageID + metadata.HistoryCoverageVersion = 1 if err := portal.Save(ctx); err != nil { metadata.HistoryCheckpoint = previous + metadata.HistoryCoverageVersion = previousVersion return fmt.Errorf("save Inline history checkpoint: %w", err) } return nil } func historyDeliveryAccepted(result bridgev2.EventHandlingResult) bool { - return result.Success + return result.Success && !result.Queued && result.Error == nil } func (ic *InlineClient) queueInlineMessageDelete(chatID, messageID int64) bridgev2.EventHandlingResult { @@ -3049,28 +3071,22 @@ func upsertExistingInlineMessage(ctx context.Context, portal *bridgev2.Portal, i if len(existing) == 0 { return bridgev2.UpsertResult{ContinueMessageHandling: true}, nil } - fingerprint := inlineMessageFingerprint(data) - if metadata, ok := existing[0].Metadata.(*MessageMetadata); ok && - metadata.InlineFingerprint == fingerprint && - (data.Content.Type != "media" || metadata.MediaHandled) { - return bridgev2.UpsertResult{}, nil + if metadata, ok := existing[0].Metadata.(*MessageMetadata); ok && metadata != nil { + if inlineRevisionIsStale(metadata, data) { + return bridgev2.UpsertResult{}, nil + } + fingerprintMatches := metadata.InlineFingerprint == inlineMessageFingerprint(data) + if len(data.Metadata.Entities) == 0 && data.Metadata.Revision == nil && data.Metadata.EditTimestamp == nil { + fingerprintMatches = fingerprintMatches || metadata.InlineFingerprint == legacyInlineMessageFingerprint(data) + } + if fingerprintMatches && (data.Content.Type != "media" || metadata.MediaHandled) { + return bridgev2.UpsertResult{}, nil + } } - return bridgev2.UpsertResult{ - SubEvents: []bridgev2.RemoteEvent{&simplevent.Message[sidecar.MessageRecord]{ - EventMeta: simplevent.EventMeta{ - Type: bridgev2.RemoteEventEdit, - PortalKey: portal.PortalKey, - Sender: bridgev2.EventSender{ - Sender: makeUserID(strconv.FormatInt(data.SenderID, 10)), - IsFromMe: data.IsOutgoing, - }, - Timestamp: inlineTimestamp(data.Timestamp), - }, - Data: data, - TargetMessage: makeMessageID(data.ChatID, data.MessageID), - ConvertEditFunc: convertInlineMessageEdit, - }}, - }, nil + return bridgev2.UpsertResult{SubEvents: []bridgev2.RemoteEvent{&simplevent.Message[sidecar.MessageRecord]{ + EventMeta: simplevent.EventMeta{Type: bridgev2.RemoteEventEdit, PortalKey: portal.PortalKey, Sender: bridgev2.EventSender{Sender: makeUserID(strconv.FormatInt(data.SenderID, 10)), IsFromMe: data.IsOutgoing}, Timestamp: inlineTimestamp(inlineEditTimestamp(data))}, + Data: data, TargetMessage: makeMessageID(data.ChatID, data.MessageID), ConvertEditFunc: convertInlineMessageEdit, + }}}, nil } func convertInlineMessage(ctx context.Context, portal *bridgev2.Portal, intent bridgev2.MatrixAPI, data sidecar.MessageRecord) (*bridgev2.ConvertedMessage, error) { @@ -3079,10 +3095,9 @@ func convertInlineMessage(ctx context.Context, portal *bridgev2.Portal, intent b return nil, err } if data.ReplyToMessageID != nil { - converted.ReplyTo = &networkid.MessageOptionalPartID{ - MessageID: makeMessageID(data.ChatID, *data.ReplyToMessageID), - } + converted.ReplyTo = &networkid.MessageOptionalPartID{MessageID: makeMessageID(data.ChatID, *data.ReplyToMessageID)} } + applyInlineRecordEntities(ctx, portal, converted, data) setInlineMessageFingerprint(converted, data) return converted, nil } @@ -3091,48 +3106,62 @@ func convertInlineMessageEdit(ctx context.Context, portal *bridgev2.Portal, inte if len(existing) == 0 { return nil, bridgev2.ErrTargetMessageNotFound } - converted, err := convertedContentFromInline(ctx, portal, intent, data.Content) - if err != nil { - return nil, err + if portal == nil || string(portal.ID) != strconv.FormatInt(data.ChatID, 10) || existing[0].Room != portal.PortalKey || existing[0].ID != makeMessageID(data.ChatID, data.MessageID) { + return nil, fmt.Errorf("Inline edit target belongs to another chat or login") + } + if metadata, ok := existing[0].Metadata.(*MessageMetadata); ok && inlineRevisionIsStale(metadata, data) { + return &bridgev2.ConvertedEdit{}, nil + } + converted := reuseInlineMedia(existing[0], data) + if converted == nil { + var err error + converted, err = convertedContentFromInline(ctx, portal, intent, data.Content) + if err != nil { + return nil, err + } } if len(converted.Parts) == 0 { return &bridgev2.ConvertedEdit{}, nil } + applyInlineRecordEntities(ctx, portal, converted, data) setInlineMessageFingerprint(converted, data) - return &bridgev2.ConvertedEdit{ - ModifiedParts: []*bridgev2.ConvertedEditPart{ - converted.Parts[0].ToEditPart(existing[0]), - }, - }, nil + return &bridgev2.ConvertedEdit{ModifiedParts: []*bridgev2.ConvertedEditPart{converted.Parts[0].ToEditPart(existing[0])}}, nil } func setInlineMessageFingerprint(converted *bridgev2.ConvertedMessage, data sidecar.MessageRecord) { if converted == nil { return } - metadata := &MessageMetadata{ - InlineFingerprint: inlineMessageFingerprint(data), - MediaHandled: data.Content.Type == "media", - } for _, part := range converted.Parts { - if part != nil { - part.DBMetadata = metadata + if part == nil { + continue } + part.DBMetadata = &MessageMetadata{InlineFingerprint: inlineMessageFingerprint(data), MediaHandled: data.Content.Type == "media", SourceRevision: data.Metadata.Revision, SourceEditTimestamp: data.Metadata.EditTimestamp, MatrixContent: cloneMatrixContent(part.Content), MediaIdentity: inlineMediaIdentity(data.Content)} } } func inlineMessageFingerprint(data sidecar.MessageRecord) string { + payload, err := json.Marshal(struct { + Legacy string `json:"legacy"` + Timestamp int64 `json:"timestamp"` + Entities []sidecar.MessageEntityRecord `json:"entities,omitempty"` + Revision *int64 `json:"revision,omitempty"` + EditTimestamp *int64 `json:"edit_timestamp,omitempty"` + }{legacyInlineMessageFingerprint(data), data.Timestamp, data.Metadata.Entities, data.Metadata.Revision, data.Metadata.EditTimestamp}) + if err != nil { + return "" + } + digest := sha256.Sum256(payload) + return fmt.Sprintf("v2:%x", digest[:]) +} + +func legacyInlineMessageFingerprint(data sidecar.MessageRecord) string { payload, err := json.Marshal(struct { SenderID int64 `json:"sender_id"` IsOutgoing bool `json:"is_outgoing"` Content sidecar.MessageContent `json:"content"` ReplyToMessageID *int64 `json:"reply_to_message_id,omitempty"` - }{ - SenderID: data.SenderID, - IsOutgoing: data.IsOutgoing, - Content: data.Content, - ReplyToMessageID: data.ReplyToMessageID, - }) + }{data.SenderID, data.IsOutgoing, data.Content, data.ReplyToMessageID}) if err != nil { return "" } @@ -3168,6 +3197,9 @@ func convertedContentFromInline(ctx context.Context, portal *bridgev2.Portal, in } func convertedInlineMedia(ctx context.Context, portal *bridgev2.Portal, intent bridgev2.MatrixAPI, content sidecar.MessageContent) (*bridgev2.ConvertedMessage, error) { + if content.SizeBytes != nil && *content.SizeBytes > maxInlineMediaDownload { + return convertedInlineMediaUnavailable(content), nil + } if strings.TrimSpace(content.URL) == "" || portal == nil || intent == nil { return convertedInlineMediaUnavailable(content), nil } @@ -3205,12 +3237,13 @@ func convertedInlineMedia(ctx context.Context, portal *bridgev2.Portal, intent b Parts: []*bridgev2.ConvertedMessagePart{{ Type: event.EventMessage, Content: &event.MessageEventContent{ - MsgType: inlineMediaMessageType(content), - Body: inlineMediaBody(content, fileName), - URL: mxc, - File: file, - FileName: fileName, - Info: info, + MsgType: inlineMediaMessageType(content), + MSC3245Voice: inlineVoiceMetadata(content), + Body: inlineMediaBody(content, fileName), + URL: mxc, + File: file, + FileName: fileName, + Info: info, }, }}, }, nil @@ -3254,6 +3287,9 @@ func downloadInlineURL(ctx context.Context, rawURL string, maxBytes int64, label return nil, err } + if resp.ContentLength > maxBytes { + return nil, permanentMediaFailure(fmt.Errorf("%s Content-Length exceeds %d byte bridge download limit", label, maxBytes)) + } data, err := io.ReadAll(io.LimitReader(resp.Body, maxBytes+1)) if err != nil { return nil, fmt.Errorf("read %s response: %w", label, err) @@ -3284,6 +3320,11 @@ func inlineMediaMessageType(content sidecar.MessageContent) event.MessageType { return event.MsgVideo case "voice": return event.MsgAudio + case "document": + if content.MimeType != nil && strings.HasPrefix(strings.ToLower(*content.MimeType), "audio/") { + return event.MsgAudio + } + return event.MsgFile default: return event.MsgFile } @@ -3329,8 +3370,8 @@ func inlineMediaFileName(content sidecar.MessageContent) string { } func inlineMediaBody(content sidecar.MessageContent, fileName string) string { - if content.Caption != nil && strings.TrimSpace(*content.Caption) != "" { - return strings.TrimSpace(*content.Caption) + if content.Caption != nil && *content.Caption != "" { + return *content.Caption } return fileName } @@ -3435,7 +3476,10 @@ func sidecarUploadKindForMatrix(content *event.MessageEventContent) string { case event.MsgVideo: return "video" case event.MsgAudio: - return "voice" + if content.MSC3245Voice != nil { + return "voice" + } + return "document" default: return "document" } diff --git a/pkg/connector/client_test.go b/pkg/connector/client_test.go index ca4c2ce..9c12ea8 100644 --- a/pkg/connector/client_test.go +++ b/pkg/connector/client_test.go @@ -183,7 +183,10 @@ func TestBackfillMessagesFromHistorySortsAndBuildsCursor(t *testing.T) { }, }} - messages, cursor := (&InlineClient{}).backfillMessagesFromHistory(context.Background(), nil, records) + messages, cursor, err := (&InlineClient{}).backfillMessagesFromHistory(context.Background(), nil, records) + if err != nil { + t.Fatal(err) + } if cursor != networkid.PaginationCursor("7/10") { t.Fatalf("cursor = %q, want 7/10", cursor) } @@ -210,7 +213,10 @@ func TestBackfillCursorUsesMinimumIDNotFirstChronologicalMessage(t *testing.T) { {ChatID: 7, MessageID: 10, SenderID: 1, Timestamp: 200, Content: sidecar.MessageContent{Type: "text", Text: "lower id"}}, } - messages, cursor := (&InlineClient{}).backfillMessagesFromHistory(context.Background(), nil, records) + messages, cursor, err := (&InlineClient{}).backfillMessagesFromHistory(context.Background(), nil, records) + if err != nil { + t.Fatal(err) + } if cursor != networkid.PaginationCursor("7/10") { t.Fatalf("cursor = %q, want minimum ID 7/10", cursor) } @@ -272,7 +278,7 @@ func TestFetchMessagesUsesSidecarHistory(t *testing.T) { if err != nil { t.Fatalf("FetchMessages() error = %v", err) } - if !resp.HasMore || !resp.MarkRead || resp.Forward { + if !resp.HasMore || resp.MarkRead || resp.Forward { t.Fatalf("response flags = has_more:%v mark_read:%v forward:%v", resp.HasMore, resp.MarkRead, resp.Forward) } if resp.Cursor != networkid.PaginationCursor("7/19") { @@ -288,6 +294,10 @@ func TestFetchMessagesUsesSidecarHistory(t *testing.T) { func TestGetChatInfoFetchesFullInlineMembers(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/rpc/state/chat" { + writeConnectorSidecarResult(t, w, "chat_state", sidecar.ChatStateSnapshot{ChatID: 7, Dialog: &sidecar.DialogRecord{ChatID: 7, Kind: sidecar.ConversationChat, StructureKnown: true}}) + return + } if r.URL.Path != "/rpc/chat/participants" { t.Fatalf("unexpected path %s", r.URL.Path) } @@ -327,8 +337,8 @@ func TestGetChatInfoFetchesFullInlineMembers(t *testing.T) { if info.Members.TotalMemberCount != 2 || len(info.Members.MemberMap) != 2 { t.Fatalf("member count = %d/%d, want 2", info.Members.TotalMemberCount, len(info.Members.MemberMap)) } - if info.Type == nil || *info.Type != database.RoomTypeGroupDM { - t.Fatalf("Type = %#v, want group DM", info.Type) + if info.Type == nil || *info.Type != database.RoomTypeDefault { + t.Fatalf("Type = %#v, want ordinary room", info.Type) } if info.Members.OtherUserID != "" { t.Fatalf("OtherUserID = %q, want empty for group chat", info.Members.OtherUserID) @@ -402,11 +412,12 @@ func TestChatInfoForDialogIncludesSelfForGroupWithoutParticipantFetch(t *testing info := ic.chatInfoForDialog(context.Background(), sidecar.DialogRecord{ ChatID: 7, + Kind: sidecar.ConversationChat, Title: &title, }, &title) - if info.Type == nil || *info.Type != database.RoomTypeGroupDM { - t.Fatalf("Type = %#v, want group DM", info.Type) + if info.Type == nil || *info.Type != database.RoomTypeDefault { + t.Fatalf("Type = %#v, want ordinary room", info.Type) } if info.Name == nil || *info.Name != title { t.Fatalf("Name = %#v, want %q", info.Name, title) @@ -473,22 +484,11 @@ func TestResolveIdentifierCreatesDM(t *testing.T) { } } -func TestHandleMatrixViewingChatMarksInlineChatRead(t *testing.T) { +func TestHandleMatrixViewingChatDoesNotMarkUndeliveredSourceHistoryRead(t *testing.T) { + calls := 0 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.URL.Path != "/rpc/read" { - t.Fatalf("unexpected path %s", r.URL.Path) - } - var request sidecar.ReadRequest - if err := json.NewDecoder(r.Body).Decode(&request); err != nil { - t.Fatalf("decode read request: %v", err) - } - if request.ChatID != 7 { - t.Fatalf("ChatID = %d, want 7", request.ChatID) - } - if request.MaxMessageID != nil { - t.Fatalf("MaxMessageID = %#v, want nil", request.MaxMessageID) - } - writeConnectorSidecarResult(t, w, "empty", struct{}{}) + calls++ + http.Error(w, "opening a room must not mutate source reads", http.StatusInternalServerError) })) defer server.Close() @@ -498,6 +498,9 @@ func TestHandleMatrixViewingChatMarksInlineChatRead(t *testing.T) { }); err != nil { t.Fatalf("HandleMatrixViewingChat() error = %v", err) } + if calls != 0 { + t.Fatalf("opening a room made %d source requests", calls) + } } func TestChatManagementCapabilitiesAreScopedToInlineThreads(t *testing.T) { @@ -836,7 +839,7 @@ func TestEffectiveMatrixMessageTypeInfersMediaFromFileMime(t *testing.T) { }{ "image": {mime: "image/png", want: event.MsgImage, kind: "photo"}, "video": {mime: "video/mp4", want: event.MsgVideo, kind: "video"}, - "audio": {mime: "audio/ogg", want: event.MsgAudio, kind: "voice"}, + "audio": {mime: "audio/ogg", want: event.MsgAudio, kind: "document"}, "file": {mime: "application/pdf", want: event.MsgFile, kind: "document"}, } for name, tt := range cases { diff --git a/pkg/connector/commands.go b/pkg/connector/commands.go index e4d55ba..2459581 100644 --- a/pkg/connector/commands.go +++ b/pkg/connector/commands.go @@ -62,7 +62,7 @@ func registerInlineCommands(bridge *bridgev2.Bridge) { bridge.Log.Warn().Msg("Bridge command processor does not support custom Inline commands") return } - processor.AddHandlers(commandInlineStatus, commandInlineReconnect, commandInlineSettings, commandInlineHiddenChats) + processor.AddHandlers(commandInlineStatus, commandInlineReconnect, commandInlineSettings, commandInlineHiddenChats, commandInlineRepairRoom) } func fnInlineStatus(ce *commands.Event) { diff --git a/pkg/connector/connector.go b/pkg/connector/connector.go index f3e9c13..3d395b1 100644 --- a/pkg/connector/connector.go +++ b/pkg/connector/connector.go @@ -13,6 +13,7 @@ import ( "maunium.net/go/mautrix/bridgev2" "maunium.net/go/mautrix/bridgev2/database" "maunium.net/go/mautrix/bridgev2/networkid" + "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/id" "github.com/inline-chat/matrix-inline/pkg/sidecar" @@ -55,26 +56,38 @@ type Config struct { } type UserLoginMetadata struct { - AccountID string `json:"account_id"` - RemoteName string `json:"remote_name,omitempty"` - SidecarURL string `json:"sidecar_url,omitempty"` - StoreNamespace string `json:"store_namespace,omitempty"` - LastSidecarSequence uint64 `json:"last_sidecar_sequence,omitempty"` - SidecarEventGeneration string `json:"sidecar_event_generation,omitempty"` - BridgeStateVersion uint32 `json:"bridge_state_version,omitempty"` - BridgeRecoveryCursor int64 `json:"bridge_recovery_cursor,omitempty"` - DialogSyncCursor string `json:"dialog_sync_cursor,omitempty"` - HiddenDialogs string `json:"hidden_dialogs,omitempty"` - SessionInvalidated bool `json:"session_invalidated,omitempty"` + AccountID string `json:"account_id"` + RemoteName string `json:"remote_name,omitempty"` + SidecarURL string `json:"sidecar_url,omitempty"` + StoreNamespace string `json:"store_namespace,omitempty"` + LastSidecarSequence uint64 `json:"last_sidecar_sequence,omitempty"` + SidecarEventGeneration string `json:"sidecar_event_generation,omitempty"` + BridgeStateVersion uint32 `json:"bridge_state_version,omitempty"` + BridgeRecoveryCursor int64 `json:"bridge_recovery_cursor,omitempty"` + DialogSyncCursor string `json:"dialog_sync_cursor,omitempty"` + HiddenDialogs string `json:"hidden_dialogs,omitempty"` + WorkspaceNavigation map[string]*WorkspaceNavigation `json:"workspace_navigation,omitempty"` + SessionInvalidated bool `json:"session_invalidated,omitempty"` } type MessageMetadata struct { - InlineFingerprint string `json:"inline_fingerprint,omitempty"` - MediaHandled bool `json:"media_handled,omitempty"` + InlineFingerprint string `json:"inline_fingerprint,omitempty"` + MediaHandled bool `json:"media_handled,omitempty"` + SourceRevision *int64 `json:"source_revision,omitempty"` + SourceEditTimestamp *int64 `json:"source_edit_timestamp,omitempty"` + MatrixContent *event.MessageEventContent `json:"matrix_content,omitempty"` + MediaIdentity string `json:"media_identity,omitempty"` } type PortalMetadata struct { - HistoryCheckpoint int64 `json:"history_checkpoint,omitempty"` + HistoryCheckpoint int64 `json:"history_checkpoint,omitempty"` + ChildTopicOwned bool `json:"child_topic_owned,omitempty"` + OwnedChildTopic string `json:"owned_child_topic,omitempty"` + UserTopic bool `json:"user_topic,omitempty"` + InlineParentChatID *int64 `json:"inline_parent_chat_id,omitempty"` + InlineParentMessageID *int64 `json:"inline_parent_message_id,omitempty"` + ClassificationRepair *RoomClassificationRepair `json:"classification_repair,omitempty"` + HistoryCoverageVersion uint32 `json:"history_coverage_version,omitempty"` } func (meta *PortalMetadata) CopyFrom(other any) { @@ -121,6 +134,9 @@ func (ic *InlineConnector) Start(ctx context.Context) error { func validatePortalEventDelivery() error { if bridgev2.PortalEventBuffer == 0 { + if bridgev2.EventHandlingTimeoutTicks != int(^uint(0)>>1) { + return fmt.Errorf("Inline delivery requires waiting for handler completion without backgrounding") + } return nil } return fmt.Errorf( @@ -140,28 +156,11 @@ func (ic *InlineConnector) GetCapabilities() *bridgev2.NetworkGeneralCapabilitie Provisioning: bridgev2.ProvisioningCapabilities{ ResolveIdentifier: bridgev2.ResolveIdentifierCapabilities{ LookupUsername: true, - Search: false, + Search: true, ContactList: false, CreateDM: true, }, - GroupCreation: map[string]bridgev2.GroupTypeCapabilities{ - "thread": { - TypeDescription: "Inline thread", - Name: bridgev2.GroupFieldCapability{ - Allowed: true, - Required: true, - MinLength: 1, - MaxLength: 256, - }, - Topic: bridgev2.GroupFieldCapability{ - Allowed: true, - MaxLength: 4096, - }, - Participants: bridgev2.GroupFieldCapability{ - Allowed: true, - }, - }, - }, + GroupCreation: inlineGroupCreationCapabilities(), }, } } diff --git a/pkg/connector/creation.go b/pkg/connector/creation.go new file mode 100644 index 0000000..0e02fe5 --- /dev/null +++ b/pkg/connector/creation.go @@ -0,0 +1,101 @@ +package connector + +import ( + "context" + "fmt" + "strconv" + "strings" + "unicode/utf8" + + "github.com/inline-chat/matrix-inline/pkg/sidecar" + "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/bridgev2/networkid" +) + +func inlineGroupCreationCapabilities() map[string]bridgev2.GroupTypeCapabilities { + capability := bridgev2.GroupTypeCapabilities{ + TypeDescription: "Private Inline chat", + Name: bridgev2.GroupFieldCapability{Allowed: true, Required: true, MinLength: 1, MaxLength: 256}, + Topic: bridgev2.GroupFieldCapability{Allowed: true, MaxLength: 4096}, + Participants: bridgev2.GroupFieldCapability{Allowed: true}, + Parent: bridgev2.GroupFieldCapability{Allowed: true}, + } + public := capability + public.TypeDescription = "Public Inline workspace channel" + public.Parent.Required = true + private := capability + private.TypeDescription = "Private Inline workspace channel" + private.Parent.Required = true + return map[string]bridgev2.GroupTypeCapabilities{"thread": capability, "private_channel": private, "public_channel": public} +} + +func (ic *InlineClient) createInlineGroup(ctx context.Context, params *bridgev2.GroupCreateParams) (*bridgev2.CreateChatResponse, error) { + request, participantIDs, err := ic.inlineGroupRequest(params) + if err != nil { + return nil, err + } + chat, err := ic.Sidecar.CreateThread(ctx, request) + if err != nil { + return nil, fmt.Errorf("Inline sidecar create chat failed: %w", err) + } + if chat == nil || chat.ChatID <= 0 { + return nil, fmt.Errorf("Inline returned no created chat") + } + ic.rememberDialog(sidecar.DialogRecord{ChatID: chat.ChatID, Kind: sidecar.ConversationChat, StructureKnown: true, Title: chat.Title, Description: request.Description, SpaceID: request.SpaceID, IsPublic: &request.IsPublic}) + return ic.createChatResponse(ctx, chat, ic.chatInfoForCreatedChat(ctx, chat, participantIDs)), nil +} + +func (ic *InlineClient) inlineGroupRequest(params *bridgev2.GroupCreateParams) (sidecar.CreateThreadRequest, []networkid.UserID, error) { + request := sidecar.CreateThreadRequest{} + if params == nil { + return request, nil, fmt.Errorf("missing Inline group create params") + } + kind := strings.TrimSpace(params.Type) + switch kind { + case "", "thread", "private_channel": + case "public_channel": + request.IsPublic = true + default: + return request, nil, fmt.Errorf("unsupported Inline creation type %q; native child conversations and group DMs cannot be created from Matrix", kind) + } + if params.Username != "" || params.Avatar != nil || params.Disappear != nil { + return request, nil, fmt.Errorf("Inline chat creation does not support usernames, avatars or disappearing timers") + } + if params.Name == nil || strings.TrimSpace(params.Name.Name) == "" || utf8.RuneCountInString(params.Name.Name) > 256 { + return request, nil, fmt.Errorf("Inline chat name must contain 1–256 characters") + } + request.Title = optionalString(strings.TrimSpace(params.Name.Name)) + if params.Topic != nil { + if utf8.RuneCountInString(params.Topic.Topic) > 4096 { + return request, nil, fmt.Errorf("Inline chat description exceeds 4096 characters") + } + request.Description = ¶ms.Topic.Topic + } + if params.Parent != nil { + spaceID, err := ic.workspaceIDFromKey(*params.Parent) + if err != nil { + return request, nil, err + } + request.SpaceID = &spaceID + } + if (kind == "public_channel" || kind == "private_channel") && request.SpaceID == nil { + return request, nil, fmt.Errorf("Inline workspace channels require an Inline workspace parent") + } + ids := make([]networkid.UserID, 0, len(params.Participants)) + seen := make(map[int64]bool) + for _, participant := range params.Participants { + userID, err := parseInlineUserID(string(participant)) + if err != nil { + return request, nil, fmt.Errorf("invalid Inline participant %q: %w", participant, err) + } + if seen[userID] { + continue + } + seen[userID] = true + request.Participants = append(request.Participants, sidecar.ChatCreateParticipant{UserID: userID}) + ids = append(ids, makeUserID(strconv.FormatInt(userID, 10))) + } + // Workspace/participant authorization remains with the source mutation; cached + // roles or a local directory are never used to grant or deny source permission. + return request, ids, nil +} diff --git a/pkg/connector/delivery.go b/pkg/connector/delivery.go new file mode 100644 index 0000000..25bc558 --- /dev/null +++ b/pkg/connector/delivery.go @@ -0,0 +1,26 @@ +package connector + +import ( + "context" + "errors" + "fmt" +) + +// verifyMessageDelivery requires a durable projection, rather than trusting +// queue admission or an ignored event for a room that has not been created. +func (ic *InlineClient) verifyMessageDelivery(ctx context.Context, chatID, messageID int64) error { + key := ic.portalKeyForChat(chatID) + parts, err := ic.UserLogin.Bridge.DB.Message.GetAllPartsByID(ctx, key.Receiver, makeMessageID(chatID, messageID)) + if err != nil { + return fmt.Errorf("read delivered message mapping: %w", err) + } + if len(parts) == 0 { + return errors.New("message delivery has no durable Matrix mapping") + } + for _, part := range parts { + if part.Room != key || part.MXID == "" || part.HasFakeMXID() { + return errors.New("message delivery has no real same-portal Matrix mapping") + } + } + return nil +} diff --git a/pkg/connector/delivery_test.go b/pkg/connector/delivery_test.go new file mode 100644 index 0000000..d13918f --- /dev/null +++ b/pkg/connector/delivery_test.go @@ -0,0 +1,346 @@ +package connector + +import ( + "context" + "database/sql" + "encoding/json" + "errors" + "fmt" + "net/http" + "net/http/httptest" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/inline-chat/matrix-inline/pkg/sidecar" + _ "github.com/mattn/go-sqlite3" + "github.com/rs/zerolog" + "go.mau.fi/util/dbutil" + "maunium.net/go/mautrix" + "maunium.net/go/mautrix/appservice" + "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/bridgev2/bridgeconfig" + "maunium.net/go/mautrix/bridgev2/database" + "maunium.net/go/mautrix/bridgev2/matrix" + "maunium.net/go/mautrix/bridgev2/networkid" + "maunium.net/go/mautrix/bridgev2/simplevent" + "maunium.net/go/mautrix/event" + "maunium.net/go/mautrix/id" +) + +func openTestBridgeDatabase(t *testing.T, path string) *database.Database { + t.Helper() + raw, err := sql.Open("sqlite3", path+"?_foreign_keys=on") + if err != nil { + t.Fatal(err) + } + raw.SetMaxOpenConns(1) + wrapped, err := dbutil.NewWithDB(raw, "sqlite3") + if err != nil { + t.Fatal(err) + } + db := database.New("inline-test", (&InlineConnector{}).GetDBMetaTypes(), wrapped) + if err := db.Upgrade(t.Context()); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { raw.Close() }) + return db +} +func testBridgeDatabase(t *testing.T) *database.Database { + return openTestBridgeDatabase(t, filepath.Join(t.TempDir(), "bridge.db")) +} +func testDeliveryClient(db *database.Database) *InlineClient { + return &InlineClient{UserLogin: &bridgev2.UserLogin{UserLogin: &database.UserLogin{ID: "42"}, Bridge: &bridgev2.Bridge{ID: db.BridgeID, DB: db, BackgroundCtx: context.Background()}}, storeNamespace: "account-42"} +} +func TestDeliveryMappingSurvivesReopenAndRejectsMissingOrForeign(t *testing.T) { + path := filepath.Join(t.TempDir(), "bridge.db") + db := openTestBridgeDatabase(t, path) + client := testDeliveryClient(db) + if err := client.verifyMessageDelivery(t.Context(), 7, 11); err == nil { + t.Fatal("unmapped delivery accepted") + } + key := client.portalKeyForChat(7) + if err := db.Portal.Insert(t.Context(), &database.Portal{PortalKey: key, MXID: "!room:example.org"}); err != nil { + t.Fatal(err) + } + if err := db.Ghost.Insert(t.Context(), &database.Ghost{ID: "1"}); err != nil { + t.Fatal(err) + } + if err := db.Message.Insert(t.Context(), &database.Message{ID: makeMessageID(7, 11), Room: key, SenderID: "1", MXID: "$message", Timestamp: time.Now(), Metadata: &MessageMetadata{}}); err != nil { + t.Fatal(err) + } + if err := client.verifyMessageDelivery(t.Context(), 7, 11); err != nil { + t.Fatal(err) + } + db.RawDB.Close() + reopened := testDeliveryClient(openTestBridgeDatabase(t, path)) + if err := reopened.verifyMessageDelivery(t.Context(), 7, 11); err != nil { + t.Fatal(err) + } + reopened.UserLogin.ID = "43" + if err := reopened.verifyMessageDelivery(t.Context(), 7, 11); err == nil { + t.Fatal("foreign login mapping accepted") + } +} +func TestReadIntentDurableAndAccountIsolated(t *testing.T) { + path := filepath.Join(t.TempDir(), "bridge.db") + db := openTestBridgeDatabase(t, path) + client := testDeliveryClient(db) + maxID := int64(99) + if err := client.saveReadIntent(t.Context(), &sidecar.ReadStateRecord{ChatID: 7, ReadMaxID: &maxID, MarkedUnread: true}); err != nil { + t.Fatal(err) + } + db.RawDB.Close() + client = testDeliveryClient(openTestBridgeDatabase(t, path)) + state, err := client.loadReadIntent(t.Context(), 7) + if err != nil || state == nil || *state.ReadMaxID != 99 || !state.MarkedUnread { + t.Fatalf("lost durable intent: %+v %v", state, err) + } + client.storeNamespace = "new-account" + if state, err := client.loadReadIntent(t.Context(), 7); err != nil || state != nil { + t.Fatalf("intent crossed namespaces: %+v %v", state, err) + } + client.UserLogin.Bridge.DB.RawDB.Close() + if err := client.saveReadIntent(t.Context(), &sidecar.ReadStateRecord{ChatID: 7}); err == nil { + t.Fatal("failed write accepted") + } +} +func TestHistoryCompletionRejectsAdmissionAndBackgrounding(t *testing.T) { + for _, res := range []bridgev2.EventHandlingResult{bridgev2.EventHandlingResultQueued, {Success: true, Queued: true, Error: bridgev2.ErrHandlerBackgrounded}, {Success: true, Error: errors.New("late failure")}} { + if historyDeliveryAccepted(res) || eventHandlingResultError("test", res) == nil { + t.Fatalf("pending result accepted: %+v", res) + } + } + request := historyRequestForDelivery(sidecar.DialogRecord{ChatID: 7}, 0, 20) + if request.AfterMessageID == nil || *request.AfterMessageID != 0 { + t.Fatal("initial catchup requests latest window instead of oldest page") + } +} +func TestVerifiedReadPrefixStopsBeforeUnreadOrUnmappedEvent(t *testing.T) { + key := networkid.PortalKey{ID: "7", Receiver: "42"} + lookup := func(_ context.Context, eventID id.EventID) (*database.Message, error) { + switch eventID { + case "$read1": + return &database.Message{ID: "7/1", Room: key, MXID: eventID}, nil + case "$unread": + return &database.Message{ID: "7/9", Room: key, MXID: eventID}, nil + case "$read2": + return &database.Message{ID: "7/2", Room: key, MXID: eventID}, nil + } + return nil, nil + } + for _, barrier := range []*event.Event{{ID: "$unread", Type: event.EventMessage}, {ID: "$unknown", Type: event.EventSticker}} { + target, err := verifiedReadPrefix(t.Context(), []*event.Event{{ID: "$read1", Type: event.EventMessage}, barrier, {ID: "$read2", Type: event.EventMessage}}, key, 2, lookup) + if err != nil || target != "$read1" { + t.Fatalf("crossed timeline barrier: %s %v", target, err) + } + } + target, err := verifiedReadPrefix(t.Context(), []*event.Event{{ID: "$read1", Type: event.EventMessage}}, networkid.PortalKey{ID: "7", Receiver: "other"}, 2, lookup) + if err != nil || target != "" { + t.Fatalf("crossed account boundary: %s %v", target, err) + } +} +func TestBackfillRejectsForeignChatInsteadOfSkipping(t *testing.T) { + _, _, err := (&InlineClient{}).backfillMessagesFromHistory(t.Context(), testPortal("7"), []sidecar.MessageRecord{{ChatID: 8, MessageID: 1, Content: sidecar.MessageContent{Type: "text", Text: "foreign"}}}) + if err == nil { + t.Fatal("foreign backfill accepted") + } +} + +// Exercise the pinned framework itself: an unbuffered queue can still return +// success while its handler is blocked. Such a result must not retire source work. +type deliveryTestMatrix struct{ bridgev2.MatrixConnector } + +func (*deliveryTestMatrix) Init(*bridgev2.Bridge) {} +func (*deliveryTestMatrix) BotIntent() bridgev2.MatrixAPI { return &deliveryTestIntent{} } +func (*deliveryTestMatrix) NewUserIntent(context.Context, id.UserID, string) (bridgev2.MatrixAPI, string, error) { + return nil, "", nil +} + +type deliveryTestIntent struct{ bridgev2.MatrixAPI } + +func (*deliveryTestIntent) EnsureInvited(context.Context, id.RoomID, id.UserID) error { return nil } + +type deliveryTestNetwork struct{ bridgev2.NetworkConnector } + +func (*deliveryTestNetwork) Init(*bridgev2.Bridge) {} +func (*deliveryTestNetwork) GetDBMetaTypes() database.MetaTypes { + return (&InlineConnector{}).GetDBMetaTypes() +} +func (*deliveryTestNetwork) LoadUserLogin(_ context.Context, login *bridgev2.UserLogin) error { + login.Client = &InlineClient{UserLogin: login} + return nil +} +func (*deliveryTestNetwork) GetCapabilities() *bridgev2.NetworkGeneralCapabilities { + return &bridgev2.NetworkGeneralCapabilities{} +} +func TestFrameworkBackgroundResultCannotCommitDelivery(t *testing.T) { + db := testBridgeDatabase(t) + br := bridgev2.NewBridge(db.BridgeID, db.Database, zerolog.Nop(), &bridgeconfig.BridgeConfig{SplitPortals: true}, &deliveryTestMatrix{}, &deliveryTestNetwork{}, func(*bridgev2.Bridge) bridgev2.CommandProcessor { return nil }) + br.BackgroundCtx = t.Context() + if err := db.User.Insert(t.Context(), &database.User{MXID: "@test:example.org"}); err != nil { + t.Fatal(err) + } + if err := db.UserLogin.Insert(t.Context(), &database.UserLogin{ID: "42", UserMXID: "@test:example.org", Metadata: &UserLoginMetadata{}}); err != nil { + t.Fatal(err) + } + login, err := br.GetExistingUserLoginByID(t.Context(), "42") + if err != nil || login == nil { + t.Fatalf("load login: %v", err) + } + t.Cleanup(login.BridgeState.Destroy) + key := networkid.PortalKey{ID: "7", Receiver: "42"} + if err := db.Portal.Insert(t.Context(), &database.Portal{PortalKey: key, MXID: "!room:example.org"}); err != nil { + t.Fatal(err) + } + previousBuffer, previousTicks := bridgev2.PortalEventBuffer, bridgev2.EventHandlingTimeoutTicks + bridgev2.PortalEventBuffer = 0 + bridgev2.EventHandlingTimeoutTicks = 0 + t.Cleanup(func() { + bridgev2.PortalEventBuffer = previousBuffer + bridgev2.EventHandlingTimeoutTicks = previousTicks + }) + release, done := make(chan struct{}), make(chan struct{}) + res := br.QueueRemoteEvent(login, &simplevent.EventMeta{Type: bridgev2.RemoteEventUnknown, PortalKey: key, PreHandleFunc: func(context.Context, *bridgev2.Portal) { <-release }, PostHandleFunc: func(context.Context, *bridgev2.Portal) { close(done) }}) + if !res.Success || !res.Queued { + close(release) + t.Fatalf("framework behavior changed: %+v", res) + } + if eventHandlingResultError("held event", res) == nil { + close(release) + t.Fatal("backgrounded event acknowledged") + } + close(release) + select { + case <-done: + case <-time.After(time.Second): + t.Fatal("held handler failed to finish") + } + // The production configuration keeps the lane synchronous even for slow work. + bridgev2.EventHandlingTimeoutTicks = int(^uint(0) >> 1) + if err := validatePortalEventDelivery(); err != nil { + t.Fatal(err) + } + release, done = make(chan struct{}), make(chan struct{}) + entered := make(chan struct{}) + results := make(chan bridgev2.EventHandlingResult, 1) + go func() { + results <- br.QueueRemoteEvent(login, &simplevent.EventMeta{Type: bridgev2.RemoteEventUnknown, PortalKey: key, PreHandleFunc: func(context.Context, *bridgev2.Portal) { close(entered); <-release }}) + }() + <-entered + select { + case result := <-results: + close(release) + t.Fatalf("returned before completion: %+v", result) + default: + } + close(release) + select { + case result := <-results: + if err := eventHandlingResultError("finished event", result); err != nil { + t.Fatal(err) + } + case <-time.After(time.Second): + t.Fatal("completed event did not return") + } +} + +func TestPrivateReadTransportDoesNotClearManualUnread(t *testing.T) { + calls := 0 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + calls++ + if r.Method != "POST" || !strings.HasSuffix(r.URL.Path, "/read_markers") { + t.Errorf("unexpected read side effect: %s %s", r.Method, r.URL.Path) + } + var request map[string]any + if err := json.NewDecoder(r.Body).Decode(&request); err != nil { + t.Error(err) + } + if request["m.read.private"] != "$safe" || request["m.fully_read"] != "$safe" { + t.Errorf("missing private markers: %+v", request) + } + if _, public := request["m.read"]; public { + t.Error("source receipt exposed publicly") + } + if _, unread := request["marked_unread"]; unread { + t.Error("manual unread overwritten") + } + w.Header().Set("Content-Type", "application/json") + fmt.Fprint(w, "{}") + })) + defer server.Close() + client, err := mautrix.NewClient(server.URL, "@self:example.org", "test-token") + if err != nil { + t.Fatal(err) + } + as := appservice.Create() + as.HomeserverDomain = "example.org" + as.Registration = appservice.CreateRegistration() + as.Registration.SenderLocalpart = "bot" + as.DoublePuppetValue = "inline-test" + if err := as.SetHomeserverURL(server.URL); err != nil { + t.Fatal(err) + } + intent := as.NewIntentAPI("self") + intent.Client = client + intent.IsCustomPuppet = true + puppet := &matrix.ASIntent{Matrix: intent} + if err := writePrivateReadMarkers(t.Context(), puppet, "!room:example.org", "$safe"); err != nil { + t.Fatal(err) + } + if calls != 1 { + t.Fatalf("receipt made %d writes", calls) + } +} +func TestReadTimelineRequiresOriginAndHonorsBound(t *testing.T) { + calls := 0 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + calls++ + w.Header().Set("Content-Type", "application/json") + fmt.Fprintf(w, `{"start":"%d","end":"%d","chunk":[{"event_id":"$%d","type":"m.room.message","content":{"msgtype":"m.text","body":"x"}}]}`, calls, calls+1, calls) + })) + defer server.Close() + client, err := mautrix.NewClient(server.URL, "@self:example.org", "test-token") + if err != nil { + t.Fatal(err) + } + if _, err := readTimeline(t.Context(), client, "!room:example.org"); err == nil { + t.Fatal("partial timeline was treated as complete") + } + if calls != 20 { + t.Fatalf("unbounded inspection: %d requests", calls) + } +} + +func TestReadTimelineContinuesAcrossEmptyIntermediatePage(t *testing.T) { + var cursors []string + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + cursor := r.URL.Query().Get("from") + cursors = append(cursors, cursor) + w.Header().Set("Content-Type", "application/json") + switch cursor { + case "": + fmt.Fprint(w, `{"start":"latest","end":"gap","chunk":[{"event_id":"$new","type":"m.room.message","content":{"msgtype":"m.text","body":"new"}}]}`) + case "gap": + fmt.Fprint(w, `{"start":"gap","end":"old","chunk":[]}`) + case "old": + fmt.Fprint(w, `{"start":"old","chunk":[{"event_id":"$old","type":"m.room.message","content":{"msgtype":"m.text","body":"old"}}]}`) + default: + t.Errorf("unexpected timeline cursor %q", cursor) + http.Error(w, "unexpected cursor", 400) + } + })) + defer server.Close() + client, err := mautrix.NewClient(server.URL, "@self:example.org", "test-token") + if err != nil { + t.Fatal(err) + } + timeline, err := readTimeline(t.Context(), client, "!room:example.org") + if err != nil { + t.Fatal(err) + } + if len(cursors) != 3 || len(timeline) != 2 || timeline[0].ID != "$old" || timeline[1].ID != "$new" { + t.Fatalf("empty page silently omitted older history: cursors=%v events=%v", cursors, timeline) + } +} diff --git a/pkg/connector/directory.go b/pkg/connector/directory.go new file mode 100644 index 0000000..d540b8d --- /dev/null +++ b/pkg/connector/directory.go @@ -0,0 +1,123 @@ +package connector + +import ( + "context" + "fmt" + "sort" + "strconv" + "strings" + "unicode/utf8" + + "github.com/inline-chat/matrix-inline/pkg/sidecar" + "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/bridgev2/networkid" +) + +const inlineDirectoryLimit uint32 = 50 + +var _ bridgev2.UserSearchingNetworkAPI = (*InlineClient)(nil) + +func canonicalInlineUsername(value string) string { + return strings.ToLower(strings.TrimPrefix(strings.TrimSpace(value), "@")) +} + +func (ic *InlineClient) resolveInlineIdentifier(ctx context.Context, identifier string, createChat bool) (*bridgev2.ResolveIdentifierResponse, error) { + identifier = strings.TrimSpace(identifier) + if identifier == "" { + return nil, fmt.Errorf("enter an Inline user ID or canonical username") + } + var user sidecar.UserRecord + if userID, err := parseInlineUserID(identifier); err == nil { + if createChat { + // Exact source IDs can go directly to the authoritative mutation. Requiring + // a complete directory would wrongly reject valid users in large accounts. + chat, err := ic.createDMWithUserID(ctx, userID) + if err != nil { + return nil, err + } + return &bridgev2.ResolveIdentifierResponse{UserID: makeUserID(strconv.FormatInt(userID, 10)), UserInfo: ic.userInfoForID(userID), Chat: chat}, nil + } + page, err := ic.Sidecar.Users(ctx, sidecar.UsersRequest{UserIDs: []int64{userID}}) + if err != nil { + return nil, fmt.Errorf("Inline user lookup failed: %w", err) + } + for _, candidate := range page.Users { + if candidate.UserID == userID { + user = candidate + break + } + } + if user.UserID == 0 { + return nil, fmt.Errorf("Inline user %d is not available to this account", userID) + } + } else { + username := canonicalInlineUsername(identifier) + if strings.ContainsAny(username, " @+:/\\\t\r\n") || utf8.RuneCountInString(username) > 100 { + return nil, fmt.Errorf("use an Inline user ID or username; phone and email lookup are not supported") + } + limit := inlineDirectoryLimit + page, err := ic.Sidecar.SearchUsers(ctx, sidecar.SearchUsersRequest{Query: username, Limit: &limit}) + if err != nil { + return nil, fmt.Errorf("Inline username lookup failed: %w", err) + } + matches := make(map[int64]sidecar.UserRecord) + for _, candidate := range page.Users { + if candidate.UserID > 0 && candidate.Username != nil && canonicalInlineUsername(*candidate.Username) == username { + matches[candidate.UserID] = candidate + } + } + if len(matches) == 0 { + return nil, fmt.Errorf("no accessible Inline user has username @%s", username) + } + if len(matches) > 1 { + return nil, fmt.Errorf("Inline username @%s is ambiguous; use a numeric user ID", username) + } + for _, candidate := range matches { + user = candidate + } + } + ic.cacheUsers([]sidecar.UserRecord{user}) + response := &bridgev2.ResolveIdentifierResponse{UserID: makeUserID(strconv.FormatInt(user.UserID, 10)), UserInfo: userInfoFromRecord(user)} + if createChat { + chat, err := ic.createDMWithUserID(ctx, user.UserID) + if err != nil { + return nil, err + } + response.Chat = chat + } + return response, nil +} + +func (ic *InlineClient) SearchUsers(ctx context.Context, query string) ([]*bridgev2.ResolveIdentifierResponse, error) { + query = strings.TrimSpace(query) + if query == "" || utf8.RuneCountInString(query) > 100 { + return nil, fmt.Errorf("Inline people search requires 1–100 characters") + } + limit := inlineDirectoryLimit + page, err := ic.Sidecar.SearchUsers(ctx, sidecar.SearchUsersRequest{Query: query, Limit: &limit}) + if err != nil { + return nil, fmt.Errorf("Inline people search failed: %w", err) + } + // Only source-account results are exposed. The shared ghost cache is never a directory. + unique := make(map[int64]sidecar.UserRecord) + for _, user := range page.Users { + if user.UserID > 0 { + unique[user.UserID] = user + } + } + ids := make([]int64, 0, len(unique)) + for userID := range unique { + ids = append(ids, userID) + } + sort.Slice(ids, func(i, j int) bool { return ids[i] < ids[j] }) + if len(ids) > int(inlineDirectoryLimit) { + ids = ids[:inlineDirectoryLimit] + } + result := make([]*bridgev2.ResolveIdentifierResponse, 0, len(ids)) + for _, userID := range ids { + user := unique[userID] + ic.cacheUsers([]sidecar.UserRecord{user}) + result = append(result, &bridgev2.ResolveIdentifierResponse{UserID: networkid.UserID(strconv.FormatInt(userID, 10)), UserInfo: userInfoFromRecord(user)}) + } + return result, nil +} diff --git a/pkg/connector/identity.go b/pkg/connector/identity.go new file mode 100644 index 0000000..da7fd48 --- /dev/null +++ b/pkg/connector/identity.go @@ -0,0 +1,197 @@ +package connector + +import ( + "context" + "fmt" + "strings" + "time" + + "github.com/inline-chat/matrix-inline/pkg/sidecar" + "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/bridgev2/database" + "maunium.net/go/mautrix/event" +) + +// A partial cache record cannot erase source structure. A complete newer record +// can explicitly detach a chat from its workspace or parent. +func mergeDialogStructure(previous, next sidecar.DialogRecord) sidecar.DialogRecord { + stale := previous.StructureSeq != nil && (next.StructureSeq == nil || *next.StructureSeq < *previous.StructureSeq) + if !next.StructureKnown || stale { + if previous.Kind != "" && previous.Kind != "unknown" { + next.Kind = previous.Kind + } + if next.PeerUserID == nil || stale { + next.PeerUserID = previous.PeerUserID + } + next.SpaceID, next.ParentChatID, next.ParentMessageID = previous.SpaceID, previous.ParentChatID, previous.ParentMessageID + next.Description, next.StructureKnown, next.StructureSeq = previous.Description, previous.StructureKnown, previous.StructureSeq + next.IsPublic = previous.IsPublic + } + return next +} + +func sameDialogStructure(left, right sidecar.DialogRecord) bool { + return left.Kind == right.Kind && left.StructureKnown == right.StructureKnown && + int64PtrEqual(left.SpaceID, right.SpaceID) && int64PtrEqual(left.ParentChatID, right.ParentChatID) && + int64PtrEqual(left.ParentMessageID, right.ParentMessageID) && stringPtrEqual(left.Description, right.Description) && + boolPtrEqual(left.IsPublic, right.IsPublic) +} + +// Existing incorrect group-DM classifications are repaired explicitly by the +// account command. New ordinary rooms use the correct classification at creation. +func preserveExistingRoomClassification(portal *bridgev2.Portal, info *bridgev2.ChatInfo) *bridgev2.ChatInfo { + if portal != nil && portal.MXID != "" && portal.RoomType == database.RoomTypeGroupDM && info != nil && info.Type != nil && *info.Type == database.RoomTypeDefault { + copy := *info + copy.Type = nil + return © + } + return info +} + +func guardedChatInfo(info *bridgev2.ChatInfo) func(context.Context, *bridgev2.Portal) (*bridgev2.ChatInfo, error) { + return func(_ context.Context, portal *bridgev2.Portal) (*bridgev2.ChatInfo, error) { + return preserveExistingRoomClassification(portal, info), nil + } +} + +var _ bridgev2.RoomTopicHandlingNetworkAPI = (*InlineClient)(nil) + +// Topics are Matrix-local. Recording the user's choice also preserves a cleared +// topic when child context or source descriptions change later. +func (ic *InlineClient) HandleMatrixRoomTopic(_ context.Context, msg *bridgev2.MatrixRoomTopic) (bool, error) { + if msg == nil || msg.Portal == nil || msg.Content == nil { + return false, fmt.Errorf("missing Matrix room topic") + } + meta := portalIdentityMetadata(msg.Portal) + msg.Portal.Topic, msg.Portal.TopicSet = msg.Content.Topic, true + meta.UserTopic, meta.ChildTopicOwned = true, false + meta.OwnedChildTopic = "" + return true, nil +} + +func portalIdentityMetadata(portal *bridgev2.Portal) *PortalMetadata { + if meta, ok := portal.Metadata.(*PortalMetadata); ok && meta != nil { + return meta + } + meta := &PortalMetadata{} + portal.Metadata = meta + return meta +} + +func (ic *InlineClient) updateChildContext(ctx context.Context, portal *bridgev2.Portal, dialog sidecar.DialogRecord) bool { + if portal == nil || !dialog.StructureKnown { + return false + } + meta := portalIdentityMetadata(portal) + changed := !int64PtrEqual(meta.InlineParentChatID, dialog.ParentChatID) || !int64PtrEqual(meta.InlineParentMessageID, dialog.ParentMessageID) + meta.InlineParentChatID, meta.InlineParentMessageID = dialog.ParentChatID, dialog.ParentMessageID + if meta.UserTopic || (portal.Topic != "" && (!meta.ChildTopicOwned || portal.Topic != meta.OwnedChildTopic)) { + return changed + } + if portal.MXID != "" { + // Offline edits (including an intentionally empty topic) may precede + // this bridge version or its local state. Inspect Matrix ownership before + // replacing a topic; a failed read leaves the pending context intact. + if portal.Bridge == nil || portal.Bridge.Bot == nil { + return changed + } + reader, ok := portal.Bridge.Matrix.(bridgev2.MatrixConnectorWithArbitraryRoomState) + if !ok { + return changed + } + current, err := reader.GetStateEvent(ctx, portal.MXID, event.StateTopic, "") + if err != nil { + return changed + } + if current != nil { + _ = current.Content.ParseRaw(event.StateTopic) + topic := current.Content.AsTopic().Topic + if current.Sender != "" && current.Sender != portal.Bridge.Bot.GetMXID() { + meta.UserTopic, meta.ChildTopicOwned, meta.OwnedChildTopic = true, false, "" + portal.Topic, portal.TopicSet = topic, true + return true + } + if topic != portal.Topic { + portal.Topic, portal.TopicSet = topic, true + changed = true + if topic != "" && (!meta.ChildTopicOwned || topic != meta.OwnedChildTopic) { + return changed + } + } + } + } + topic := "" + if dialog.Description != nil { + topic = strings.TrimSpace(*dialog.Description) + } + if dialog.ParentChatID != nil && *dialog.ParentChatID > 0 { + parent, err := ic.accessibleMappedParent(ctx, *dialog.ParentChatID) + // A temporary source lookup failure must not delete known context. Missing + // mappings also never cause the bridge to create or advertise a parent room. + if err != nil || parent == nil { + return changed + } + link := "https://matrix.to/#/" + string(parent.MXID) + if dialog.ParentMessageID != nil && *dialog.ParentMessageID > 0 { + message, err := ic.UserLogin.Bridge.DB.Message.GetFirstPartByID(ctx, parent.Receiver, makeMessageID(*dialog.ParentChatID, *dialog.ParentMessageID)) + if err == nil && message != nil && message.Room == parent.PortalKey && message.MXID != "" { + link += "/" + string(message.MXID) + } + } + name := strings.TrimSpace(parent.Name) + if name == "" { + name = fmt.Sprintf("Inline chat %d", *dialog.ParentChatID) + } + if topic != "" { + topic += "\n\n" + } + topic += "Parent conversation: " + name + " — " + link + } + if topic == portal.Topic && meta.ChildTopicOwned && meta.OwnedChildTopic == topic { + return changed + } + if portal.MXID != "" { + if portal.Bridge == nil || portal.Bridge.Bot == nil { + return changed + } + _, err := portal.Bridge.Bot.SendState(ctx, portal.MXID, event.StateTopic, "", &event.Content{Parsed: &event.TopicEventContent{Topic: topic}, Raw: map[string]any{"com.beeper.exclude_from_timeline": true}}, time.Now()) + if err != nil { + return changed + } + } + portal.Topic, portal.TopicSet = topic, portal.MXID != "" + meta.ChildTopicOwned, meta.OwnedChildTopic = true, topic + return true +} + +func (ic *InlineClient) accessibleMappedParent(ctx context.Context, chatID int64) (*bridgev2.Portal, error) { + if ic.UserLogin == nil || ic.UserLogin.Bridge == nil || ic.Sidecar == nil { + return nil, nil + } + dialog, known := ic.cachedDialog(chatID) + if !known || !ic.shouldProjectDialog(dialog) { + return nil, nil + } + parent, err := ic.UserLogin.Bridge.GetExistingPortalByKey(ctx, ic.portalKeyForChat(chatID)) + if err != nil || parent == nil || parent.MXID == "" { + return nil, err + } + membership, err := ic.UserLogin.Bridge.DB.UserPortal.Get(ctx, ic.UserLogin.UserLogin, parent.PortalKey) + if err != nil || membership == nil { + return nil, err + } + // The source endpoint, not shared ghost/cache presence, proves this login can + // still inspect the parent. No parent or child data is written to the parent. + if _, err := ic.Sidecar.ChatParticipants(ctx, sidecar.ChatParticipantsRequest{ChatID: chatID}); err != nil { + return nil, err + } + return parent, nil +} + +// Saved once before applying the narrow in-place classification repair. +// Notification preferences, topic, room identity and all message rows are untouched. +type RoomClassificationRepair struct { + PreviousType database.RoomType `json:"previous_type"` + PreviousOtherUserID string `json:"previous_other_user_id,omitempty"` + AppliedAt time.Time `json:"applied_at"` +} diff --git a/pkg/connector/identity_retry.go b/pkg/connector/identity_retry.go new file mode 100644 index 0000000..dc6beef --- /dev/null +++ b/pkg/connector/identity_retry.go @@ -0,0 +1,111 @@ +package connector + +import ( + "context" + "sort" + "strconv" + "time" + + "github.com/inline-chat/matrix-inline/pkg/sidecar" + "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/bridgev2/simplevent" +) + +const identityProjectionRetryLimit = 8 + +// Metadata work is independent of chat traffic: a quiet child's topic and a +// pending workspace link both recover after transient Matrix/source failures. +func (ic *InlineClient) retryIdentityProjection(ctx context.Context) { + if ic.UserLogin == nil || ic.UserLogin.Bridge == nil { + return + } + ic.retryWorkspaceLinks(ctx) + policy := ic.effectiveHiddenDialogsPolicy() + ic.mu.Lock() + dialogs := make([]sidecar.DialogRecord, 0, len(ic.dialogs)) + for _, dialog := range ic.dialogs { + if dialog.StructureKnown && (policy == hiddenDialogsInclude || dialog.ChatListHidden == nil || !*dialog.ChatListHidden) { + dialogs = append(dialogs, dialog) + } + } + cursor := ic.identityRetryCursor + ic.mu.Unlock() + sort.Slice(dialogs, func(i, j int) bool { return dialogs[i].ChatID < dialogs[j].ChatID }) + if len(dialogs) == 0 { + return + } + start := sort.Search(len(dialogs), func(i int) bool { return dialogs[i].ChatID > cursor }) + if start == len(dialogs) { + start = 0 + } + count := min(len(dialogs), identityProjectionRetryLimit) + for offset := 0; offset < count; offset++ { + if ctx.Err() != nil { + return + } + dialog := dialogs[(start+offset)%len(dialogs)] + ic.mu.Lock() + ic.identityRetryCursor = dialog.ChatID + ic.mu.Unlock() + // This event cannot create a room or change its audience/classification. + info := ic.chatInfoForDialog(ctx, dialog, nil) + ic.UserLogin.Bridge.QueueRemoteEvent(ic.UserLogin, &simplevent.ChatResync{ + EventMeta: simplevent.EventMeta{Type: bridgev2.RemoteEventChatResync, PortalKey: ic.portalKeyForChat(dialog.ChatID), Timestamp: time.Now()}, + ChatInfo: &bridgev2.ChatInfo{ExtraUpdates: info.ExtraUpdates, ExcludeChangesFromTimeline: true}, + }) + } +} + +func (ic *InlineClient) retryWorkspaceLinks(ctx context.Context) { + ic.navigationMu.Lock() + defer ic.navigationMu.Unlock() + spaces := ic.workspaceNavigationSnapshot() + remaining := identityProjectionRetryLimit + for _, space := range spaces { + if remaining <= 0 { + break + } + if space.Active && space.RoomID == "" { + remaining-- + if err := ic.ensureWorkspaceRoom(ctx, space); err != nil { + continue + } + } + for chatID, link := range space.Links { + if remaining <= 0 { + break + } + remove := !space.Active + if parsed, err := strconv.ParseInt(chatID, 10, 64); err == nil { + if dialog, known := ic.cachedDialog(parsed); known { + remove = remove || !ic.shouldProjectDialog(dialog) || (dialog.StructureKnown && (dialog.SpaceID == nil || *dialog.SpaceID != space.SpaceID)) + } + } + if !remove && link.Attached { + continue + } + remaining-- + if !remove { + parsed, err := strconv.ParseInt(chatID, 10, 64) + if err != nil || parsed <= 0 || ic.Sidecar == nil { + continue + } + if _, err := ic.Sidecar.ChatParticipants(ctx, sidecar.ChatParticipantsRequest{ChatID: parsed}); err != nil { + continue + } + } + if err := ic.sendWorkspaceLink(ctx, space.RoomID, link.RoomID, remove); err != nil { + continue + } + if remove { + delete(space.Links, chatID) + } else { + link.Attached = true + space.Links[chatID] = link + } + } + } + if err := ic.saveWorkspaceNavigation(ctx, spaces); err != nil { + ic.UserLogin.Bridge.Log.Warn().Err(err).Msg("Failed to save pending Inline navigation") + } +} diff --git a/pkg/connector/identity_test.go b/pkg/connector/identity_test.go new file mode 100644 index 0000000..71c1065 --- /dev/null +++ b/pkg/connector/identity_test.go @@ -0,0 +1,393 @@ +package connector + +import ( + "context" + "encoding/json" + "errors" + "net/http" + "net/http/httptest" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/inline-chat/matrix-inline/pkg/sidecar" + "github.com/rs/zerolog" + "maunium.net/go/mautrix" + "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/bridgev2/bridgeconfig" + "maunium.net/go/mautrix/bridgev2/database" + "maunium.net/go/mautrix/bridgev2/networkid" + "maunium.net/go/mautrix/event" + "maunium.net/go/mautrix/id" +) + +func TestConversationIdentityDoesNotFollowMemberCount(t *testing.T) { + client := &InlineClient{AccountID: "1", UserLogin: &bridgev2.UserLogin{UserLogin: &database.UserLogin{ID: "1"}}} + peer := int64(2) + for _, test := range []struct { + name string + dialog sidecar.DialogRecord + kind *database.RoomType + }{ + {"legacy unknown", sidecar.DialogRecord{ChatID: 7}, nil}, + {"direct", sidecar.DialogRecord{ChatID: 7, Kind: sidecar.ConversationDirectMessage, PeerUserID: &peer}, roomTypePtr(database.RoomTypeDM)}, + {"channel", sidecar.DialogRecord{ChatID: 7, Kind: sidecar.ConversationChat}, roomTypePtr(database.RoomTypeDefault)}, + {"private child", sidecar.DialogRecord{ChatID: 8, Kind: sidecar.ConversationChat, ParentChatID: identityInt64Ptr(7)}, roomTypePtr(database.RoomTypeDefault)}, + } { + t.Run(test.name, func(t *testing.T) { + info := client.chatInfoForDialog(t.Context(), test.dialog, nil) + if (info.Type == nil) != (test.kind == nil) || (info.Type != nil && *info.Type != *test.kind) { + t.Fatalf("classification = %v", info.Type) + } + if info.ParentID != nil { + t.Fatal("child context creates a parent navigation link") + } + }) + } + created := client.chatInfoForCreatedChat(t.Context(), &sidecar.CreatedChat{ChatID: 8}, []networkid.UserID{"2"}) + if created.Type == nil || *created.Type != database.RoomTypeDefault || created.Members.OtherUserID != "" { + t.Fatal("two-person ordinary chat classified as DM") + } +} + +func roomTypePtr(value database.RoomType) *database.RoomType { return &value } + +func TestAuthoritativeDetachAndStaleStructure(t *testing.T) { + seq := int32(8) + oldSeq := int32(7) + original := sidecar.DialogRecord{ChatID: 7, Kind: sidecar.ConversationChat, StructureKnown: true, StructureSeq: &seq, SpaceID: identityInt64Ptr(3), ParentChatID: identityInt64Ptr(1)} + partial := mergeDialogStructure(original, sidecar.DialogRecord{ChatID: 7}) + if partial.SpaceID == nil || partial.ParentChatID == nil || partial.Kind != sidecar.ConversationChat { + t.Fatal("partial refresh detached source structure") + } + stale := mergeDialogStructure(original, sidecar.DialogRecord{ChatID: 7, Kind: sidecar.ConversationChat, StructureKnown: true, StructureSeq: &oldSeq}) + if stale.SpaceID == nil || stale.ParentChatID == nil { + t.Fatal("stale replay detached source structure") + } + unversioned := mergeDialogStructure(original, sidecar.DialogRecord{ChatID: 7, Kind: sidecar.ConversationChat, StructureKnown: true}) + if unversioned.SpaceID == nil || unversioned.ParentChatID == nil { + t.Fatal("unversioned snapshot erased versioned source structure") + } + next := int32(9) + detached := mergeDialogStructure(original, sidecar.DialogRecord{ChatID: 7, Kind: sidecar.ConversationChat, StructureKnown: true, StructureSeq: &next}) + if detached.SpaceID != nil || detached.ParentChatID != nil { + t.Fatal("canonical detach retained cached structure") + } +} + +type identityStateSend struct { + room id.RoomID + typ event.Type + key string + content *event.Content +} +type identityMatrixAPI struct { + bridgev2.MatrixAPI + states []identityStateSend + failure error +} + +func (api *identityMatrixAPI) GetMXID() id.UserID { return "@bridge:example.org" } +func (api *identityMatrixAPI) SendState(_ context.Context, room id.RoomID, typ event.Type, key string, content *event.Content, _ time.Time) (*mautrix.RespSendEvent, error) { + if api.failure != nil { + return nil, api.failure + } + api.states = append(api.states, identityStateSend{room, typ, key, content}) + return &mautrix.RespSendEvent{EventID: "$state"}, nil +} + +type identityMatrixConnector struct { + bridgev2.MatrixConnector + api *identityMatrixAPI + topicSender id.UserID + topic string +} + +func (matrix *identityMatrixConnector) Init(*bridgev2.Bridge) {} +func (matrix *identityMatrixConnector) BotIntent() bridgev2.MatrixAPI { return matrix.api } +func (matrix *identityMatrixConnector) ServerName() string { return "example.org" } +func (matrix *identityMatrixConnector) GetCapabilities() *bridgev2.MatrixCapabilities { + return &bridgev2.MatrixCapabilities{} +} +func (matrix *identityMatrixConnector) GetStateEvent(_ context.Context, _ id.RoomID, typ event.Type, _ string) (*event.Event, error) { + sender := matrix.topicSender + if sender == "" { + sender = matrix.api.GetMXID() + } + return &event.Event{Type: typ, Sender: sender, Content: event.Content{Parsed: &event.TopicEventContent{Topic: matrix.topic}}}, nil +} +func identityTestBridge(t *testing.T, db *database.Database) (*bridgev2.Bridge, *identityMatrixAPI) { + t.Helper() + api := &identityMatrixAPI{} + bridge := bridgev2.NewBridge(db.BridgeID, db.Database, zerolog.Nop(), &bridgeconfig.BridgeConfig{SplitPortals: true}, &identityMatrixConnector{api: api}, &InlineConnector{}, func(*bridgev2.Bridge) bridgev2.CommandProcessor { return nil }) + bridge.BackgroundCtx = t.Context() + return bridge, api +} + +func TestClassificationRepairUsesFrameworkWithoutChangingRoomOrMessages(t *testing.T) { + path := filepath.Join(t.TempDir(), "bridge.db") + db := openTestBridgeDatabase(t, path) + bridge, api := identityTestBridge(t, db) + key := networkid.PortalKey{ID: "7", Receiver: "1"} + stored := &database.Portal{PortalKey: key, MXID: "!original:example.org", RoomType: database.RoomTypeGroupDM, Topic: "My topic", TopicSet: true, Metadata: &PortalMetadata{}} + if err := db.Portal.Insert(t.Context(), stored); err != nil { + t.Fatal(err) + } + if err := db.Ghost.Insert(t.Context(), &database.Ghost{ID: "2"}); err != nil { + t.Fatal(err) + } + if err := db.Message.Insert(t.Context(), &database.Message{ID: "7/11", Room: key, SenderID: "2", MXID: "$original", Timestamp: time.Now()}); err != nil { + t.Fatal(err) + } + portal := &bridgev2.Portal{Portal: stored, Bridge: bridge} + ordinary := database.RoomTypeDefault + if preserveExistingRoomClassification(portal, &bridgev2.ChatInfo{Type: &ordinary}).Type != nil { + t.Fatal("normal refresh applied an unrequested repair") + } + info, err := classificationRepairInfo(t.Context(), portal) + if err != nil { + t.Fatal(err) + } + portal.UpdateInfo(t.Context(), info, nil, nil, time.Time{}) + if len(api.states) != 2 { + t.Fatalf("bridge info events=%d", len(api.states)) + } + for _, state := range api.states { + content, ok := state.content.Parsed.(*event.BridgeEventContent) + if !ok || content.BeeperRoomType != "" || content.BeeperRoomTypeV2 != "" { + t.Fatalf("wrong repaired Beeper metadata: %#v", state.content.Parsed) + } + } + db.RawDB.Close() + db = openTestBridgeDatabase(t, path) + restored, err := db.Portal.GetByKey(t.Context(), key) + if err != nil { + t.Fatal(err) + } + if restored.MXID != "!original:example.org" || restored.RoomType != database.RoomTypeDefault || restored.Topic != "My topic" { + t.Fatalf("repair changed unrelated state: %+v", restored) + } + meta := restored.Metadata.(*PortalMetadata) + if meta.ClassificationRepair == nil || meta.ClassificationRepair.PreviousType != database.RoomTypeGroupDM { + t.Fatal("missing saved previous classification") + } + message, err := db.Message.GetFirstPartByID(t.Context(), "1", "7/11") + if err != nil || message == nil || message.MXID != "$original" { + t.Fatalf("message changed: %+v %v", message, err) + } +} + +func TestChildTopicPreservesUserChoiceAndAuthoritativeDetach(t *testing.T) { + client := &InlineClient{} + portal := testPortal("8") + portal.Metadata = &PortalMetadata{ChildTopicOwned: true, OwnedChildTopic: "Old parent"} + portal.Topic = "Old parent" + if !client.updateChildContext(t.Context(), portal, sidecar.DialogRecord{ChatID: 8, StructureKnown: true}) || portal.Topic != "" { + t.Fatal("authoritative detach retained bridge-owned topic") + } + portal.Topic = "My topic" + client.updateChildContext(t.Context(), portal, sidecar.DialogRecord{ChatID: 8, StructureKnown: true, Description: stringPtr("Source description")}) + if portal.Topic != "My topic" { + t.Fatal("source changed user topic") + } + _, err := client.HandleMatrixRoomTopic(t.Context(), &bridgev2.MatrixRoomTopic{MatrixEventBase: bridgev2.MatrixEventBase[*event.TopicEventContent]{Portal: portal, Content: &event.TopicEventContent{Topic: ""}}}) + if err != nil { + t.Fatal(err) + } + client.updateChildContext(t.Context(), portal, sidecar.DialogRecord{ChatID: 8, StructureKnown: true, Description: stringPtr("Source description")}) + if portal.Topic != "" { + t.Fatal("user-cleared topic was replaced") + } + portal.Metadata = &PortalMetadata{ChildTopicOwned: true, OwnedChildTopic: "Known parent"} + portal.Topic = "Known parent" + client.updateChildContext(t.Context(), portal, sidecar.DialogRecord{ChatID: 8, StructureKnown: true, ParentChatID: identityInt64Ptr(7)}) + if portal.Topic != "Known parent" { + t.Fatal("temporary missing parent erased context") + } +} + +func TestChildContextOnlyWritesAccessibleChildAndKeepsIndependentIDs(t *testing.T) { + db := testBridgeDatabase(t) + bridge, api := identityTestBridge(t, db) + login := &database.UserLogin{ID: "1", UserMXID: "@owner:example.org", Metadata: &UserLoginMetadata{AccountID: "1"}} + if err := db.User.Insert(t.Context(), &database.User{MXID: login.UserMXID}); err != nil { + t.Fatal(err) + } + if err := db.UserLogin.Insert(t.Context(), login); err != nil { + t.Fatal(err) + } + parentKey := networkid.PortalKey{ID: "7", Receiver: "1"} + childKey := networkid.PortalKey{ID: "8", Receiver: "1"} + for _, record := range []*database.Portal{{PortalKey: parentKey, MXID: "!parent:example.org", Name: "Parent"}, {PortalKey: childKey, MXID: "!child:example.org"}} { + if err := db.Portal.Insert(t.Context(), record); err != nil { + t.Fatal(err) + } + } + if _, err := db.UserPortal.GetOrCreate(t.Context(), login, parentKey); err != nil { + t.Fatal(err) + } + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/rpc/chat/participants" { + t.Fatalf("unexpected %s", r.URL.Path) + } + writeConnectorSidecarResult(t, w, "chat_participants", sidecar.ChatParticipantsPage{}) + })) + defer server.Close() + client := &InlineClient{UserLogin: &bridgev2.UserLogin{UserLogin: login, Bridge: bridge}, AccountID: "1", Sidecar: sidecar.NewClient(server.URL), dialogs: map[int64]sidecar.DialogRecord{7: {ChatID: 7, Kind: sidecar.ConversationChat}}} + child, err := bridge.GetExistingPortalByKey(t.Context(), childKey) + if err != nil { + t.Fatal(err) + } + if !client.updateChildContext(t.Context(), child, sidecar.DialogRecord{ChatID: 8, Kind: sidecar.ConversationChat, StructureKnown: true, ParentChatID: identityInt64Ptr(7)}) { + t.Fatal("child context not updated") + } + if !strings.Contains(child.Topic, "!parent:example.org") || len(api.states) != 1 || api.states[0].room != child.MXID || child.ParentKey.ID != "" { + t.Fatalf("context leaked or missing: %+v", api.states) + } + // Another account's cached portal is insufficient without that login's mapping. + client.UserLogin.ID = "2" + if parent, err := client.accessibleMappedParent(t.Context(), 7); err != nil || parent != nil { + t.Fatalf("foreign mapping accepted: %+v %v", parent, err) + } +} + +func TestWorkspaceRevocationSurvivesReopenAndRetainsIndependentRoom(t *testing.T) { + path := filepath.Join(t.TempDir(), "bridge.db") + db := openTestBridgeDatabase(t, path) + bridge, api := identityTestBridge(t, db) + login := &database.UserLogin{ID: "1", UserMXID: "@owner:example.org", Metadata: &UserLoginMetadata{AccountID: "1", WorkspaceNavigation: map[string]*WorkspaceNavigation{"3": {SpaceID: 3, Name: "Work", Active: true, RoomID: "!space:example.org", Links: map[string]WorkspaceRoomLink{"7": {RoomID: "!chat:example.org", Attached: true}}}}}} + if err := db.User.Insert(t.Context(), &database.User{MXID: login.UserMXID}); err != nil { + t.Fatal(err) + } + if err := db.UserLogin.Insert(t.Context(), login); err != nil { + t.Fatal(err) + } + key := networkid.PortalKey{ID: "7", Receiver: "1"} + if err := db.Portal.Insert(t.Context(), &database.Portal{PortalKey: key, MXID: "!chat:example.org"}); err != nil { + t.Fatal(err) + } + client := &InlineClient{AccountID: "1", UserLogin: &bridgev2.UserLogin{UserLogin: login, Bridge: bridge}} + api.failure = errors.New("temporary Matrix outage") + if err := client.reconcileWorkspaceNavigation(t.Context(), &sidecar.AccountStateSnapshot{RemovedSpaceIDs: []int64{3}}); err == nil { + t.Fatal("failed navigation write hidden") + } + db.RawDB.Close() + db = openTestBridgeDatabase(t, path) + bridge, api = identityTestBridge(t, db) + restored, err := db.UserLogin.GetByID(t.Context(), "1") + if err != nil { + t.Fatal(err) + } + client = &InlineClient{AccountID: "1", UserLogin: &bridgev2.UserLogin{UserLogin: restored, Bridge: bridge}} + saved := client.workspaceNavigationSnapshot()["3"] + if saved.Active || len(saved.Links) != 1 { + t.Fatal("revocation/pending cleanup lost at restart") + } + client.retryWorkspaceLinks(t.Context()) + if len(api.states) != 1 || api.states[0].room != "!space:example.org" || len(api.states[0].content.Parsed.(*event.SpaceChildEventContent).Via) != 0 { + t.Fatal("did not detach revoked navigation") + } + portal, err := db.Portal.GetByKey(t.Context(), key) + if err != nil || portal == nil || portal.MXID != "!chat:example.org" { + t.Fatal("workspace leave deleted independent room") + } +} + +func TestWorkspaceMembershipIsAccountScopedAndIncompleteRefreshRetainsStructure(t *testing.T) { + spaces := map[string]*WorkspaceNavigation{"3": {SpaceID: 3, Active: true}} + applyWorkspaceAccess(spaces, &sidecar.AccountStateSnapshot{}, 1) + if !spaces["3"].Active { + t.Fatal("incomplete refresh revoked membership") + } + applyWorkspaceAccess(spaces, &sidecar.AccountStateSnapshot{Spaces: []sidecar.SpaceRecord{{SpaceID: 4, Name: "Other"}}, SpaceMemberships: []sidecar.SpaceMemberRecord{{SpaceID: 4, UserID: 2}}}, 1) + if spaces["4"] != nil { + t.Fatal("foreign account membership created space") + } + applyWorkspaceAccess(spaces, &sidecar.AccountStateSnapshot{Spaces: []sidecar.SpaceRecord{{SpaceID: 5, Name: "Known"}}, SpaceMemberships: []sidecar.SpaceMemberRecord{{SpaceID: 5, UserID: 1}}}, 1) + if spaces["5"] == nil || !spaces["5"].Active { + t.Fatal("one incomplete workspace blocked a separately proven membership") + } + applyWorkspaceAccess(spaces, &sidecar.AccountStateSnapshot{SpacesComplete: true}, 1) + if spaces["3"].Active || spaces["5"].Active { + t.Fatal("verified workspace leave was ignored") + } +} + +func TestSourceDirectoryRejectsCachedUsersAndAmbiguousNames(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/rpc/users": + writeConnectorSidecarResult(t, w, "users", sidecar.UsersPage{}) + case "/rpc/users/search": + writeConnectorSidecarResult(t, w, "users", sidecar.UsersPage{Users: []sidecar.UserRecord{{UserID: 2, Username: stringPtr("Ada")}, {UserID: 3, Username: stringPtr("ada")}}}) + default: + t.Fatalf("unexpected %s", r.URL.Path) + } + })) + defer server.Close() + client := &InlineClient{Sidecar: sidecar.NewClient(server.URL), users: map[int64]sidecar.UserRecord{2: {UserID: 2, Username: stringPtr("ada")}}} + if _, err := client.ResolveIdentifier(t.Context(), "2", false); err == nil { + t.Fatal("cached presence granted source-account lookup") + } + if _, err := client.ResolveIdentifier(t.Context(), "@ADA", false); err == nil || !strings.Contains(err.Error(), "ambiguous") { + t.Fatalf("duplicate username selected arbitrary user: %v", err) + } +} + +func TestCreateChannelPreservesWorkspaceVisibilityAndValidatesBeforeMutation(t *testing.T) { + var requests []sidecar.CreateThreadRequest + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/rpc/chat/create-thread" { + t.Fatalf("unexpected %s", r.URL.Path) + } + var request sidecar.CreateThreadRequest + if err := json.NewDecoder(r.Body).Decode(&request); err != nil { + t.Fatal(err) + } + requests = append(requests, request) + writeConnectorSidecarResult(t, w, "created_chat", sidecar.CreatedChat{ChatID: 7, Title: request.Title}) + })) + defer server.Close() + client := &InlineClient{AccountID: "1", UserLogin: &bridgev2.UserLogin{UserLogin: &database.UserLogin{ID: "1"}}, Sidecar: sidecar.NewClient(server.URL)} + for _, kind := range []string{"public_channel", "private_channel"} { + response, err := client.CreateGroup(t.Context(), &bridgev2.GroupCreateParams{Type: kind, Name: &event.RoomNameEventContent{Name: "Project"}, Parent: &networkid.PortalKey{ID: client.workspacePortalID(3), Receiver: "1"}, Participants: []networkid.UserID{"2"}}) + if err != nil { + t.Fatal(err) + } + if response.PortalInfo.Type == nil || *response.PortalInfo.Type != database.RoomTypeDefault || response.PortalInfo.Members.OtherUserID != "" { + t.Fatal("created workspace channel looks like DM") + } + } + if len(requests) != 2 || !requests[0].IsPublic || requests[1].IsPublic || requests[0].SpaceID == nil || *requests[0].SpaceID != 3 || requests[1].SpaceID == nil || *requests[1].SpaceID != 3 { + t.Fatalf("wrong creation payload: %+v", requests) + } + if _, err := client.CreateGroup(t.Context(), &bridgev2.GroupCreateParams{Type: "thread", Name: &event.RoomNameEventContent{Name: "Child"}, Parent: &networkid.PortalKey{ID: "7"}}); err == nil { + t.Fatal("native child creation flattened into ordinary chat") + } + if _, err := client.CreateGroup(t.Context(), &bridgev2.GroupCreateParams{Type: "public_channel", Name: &event.RoomNameEventContent{Name: "No workspace"}}); err == nil { + t.Fatal("public workspace channel accepted without workspace") + } + if len(requests) != 2 { + t.Fatal("invalid shape reached source mutation") + } +} + +func identityInt64Ptr(value int64) *int64 { return &value } + +func TestOfflineUserClearedTopicIsNotReplaced(t *testing.T) { + db := testBridgeDatabase(t) + bridge, _ := identityTestBridge(t, db) + matrix := bridge.Matrix.(*identityMatrixConnector) + matrix.topicSender = "@owner:example.org" + portal := testPortal("8") + portal.MXID = "!child:example.org" + portal.Bridge = bridge + portal.Topic = "Old bridge context" + portal.Metadata = &PortalMetadata{ChildTopicOwned: true, OwnedChildTopic: portal.Topic} + client := &InlineClient{} + client.updateChildContext(t.Context(), portal, sidecar.DialogRecord{ChatID: 8, StructureKnown: true, Description: stringPtr("New source description")}) + if portal.Topic != "" || !portal.Metadata.(*PortalMetadata).UserTopic { + t.Fatal("offline user-cleared topic was overwritten") + } +} diff --git a/pkg/connector/matrix_media.go b/pkg/connector/matrix_media.go new file mode 100644 index 0000000..a629961 --- /dev/null +++ b/pkg/connector/matrix_media.go @@ -0,0 +1,97 @@ +package connector + +import ( + "context" + "errors" + "fmt" + "io" + "net/http" + + "maunium.net/go/mautrix" + "maunium.net/go/mautrix/bridgev2/matrix" + "maunium.net/go/mautrix/event" + "maunium.net/go/mautrix/id" +) + +type matrixMediaDownloader interface { + Download(context.Context, id.ContentURI) (*http.Response, error) +} + +func (ic *InlineClient) downloadMatrixMedia(ctx context.Context, content *event.MessageEventContent) ([]byte, error) { + if ic.UserLogin == nil || ic.UserLogin.Bridge == nil { + return nil, fmt.Errorf("Matrix media transport is unavailable") + } + intent, ok := ic.UserLogin.Bridge.Bot.(*matrix.ASIntent) + if !ok || intent.Matrix == nil { + return nil, fmt.Errorf("Matrix media transport does not support bounded authenticated downloads") + } + return downloadBoundedMatrixMedia(ctx, intent.Matrix, content, maxInlineMediaDownload) +} + +// Use the homeserver client's configured authenticated MXC endpoint. Reading a +// limited response here avoids the framework's unbounded bytes/file helpers. +func downloadBoundedMatrixMedia(ctx context.Context, client matrixMediaDownloader, content *event.MessageEventContent, maxBytes int64) ([]byte, error) { + if content == nil { + return nil, permanentMediaFailure(fmt.Errorf("missing Matrix attachment")) + } + if content.Info != nil && int64(content.Info.Size) > maxBytes { + return nil, permanentMediaFailure(fmt.Errorf("Matrix media declared size exceeds %d byte limit", maxBytes)) + } + uri := content.URL + if content.File != nil { + uri = content.File.URL + if err := content.File.PrepareForDecryption(); err != nil { + return nil, permanentMediaFailure(fmt.Errorf("invalid Matrix encryption metadata: %w", err)) + } + } + parsed, err := uri.Parse() + if err != nil || parsed.IsEmpty() { + return nil, permanentMediaFailure(fmt.Errorf("invalid Matrix media URI")) + } + response, err := client.Download(ctx, parsed) + if err != nil { + if response != nil && response.Body != nil { + response.Body.Close() + } + var httpErr mautrix.HTTPError + if errors.As(err, &httpErr) && httpErr.Response != nil && terminalMediaStatus(httpErr.Response.StatusCode) { + return nil, permanentMediaFailure(err) + } + return nil, err + } + if response == nil || response.Body == nil { + return nil, fmt.Errorf("Matrix media transport returned no response body") + } + defer response.Body.Close() + if response.StatusCode < 200 || response.StatusCode >= 300 { + err = fmt.Errorf("Matrix media returned HTTP %d", response.StatusCode) + if terminalMediaStatus(response.StatusCode) { + return nil, permanentMediaFailure(err) + } + return nil, err + } + if response.ContentLength > maxBytes { + return nil, permanentMediaFailure(fmt.Errorf("Matrix media Content-Length exceeds %d byte limit", maxBytes)) + } + data, err := io.ReadAll(io.LimitReader(response.Body, maxBytes+1)) + if err != nil { + return nil, fmt.Errorf("read Matrix attachment: %w", err) + } + if int64(len(data)) > maxBytes { + return nil, permanentMediaFailure(fmt.Errorf("Matrix media exceeds %d byte limit", maxBytes)) + } + if len(data) == 0 { + return nil, permanentMediaFailure(fmt.Errorf("Matrix media contains no bytes")) + } + if content.File != nil { + // DecryptInPlace verifies SHA-256 of the entire bounded ciphertext before + // exposing any plaintext to the sidecar, including for chunked responses. + if err := content.File.DecryptInPlace(data); err != nil { + return nil, permanentMediaFailure(fmt.Errorf("verify Matrix attachment: %w", err)) + } + } + return data, nil +} +func terminalMediaStatus(status int) bool { + return status >= 400 && status < 500 && status != 408 && status != 425 && status != 429 +} diff --git a/pkg/connector/message_fidelity.go b/pkg/connector/message_fidelity.go new file mode 100644 index 0000000..5d1b03f --- /dev/null +++ b/pkg/connector/message_fidelity.go @@ -0,0 +1,192 @@ +package connector + +import ( + "context" + "encoding/json" + "fmt" + "strconv" + + "github.com/inline-chat/matrix-inline/pkg/sidecar" + "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/bridgev2/database" + "maunium.net/go/mautrix/event" +) + +func unsupportedMatrixOperation(err error) error { + return bridgev2.WrapErrorInStatus(err).WithErrorAsMessage().WithIsCertain(true).WithSendNotice(true).WithErrorReason(event.MessageStatusUnsupported) +} + +func (ic *InlineClient) validateMessagePortal(portal *bridgev2.Portal) (int64, error) { + if portal == nil || ic.UserLogin == nil || portal.Receiver != ic.UserLogin.ID { + return 0, fmt.Errorf("Inline message portal belongs to another login") + } + chatID, err := strconv.ParseInt(string(portal.ID), 10, 64) + if err != nil || chatID <= 0 { + return 0, fmt.Errorf("invalid Inline chat portal") + } + return chatID, nil +} +func (ic *InlineClient) validatedTarget(portal *bridgev2.Portal, target *database.Message) (int64, int64, error) { + chatID, err := ic.validateMessagePortal(portal) + if err != nil { + return 0, 0, err + } + if target == nil || target.Room != portal.PortalKey { + return 0, 0, fmt.Errorf("Inline target belongs to another room or login") + } + targetChatID, messageID, err := parseMessageID(target.ID) + if err != nil || targetChatID != chatID || messageID <= 0 { + return 0, 0, fmt.Errorf("Inline target does not match this chat") + } + return chatID, messageID, nil +} +func (ic *InlineClient) validatedReply(portal *bridgev2.Portal, target *database.Message) (*int64, error) { + if target == nil { + return nil, nil + } + _, messageID, err := ic.validatedTarget(portal, target) + if err != nil { + return nil, err + } + return &messageID, nil +} + +func cloneMatrixContent(content *event.MessageEventContent) *event.MessageEventContent { + if content == nil { + return nil + } + raw, err := json.Marshal(content) + if err != nil { + return nil + } + var clone event.MessageEventContent + if json.Unmarshal(raw, &clone) != nil { + return nil + } + clone.RelatesTo = nil + clone.NewContent = nil + return &clone +} +func inlineMediaIdentity(content sidecar.MessageContent) string { + if content.Type != "media" { + return "" + } + if content.FileID != "" { + return content.Kind + "/" + content.FileID + } + return content.Kind + "/" + content.URL +} +func inlineRevisionIsStale(metadata *MessageMetadata, data sidecar.MessageRecord) bool { + if metadata == nil { + return false + } + if metadata.SourceRevision != nil { + if data.Metadata.Revision == nil { + return true + } + if *data.Metadata.Revision != *metadata.SourceRevision { + return *data.Metadata.Revision < *metadata.SourceRevision + } + if metadata.InlineFingerprint != "" && metadata.InlineFingerprint != inlineMessageFingerprint(data) { + return true + } + } + if metadata.SourceEditTimestamp != nil { + return data.Metadata.EditTimestamp == nil || *data.Metadata.EditTimestamp < *metadata.SourceEditTimestamp + } + return false +} +func inlineEditTimestamp(data sidecar.MessageRecord) int64 { + if data.Metadata.EditTimestamp != nil { + return *data.Metadata.EditTimestamp + } + return data.Timestamp +} +func reuseInlineMedia(existing *database.Message, data sidecar.MessageRecord) *bridgev2.ConvertedMessage { + metadata, ok := existing.Metadata.(*MessageMetadata) + if !ok || metadata == nil || metadata.MatrixContent == nil || data.Content.Type != "media" { + return nil + } + if metadata.MediaIdentity != inlineMediaIdentity(data.Content) { + // The completed Matrix upload row already has the authoritative chat/message + // ID. Its first native echo learns the source file identity without transfer. + if metadata.MediaIdentity != "" || !data.IsOutgoing || existing.SenderID != makeUserID(strconv.FormatInt(data.SenderID, 10)) { + return nil + } + } + content := cloneMatrixContent(metadata.MatrixContent) + if content == nil || (content.URL == "" && content.File == nil) { + return nil + } + fileName := content.GetFileName() + if fileName == "" { + fileName = inlineMediaFileName(data.Content) + } + content.FileName = fileName + content.Body = inlineMediaBody(data.Content, fileName) + content.Format = "" + content.FormattedBody = "" + content.Mentions = &event.Mentions{} + return &bridgev2.ConvertedMessage{Parts: []*bridgev2.ConvertedMessagePart{{Type: event.EventMessage, Content: content}}} +} + +// Matrix only edits the existing attachment's caption. Check every attachment +// field before calling the source mutation so unsupported replacements cannot +// partially edit the Inline message. +func validateCaptionEdit(original, updated *event.MessageEventContent) error { + if original == nil || updated == nil { + return fmt.Errorf("Inline attachment caption cannot be edited without its saved media identity") + } + if original.MsgType != updated.MsgType || original.URL != updated.URL || original.GetFileName() != updated.GetFileName() { + return fmt.Errorf("replacing an Inline attachment is not supported; send a new message") + } + oldFile, _ := json.Marshal(original.File) + newFile, _ := json.Marshal(updated.File) + oldInfo, _ := json.Marshal(struct { + Info *event.FileInfo + Voice *event.MSC3245Voice + Audio *event.MSC1767Audio + }{original.Info, original.MSC3245Voice, original.MSC1767Audio}) + newInfo, _ := json.Marshal(struct { + Info *event.FileInfo + Voice *event.MSC3245Voice + Audio *event.MSC1767Audio + }{updated.Info, updated.MSC3245Voice, updated.MSC1767Audio}) + if string(oldFile) != string(newFile) || string(oldInfo) != string(newInfo) { + return fmt.Errorf("replacing Inline attachment data or metadata is not supported") + } + return nil +} + +func inlineVoiceMetadata(content sidecar.MessageContent) *event.MSC3245Voice { + if content.Kind == "voice" { + return &event.MSC3245Voice{} + } + return nil +} + +func nonemptyCaption(value string) *string { + if value == "" { + return nil + } + return &value +} + +// Old bridge rows have no saved envelope. Resolve the original Matrix event +// before deciding whether the requested edit is text, a caption or replacement. +func (ic *InlineClient) originalEditContent(ctx context.Context, msg *bridgev2.MatrixEdit, metadata *MessageMetadata) (*event.MessageEventContent, error) { + if metadata != nil && metadata.MatrixContent != nil { + return metadata.MatrixContent, nil + } + if ic.UserLogin == nil || ic.UserLogin.Bridge == nil || ic.UserLogin.Bridge.Bot == nil { + return nil, fmt.Errorf("cannot verify original Inline message content") + } + original, err := ic.UserLogin.Bridge.Bot.GetEvent(ctx, msg.Portal.MXID, msg.EditTarget.MXID) + if err != nil { + return nil, fmt.Errorf("fetch original Matrix message before Inline edit: %w", err) + } + if original == nil || original.Type != event.EventMessage || original.ID != msg.EditTarget.MXID || (original.RoomID != "" && original.RoomID != msg.Portal.MXID) { + return nil, fmt.Errorf("original Inline edit event identity is invalid") + } + return original.Content.AsMessage(), nil +} diff --git a/pkg/connector/message_fidelity_test.go b/pkg/connector/message_fidelity_test.go new file mode 100644 index 0000000..8a946e9 --- /dev/null +++ b/pkg/connector/message_fidelity_test.go @@ -0,0 +1,352 @@ +package connector + +import ( + "context" + "fmt" + "io" + "net/http" + "net/http/httptest" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/inline-chat/matrix-inline/pkg/sidecar" + "maunium.net/go/mautrix" + "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/bridgev2/database" + "maunium.net/go/mautrix/bridgev2/networkid" + "maunium.net/go/mautrix/crypto/attachment" + "maunium.net/go/mautrix/event" + "maunium.net/go/mautrix/id" +) + +func fidelityInt(v int64) *int64 { return &v } +func fidelityString(v string) *string { return &v } + +func TestMatrixRichTextUsesUTF16AndVerifiedMentions(t *testing.T) { + content := &event.MessageEventContent{MsgType: event.MsgText, Body: "fallback", Format: event.FormatHTML, FormattedBody: `

😀 bold and italic *literal* link Known unsafe

x < y
`} + text, entities, err := matrixTextToInline(content, false, func(mxid id.UserID) *int64 { + if mxid == "@known:test" { + return fidelityInt(42) + } + return nil + }) + if err != nil { + t.Fatal(err) + } + if !strings.HasPrefix(text, "😀 bold and italic *literal* link Known unsafe\n") { + t.Fatalf("text = %q", text) + } + kinds := map[string]sidecar.MessageEntityRecord{} + for _, entity := range entities { + kinds[entity.Kind] = entity + } + if kinds["TYPE_BOLD"].Offset != 3 || kinds["TYPE_BOLD"].Length != 15 { + t.Fatalf("bold range = %+v", kinds["TYPE_BOLD"]) + } + if kinds["TYPE_ITALIC"].Offset != 8 { + t.Fatalf("italic = %+v", kinds["TYPE_ITALIC"]) + } + if kinds["TYPE_MENTION"].UserID == nil || *kinds["TYPE_MENTION"].UserID != 42 { + t.Fatalf("mention = %+v", kinds["TYPE_MENTION"]) + } + if kinds["TYPE_PRE"].Value == nil || *kinds["TYPE_PRE"].Value != "go" { + t.Fatalf("pre = %+v", kinds["TYPE_PRE"]) + } + if len(entities) != 6 { + t.Fatalf("entities = %#v", entities) + } +} + +func TestInlineRichTextEscapesCrossingsAndMalformedRanges(t *testing.T) { + content := &event.MessageEventContent{MsgType: event.MsgText, Body: "😀 &"} + applyInlineEntities(content, content.Body, []sidecar.MessageEntityRecord{ + {Kind: "TYPE_BOLD", Offset: 0, Length: 5}, + {Kind: "TYPE_ITALIC", Offset: 3, Length: 4}, // crosses bold + {Kind: "TYPE_CODE", Offset: 1, Length: 1}, // half surrogate, ignored + {Kind: "TYPE_PRE", Offset: -1, Length: 2}, + {Kind: "TYPE_BOLD", Offset: 0, Length: 1 << 62}, + {Kind: "TYPE_TEXT_URL", Offset: 3, Length: 2, Value: fidelityString("javascript:alert(1)")}, + }, nil) + if content.Format != event.FormatHTML || strings.Contains(content.FormattedBody, "") || strings.Contains(content.FormattedBody, "javascript") || strings.Contains(content.FormattedBody, "") { + t.Fatalf("HTML = %q", content.FormattedBody) + } + roundTrip, entities, err := matrixTextToInline(content, false, nil) + if err != nil { + t.Fatal(err) + } + if roundTrip != content.Body { + t.Fatalf("round trip = %q, want %q", roundTrip, content.Body) + } + if len(entities) < 2 { + t.Fatalf("lost formatting: %+v", entities) + } + if content.Mentions == nil || content.Mentions.Room { + t.Fatalf("mentions = %+v", content.Mentions) + } +} + +func TestInlineMentionsDoNotEscalateBroadcastOrUnknownIdentities(t *testing.T) { + content := &event.MessageEventContent{Body: "Alice @room Unknown"} + applyInlineEntities(content, content.Body, []sidecar.MessageEntityRecord{ + {Kind: "TYPE_MENTION", Offset: 0, Length: 5, UserID: fidelityInt(42)}, + {Kind: "TYPE_MENTION", Offset: 6, Length: 5, GroupID: fidelityInt(1)}, + {Kind: "TYPE_MENTION", Offset: 12, Length: 7, UserID: fidelityInt(99)}, + }, func(userID int64) id.UserID { + if userID == 42 { + return "@alice:test" + } + return "" + }) + if content.Mentions.Room || len(content.Mentions.UserIDs) != 1 || content.Mentions.UserIDs[0] != "@alice:test" { + t.Fatalf("mentions = %+v", content.Mentions) + } + if strings.Count(content.FormattedBody, "new") { + t.Fatalf("caption edit = %+v", content) + } + replacement := cloneMatrixContent(content) + replacement.URL = "mxc://test/replacement" + if err := validateCaptionEdit(content, replacement); err == nil { + t.Fatal("accepted attachment replacement") + } + textOnly := &event.MessageEventContent{MsgType: event.MsgText, Body: "replace attachment"} + if err := validateCaptionEdit(content, textOnly); err == nil { + t.Fatal("accepted attachment-to-text replacement") + } +} + +func TestCompletedMatrixUploadEchoReusesOriginalMedia(t *testing.T) { + portal := testPortal("7") + source := sidecar.MessageRecord{ChatID: 7, MessageID: 11, SenderID: 42, IsOutgoing: true, Content: sidecar.MessageContent{Type: "media", Kind: "photo", FileID: "native", URL: "https://must-not-download.invalid"}} + row := &database.Message{ID: makeMessageID(7, 11), Room: portal.PortalKey, SenderID: makeUserID("42"), Metadata: &MessageMetadata{MatrixContent: &event.MessageEventContent{MsgType: event.MsgImage, Body: "picture.jpg", URL: "mxc://test/original"}}} + edit, err := convertInlineMessageEdit(t.Context(), portal, nil, []*database.Message{row}, source) + if err != nil { + t.Fatal(err) + } + if edit.ModifiedParts[0].Content.URL != "mxc://test/original" || edit.ModifiedParts[0].Content.Body != "picture.jpg" { + t.Fatalf("echo = %+v", edit) + } +} + +func TestAuthenticatedMatrixMediaTransportBoundsAndClassifies(t *testing.T) { + for _, test := range []struct { + name string + status int + length int64 + data string + max int64 + permanent bool + wantErr bool + }{ + {"small", 200, 4, "data", 8, false, false}, + {"declared oversize", 200, 100, "data", 8, true, true}, + {"chunked oversize", 200, -1, strings.Repeat("x", 33), 8, true, true}, + {"throttle", 429, 0, "", 8, false, true}, + {"server", 503, 0, "", 8, false, true}, + {"invalid", 404, 0, "", 8, true, true}, + } { + t.Run(test.name, func(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Header.Get("Authorization") != "Bearer secret-test-token" { + t.Errorf("missing authenticated media request") + } + if r.URL.Path != "/_matrix/client/v1/media/download/test/file" { + t.Errorf("media route = %s", r.URL.Path) + } + if test.length >= 0 { + w.Header().Set("Content-Length", fmt.Sprint(test.length)) + } + w.WriteHeader(test.status) + if test.length < 0 { + w.(http.Flusher).Flush() + } + _, _ = io.WriteString(w, test.data) + })) + defer server.Close() + client, err := mautrix.NewClient(server.URL, "@bridge:test", "secret-test-token") + if err != nil { + t.Fatal(err) + } + client.DefaultHTTPRetries = 0 + data, err := downloadBoundedMatrixMedia(t.Context(), client, &event.MessageEventContent{URL: "mxc://test/file"}, test.max) + if (err != nil) != test.wantErr { + t.Fatalf("download bytes %q, error %v", data, err) + } + if err != nil && isPermanentMediaFailure(err) != test.permanent { + t.Fatalf("error classification = %v: %v", isPermanentMediaFailure(err), err) + } + }) + } +} + +type fidelityDownloadFunc func(context.Context, id.ContentURI) (*http.Response, error) + +func (f fidelityDownloadFunc) Download(ctx context.Context, uri id.ContentURI) (*http.Response, error) { + return f(ctx, uri) +} +func TestBoundedMatrixMediaRejectsLyingLengthAndEncryptedHashMismatch(t *testing.T) { + file := attachment.NewEncryptedFile() + encrypted := file.Encrypt([]byte("plaintext")) + encrypted[0] ^= 0xff + downloader := fidelityDownloadFunc(func(context.Context, id.ContentURI) (*http.Response, error) { + return &http.Response{StatusCode: 200, ContentLength: 1, Body: io.NopCloser(strings.NewReader(string(encrypted)))}, nil + }) + content := &event.MessageEventContent{File: &event.EncryptedFileInfo{EncryptedFile: *file, URL: "mxc://test/file"}} + if _, err := downloadBoundedMatrixMedia(t.Context(), downloader, content, 64); err == nil || !isPermanentMediaFailure(err) { + t.Fatalf("hash mismatch = %v", err) + } + if _, err := downloadBoundedMatrixMedia(t.Context(), downloader, content, 2); err == nil || !isPermanentMediaFailure(err) { + t.Fatalf("lying Content-Length = %v", err) + } + content.Info = &event.FileInfo{Size: 100} + calls := 0 + neverDownload := fidelityDownloadFunc(func(context.Context, id.ContentURI) (*http.Response, error) { + calls++ + return nil, fmt.Errorf("unexpected download") + }) + if _, err := downloadBoundedMatrixMedia(t.Context(), neverDownload, content, 2); err == nil || calls != 0 { + t.Fatalf("declared size = %v, calls %d", err, calls) + } +} + +func TestBoundedMatrixMediaNetworkFailureIsRetryable(t *testing.T) { + downloader := fidelityDownloadFunc(func(context.Context, id.ContentURI) (*http.Response, error) { return nil, context.DeadlineExceeded }) + if _, err := downloadBoundedMatrixMedia(t.Context(), downloader, &event.MessageEventContent{URL: "mxc://test/file"}, 8); err == nil || isPermanentMediaFailure(err) { + t.Fatalf("network error = %v", err) + } +} + +func TestMatrixEditRejectsAttachmentReplacementBeforeSourceMutation(t *testing.T) { + calls := 0 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + calls++ + http.Error(w, "unexpected source mutation", 500) + })) + defer server.Close() + client := &InlineClient{UserLogin: &bridgev2.UserLogin{UserLogin: &database.UserLogin{ID: "42"}}, Sidecar: sidecar.NewClient(server.URL)} + portal := testPortal("7") + portal.Receiver = "42" + original := &event.MessageEventContent{MsgType: event.MsgImage, Body: "picture.jpg", URL: "mxc://test/file", Info: &event.FileInfo{MimeType: "image/jpeg"}} + target := &database.Message{ID: makeMessageID(7, 11), Room: portal.PortalKey, Metadata: &MessageMetadata{MatrixContent: original}} + replacements := []*event.MessageEventContent{ + {MsgType: event.MsgText, Body: "replace with text"}, + {MsgType: event.MsgImage, Body: "new caption", FileName: "picture.jpg", URL: "mxc://test/replacement"}, + } + for _, replacement := range replacements { + err := client.HandleMatrixEdit(t.Context(), &bridgev2.MatrixEdit{MatrixEventBase: bridgev2.MatrixEventBase[*event.MessageEventContent]{Portal: portal, Content: replacement}, EditTarget: target}) + if err == nil { + t.Fatal("accepted attachment replacement") + } + } + if calls != 0 { + t.Fatalf("source mutations before validation: %d", calls) + } + legitimate := cloneMatrixContent(original) + legitimate.Body = "caption" + legitimate.FileName = "picture.jpg" + if err := validateCaptionEdit(original, legitimate); err != nil { + t.Fatalf("caption with original filename rejected: %v", err) + } +} diff --git a/pkg/connector/navigation.go b/pkg/connector/navigation.go new file mode 100644 index 0000000..68eceb5 --- /dev/null +++ b/pkg/connector/navigation.go @@ -0,0 +1,429 @@ +package connector + +import ( + "context" + "fmt" + "strconv" + "strings" + "time" + + "github.com/inline-chat/matrix-inline/pkg/sidecar" + "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/bridgev2/database" + "maunium.net/go/mautrix/bridgev2/networkid" + "maunium.net/go/mautrix/event" + "maunium.net/go/mautrix/id" +) + +// Navigation belongs to a source login, including on bridges that share chat +// portals across logins. Every generated workspace Space has an account-qualified +// ID, and only this login's current source membership can activate it. +type WorkspaceNavigation struct { + SpaceID int64 `json:"space_id"` + Name string `json:"name"` + Active bool `json:"active"` + RoomID id.RoomID `json:"room_id,omitempty"` + Links map[string]WorkspaceRoomLink `json:"links,omitempty"` +} + +type WorkspaceRoomLink struct { + RoomID id.RoomID `json:"room_id"` + Attached bool `json:"attached"` +} + +func (ic *InlineClient) workspacePortalID(spaceID int64) networkid.PortalID { + account := ic.AccountID + if account == "" && ic.UserLogin != nil { + account = string(ic.UserLogin.ID) + } + return networkid.PortalID("workspace:" + account + ":" + strconv.FormatInt(spaceID, 10)) +} + +func (ic *InlineClient) workspaceIDFromKey(key networkid.PortalKey) (int64, error) { + if ic.UserLogin == nil { + return 0, fmt.Errorf("Inline login is unavailable") + } + if key.Receiver != "" && key.Receiver != ic.UserLogin.ID { + return 0, fmt.Errorf("Inline workspace belongs to a different login") + } + prefix := string(ic.workspacePortalID(0)) + prefix = strings.TrimSuffix(prefix, "0") + raw, ok := strings.CutPrefix(string(key.ID), prefix) + if !ok { + return 0, fmt.Errorf("parent must be an Inline workspace for this login; creating native child conversations from Matrix is not supported") + } + value, err := strconv.ParseInt(raw, 10, 64) + if err != nil || value <= 0 { + return 0, fmt.Errorf("invalid Inline workspace ID") + } + return value, nil +} + +func cloneWorkspaceNavigation(source map[string]*WorkspaceNavigation) map[string]*WorkspaceNavigation { + copy := make(map[string]*WorkspaceNavigation, len(source)) + for key, value := range source { + if value == nil { + continue + } + entry := *value + entry.Links = make(map[string]WorkspaceRoomLink, len(value.Links)) + for chatID, link := range value.Links { + entry.Links[chatID] = link + } + copy[key] = &entry + } + return copy +} + +func (ic *InlineClient) workspaceNavigationSnapshot() map[string]*WorkspaceNavigation { + ic.metadataSaveMu.Lock() + defer ic.metadataSaveMu.Unlock() + if ic.UserLogin == nil { + return make(map[string]*WorkspaceNavigation) + } + meta, ok := ic.UserLogin.Metadata.(*UserLoginMetadata) + if !ok || meta == nil { + return make(map[string]*WorkspaceNavigation) + } + return cloneWorkspaceNavigation(meta.WorkspaceNavigation) +} + +func (ic *InlineClient) saveWorkspaceNavigation(ctx context.Context, spaces map[string]*WorkspaceNavigation) error { + ic.metadataSaveMu.Lock() + defer ic.metadataSaveMu.Unlock() + if ic.UserLogin == nil { + return fmt.Errorf("Inline login unavailable") + } + meta, ok := ic.UserLogin.Metadata.(*UserLoginMetadata) + if !ok || meta == nil { + return fmt.Errorf("Inline login metadata unavailable") + } + meta.WorkspaceNavigation = cloneWorkspaceNavigation(spaces) + if ic.UserLogin.Bridge == nil { + return nil + } + return ic.UserLogin.Save(ctx) +} + +func (ic *InlineClient) workspacePortalInfo(portal *bridgev2.Portal) (*bridgev2.ChatInfo, bool, error) { + if portal == nil || !strings.HasPrefix(string(portal.ID), "workspace:") { + return nil, false, nil + } + spaceID, err := ic.workspaceIDFromKey(portal.PortalKey) + if err != nil { + return nil, true, err + } + space := ic.workspaceNavigationSnapshot()[strconv.FormatInt(spaceID, 10)] + if space == nil || !space.Active { + return nil, true, fmt.Errorf("Inline workspace membership is not confirmed for this login") + } + return ic.chatInfoForWorkspace(space), true, nil +} + +func (ic *InlineClient) chatInfoForWorkspace(space *WorkspaceNavigation) *bridgev2.ChatInfo { + roomType := database.RoomTypeSpace + members := ic.chatMemberListForKnownSelf(context.Background()) + if members != nil { + members.CheckAllLogins = false + } + return &bridgev2.ChatInfo{Name: &space.Name, Type: &roomType, Members: members, ExcludeChangesFromTimeline: true} +} + +func applyWorkspaceAccess(spaces map[string]*WorkspaceNavigation, snapshot *sidecar.AccountStateSnapshot, selfID int64) { + if snapshot == nil { + return + } + removed := make(map[int64]bool, len(snapshot.RemovedSpaceIDs)) + for _, spaceID := range snapshot.RemovedSpaceIDs { + removed[spaceID] = true + } + current := make(map[int64]bool) + for _, member := range snapshot.SpaceMemberships { + if member.UserID == selfID { + current[member.SpaceID] = true + } + } + present := make(map[int64]bool) + for _, record := range snapshot.Spaces { + if record.SpaceID <= 0 { + continue + } + present[record.SpaceID] = true + key := strconv.FormatInt(record.SpaceID, 10) + entry := spaces[key] + // Incomplete refreshes retain structure but never invent new memberships. + if entry == nil && (!current[record.SpaceID] || removed[record.SpaceID]) { + continue + } + if entry == nil { + entry = &WorkspaceNavigation{SpaceID: record.SpaceID, Links: make(map[string]WorkspaceRoomLink)} + spaces[key] = entry + } + if record.Name != "" { + entry.Name = record.Name + } + if current[record.SpaceID] && !removed[record.SpaceID] { + entry.Active = true + } else if snapshot.SpacesComplete { + entry.Active = false + } + } + for _, entry := range spaces { + if removed[entry.SpaceID] || (snapshot.SpacesComplete && (!present[entry.SpaceID] || !current[entry.SpaceID])) { + entry.Active = false + } + } +} + +func (ic *InlineClient) refreshWorkspaceNavigation(ctx context.Context) error { + _, err := ic.fetchWorkspaceNavigation(ctx, true) + return err +} + +// Serialize the source read as well as reconciliation. Otherwise an older HTTP +// response can arrive after a revocation has been persisted and reactivate it. +func (ic *InlineClient) fetchWorkspaceNavigation(ctx context.Context, refresh bool) (*sidecar.AccountStateSnapshot, error) { + ic.workspaceStateMu.Lock() + defer ic.workspaceStateMu.Unlock() + var snapshot *sidecar.AccountStateSnapshot + var err error + if refresh { + snapshot, err = ic.Sidecar.RefreshAccountState(ctx) + } else { + snapshot, err = ic.Sidecar.AccountState(ctx) + } + if err != nil { + return nil, err + } + if snapshot == nil { + return nil, fmt.Errorf("Inline returned no account navigation state") + } + if err := ic.reconcileWorkspaceNavigation(ctx, snapshot); err != nil { + return nil, err + } + return snapshot, nil +} + +func (ic *InlineClient) reconcileWorkspaceNavigation(ctx context.Context, snapshot *sidecar.AccountStateSnapshot) error { + if ic.UserLogin == nil { + return nil + } + ic.navigationMu.Lock() + defer ic.navigationMu.Unlock() + spaces := ic.workspaceNavigationSnapshot() + selfID, err := parseInlineUserID(string(ic.GetUserID())) + if err != nil { + return err + } + applyWorkspaceAccess(spaces, snapshot, selfID) + // Persist revocation before touching Matrix so a restart cannot recreate links + // from the previous access view. Pending links are retried idempotently. + if err := ic.saveWorkspaceNavigation(ctx, spaces); err != nil { + return err + } + if ic.UserLogin.Bridge == nil { + return nil + } + var firstErr error + for _, space := range spaces { + if space.Active { + if err := ic.ensureWorkspaceRoom(ctx, space); err != nil { + if firstErr == nil { + firstErr = err + } + continue + } + } + for chatID, link := range space.Links { + remove := !space.Active + if parsed, err := strconv.ParseInt(chatID, 10, 64); err == nil { + if dialog, ok := ic.cachedDialog(parsed); ok { + remove = remove || !ic.shouldProjectDialog(dialog) || (dialog.StructureKnown && (dialog.SpaceID == nil || *dialog.SpaceID != space.SpaceID)) + } + } + if remove || !link.Attached { + if !remove { + parsed, err := strconv.ParseInt(chatID, 10, 64) + if err != nil || parsed <= 0 || ic.Sidecar == nil { + if firstErr == nil { + firstErr = fmt.Errorf("pending Inline navigation has no verifiable source chat") + } + continue + } + if _, err := ic.Sidecar.ChatParticipants(ctx, sidecar.ChatParticipantsRequest{ChatID: parsed}); err != nil { + if firstErr == nil { + firstErr = err + } + continue + } + } + if err := ic.sendWorkspaceLink(ctx, space.RoomID, link.RoomID, remove); err != nil { + if firstErr == nil { + firstErr = err + } + continue + } + if remove { + delete(space.Links, chatID) + } else { + link.Attached = true + space.Links[chatID] = link + } + } + } + } + if err := ic.saveWorkspaceNavigation(ctx, spaces); err != nil { + return err + } + // Existing independently accessible rooms can be attached after a successful + // refresh; no new chat portal is created merely because a workspace lists it. + ic.mu.RLock() + dialogs := make([]sidecar.DialogRecord, 0, len(ic.dialogs)) + for _, dialog := range ic.dialogs { + dialogs = append(dialogs, dialog) + } + ic.mu.RUnlock() + for _, dialog := range dialogs { + portal, err := ic.UserLogin.Bridge.GetExistingPortalByKey(ctx, ic.portalKeyForChat(dialog.ChatID)) + if err != nil { + if firstErr == nil { + firstErr = err + } + continue + } + if err := ic.syncDialogWorkspaceLinkLocked(ctx, portal, dialog, spaces); err != nil && firstErr == nil { + firstErr = err + } + } + return firstErr +} + +func (ic *InlineClient) ensureWorkspaceRoom(ctx context.Context, space *WorkspaceNavigation) error { + portal, err := ic.UserLogin.Bridge.GetPortalByKey(ctx, networkid.PortalKey{ID: ic.workspacePortalID(space.SpaceID), Receiver: ic.UserLogin.ID}) + if err != nil { + return err + } + if portal.MXID != "" && portal.RoomType != database.RoomTypeSpace { + return fmt.Errorf("Inline workspace portal is not a Matrix Space") + } + info := ic.chatInfoForWorkspace(space) + if portal.MXID == "" { + err = portal.CreateMatrixRoom(ctx, ic.UserLogin, info) + } else { + portal.UpdateInfo(ctx, info, ic.UserLogin, nil, time.Time{}) + } + if err != nil { + return err + } + space.RoomID = portal.MXID + return nil +} + +func (ic *InlineClient) syncDialogWorkspaceLink(ctx context.Context, portal *bridgev2.Portal, dialog sidecar.DialogRecord) error { + if ic.UserLogin == nil || ic.UserLogin.Bridge == nil { + return nil + } + ic.navigationMu.Lock() + defer ic.navigationMu.Unlock() + return ic.syncDialogWorkspaceLinkLocked(ctx, portal, dialog, ic.workspaceNavigationSnapshot()) +} + +// A source chat deletion revokes its navigation independently of workspace +// membership. Persisted source deletion events keep this cleanup retryable. +func (ic *InlineClient) removeChatWorkspaceLinks(ctx context.Context, chatID int64) error { + if ic.UserLogin == nil || ic.UserLogin.Bridge == nil { + return nil + } + ic.navigationMu.Lock() + defer ic.navigationMu.Unlock() + spaces := ic.workspaceNavigationSnapshot() + chatKey := strconv.FormatInt(chatID, 10) + changed := false + for _, space := range spaces { + link, exists := space.Links[chatKey] + if !exists { + continue + } + if err := ic.sendWorkspaceLink(ctx, space.RoomID, link.RoomID, true); err != nil { + return err + } + delete(space.Links, chatKey) + changed = true + } + if changed { + return ic.saveWorkspaceNavigation(ctx, spaces) + } + return nil +} + +func (ic *InlineClient) syncDialogWorkspaceLinkLocked(ctx context.Context, portal *bridgev2.Portal, dialog sidecar.DialogRecord, spaces map[string]*WorkspaceNavigation) error { + if portal == nil || portal.MXID == "" || !dialog.StructureKnown { + return nil + } + chatKey := strconv.FormatInt(dialog.ChatID, 10) + var desired *WorkspaceNavigation + if dialog.SpaceID != nil && ic.shouldProjectDialog(dialog) && !isDMDialog(dialog) { + desired = spaces[strconv.FormatInt(*dialog.SpaceID, 10)] + if desired != nil && !desired.Active { + desired = nil + } + } + for _, space := range spaces { + link, exists := space.Links[chatKey] + if exists && (space != desired || link.RoomID != portal.MXID) { + if err := ic.sendWorkspaceLink(ctx, space.RoomID, link.RoomID, true); err != nil { + return err + } + delete(space.Links, chatKey) + if err := ic.saveWorkspaceNavigation(ctx, spaces); err != nil { + return err + } + } + } + if desired == nil { + return nil + } + if desired.RoomID == "" { + if err := ic.ensureWorkspaceRoom(ctx, desired); err != nil { + return err + } + } + if desired.Links == nil { + desired.Links = make(map[string]WorkspaceRoomLink) + } + if link, exists := desired.Links[chatKey]; exists && link.RoomID == portal.MXID && link.Attached { + return nil + } + // Workspace membership alone does not grant access to a private chat. + // Only create a new navigation entry after this login can inspect its source audience. + if ic.Sidecar == nil { + return fmt.Errorf("Inline source access check is unavailable") + } + if _, err := ic.Sidecar.ChatParticipants(ctx, sidecar.ChatParticipantsRequest{ChatID: dialog.ChatID}); err != nil { + return fmt.Errorf("verify Inline chat access before adding workspace navigation: %w", err) + } + desired.Links[chatKey] = WorkspaceRoomLink{RoomID: portal.MXID} + if err := ic.saveWorkspaceNavigation(ctx, spaces); err != nil { + return err + } + if err := ic.sendWorkspaceLink(ctx, desired.RoomID, portal.MXID, false); err != nil { + return err + } + desired.Links[chatKey] = WorkspaceRoomLink{RoomID: portal.MXID, Attached: true} + return ic.saveWorkspaceNavigation(ctx, spaces) +} + +func (ic *InlineClient) sendWorkspaceLink(ctx context.Context, spaceID, roomID id.RoomID, remove bool) error { + if spaceID == "" || roomID == "" { + if remove { + return nil + } + return fmt.Errorf("workspace room is not created yet") + } + via := []string{ic.UserLogin.Bridge.Matrix.ServerName()} + if remove { + via = nil + } + _, err := ic.UserLogin.Bridge.Bot.SendState(ctx, spaceID, event.StateSpaceChild, string(roomID), &event.Content{Parsed: &event.SpaceChildEventContent{Via: via}}, time.Now()) + return err +} diff --git a/pkg/connector/read_projection.go b/pkg/connector/read_projection.go new file mode 100644 index 0000000..e364857 --- /dev/null +++ b/pkg/connector/read_projection.go @@ -0,0 +1,322 @@ +package connector + +import ( + "context" + "database/sql" + "encoding/json" + "errors" + "fmt" + "time" + + "github.com/inline-chat/matrix-inline/pkg/sidecar" + "maunium.net/go/mautrix" + "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/bridgev2/database" + "maunium.net/go/mautrix/bridgev2/matrix" + "maunium.net/go/mautrix/bridgev2/networkid" + "maunium.net/go/mautrix/event" + "maunium.net/go/mautrix/id" +) + +// Use the bridge's existing durable KV table, with errors propagated. The +// framework's convenience KV helpers only log write failures. +func (ic *InlineClient) readProjectionKey(chatID int64) string { + namespace, _, _ := ic.sidecarEventCursor() + key, _ := json.Marshal([]any{"inline_read_v1", string(ic.UserLogin.ID), namespace, chatID}) + return string(key) +} + +func (ic *InlineClient) saveReadIntent(ctx context.Context, state *sidecar.ReadStateRecord) error { + data, err := json.Marshal(state) + if err != nil { + return err + } + _, err = ic.UserLogin.Bridge.DB.Exec(ctx, `INSERT INTO kv_store (bridge_id, key, value) VALUES ($1, $2, $3) ON CONFLICT (bridge_id, key) DO UPDATE SET value=$3`, ic.UserLogin.Bridge.ID, ic.readProjectionKey(state.ChatID), string(data)) + return err +} + +func (ic *InlineClient) loadReadIntent(ctx context.Context, chatID int64) (*sidecar.ReadStateRecord, error) { + var data string + err := ic.UserLogin.Bridge.DB.QueryRow(ctx, `SELECT value FROM kv_store WHERE bridge_id=$1 AND key=$2`, ic.UserLogin.Bridge.ID, ic.readProjectionKey(chatID)).Scan(&data) + if errors.Is(err, sql.ErrNoRows) { + return nil, nil + } + if err != nil { + return nil, err + } + var state sidecar.ReadStateRecord + if err := json.Unmarshal([]byte(data), &state); err != nil { + return nil, err + } + return &state, nil +} + +// A read intent is durable before the lossless source event is acknowledged. +// Failed/unsupported projections remain retryable on the regular dialog scan. +func (ic *InlineClient) queueReadState(state *sidecar.ReadStateRecord) error { + if state == nil { + return nil + } + if state.ChatID <= 0 { + return errors.New("invalid read-state chat") + } + ctx := ic.UserLogin.Bridge.BackgroundCtx + ic.readProjectionMu.Lock() + defer ic.readProjectionMu.Unlock() + if err := ic.saveReadIntent(ctx, state); err != nil { + return fmt.Errorf("persist Inline read intent: %w", err) + } + ic.tryReadProjection(ctx, state) + return nil +} + +func (ic *InlineClient) retryReadProjection(ctx context.Context, chatID int64) { + // The source consumer and periodic scan share a lock so an old intent cannot + // finish after a newer one. Network operations below have a short deadline. + ic.readProjectionMu.Lock() + defer ic.readProjectionMu.Unlock() + if ic.readProjectionAttempts == nil { + ic.readProjectionAttempts = make(map[int64]time.Time) + } + if time.Since(ic.readProjectionAttempts[chatID]) < time.Minute { + return + } + state, err := ic.loadReadIntent(ctx, chatID) + if err != nil { + ic.UserLogin.Log.Warn().Err(err).Msg("Unable to load pending read projection") + return + } + if state != nil { + ic.tryReadProjection(ctx, state) + } +} + +func (ic *InlineClient) tryReadProjection(ctx context.Context, state *sidecar.ReadStateRecord) { + if ic.readProjectionAttempts == nil { + ic.readProjectionAttempts = make(map[int64]time.Time) + } + ic.readProjectionAttempts[state.ChatID] = time.Now() + ctx, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + if err := ic.projectReadState(ctx, state); err != nil { + ic.UserLogin.Log.Debug().Err(err).Int64("chat_id", state.ChatID).Msg("Inline read projection remains pending") + } +} + +func (ic *InlineClient) projectReadState(ctx context.Context, state *sidecar.ReadStateRecord) error { + // Native marked-unread is not allowed to overwrite a later Matrix user choice. + // Nor does writing a receipt clear the Matrix marked-unread account data. + if state.MarkedUnread || state.ReadMaxID == nil || *state.ReadMaxID <= 0 { + return nil + } + if ic.UserLogin.User == nil { + return errors.New("read projection needs a double puppet") + } + puppet, ok := ic.UserLogin.User.DoublePuppet(ctx).(*matrix.ASIntent) + if !ok || puppet == nil || !puppet.IsDoublePuppet() { + return errors.New("read projection needs supported double-puppet transport") + } + portal, err := ic.UserLogin.Bridge.GetExistingPortalByKey(ctx, ic.portalKeyForChat(state.ChatID)) + if err != nil { + return err + } + if portal == nil || portal.MXID == "" { + return errors.New("read projection awaits room creation") + } + timeline, err := readTimeline(ctx, puppet.Matrix.Client, portal.MXID) + if err != nil { + return err + } + target, err := verifiedReadPrefix(ctx, timeline, portal.PortalKey, *state.ReadMaxID, ic.UserLogin.Bridge.DB.Message.GetPartByMXID) + if err != nil || target == "" { + return err + } + return writePrivateReadMarkers(ctx, puppet, portal.MXID, target) +} + +func writePrivateReadMarkers(ctx context.Context, puppet *matrix.ASIntent, room id.RoomID, target id.EventID) error { + extra := map[string]any{} + puppet.Matrix.AddDoublePuppetValue(extra) + // Bypass ASIntent.MarkRead: it sends a public receipt and can also clear + // marked-unread. These private markers leave the user's unread flag alone. + return puppet.Matrix.SetReadMarkers(ctx, room, &mautrix.ReqSetReadMarkers{ + ReadPrivate: target, FullyRead: target, + BeeperReadPrivateExtra: extra, BeeperFullyReadExtra: extra, + }) +} + +func (ic *InlineClient) verifySourceReadReceipt(ctx context.Context, msg *bridgev2.MatrixReadReceipt, chatID, messageID int64) error { + if msg.EventID == "" || msg.ExactMessage.MXID != msg.EventID || msg.ExactMessage.HasFakeMXID() { + return errors.New("source read requires an explicit real Matrix event") + } + if ic.UserLogin.Bridge == nil || ic.UserLogin.Bridge.DB == nil { + return errors.New("source read coverage is unavailable") + } + coverage, err := ic.loadHistoryCheckpoint(ctx, chatID) + if err != nil { + return err + } + if coverage < messageID { + return errors.New("source read awaits completed history delivery") + } + intent, ok := ic.UserLogin.Bridge.Bot.(*matrix.ASIntent) + if !ok || intent == nil || intent.Matrix == nil || intent.Matrix.Client == nil { + return errors.New("source read requires supported timeline inspection") + } + ctx, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + timeline, err := readTimeline(ctx, intent.Matrix.Client, msg.Portal.MXID) + if err != nil { + return fmt.Errorf("inspect Matrix source read order: %w", err) + } + safe, err := verifiedSourceReadReceipt(ctx, timeline, msg.Portal.PortalKey, msg.EventID, messageID, ic.UserLogin.Bridge.DB.Message.GetPartByMXID) + if err != nil { + return err + } + if !safe { + return errors.New("source read would cross an unverified or later Matrix message") + } + return nil +} + +// A source receipt reads every source ID up to its target. All of those messages +// must therefore already be delivered, and none may occur after the explicitly +// read Matrix event. Timestamp and source-ID order cannot prove Matrix order. +func verifiedSourceReadReceipt(ctx context.Context, timeline []*event.Event, key networkid.PortalKey, target id.EventID, readMax int64, lookup func(context.Context, id.EventID) (*database.Message, error)) (bool, error) { + found := false + seenSenders := make(map[id.EventID]id.UserID) + for _, evt := range timeline { + if evt == nil { + return false, nil + } + if evt.StateKey != nil || evt.Type == event.EventReaction || evt.Type == event.EventRedaction { + continue + } + mapped, err := lookup(ctx, evt.ID) + if err != nil { + return false, err + } + if mapped == nil { + transparent, err := verifiedTimelineEdit(ctx, evt, key, seenSenders, lookup) + if err != nil { + return false, err + } + if transparent { + continue + } + } + if mapped == nil || mapped.Room != key || mapped.MXID != evt.ID || mapped.HasFakeMXID() { + return false, nil + } + chatID, messageID, err := parseMessageID(mapped.ID) + if err != nil || key.ID != networkid.PortalID(fmt.Sprint(chatID)) || messageID <= 0 { + return false, nil + } + seenSenders[evt.ID] = evt.Sender + if evt.ID == target { + if found || messageID != readMax { + return false, nil + } + found = true + } else if found && messageID <= readMax { + return false, nil + } + } + return found, nil +} + +// An authenticated plaintext replacement from the original Matrix sender does +// not introduce another source message. Its original must already have appeared +// in this inspected timeline with a real mapping for this portal. Edits are only +// transparent to traversal; they never become receipt targets themselves. +func verifiedTimelineEdit(ctx context.Context, evt *event.Event, key networkid.PortalKey, seenSenders map[id.EventID]id.UserID, lookup func(context.Context, id.EventID) (*database.Message, error)) (bool, error) { + if evt.Type != event.EventMessage || evt.Sender == "" { + return false, nil + } + if evt.Content.Parsed == nil { + if err := evt.Content.ParseRaw(evt.Type); err != nil { + return false, nil + } + } + content := evt.Content.AsMessage() + relation := content.OptionalGetRelatesTo() + if content.NewContent == nil || relation == nil || relation.Type != event.RelReplace || relation.EventID == evt.ID || seenSenders[relation.EventID] != evt.Sender { + return false, nil + } + mapped, err := lookup(ctx, relation.EventID) + if err != nil { + return false, err + } + if mapped == nil || mapped.Room != key || mapped.MXID != relation.EventID || mapped.HasFakeMXID() || (mapped.SenderMXID != "" && mapped.SenderMXID != evt.Sender) { + return false, nil + } + chatID, messageID, err := parseMessageID(mapped.ID) + return err == nil && messageID > 0 && key.ID == networkid.PortalID(fmt.Sprint(chatID)), nil +} + +// Read an entire bounded timeline before proving a prefix. Source IDs and +// timestamps are not Matrix order; newer delivery can insert older source IDs. +func readTimeline(ctx context.Context, client *mautrix.Client, room id.RoomID) ([]*event.Event, error) { + var reverse []*event.Event + cursor := "" + for page := 0; page < 20; page++ { + response, err := client.Messages(ctx, room, cursor, "", mautrix.DirectionBackward, nil, 500) + if err != nil { + return nil, err + } + reverse = append(reverse, response.Chunk...) + if len(reverse) > 10000 { + return nil, errors.New("read timeline inspection limit reached") + } + // Matrix may return an empty intermediate page. Only an absent end + // token proves that there is no earlier visible history to inspect. + if response.End == "" { + for i, j := 0, len(reverse)-1; i < j; i, j = i+1, j-1 { + reverse[i], reverse[j] = reverse[j], reverse[i] + } + return reverse, nil + } + if response.End == cursor { + return nil, errors.New("read timeline cursor did not advance") + } + cursor = response.End + } + return nil, errors.New("read timeline inspection limit reached") +} + +func verifiedReadPrefix(ctx context.Context, timeline []*event.Event, key networkid.PortalKey, readMax int64, lookup func(context.Context, id.EventID) (*database.Message, error)) (id.EventID, error) { + var target id.EventID + seenSenders := make(map[id.EventID]id.UserID) + for _, evt := range timeline { + if evt == nil { + return target, nil + } + if evt.StateKey != nil || evt.Type == event.EventReaction || evt.Type == event.EventRedaction { + continue + } + // Unknown non-state events are barriers too, including polls and stickers. + mapped, err := lookup(ctx, evt.ID) + if err != nil { + return "", err + } + if mapped == nil { + transparent, err := verifiedTimelineEdit(ctx, evt, key, seenSenders, lookup) + if err != nil { + return "", err + } + if transparent { + continue + } + } + if mapped == nil || mapped.Room != key || mapped.HasFakeMXID() { + break + } + chatID, messageID, err := parseMessageID(mapped.ID) + if err != nil || key.ID != networkid.PortalID(fmt.Sprint(chatID)) || messageID > readMax { + break + } + seenSenders[evt.ID] = evt.Sender + target = evt.ID + } + return target, nil +} diff --git a/pkg/connector/repair.go b/pkg/connector/repair.go new file mode 100644 index 0000000..12fd564 --- /dev/null +++ b/pkg/connector/repair.go @@ -0,0 +1,107 @@ +package connector + +import ( + "context" + "fmt" + "strconv" + "time" + + "github.com/inline-chat/matrix-inline/pkg/sidecar" + "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/bridgev2/commands" + "maunium.net/go/mautrix/bridgev2/database" + "maunium.net/go/mautrix/bridgev2/simplevent" +) + +var commandInlineRepairRoom = &commands.FullHandler{ + Func: fnInlineRepairRoom, Name: "inline-repair-room", RequiresLogin: true, + Help: commands.HelpMeta{Section: commands.HelpSectionMisc, Description: "Preview or apply an in-place correction for a channel previously classified as a group DM", Args: " [apply]"}, +} + +func fnInlineRepairRoom(ce *commands.Event) { + _, client := inlineCommandLogin(ce) + if client == nil { + ce.Reply("Your default login is not an Inline login.") + return + } + if len(ce.Args) < 1 || len(ce.Args) > 2 || (len(ce.Args) == 2 && ce.Args[1] != "apply") { + ce.Reply("Usage: `$cmdprefix inline-repair-room [apply]`") + return + } + chatID, err := parseInlineUserID(ce.Args[0]) + if err != nil { + ce.Reply("Invalid Inline chat ID: %v", err) + return + } + summary, err := client.repairRoomClassification(ce.Ctx, chatID, len(ce.Args) == 2) + if err != nil { + ce.Reply("Inline room repair failed: %v", err) + return + } + ce.Reply("%s", summary) +} + +func (ic *InlineClient) repairRoomClassification(ctx context.Context, chatID int64, apply bool) (string, error) { + if ic.UserLogin == nil || ic.UserLogin.Bridge == nil { + return "", fmt.Errorf("Inline bridge is unavailable") + } + state, err := ic.Sidecar.ChatState(ctx, sidecar.ChatStateRequest{ChatID: chatID}) + if err != nil { + return "", err + } + if state.Deleted || state.Dialog == nil || state.Dialog.ChatID != chatID || !state.Dialog.StructureKnown || state.Dialog.Kind != sidecar.ConversationChat { + return "", fmt.Errorf("source has not confirmed an ordinary Inline conversation for this chat") + } + if !ic.shouldProjectDialog(*state.Dialog) { + return "", fmt.Errorf("this Inline chat is excluded by the hidden chat setting") + } + if _, err := ic.Sidecar.ChatParticipants(ctx, sidecar.ChatParticipantsRequest{ChatID: chatID}); err != nil { + return "", fmt.Errorf("source access check failed: %w", err) + } + portal, err := ic.UserLogin.Bridge.GetExistingPortalByKey(ctx, ic.portalKeyForChat(chatID)) + if err != nil { + return "", err + } + if portal == nil || portal.MXID == "" { + return "", fmt.Errorf("there is no existing Matrix room for Inline chat %d", chatID) + } + membership, err := ic.UserLogin.Bridge.DB.UserPortal.Get(ctx, ic.UserLogin.UserLogin, portal.PortalKey) + if err != nil { + return "", err + } + if membership == nil { + return "", fmt.Errorf("the Matrix room is not mapped for this Inline login") + } + if portal.RoomType != database.RoomTypeGroupDM { + return fmt.Sprintf("Inline chat %d already uses room classification %q; no repair needed.", chatID, portal.RoomType), nil + } + if !apply { + return fmt.Sprintf("Preview: change Inline chat %d (%s) from group DM to ordinary room in place. Room and message IDs, topics and notification preferences stay the same. Apply with `$cmdprefix inline-repair-room %d apply`.", chatID, portal.MXID, chatID), nil + } + result := ic.UserLogin.Bridge.QueueRemoteEvent(ic.UserLogin, &simplevent.ChatResync{ + EventMeta: simplevent.EventMeta{Type: bridgev2.RemoteEventChatResync, PortalKey: portal.PortalKey, Timestamp: time.Now()}, + GetChatInfoFunc: classificationRepairInfo, + }) + if err := eventHandlingResultError("room classification repair", result); err != nil { + return "", err + } + if portal.RoomType != database.RoomTypeDefault { + return "", fmt.Errorf("room classification repair did not complete") + } + return "Repaired Inline chat " + strconv.FormatInt(chatID, 10) + " in place (" + string(portal.MXID) + "). The previous classification is saved in bridge metadata.", nil +} + +func classificationRepairInfo(ctx context.Context, actual *bridgev2.Portal) (*bridgev2.ChatInfo, error) { + if actual.RoomType != database.RoomTypeGroupDM { + return nil, nil + } + meta := portalIdentityMetadata(actual) + if meta.ClassificationRepair == nil { + meta.ClassificationRepair = &RoomClassificationRepair{PreviousType: actual.RoomType, PreviousOtherUserID: string(actual.OtherUserID), AppliedAt: time.Now().UTC()} + if err := actual.Save(ctx); err != nil { + return nil, err + } + } + roomType := database.RoomTypeDefault + return &bridgev2.ChatInfo{Type: &roomType, ExcludeChangesFromTimeline: true}, nil +} diff --git a/pkg/connector/review_test.go b/pkg/connector/review_test.go new file mode 100644 index 0000000..caf1cb5 --- /dev/null +++ b/pkg/connector/review_test.go @@ -0,0 +1,392 @@ +package connector + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "net/http" + "net/http/httptest" + "sync/atomic" + "testing" + "time" + + "github.com/inline-chat/matrix-inline/pkg/sidecar" + "maunium.net/go/mautrix" + "maunium.net/go/mautrix/appservice" + "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/bridgev2/database" + "maunium.net/go/mautrix/bridgev2/matrix" + "maunium.net/go/mautrix/bridgev2/networkid" + "maunium.net/go/mautrix/event" + "maunium.net/go/mautrix/id" +) + +func TestWorkspaceFetchCannotReapplyAnOlderMembershipAfterRevocation(t *testing.T) { + oldStarted, releaseOld, removalFetched := make(chan struct{}), make(chan struct{}), make(chan struct{}) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/rpc/state/account/refresh": + close(oldStarted) + select { + case <-releaseOld: + case <-r.Context().Done(): + return + } + writeConnectorSidecarResult(t, w, "account_state", sidecar.AccountStateSnapshot{ + Spaces: []sidecar.SpaceRecord{{SpaceID: 3, Name: "Work"}}, SpaceMemberships: []sidecar.SpaceMemberRecord{{SpaceID: 3, UserID: 1}}, SpacesComplete: true, + }) + case "/rpc/state/account": + close(removalFetched) + writeConnectorSidecarResult(t, w, "account_state", sidecar.AccountStateSnapshot{RemovedSpaceIDs: []int64{3}, SpacesComplete: true}) + default: + t.Errorf("unexpected source request: %s", r.URL.Path) + http.Error(w, "unexpected request", 500) + } + })) + defer server.Close() + client := &InlineClient{AccountID: "1", UserLogin: &bridgev2.UserLogin{UserLogin: &database.UserLogin{ID: "1", Metadata: &UserLoginMetadata{}}}, Sidecar: sidecar.NewClient(server.URL)} + ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second) + defer cancel() + results := make(chan error, 2) + go func() { results <- client.refreshWorkspaceNavigation(ctx) }() + select { + case <-oldStarted: + case <-ctx.Done(): + close(releaseOld) + t.Fatal("old snapshot fetch did not start") + } + go func() { results <- client.reconcileAccountDeletes(ctx) }() + overlapped := false + select { + case <-removalFetched: + overlapped = true + case <-time.After(100 * time.Millisecond): + } + close(releaseOld) + for range 2 { + select { + case err := <-results: + if err != nil { + t.Fatal(err) + } + case <-ctx.Done(): + t.Fatal("account reconciliation did not finish") + } + } + if overlapped { + t.Fatal("a second account snapshot was fetched before the first was reconciled") + } + if saved := client.workspaceNavigationSnapshot()["3"]; saved == nil || saved.Active { + t.Fatalf("revoked workspace reactivated by older response: %+v", saved) + } +} + +func TestMatrixMutationsRejectTargetsFromAnotherChatOrLogin(t *testing.T) { + var calls atomic.Int32 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + calls.Add(1) + http.Error(w, "foreign target reached source", 500) + })) + defer server.Close() + client := &InlineClient{UserLogin: &bridgev2.UserLogin{UserLogin: &database.UserLogin{ID: "42"}}, Sidecar: sidecar.NewClient(server.URL)} + portal := testPortal("7") + portal.Receiver = "42" + for _, target := range []*database.Message{ + {ID: "8/11", Room: portal.PortalKey}, + {ID: "7/11", Room: networkid.PortalKey{ID: "7", Receiver: "43"}}, + {ID: "7/0", Room: portal.PortalKey}, + } { + if err := client.HandleMatrixMessageRemove(t.Context(), &bridgev2.MatrixMessageRemove{MatrixEventBase: bridgev2.MatrixEventBase[*event.RedactionEventContent]{Portal: portal}, TargetMessage: target}); err == nil { + t.Fatal("foreign redaction target accepted") + } + if _, err := client.HandleMatrixReaction(t.Context(), &bridgev2.MatrixReaction{MatrixEventBase: bridgev2.MatrixEventBase[*event.ReactionEventContent]{Portal: portal, Content: &event.ReactionEventContent{RelatesTo: event.RelatesTo{Key: "👍"}}}, TargetMessage: target}); err == nil { + t.Fatal("foreign reaction target accepted") + } + if err := client.HandleMatrixReactionRemove(t.Context(), &bridgev2.MatrixReactionRemove{MatrixEventBase: bridgev2.MatrixEventBase[*event.RedactionEventContent]{Portal: portal}, TargetReaction: &database.Reaction{Room: target.Room, MessageID: target.ID, Emoji: "👍"}}); err == nil { + t.Fatal("foreign reaction removal target accepted") + } + if err := client.HandleMatrixReadReceipt(t.Context(), &bridgev2.MatrixReadReceipt{Portal: portal, ExactMessage: target}); err == nil { + t.Fatal("foreign receipt target accepted") + } + } + if calls.Load() != 0 { + t.Fatalf("foreign target made %d source mutations", calls.Load()) + } +} + +func TestMatrixReceiptRequiresExplicitMappedTarget(t *testing.T) { + var calls atomic.Int32 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/rpc/read" { + w.Header().Set("Content-Type", "application/json") + fmt.Fprint(w, `{"chunk":[{"event_id":"$read","type":"m.room.message","content":{"msgtype":"m.text","body":"x"}}]}`) + return + } + calls.Add(1) + var request sidecar.ReadRequest + if err := json.NewDecoder(r.Body).Decode(&request); err != nil { + t.Error(err) + } + if r.URL.Path != "/rpc/read" || request.ChatID != 7 || request.MaxMessageID == nil || *request.MaxMessageID != 11 { + t.Errorf("receipt did not preserve exact target: %s %+v", r.URL.Path, request) + } + writeConnectorSidecarResult(t, w, "empty", struct{}{}) + })) + defer server.Close() + db := testBridgeDatabase(t) + bridge, _ := identityTestBridge(t, db) + matrixClient, err := mautrix.NewClient(server.URL, "@bridge:example.org", "test-token") + if err != nil { + t.Fatal(err) + } + bridge.Bot = &matrix.ASIntent{Matrix: &appservice.IntentAPI{Client: matrixClient}} + client := &InlineClient{UserLogin: &bridgev2.UserLogin{UserLogin: &database.UserLogin{ID: "42"}, Bridge: bridge}, Sidecar: sidecar.NewClient(server.URL), history: map[int64]int64{7: 10}, historyLoaded: map[int64]struct{}{7: {}}} + portal := testPortal("7") + portal.Receiver = "42" + portal.MXID = "!room:example.org" + target := &database.Message{ID: "7/11", Room: portal.PortalKey, MXID: "$read", SenderID: "1", Timestamp: time.Now()} + if err := db.Portal.Insert(t.Context(), portal.Portal); err != nil { + t.Fatal(err) + } + if err := db.Ghost.Insert(t.Context(), &database.Ghost{ID: "1"}); err != nil { + t.Fatal(err) + } + if err := db.Message.Insert(t.Context(), target); err != nil { + t.Fatal(err) + } + if err := client.HandleMatrixReadReceipt(t.Context(), &bridgev2.MatrixReadReceipt{Portal: portal}); err != nil { + t.Fatal(err) + } + if calls.Load() != 0 { + t.Fatal("unknown receipt read all source messages") + } + if err := client.HandleMatrixReadReceipt(t.Context(), &bridgev2.MatrixReadReceipt{Portal: portal, EventID: "$read", ExactMessage: target}); err == nil { + t.Fatal("live receipt advanced past incomplete source history") + } + if calls.Load() != 0 { + t.Fatal("uncovered source history was marked read") + } + client.history[7] = 11 + if err := client.HandleMatrixReadReceipt(t.Context(), &bridgev2.MatrixReadReceipt{Portal: portal, EventID: "$read", ExactMessage: target}); err != nil { + t.Fatal(err) + } + if calls.Load() != 1 { + t.Fatalf("expected one exact receipt, got %d", calls.Load()) + } +} + +func TestSourceReadReceiptRequiresTimelineOrderAsWellAsHistoryCoverage(t *testing.T) { + key := networkid.PortalKey{ID: "7", Receiver: "42"} + lookup := func(_ context.Context, eventID id.EventID) (*database.Message, error) { + var messageID int64 + switch eventID { + case "$99": + messageID = 99 + case "$100": + messageID = 100 + case "$101": + messageID = 101 + default: + return nil, nil + } + return &database.Message{ID: makeMessageID(7, messageID), Room: key, MXID: eventID}, nil + } + for _, test := range []struct { + name string + ids []id.EventID + safe bool + }{ + {"contiguous", []id.EventID{"$99", "$100", "$101"}, true}, + {"older source delivered after live target", []id.EventID{"$100", "$99"}, false}, + {"unknown timeline event", []id.EventID{"$99", "$100", "$unknown"}, false}, + {"target absent", []id.EventID{"$99", "$101"}, false}, + } { + t.Run(test.name, func(t *testing.T) { + var timeline []*event.Event + for _, eventID := range test.ids { + timeline = append(timeline, &event.Event{ID: eventID, Type: event.EventMessage}) + } + safe, err := verifiedSourceReadReceipt(t.Context(), timeline, key, "$100", 100, lookup) + if err != nil || safe != test.safe { + t.Fatalf("receipt proof = %v, %v; want %v", safe, err, test.safe) + } + }) + } +} + +func TestReadTraversalOnlyTreatsVerifiedPlaintextEditsAsTransparent(t *testing.T) { + key := networkid.PortalKey{ID: "7", Receiver: "42"} + lookup := func(_ context.Context, eventID id.EventID) (*database.Message, error) { + if eventID == "$original" { + return &database.Message{ID: "7/1", Room: key, MXID: eventID, SenderMXID: "@alice:example.org"}, nil + } + if eventID == "$next" { + return &database.Message{ID: "7/2", Room: key, MXID: eventID, SenderMXID: "@bob:example.org"}, nil + } + return nil, nil + } + original := &event.Event{ID: "$original", Type: event.EventMessage, Sender: "@alice:example.org"} + next := &event.Event{ID: "$next", Type: event.EventMessage, Sender: "@bob:example.org"} + for _, test := range []struct { + name string + edit *event.Event + first bool + prefix id.EventID + safe bool + }{ + {"verified raw edit", &event.Event{ID: "$edit", Type: event.EventMessage, Sender: original.Sender, Content: event.Content{VeryRaw: json.RawMessage(`{"msgtype":"m.text","body":"* changed","m.new_content":{"msgtype":"m.text","body":"changed"},"m.relates_to":{"rel_type":"m.replace","event_id":"$original"}}`)}}, false, "$next", true}, + {"sender spoof", &event.Event{ID: "$edit", Type: event.EventMessage, Sender: "@mallory:example.org", Content: event.Content{Parsed: &event.MessageEventContent{NewContent: &event.MessageEventContent{Body: "changed"}, RelatesTo: &event.RelatesTo{Type: event.RelReplace, EventID: "$original"}}}}, false, "$original", false}, + {"missing original", &event.Event{ID: "$edit", Type: event.EventMessage, Sender: original.Sender, Content: event.Content{Parsed: &event.MessageEventContent{NewContent: &event.MessageEventContent{Body: "changed"}, RelatesTo: &event.RelatesTo{Type: event.RelReplace, EventID: "$original"}}}}, true, "", false}, + {"encrypted relation", &event.Event{ID: "$edit", Type: event.EventEncrypted, Sender: original.Sender}, false, "$original", false}, + } { + t.Run(test.name, func(t *testing.T) { + timeline := []*event.Event{original, test.edit, next} + if test.first { + timeline = []*event.Event{test.edit, original, next} + } + prefix, err := verifiedReadPrefix(t.Context(), timeline, key, 2, lookup) + if err != nil || prefix != test.prefix { + t.Fatalf("read prefix = %s, %v; want %s", prefix, err, test.prefix) + } + safe, err := verifiedSourceReadReceipt(t.Context(), timeline, key, "$next", 2, lookup) + if err != nil || safe != test.safe { + t.Fatalf("source receipt = %v, %v; want %v", safe, err, test.safe) + } + if test.safe { + prefix, err := verifiedReadPrefix(t.Context(), timeline, key, 1, lookup) + if err != nil || prefix != "$original" { + t.Fatalf("edit became a receipt target: %s %v", prefix, err) + } + foreign := key + foreign.Receiver = "43" + if safe, err := verifiedTimelineEdit(t.Context(), test.edit, foreign, map[id.EventID]id.UserID{"$original": original.Sender}, lookup); err != nil || safe { + t.Fatalf("edit accepted another login's original mapping: %v %v", safe, err) + } + } + }) + } +} + +func TestPendingWorkspaceLinkRetryRechecksPrivateChatAccess(t *testing.T) { + db := testBridgeDatabase(t) + bridge, api := identityTestBridge(t, db) + login := &database.UserLogin{ID: "1", UserMXID: "@owner:example.org", Metadata: &UserLoginMetadata{WorkspaceNavigation: map[string]*WorkspaceNavigation{"3": {SpaceID: 3, Active: true, RoomID: "!space:example.org", Links: map[string]WorkspaceRoomLink{"7": {RoomID: "!private:example.org"}}}}}} + if err := db.User.Insert(t.Context(), &database.User{MXID: login.UserMXID}); err != nil { + t.Fatal(err) + } + if err := db.UserLogin.Insert(t.Context(), login); err != nil { + t.Fatal(err) + } + var allowed atomic.Bool + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if !allowed.Load() { + http.Error(w, "private access removed", http.StatusForbidden) + return + } + writeConnectorSidecarResult(t, w, "chat_participants", sidecar.ChatParticipantsPage{}) + })) + defer server.Close() + client := &InlineClient{UserLogin: &bridgev2.UserLogin{UserLogin: login, Bridge: bridge}, Sidecar: sidecar.NewClient(server.URL)} + client.retryWorkspaceLinks(t.Context()) + if len(api.states) != 0 || client.workspaceNavigationSnapshot()["3"].Links["7"].Attached { + t.Fatal("retry advertised a private chat after source access failed") + } + allowed.Store(true) + client.retryWorkspaceLinks(t.Context()) + if len(api.states) != 1 || !client.workspaceNavigationSnapshot()["3"].Links["7"].Attached { + t.Fatal("verified pending navigation failed to recover") + } +} + +func TestDeletedChatNavigationCleanupRetriesAndPreservesOtherRooms(t *testing.T) { + db := testBridgeDatabase(t) + bridge, api := identityTestBridge(t, db) + login := &database.UserLogin{ID: "1", UserMXID: "@owner:example.org", Metadata: &UserLoginMetadata{WorkspaceNavigation: map[string]*WorkspaceNavigation{"3": {SpaceID: 3, Active: true, RoomID: "!space:example.org", Links: map[string]WorkspaceRoomLink{"7": {RoomID: "!deleted:example.org", Attached: true}, "8": {RoomID: "!retained:example.org", Attached: true}}}}}} + if err := db.User.Insert(t.Context(), &database.User{MXID: login.UserMXID}); err != nil { + t.Fatal(err) + } + if err := db.UserLogin.Insert(t.Context(), login); err != nil { + t.Fatal(err) + } + client := &InlineClient{UserLogin: &bridgev2.UserLogin{UserLogin: login, Bridge: bridge}} + api.failure = errors.New("Matrix unavailable") + if err := client.removeChatWorkspaceLinks(t.Context(), 7); err == nil { + t.Fatal("failed link removal was accepted") + } + if len(client.workspaceNavigationSnapshot()["3"].Links) != 2 { + t.Fatal("failed cleanup discarded its retry state") + } + api.failure = nil + if err := client.removeChatWorkspaceLinks(t.Context(), 7); err != nil { + t.Fatal(err) + } + stored, err := db.UserLogin.GetByID(t.Context(), "1") + if err != nil { + t.Fatal(err) + } + links := stored.Metadata.(*UserLoginMetadata).WorkspaceNavigation["3"].Links + if len(links) != 1 || links["8"].RoomID != "!retained:example.org" || len(api.states) != 1 || api.states[0].key != "!deleted:example.org" || len(api.states[0].content.Parsed.(*event.SpaceChildEventContent).Via) != 0 { + t.Fatalf("deleted-chat cleanup changed unrelated navigation: %+v %+v", links, api.states) + } +} + +func TestMatrixCaptionCanBeClearedWithoutReplacingAttachment(t *testing.T) { + var calls atomic.Int32 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + calls.Add(1) + var request sidecar.EditMessageRequest + if err := json.NewDecoder(r.Body).Decode(&request); err != nil { + t.Error(err) + } + if r.URL.Path != "/rpc/edit" || request.ChatID != 7 || request.MessageID != 11 || request.Text != "" || len(request.Entities) != 0 { + t.Errorf("caption clear payload = %s %+v", r.URL.Path, request) + } + writeConnectorSidecarResult(t, w, "empty", struct{}{}) + })) + defer server.Close() + client := &InlineClient{UserLogin: &bridgev2.UserLogin{UserLogin: &database.UserLogin{ID: "42"}}, Sidecar: sidecar.NewClient(server.URL)} + portal := testPortal("7") + portal.Receiver = "42" + original := &event.MessageEventContent{MsgType: event.MsgImage, Body: "old caption", FileName: "picture.jpg", URL: "mxc://example.org/stable"} + for _, body := range []string{"", "picture.jpg"} { + cleared := cloneMatrixContent(original) + cleared.Body = body + target := &database.Message{ID: "7/11", Room: portal.PortalKey, Metadata: &MessageMetadata{MatrixContent: original}} + if err := client.HandleMatrixEdit(t.Context(), &bridgev2.MatrixEdit{MatrixEventBase: bridgev2.MatrixEventBase[*event.MessageEventContent]{Portal: portal, Content: cleared}, EditTarget: target}); err != nil { + t.Fatal(err) + } + } + if calls.Load() != 2 { + t.Fatalf("expected two caption-only mutations, got %d", calls.Load()) + } +} + +func TestBackfillRejectsIncompleteOrDuplicatePages(t *testing.T) { + message := sidecar.MessageRecord{ChatID: 7, MessageID: 11, SenderID: 1, Content: sidecar.MessageContent{Type: "text", Text: "message"}} + if _, _, err := (&InlineClient{}).backfillMessagesFromHistory(t.Context(), nil, []sidecar.MessageRecord{message, message}); err == nil { + t.Fatal("duplicate backfill identities accepted") + } + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + writeConnectorSidecarResult(t, w, "history", sidecar.HistoryPage{HasMore: true}) + })) + defer server.Close() + client := &InlineClient{Sidecar: sidecar.NewClient(server.URL)} + if _, err := client.FetchMessages(t.Context(), bridgev2.FetchMessagesParams{Portal: testPortal("7")}); err == nil { + t.Fatal("empty nonterminal backfill silently finished") + } +} + +func TestCachedHistoryRejectsForeignChatBeforeQueueing(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + writeConnectorSidecarResult(t, w, "history", sidecar.HistoryPage{Messages: []sidecar.MessageRecord{{ChatID: 8, MessageID: 11}}}) + })) + defer server.Close() + client := &InlineClient{Sidecar: sidecar.NewClient(server.URL)} + if err := client.reconcileCachedHistory(t.Context(), 7); err == nil { + t.Fatal("foreign cached row accepted") + } + if err := client.syncMessageUpsert(t.Context(), 7, 11); err == nil { + t.Fatal("foreign row accepted as requested message") + } +} diff --git a/pkg/connector/richtext.go b/pkg/connector/richtext.go new file mode 100644 index 0000000..9384561 --- /dev/null +++ b/pkg/connector/richtext.go @@ -0,0 +1,407 @@ +package connector + +import ( + "context" + "fmt" + "html" + "net/url" + "sort" + "strconv" + "strings" + "unicode/utf8" + + "github.com/inline-chat/matrix-inline/pkg/sidecar" + xhtml "golang.org/x/net/html" + "golang.org/x/net/html/atom" + "maunium.net/go/mautrix/bridgev2" + "maunium.net/go/mautrix/event" + "maunium.net/go/mautrix/id" +) + +// Native entity offsets count UTF-16 code units, not bytes or Unicode scalars. +// A -1 boundary is the middle of a surrogate pair and must never be sliced. +func utf16Boundaries(text string) []int { + bounds := []int{0} + for offset, r := range text { + if r > 0xffff { + bounds = append(bounds, -1) + } + bounds = append(bounds, offset+utf8.RuneLen(r)) + } + return bounds +} +func utf16Length(text string) int { return len(utf16Boundaries(text)) - 1 } + +func safeEntityURL(value string) string { + if strings.ContainsAny(value, "\x00\r\n") { + return "" + } + parsed, err := url.Parse(strings.TrimSpace(value)) + if err != nil { + return "" + } + switch strings.ToLower(parsed.Scheme) { + case "http", "https": + if parsed.Host == "" || parsed.User != nil { + return "" + } + case "mailto", "tel": + if parsed.Opaque == "" && parsed.Path == "" { + return "" + } + default: + return "" + } + return parsed.String() +} + +type nativeMentionResolver func(int64) id.UserID + +func inlineMentionResolver(portal *bridgev2.Portal) nativeMentionResolver { + return func(userID int64) id.UserID { + if userID <= 0 || portal == nil || portal.Bridge == nil || portal.Bridge.Matrix == nil { + return "" + } + login := portal.Bridge.GetCachedUserLoginByID(portal.Receiver) + if login == nil { + return "" + } + client, ok := login.Client.(*InlineClient) + if !ok { + return "" + } + if _, known := client.cachedUser(userID); !known { + return "" + } + intent := portal.Bridge.Matrix.GhostIntent(makeUserID(strconv.FormatInt(userID, 10))) + if intent == nil { + return "" + } + return intent.GetMXID() + } +} + +func (ic *InlineClient) matrixMentionResolver(mxid id.UserID) *int64 { + if ic.UserLogin == nil || ic.UserLogin.Bridge == nil || ic.UserLogin.Bridge.Matrix == nil { + return nil + } + nativeID, ok := ic.UserLogin.Bridge.Matrix.ParseGhostMXID(mxid) + if !ok { + return nil + } + userID, err := strconv.ParseInt(string(nativeID), 10, 64) + if err != nil || userID <= 0 { + return nil + } + if _, known := ic.cachedUser(userID); !known { + return nil + } + return &userID +} + +// matrixTextToInline walks the Matrix HTML tree directly. Markdown is not an +// intermediate representation, so literal punctuation and nested entities survive. +func matrixTextToInline(content *event.MessageEventContent, caption bool, resolve func(id.UserID) *int64) (string, []sidecar.MessageEntityRecord, error) { + entities := make([]sidecar.MessageEntityRecord, 0) + if content == nil { + return "", entities, fmt.Errorf("missing Matrix message content") + } + text := content.Body + if caption { + text = content.GetCaption() + } + if content.Format != event.FormatHTML || content.FormattedBody == "" || (caption && text == "") { + if !utf8.ValidString(text) || utf16Length(text) > 5000 { + return "", nil, fmt.Errorf("Inline text must be valid UTF-8 and at most 5000 UTF-16 units") + } + return text, entities, nil + } + if len(content.FormattedBody) > 1<<20 { + return "", nil, fmt.Errorf("Matrix formatted message is too large") + } + nodes, err := xhtml.ParseFragment(strings.NewReader(content.FormattedBody), &xhtml.Node{Type: xhtml.ElementNode, Data: "div", DataAtom: atom.Div}) + if err != nil { + return "", nil, fmt.Errorf("parse Matrix formatted text: %w", err) + } + var body strings.Builder + units := int64(0) + write := func(value string) { body.WriteString(value); units += int64(utf16Length(value)) } + newline := func() { + if body.Len() > 0 && !strings.HasSuffix(body.String(), "\n") { + write("\n") + } + } + var walk func(*xhtml.Node, int, bool) error + walk = func(node *xhtml.Node, depth int, inPre bool) error { + if depth > 128 { + return fmt.Errorf("Matrix formatted text nesting is too deep") + } + if node.Type == xhtml.TextNode { + write(node.Data) + return nil + } + if node.Type != xhtml.ElementNode { + return nil + } + tag := strings.ToLower(node.Data) + switch tag { + case "script", "style", "mx-reply": + return nil + case "br": + write("\n") + return nil + } + block := tag == "p" || tag == "div" || tag == "blockquote" || tag == "li" || tag == "pre" + if block { + newline() + } + start := units + entity := sidecar.MessageEntityRecord{Offset: start} + attr := func(name string) string { + for _, a := range node.Attr { + if a.Key == name { + return a.Val + } + } + return "" + } + switch tag { + case "b", "strong": + entity.Kind = "TYPE_BOLD" + case "i", "em": + entity.Kind = "TYPE_ITALIC" + case "u": + entity.Kind = "TYPE_UNDERLINE" + case "s", "del", "strike": + entity.Kind = "TYPE_STRIKETHROUGH" + case "code": + if !inPre { + entity.Kind = "TYPE_CODE" + } + case "pre": + entity.Kind = "TYPE_PRE" + if child := node.FirstChild; child != nil && child.Type == xhtml.ElementNode && child.Data == "code" { + for _, a := range child.Attr { + if a.Key == "class" { + for _, cls := range strings.Fields(a.Val) { + if strings.HasPrefix(cls, "language-") { + value := strings.TrimPrefix(cls, "language-") + entity.Value = &value + } + } + } + } + } + case "a": + href := safeEntityURL(attr("href")) + if href != "" { + entity.Kind = "TYPE_TEXT_URL" + entity.Value = &href + parsed, _ := url.Parse(href) + if parsed.Host == "matrix.to" && strings.HasPrefix(parsed.Fragment, "/@") && resolve != nil { + target := strings.SplitN(strings.TrimPrefix(parsed.Fragment, "/"), "?", 2)[0] + if userID := resolve(id.UserID(target)); userID != nil { + entity.Kind = "TYPE_MENTION" + entity.UserID = userID + entity.Value = nil + } + } + } + } + for child := node.FirstChild; child != nil; child = child.NextSibling { + if err := walk(child, depth+1, inPre || tag == "pre"); err != nil { + return err + } + } + entity.Length = units - start + if entity.Kind != "" && entity.Length > 0 { + entities = append(entities, entity) + if len(entities) > 2048 { + return fmt.Errorf("Matrix formatted text has too many entities") + } + } + if block && node.NextSibling != nil { + newline() + } + if units > 5000 { + return fmt.Errorf("Inline text exceeds 5000 UTF-16 units") + } + return nil + } + for _, node := range nodes { + if err := walk(node, 0, false); err != nil { + return "", nil, err + } + } + text = body.String() + if !utf8.ValidString(text) || units > 5000 { + return "", nil, fmt.Errorf("invalid Inline message text") + } + sort.SliceStable(entities, func(i, j int) bool { + if entities[i].Offset == entities[j].Offset { + return entities[i].Length > entities[j].Length + } + return entities[i].Offset < entities[j].Offset + }) + return text, entities, nil +} + +type formattedSpan struct { + start, end int + open, close string + mention id.UserID +} + +func applyInlineEntities(content *event.MessageEventContent, text string, entities []sidecar.MessageEntityRecord, resolve nativeMentionResolver) { + // An explicit empty mention set disables legacy @room/display-name inference. + content.Mentions = &event.Mentions{} + if text == "" || !utf8.ValidString(text) || len(text) > 1<<20 || len(entities) > 2048 { + return + } + bounds := utf16Boundaries(text) + spans := make([]formattedSpan, 0, len(entities)) + points := map[int]bool{0: true, len(text): true} + for _, entity := range entities { + if entity.Offset < 0 || entity.Length <= 0 || entity.Offset >= int64(len(bounds)) || entity.Length > int64(len(bounds))-1-entity.Offset { + continue + } + start, end := bounds[int(entity.Offset)], bounds[int(entity.Offset+entity.Length)] + if start < 0 || end < 0 { + continue + } + span := formattedSpan{start: start, end: end} + switch entity.Kind { + case "TYPE_BOLD": + span.open, span.close = "", "" + case "TYPE_ITALIC": + span.open, span.close = "", "" + case "TYPE_UNDERLINE": + span.open, span.close = "", "" + case "TYPE_STRIKETHROUGH": + span.open, span.close = "", "" + case "TYPE_CODE": + span.open, span.close = "", "" + case "TYPE_PRE": + span.open, span.close = "
", "
" + if entity.Value != nil && *entity.Value != "" { + span.open = "
"
+			}
+		case "TYPE_URL", "TYPE_TEXT_URL", "TYPE_EMAIL", "TYPE_PHONE", "TYPE_PHONE_NUMBER":
+			value := text[start:end]
+			if entity.Kind == "TYPE_TEXT_URL" {
+				if entity.Value == nil {
+					continue
+				}
+				value = *entity.Value
+			}
+			if entity.Kind == "TYPE_EMAIL" {
+				value = "mailto:" + value
+			}
+			if entity.Kind == "TYPE_PHONE" || entity.Kind == "TYPE_PHONE_NUMBER" {
+				value = "tel:" + value
+			}
+			href := safeEntityURL(value)
+			if href == "" {
+				continue
+			}
+			span.open, span.close = "", ""
+		case "TYPE_MENTION":
+			if entity.UserID == nil || resolve == nil {
+				continue
+			}
+			span.mention = resolve(*entity.UserID)
+			if span.mention == "" {
+				continue
+			}
+			span.open, span.close = "", ""
+			content.Mentions.Add(span.mention)
+		default:
+			continue
+		}
+		spans = append(spans, span)
+		points[start] = true
+		points[end] = true
+	}
+	if len(spans) == 0 {
+		return
+	}
+	sort.SliceStable(spans, func(i, j int) bool {
+		if spans[i].start == spans[j].start {
+			if spans[i].end == spans[j].end {
+				return spans[i].open < spans[j].open
+			}
+			return spans[i].end > spans[j].end
+		}
+		return spans[i].start < spans[j].start
+	})
+	positions := make([]int, 0, len(points))
+	for point := range points {
+		positions = append(positions, point)
+	}
+	sort.Ints(positions)
+	var output strings.Builder
+	// Keep the common nesting prefix. Only crossing spans need to close and
+	// reopen, keeping normal nested formatting compact and linear in text size.
+	previous := make([]int, 0)
+	for i := 0; i < len(positions)-1; i++ {
+		start, end := positions[i], positions[i+1]
+		active := make([]int, 0)
+		link := false
+		for index, span := range spans {
+			if span.start <= start && span.end >= end {
+				if span.close == "" {
+					if link {
+						continue
+					}
+					link = true
+				}
+				active = append(active, index)
+			}
+		}
+		common := 0
+		for common < len(previous) && common < len(active) && previous[common] == active[common] {
+			common++
+		}
+		for j := len(previous) - 1; j >= common; j-- {
+			output.WriteString(spans[previous[j]].close)
+		}
+		for j := common; j < len(active); j++ {
+			output.WriteString(spans[active[j]].open)
+		}
+		output.WriteString(strings.ReplaceAll(html.EscapeString(text[start:end]), "\n", "
")) + if output.Len() > 1<<20 { + content.Mentions = &event.Mentions{} + return + } + previous = active + } + for j := len(previous) - 1; j >= 0; j-- { + output.WriteString(spans[previous[j]].close) + } + + content.Format = event.FormatHTML + content.FormattedBody = output.String() +} + +func applyInlineRecordEntities(ctx context.Context, portal *bridgev2.Portal, converted *bridgev2.ConvertedMessage, data sidecar.MessageRecord) { + if converted == nil { + return + } + for _, part := range converted.Parts { + if part == nil || part.Content == nil { + continue + } + text := data.Content.Text + if data.Content.Type == "media" { + if part.Content.URL == "" && part.Content.File == nil { + part.Content.Mentions = &event.Mentions{} + continue + } + text = "" + if data.Content.Caption != nil { + text = *data.Content.Caption + } + } + applyInlineEntities(part.Content, text, data.Metadata.Entities, inlineMentionResolver(portal)) + } +} diff --git a/pkg/sidecar/client.go b/pkg/sidecar/client.go index 6f9b12d..fcf09b5 100644 --- a/pkg/sidecar/client.go +++ b/pkg/sidecar/client.go @@ -146,6 +146,40 @@ func (c *Client) AccountState(ctx context.Context) (*AccountStateSnapshot, error return &snapshot, nil } +// RefreshAccountState refreshes accessible workspace navigation from the source. +// A failed refresh returns an error and does not turn missing rows into removals. +func (c *Client) RefreshAccountState(ctx context.Context) (*AccountStateSnapshot, error) { + var result AccountStateSnapshot + if err := c.doRPC(ctx, http.MethodPost, "/rpc/state/account/refresh", nil, "account_state", &result); err != nil { + return nil, err + } + return &result, nil +} + +func (c *Client) Users(ctx context.Context, request UsersRequest) (*UsersPage, error) { + var result UsersPage + if err := c.doRPC(ctx, http.MethodPost, "/rpc/users", request, "users", &result); err != nil { + return nil, err + } + return &result, nil +} + +func (c *Client) SearchUsers(ctx context.Context, request SearchUsersRequest) (*UsersPage, error) { + var result UsersPage + if err := c.doRPC(ctx, http.MethodPost, "/rpc/users/search", request, "users", &result); err != nil { + return nil, err + } + return &result, nil +} + +func (c *Client) SpaceMembers(ctx context.Context, request SpaceMembersRequest) (*SpaceMembersPage, error) { + var result SpaceMembersPage + if err := c.doRPC(ctx, http.MethodPost, "/rpc/spaces/members", request, "space_members", &result); err != nil { + return nil, err + } + return &result, nil +} + func (c *Client) ChatState(ctx context.Context, request ChatStateRequest) (*ChatStateSnapshot, error) { var snapshot ChatStateSnapshot if err := c.doRPC(ctx, http.MethodPost, "/rpc/state/chat", request, "chat_state", &snapshot); err != nil { @@ -456,6 +490,7 @@ func (c *Client) doRaw(ctx context.Context, method, path, contentType string, bo } func (c *Client) doRequest(req *http.Request, out any) error { + req.Header.Set("X-Inline-Protocol-Version", strconv.Itoa(ProtocolVersion)) if strings.TrimSpace(c.SessionNamespace) != "" { req.Header.Set("X-Inline-Session-Namespace", c.SessionNamespace) } diff --git a/pkg/sidecar/contract_v5_test.go b/pkg/sidecar/contract_v5_test.go new file mode 100644 index 0000000..f57c9c9 --- /dev/null +++ b/pkg/sidecar/contract_v5_test.go @@ -0,0 +1,131 @@ +package sidecar + +import ( + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "reflect" + "testing" +) + +func TestV5RichRequestsPreserveEntitiesAndAccountHeaders(t *testing.T) { + wantEntities := []MessageEntityRecord{{Kind: "TYPE_BOLD", Offset: 1, Length: 2}} + seen := make(map[string]bool) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Header.Get("X-Inline-Protocol-Version") != "5" || r.Header.Get("X-Inline-Session-Namespace") != "account/42" { + t.Errorf("missing version/account isolation headers: %v", r.Header) + } + seen[r.URL.Path] = true + if r.URL.Path == "/rpc/events/ack" { + writeJSON(t, w, EventAckResponse{}) + return + } + var payload struct { + Text string `json:"text"` + Caption string `json:"caption"` + Entities []MessageEntityRecord `json:"entities"` + } + var err error + if r.URL.Path == "/rpc/upload" { + if err = r.ParseMultipartForm(1024); err != nil { + t.Error(err) + http.Error(w, "invalid multipart", 400) + return + } + defer r.MultipartForm.RemoveAll() + err = json.Unmarshal([]byte(r.FormValue("metadata")), &payload) + file, _, fileErr := r.FormFile("file") + if fileErr != nil { + t.Error(fileErr) + } else { + data, readErr := io.ReadAll(file) + file.Close() + if readErr != nil || string(data) != "attachment" { + t.Errorf("attachment changed: %q %v", data, readErr) + } + } + } else { + err = json.NewDecoder(r.Body).Decode(&payload) + } + if err != nil || !reflect.DeepEqual(payload.Entities, wantEntities) || (payload.Text != "A😀B" && payload.Caption != "A😀B") { + t.Errorf("rich payload lost UTF-16 entities: %+v %v", payload, err) + } + kind := "message" + if r.URL.Path == "/rpc/edit" { + kind = "empty" + } + writeRPCResult(t, w, kind, MessageMutation{MessageID: int64Ptr(11), State: TransactionCompleted}) + })) + defer server.Close() + client := NewClient(server.URL).WithSessionNamespace("account/42") + if _, err := client.SendText(t.Context(), SendTextRequest{Peer: ChatPeer(7), Text: "A😀B", Entities: wantEntities}); err != nil { + t.Fatal(err) + } + if err := client.EditMessage(t.Context(), EditMessageRequest{ChatID: 7, MessageID: 11, Text: "A😀B", Entities: wantEntities}); err != nil { + t.Fatal(err) + } + if _, err := client.Upload(t.Context(), UploadRequest{Peer: ChatPeer(7), Kind: "document", Caption: stringPtr("A😀B"), Entities: wantEntities}, []byte("attachment")); err != nil { + t.Fatal(err) + } + if err := client.AckEventsGeneration(t.Context(), "account/42", "generation", 5); err != nil { + t.Fatal(err) + } + if len(seen) != 4 { + t.Fatalf("missing contract requests: %v", seen) + } +} + +func TestV5DirectoryAndStructureWireContract(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Header.Get("X-Inline-Protocol-Version") != "5" { + t.Error("missing protocol version") + } + switch r.URL.Path { + case "/rpc/state/account/refresh": + writeRPCResult(t, w, "account_state", json.RawMessage(`{"spaces":[{"space_id":3,"name":"Work","creator":false,"date":1}],"space_memberships":[{"space_id":3,"user_id":42,"date":1,"can_access_public_chats":true}],"removed_space_ids":[4],"spaces_complete":false,"deleted_chat_ids":[]}`)) + case "/rpc/state/dialogs": + writeRPCResult(t, w, "dialogs", json.RawMessage(`{"dialogs":[{"chat_id":7,"kind":"chat","structure_known":true,"structure_seq":9,"space_id":null,"parent_chat_id":8,"parent_message_id":11,"description":"Child"}]}`)) + case "/rpc/users", "/rpc/users/search": + writeRPCResult(t, w, "users", json.RawMessage(`{"users":[{"user_id":42,"username":"person"}],"complete":false}`)) + case "/rpc/spaces/members": + var request SpaceMembersRequest + if err := json.NewDecoder(r.Body).Decode(&request); err != nil || request.SpaceID != 3 || request.Cursor != "17" || request.Refresh { + t.Errorf("roster request: %+v %v", request, err) + } + writeRPCResult(t, w, "space_members", json.RawMessage(`{"members":[{"space_id":3,"user_id":42,"date":1,"can_access_public_chats":true}],"users":[],"complete":false,"next_cursor":"42"}`)) + default: + t.Errorf("unexpected endpoint: %s", r.URL.Path) + http.NotFound(w, r) + } + })) + defer server.Close() + client := NewClient(server.URL) + account, err := client.RefreshAccountState(t.Context()) + if err != nil || account == nil { + t.Fatal(err) + } + if account.SpacesComplete || len(account.SpaceMemberships) != 1 || account.SpaceMemberships[0].UserID != 42 || !reflect.DeepEqual(account.RemovedSpaceIDs, []int64{4}) { + t.Fatalf("authority/completeness lost: %+v", account) + } + dialogs, err := client.CachedDialogs(t.Context(), DialogsRequest{}) + if err != nil || dialogs == nil || len(dialogs.Dialogs) != 1 { + t.Fatalf("dialogs: %+v %v", dialogs, err) + } + dialog := dialogs.Dialogs[0] + if dialog.Kind != ConversationChat || !dialog.StructureKnown || dialog.SpaceID != nil || dialog.StructureSeq == nil || *dialog.StructureSeq != 9 || dialog.ParentChatID == nil || *dialog.ParentChatID != 8 || dialog.ParentMessageID == nil || *dialog.ParentMessageID != 11 { + t.Fatalf("structure/detach lost: %+v", dialog) + } + users, err := client.Users(t.Context(), UsersRequest{UserIDs: []int64{42}}) + if err != nil || users == nil || users.Complete || len(users.Users) != 1 { + t.Fatalf("users: %+v %v", users, err) + } + users, err = client.SearchUsers(t.Context(), SearchUsersRequest{Query: "person"}) + if err != nil || users == nil || users.Complete || len(users.Users) != 1 { + t.Fatalf("search: %+v %v", users, err) + } + roster, err := client.SpaceMembers(t.Context(), SpaceMembersRequest{SpaceID: 3, Cursor: "17", Refresh: false}) + if err != nil || roster == nil || roster.Complete || roster.NextCursor != "42" || len(roster.Members) != 1 { + t.Fatalf("roster: %+v %v", roster, err) + } +} diff --git a/pkg/sidecar/contracts_test.go b/pkg/sidecar/contracts_test.go new file mode 100644 index 0000000..35deaf5 --- /dev/null +++ b/pkg/sidecar/contracts_test.go @@ -0,0 +1,78 @@ +package sidecar + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "strconv" + "testing" +) + +func TestSourceMessageMetadataPreservesRevisionEntitiesAndFutureFields(t *testing.T) { + input := []byte(`{"chat_id":7,"message_id":11,"sender_id":42,"timestamp":10,"is_outgoing":false,"content":{"type":"text","text":"😀 hello"},"metadata":{"revision":9,"edit_timestamp":20,"is_service":false,"entities":[{"kind":"TYPE_MENTION","offset":3,"length":5,"user_id":42,"agent_id":99}],"attachments":[],"actions":[],"future_native_field":{"nested":true}}}`) + var message MessageRecord + if err := json.Unmarshal(input, &message); err != nil { + t.Fatal(err) + } + if message.Metadata.Revision == nil || *message.Metadata.Revision != 9 || len(message.Metadata.Entities) != 1 { + t.Fatalf("metadata lost: %#v", message.Metadata) + } + entity := message.Metadata.Entities[0] + if entity.AgentID == nil || *entity.AgentID != 99 || entity.Offset != 3 || entity.Length != 5 { + t.Fatalf("entity lost source identity/ranges: %#v", entity) + } + encoded, err := json.Marshal(message) + if err != nil { + t.Fatal(err) + } + var roundtrip MessageRecord + if err = json.Unmarshal(encoded, &roundtrip); err != nil { + t.Fatal(err) + } + if string(roundtrip.Metadata.Extra["future_native_field"]) != `{"nested":true}` { + t.Fatalf("future metadata lost: %s", encoded) + } +} + +func TestDirectoryRPCsSendCurrentProtocolAndAccountNamespace(t *testing.T) { + calls := 0 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) { + calls++ + if request.Header.Get("X-Inline-Protocol-Version") != strconv.Itoa(ProtocolVersion) || request.Header.Get("X-Inline-Session-Namespace") != "account-a" { + t.Errorf("missing current protocol/account headers: %v", request.Header) + } + result := "users" + payload := any(UsersPage{}) + switch request.URL.Path { + case "/rpc/state/account/refresh": + result = "account_state" + payload = AccountStateSnapshot{SpacesComplete: true} + case "/rpc/spaces/members": + result = "space_members" + payload = SpaceMembersPage{} + case "/rpc/users", "/rpc/users/search": + default: + t.Errorf("unexpected route %s", request.URL.Path) + } + writeJSON(t, w, Response{ProtocolVersion: ProtocolVersion, ID: "contract", Outcome: ResponseOutcome{Status: "ok", Data: mustJSON(t, Result{Type: result, Data: mustJSON(t, payload)})}}) + })) + defer server.Close() + client := NewClient(server.URL).WithSessionNamespace("account-a") + ctx := context.Background() + if _, err := client.RefreshAccountState(ctx); err != nil { + t.Fatal(err) + } + if _, err := client.Users(ctx, UsersRequest{UserIDs: []int64{42}}); err != nil { + t.Fatal(err) + } + if _, err := client.SearchUsers(ctx, SearchUsersRequest{Query: "mo"}); err != nil { + t.Fatal(err) + } + if _, err := client.SpaceMembers(ctx, SpaceMembersRequest{SpaceID: 5}); err != nil { + t.Fatal(err) + } + if calls != 4 { + t.Fatalf("got %d calls", calls) + } +} diff --git a/pkg/sidecar/types.go b/pkg/sidecar/types.go index 4ce40cc..937a634 100644 --- a/pkg/sidecar/types.go +++ b/pkg/sidecar/types.go @@ -8,7 +8,7 @@ import ( const ( // ProtocolVersion is the sidecar command/event protocol version expected by // this bridge. - ProtocolVersion = 4 + ProtocolVersion = 5 // DefaultBaseURL is the loopback sidecar endpoint. DefaultBaseURL = "http://127.0.0.1:29342" @@ -139,25 +139,39 @@ type DialogsRequest struct { const DialogsOrderStableChatID = "stable_chat_id" +type ConversationKind string + +const ( + ConversationUnknown ConversationKind = "unknown" + ConversationDirectMessage ConversationKind = "direct_message" + ConversationChat ConversationKind = "chat" +) + type DialogRecord struct { - ChatID int64 `json:"chat_id"` - PeerUserID *int64 `json:"peer_user_id,omitempty"` - Title *string `json:"title,omitempty"` - Emoji *string `json:"emoji,omitempty"` - LastMessageID *int64 `json:"last_message_id,omitempty"` - SyncedThroughMessageID *int64 `json:"synced_through_message_id,omitempty"` - UnreadCount *uint32 `json:"unread_count,omitempty"` - SpaceID *int64 `json:"space_id,omitempty"` - IsPublic *bool `json:"is_public,omitempty"` - Archived *bool `json:"archived,omitempty"` - Pinned *bool `json:"pinned,omitempty"` - Open *bool `json:"open,omitempty"` - ChatListHidden *bool `json:"chat_list_hidden,omitempty"` - Order *string `json:"order,omitempty"` - PinnedOrder *string `json:"pinned_order,omitempty"` - NotificationMode *string `json:"notification_mode,omitempty"` - FollowMode *string `json:"follow_mode,omitempty"` - PinnedMessageIDs []int64 `json:"pinned_message_ids,omitempty"` + Kind ConversationKind `json:"kind"` + StructureKnown bool `json:"structure_known"` + StructureSeq *int32 `json:"structure_seq,omitempty"` + Description *string `json:"description,omitempty"` + ParentChatID *int64 `json:"parent_chat_id,omitempty"` + ParentMessageID *int64 `json:"parent_message_id,omitempty"` + ChatID int64 `json:"chat_id"` + PeerUserID *int64 `json:"peer_user_id,omitempty"` + Title *string `json:"title,omitempty"` + Emoji *string `json:"emoji,omitempty"` + LastMessageID *int64 `json:"last_message_id,omitempty"` + SyncedThroughMessageID *int64 `json:"synced_through_message_id,omitempty"` + UnreadCount *uint32 `json:"unread_count,omitempty"` + SpaceID *int64 `json:"space_id,omitempty"` + IsPublic *bool `json:"is_public,omitempty"` + Archived *bool `json:"archived,omitempty"` + Pinned *bool `json:"pinned,omitempty"` + Open *bool `json:"open,omitempty"` + ChatListHidden *bool `json:"chat_list_hidden,omitempty"` + Order *string `json:"order,omitempty"` + PinnedOrder *string `json:"pinned_order,omitempty"` + NotificationMode *string `json:"notification_mode,omitempty"` + FollowMode *string `json:"follow_mode,omitempty"` + PinnedMessageIDs []int64 `json:"pinned_message_ids,omitempty"` } type DialogsPage struct { @@ -167,7 +181,59 @@ type DialogsPage struct { } type AccountStateSnapshot struct { - DeletedChatIDs []int64 `json:"deleted_chat_ids"` + Spaces []SpaceRecord `json:"spaces"` + SpaceMemberships []SpaceMemberRecord `json:"space_memberships"` + RemovedSpaceIDs []int64 `json:"removed_space_ids"` + SpacesComplete bool `json:"spaces_complete"` + DeletedChatIDs []int64 `json:"deleted_chat_ids"` +} + +// Workspace records are navigation facts, never permission to mutate. +type SpaceRecord struct { + SpaceID int64 `json:"space_id"` + Name string `json:"name"` + Creator bool `json:"creator"` + Date int64 `json:"date"` + IsPublic *bool `json:"is_public,omitempty"` + Seq *int32 `json:"seq,omitempty"` + GridEnabled *bool `json:"grid_enabled,omitempty"` +} + +type SpaceMemberRecord struct { + SpaceID int64 `json:"space_id"` + UserID int64 `json:"user_id"` + Role *string `json:"role,omitempty"` + Date int64 `json:"date"` + CanAccessPublicChats bool `json:"can_access_public_chats"` +} + +type SpaceMembersRequest struct { + SpaceID int64 `json:"space_id"` + Limit *uint32 `json:"limit,omitempty"` + Cursor string `json:"cursor,omitempty"` + Refresh bool `json:"refresh"` +} + +type SpaceMembersPage struct { + Members []SpaceMemberRecord `json:"members"` + Users []UserRecord `json:"users"` + Complete bool `json:"complete"` + NextCursor string `json:"next_cursor,omitempty"` +} + +type SearchUsersRequest struct { + Query string `json:"query"` + Limit *uint32 `json:"limit,omitempty"` +} + +type UsersRequest struct { + UserIDs []int64 `json:"user_ids"` +} + +// Complete is false for search: visible search results aren't a full directory. +type UsersPage struct { + Users []UserRecord `json:"users"` + Complete bool `json:"complete"` } type ChatStateRequest struct { @@ -292,6 +358,7 @@ type CreatedChat struct { } type MessageRecord struct { + Metadata MessageMetadata `json:"metadata"` ChatID int64 `json:"chat_id"` MessageID int64 `json:"message_id"` SenderID int64 `json:"sender_id"` @@ -302,6 +369,89 @@ type MessageRecord struct { Transaction *TransactionIdentity `json:"transaction,omitempty"` } +// Offsets and lengths are original source UTF-16 code units. +type MessageEntityRecord struct { + Kind string `json:"kind"` + Offset int64 `json:"offset"` + Length int64 `json:"length"` + UserID *int64 `json:"user_id,omitempty"` + AgentID *int64 `json:"agent_id,omitempty"` + GroupID *int64 `json:"group_id,omitempty"` + ChatID *int64 `json:"chat_id,omitempty"` + Value *string `json:"value,omitempty"` +} + +type MessageAttachmentRecord struct { + AttachmentID int64 `json:"attachment_id"` + Kind string `json:"kind"` + Title *string `json:"title,omitempty"` + URL *string `json:"url,omitempty"` + Provider *string `json:"provider,omitempty"` +} + +type MessageActionRecord struct { + ActionID string `json:"action_id"` + Label string `json:"label"` + Kind string `json:"kind"` +} + +type AgentSessionMessageMetadata struct { + AgentSessionID int64 `json:"agent_session_id"` + Provider int32 `json:"provider"` + Role int32 `json:"role"` + Relation int32 `json:"relation"` +} + +type MessageMetadata struct { + Mentioned *bool `json:"mentioned,omitempty"` + EditTimestamp *int64 `json:"edit_timestamp,omitempty"` + Revision *int64 `json:"revision,omitempty"` + SenderIsBot *bool `json:"sender_is_bot,omitempty"` + IsService bool `json:"is_service"` + Entities []MessageEntityRecord `json:"entities"` + Attachments []MessageAttachmentRecord `json:"attachments"` + Actions []MessageActionRecord `json:"actions"` + AgentSession *AgentSessionMessageMetadata `json:"agent_session,omitempty"` + // Retain future source metadata while persisting or forwarding this record. + Extra map[string]json.RawMessage `json:"-"` +} + +func (m *MessageMetadata) UnmarshalJSON(data []byte) error { + type known MessageMetadata + var fields map[string]json.RawMessage + if err := json.Unmarshal(data, &fields); err != nil { + return err + } + var value known + if err := json.Unmarshal(data, &value); err != nil { + return err + } + *m = MessageMetadata(value) + for _, name := range []string{"mentioned", "edit_timestamp", "revision", "sender_is_bot", "is_service", "entities", "attachments", "actions", "agent_session"} { + delete(fields, name) + } + m.Extra = fields + return nil +} + +func (m MessageMetadata) MarshalJSON() ([]byte, error) { + type known MessageMetadata + data, err := json.Marshal(known(m)) + if err != nil { + return nil, err + } + var fields map[string]json.RawMessage + if err = json.Unmarshal(data, &fields); err != nil { + return nil, err + } + for name, value := range m.Extra { + if _, present := fields[name]; !present { + fields[name] = value + } + } + return json.Marshal(fields) +} + type MessageContent struct { Type string `json:"type"` Text string `json:"text,omitempty"` @@ -339,33 +489,36 @@ type ExternalID struct { } type SendTextRequest struct { - Peer Peer `json:"peer"` - Text string `json:"text"` - ExternalID *ExternalID `json:"external_id,omitempty"` - RandomID *int64 `json:"random_id,omitempty"` - ReplyToMessageID *int64 `json:"reply_to_message_id,omitempty"` + Entities []MessageEntityRecord `json:"entities,omitempty"` + Peer Peer `json:"peer"` + Text string `json:"text"` + ExternalID *ExternalID `json:"external_id,omitempty"` + RandomID *int64 `json:"random_id,omitempty"` + ReplyToMessageID *int64 `json:"reply_to_message_id,omitempty"` } type UploadRequest struct { - Peer Peer `json:"peer"` - Kind string `json:"kind"` - FileName *string `json:"file_name,omitempty"` - MimeType *string `json:"mime_type,omitempty"` - SizeBytes *uint64 `json:"size_bytes,omitempty"` - Caption *string `json:"caption,omitempty"` - Width *uint32 `json:"width,omitempty"` - Height *uint32 `json:"height,omitempty"` - DurationMS *uint64 `json:"duration_ms,omitempty"` - ExternalID *ExternalID `json:"external_id,omitempty"` - RandomID *int64 `json:"random_id,omitempty"` - ReplyToMessageID *int64 `json:"reply_to_message_id,omitempty"` + Entities []MessageEntityRecord `json:"entities,omitempty"` + Peer Peer `json:"peer"` + Kind string `json:"kind"` + FileName *string `json:"file_name,omitempty"` + MimeType *string `json:"mime_type,omitempty"` + SizeBytes *uint64 `json:"size_bytes,omitempty"` + Caption *string `json:"caption,omitempty"` + Width *uint32 `json:"width,omitempty"` + Height *uint32 `json:"height,omitempty"` + DurationMS *uint64 `json:"duration_ms,omitempty"` + ExternalID *ExternalID `json:"external_id,omitempty"` + RandomID *int64 `json:"random_id,omitempty"` + ReplyToMessageID *int64 `json:"reply_to_message_id,omitempty"` } type EditMessageRequest struct { - ChatID int64 `json:"chat_id"` - MessageID int64 `json:"message_id"` - Text string `json:"text"` - ExternalID *ExternalID `json:"external_id,omitempty"` + Entities []MessageEntityRecord `json:"entities,omitempty"` + ChatID int64 `json:"chat_id"` + MessageID int64 `json:"message_id"` + Text string `json:"text"` + ExternalID *ExternalID `json:"external_id,omitempty"` } type DeleteMessageRequest struct { diff --git a/scripts/check.sh b/scripts/check.sh index 71984b2..9fcab53 100755 --- a/scripts/check.sh +++ b/scripts/check.sh @@ -26,10 +26,14 @@ go mod tidy -diff echo "==> Shell syntax" bash -n scripts/check.sh scripts/smoke-local.sh scripts/e2e-local.sh docker-run.sh -echo "==> Compose config" -docker compose config >/dev/null -docker compose config | grep -q 'INLINE_API_BASE_URL:' -docker compose config | grep -q 'INLINE_REALTIME_URL:' +if [[ "${SKIP_DOCKER_CHECKS:-0}" == "1" ]]; then + echo "==> Compose config skipped (SKIP_DOCKER_CHECKS=1)" +else + echo "==> Compose config" + docker compose config >/dev/null + docker compose config | grep -q 'INLINE_API_BASE_URL:' + docker compose config | grep -q 'INLINE_REALTIME_URL:' +fi echo "==> Go tests" go test -tags goolm ./... diff --git a/scripts/e2e-local.sh b/scripts/e2e-local.sh index 21f6f7e..c2b3b6f 100755 --- a/scripts/e2e-local.sh +++ b/scripts/e2e-local.sh @@ -492,13 +492,13 @@ function wait_for_room_message_body { function sidecar_post_json { local path=$1 body=${2:-} if [[ -n "${body}" ]]; then - curl -fsS \ + curl -fsS -H "X-Inline-Protocol-Version: 5" \ -X POST \ -H "Content-Type: application/json" \ -d "${body}" \ "${SIDECAR_URL}${path}" else - curl -fsS -X POST "${SIDECAR_URL}${path}" + curl -fsS -H "X-Inline-Protocol-Version: 5" -X POST "${SIDECAR_URL}${path}" fi } @@ -734,7 +734,7 @@ function smoke { echo "==> Adapter status" curl -fsS "${SIDECAR_URL}/status" | jq . echo "==> Adapter resume" - curl -fsS -X POST "${SIDECAR_URL}/rpc/resume" | jq . + curl -fsS -H "X-Inline-Protocol-Version: 5" -X POST "${SIDECAR_URL}/rpc/resume" | jq . local token bot_localpart bot_mxid room_id token=$(matrix_login_token) diff --git a/scripts/e2efixture/main.go b/scripts/e2efixture/main.go index 29a7695..163da94 100644 --- a/scripts/e2efixture/main.go +++ b/scripts/e2efixture/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "crypto/rand" "encoding/json" "errors" "flag" @@ -41,6 +42,7 @@ type fixtureServer struct { sent []sentText clients map[*websocket.Conn]struct{} sequence uint64 + generation string events []fixtureEvent } @@ -94,6 +96,7 @@ func newFixtureServer(statePath string) *fixtureServer { groupLast := int64(6001) return &fixtureServer{ statePath: statePath, + generation: rand.Text(), nextMessageID: 7000, users: []sidecar.UserRecord{ {UserID: accountID, DisplayName: stringPtr("Fixture Self"), Username: stringPtr("fixture_self")}, @@ -101,8 +104,8 @@ func newFixtureServer(statePath string) *fixtureServer { {UserID: groupUserID, DisplayName: stringPtr("Lin Fixture"), Username: stringPtr("lin_fixture")}, }, dialogs: []sidecar.DialogRecord{ - {ChatID: dmChatID, PeerUserID: int64Ptr(peerUserID), Title: &dmTitle, LastMessageID: &dmLast}, - {ChatID: groupChatID, Title: &groupTitle, LastMessageID: &groupLast}, + {ChatID: dmChatID, Kind: sidecar.ConversationDirectMessage, StructureKnown: true, PeerUserID: int64Ptr(peerUserID), Title: &dmTitle, LastMessageID: &dmLast}, + {ChatID: groupChatID, Kind: sidecar.ConversationChat, StructureKnown: true, Title: &groupTitle, LastMessageID: &groupLast}, }, history: map[int64][]sidecar.MessageRecord{ dmChatID: {{ @@ -135,6 +138,10 @@ func (srv *fixtureServer) register(mux *http.ServeMux) { mux.HandleFunc("/rpc/dialogs", srv.handleDialogs) mux.HandleFunc("/rpc/state/dialogs", srv.handleDialogs) mux.HandleFunc("/rpc/history", srv.handleHistory) + mux.HandleFunc("/rpc/state/chat/history", srv.handleHistory) + mux.HandleFunc("/rpc/state/account", srv.handleAccountState) + mux.HandleFunc("/rpc/state/account/refresh", srv.handleAccountState) + mux.HandleFunc("/rpc/state/chat", srv.handleChatState) mux.HandleFunc("/rpc/chat/participants", srv.handleParticipants) mux.HandleFunc("/rpc/chat/participants/add", srv.handleEmpty) mux.HandleFunc("/rpc/chat/participants/remove", srv.handleEmpty) @@ -154,9 +161,10 @@ func (srv *fixtureServer) register(mux *http.ServeMux) { func (srv *fixtureServer) handleHealth(w http.ResponseWriter, r *http.Request) { srv.writeJSON(w, sidecar.Health{ - OK: true, - Protocol: srv.protocol(), - Status: srv.status().Status, + OK: true, + Protocol: srv.protocol(), + Status: srv.status().Status, + EventGeneration: srv.generation, }) } @@ -240,6 +248,35 @@ func (srv *fixtureServer) handleDialogs(w http.ResponseWriter, r *http.Request) }) } +func (srv *fixtureServer) handleAccountState(w http.ResponseWriter, r *http.Request) { + if !srv.requireLogin(w) { + return + } + srv.writeResult(w, "account_state", sidecar.AccountStateSnapshot{SpacesComplete: true}) +} + +func (srv *fixtureServer) handleChatState(w http.ResponseWriter, r *http.Request) { + var req sidecar.ChatStateRequest + if !decodeRequest(w, r, &req) || !srv.requireLogin(w) { + return + } + srv.mu.Lock() + var dialog *sidecar.DialogRecord + for _, item := range srv.dialogs { + if item.ChatID == req.ChatID { + copy := item + dialog = © + break + } + } + srv.mu.Unlock() + if dialog == nil { + srv.writeSidecarError(w, "NotFound", "fixture chat not found") + return + } + srv.writeResult(w, "chat_state", sidecar.ChatStateSnapshot{ChatID: req.ChatID, Dialog: dialog}) +} + func (srv *fixtureServer) handleHistory(w http.ResponseWriter, r *http.Request) { var req sidecar.HistoryRequest if !decodeRequest(w, r, &req) || !srv.requireLogin(w) { @@ -324,6 +361,12 @@ func (srv *fixtureServer) handleEmpty(w http.ResponseWriter, r *http.Request) { } func (srv *fixtureServer) handleEvents(w http.ResponseWriter, r *http.Request) { + if generation := r.URL.Query().Get("generation"); generation != "" && generation != srv.generation { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusGone) + srv.writeJSON(w, sidecar.EventReplayUnavailableError{EventGeneration: srv.generation, ResetRequired: true}) + return + } conn, err := websocket.Accept(w, r, nil) if err != nil { return @@ -363,7 +406,13 @@ func (srv *fixtureServer) handleEventAck(w http.ResponseWriter, r *http.Request) srv.writeError(w, err) return } - srv.writeJSON(w, sidecar.EventAckResponse{AcknowledgedSequence: request.Sequence}) + if request.Generation != "" && request.Generation != srv.generation { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusGone) + srv.writeJSON(w, sidecar.EventReplayUnavailableError{EventGeneration: srv.generation, ResetRequired: true}) + return + } + srv.writeJSON(w, sidecar.EventAckResponse{Generation: srv.generation, AcknowledgedSequence: request.Sequence}) } func (srv *fixtureServer) handleFixtureSent(w http.ResponseWriter, r *http.Request) { @@ -482,6 +531,7 @@ func (srv *fixtureServer) broadcast(event map[string]any) error { envelope := map[string]any{ "protocol_version": sidecar.ProtocolVersion, "session_namespace": strconv.FormatInt(accountID, 10), + "generation": srv.generation, "sequence": sequence, "reliability": sidecar.EventLossless, "event": event, diff --git a/scripts/e2efixture/main_test.go b/scripts/e2efixture/main_test.go new file mode 100644 index 0000000..1f62b62 --- /dev/null +++ b/scripts/e2efixture/main_test.go @@ -0,0 +1,58 @@ +package main + +import ( + "context" + "errors" + "net/http" + "net/http/httptest" + "path/filepath" + "testing" + "time" + + "github.com/coder/websocket" + "github.com/inline-chat/matrix-inline/pkg/sidecar" +) + +func TestFixtureRestartChangesGenerationAndReplaysNewSequence(t *testing.T) { + statePath := filepath.Join(t.TempDir(), "fixture.json") + old := newFixtureServer(statePath) + current := newFixtureServer(statePath) + if old.generation == "" || old.generation == current.generation { + t.Fatal("fixture restart reused an event generation") + } + mux := http.NewServeMux() + current.register(mux) + server := httptest.NewServer(mux) + defer server.Close() + client := sidecar.NewClient(server.URL) + ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second) + defer cancel() + health, err := client.Health(ctx) + if err != nil || health == nil || health.EventGeneration != current.generation { + t.Fatalf("health generation: %+v %v", health, err) + } + if err := current.broadcast(map[string]any{"ChatUpserted": map[string]any{"chat_id": dmChatID}}); err != nil { + t.Fatal(err) + } + if stream, err := client.EventsAfterGeneration(ctx, "16000", old.generation, 99); !errors.Is(err, sidecar.ErrEventReplayUnavailable) { + if stream != nil { + stream.Close(websocket.StatusNormalClosure, "done") + } + t.Fatalf("old cursor was silently accepted after restart: %v", err) + } + stream, err := client.EventsAfterGeneration(ctx, "16000", current.generation, 0) + if err != nil { + t.Fatal(err) + } + defer stream.Close(websocket.StatusNormalClosure, "done") + event, err := stream.Recv(ctx) + if err != nil || event == nil || event.Generation != current.generation || event.Sequence == nil || *event.Sequence != 1 { + t.Fatalf("new process sequence did not replay: %+v %v", event, err) + } + if err := client.AckEventsGeneration(ctx, "16000", old.generation, 1); err == nil { + t.Fatal("old generation acknowledgement accepted") + } + if err := client.AckEventsGeneration(ctx, "16000", current.generation, 1); err != nil { + t.Fatal(err) + } +} diff --git a/scripts/smoke-local.sh b/scripts/smoke-local.sh index 8b243be..13725db 100755 --- a/scripts/smoke-local.sh +++ b/scripts/smoke-local.sh @@ -45,6 +45,6 @@ echo "==> Adapter status" curl -fsS "${URL}/status" | jq . echo "==> Adapter resume" -curl -fsS -X POST "${URL}/rpc/resume" | jq . +curl -fsS -H "X-Inline-Protocol-Version: 5" -X POST "${URL}/rpc/resume" | jq . echo "Smoke check passed." From 807faabb4001dfcb227f721eddc3219063d332f2 Mon Sep 17 00:00:00 2001 From: Mo Rajabi Date: Sun, 15 Nov 2026 00:29:11 +0400 Subject: [PATCH 3/3] ci: pin bridge builds to merged native foundation --- .github/workflows/ci.yml | 2 +- .github/workflows/docker.yml | 2 +- docs/deployment.md | 5 +++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 360728e..f2fa0ce 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,7 +18,7 @@ jobs: uses: actions/checkout@v4 with: repository: inline-chat/inline - ref: ef8ebd51ac5e5d884a11dbaaa74b2c117c47acc9 + ref: 83b7ab7489976bb9aa19aead2e9e06ba2a965601 path: inline-public - name: Install system dependencies diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 6cf1bef..294ff36 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -41,7 +41,7 @@ jobs: uses: actions/checkout@v4 with: repository: inline-chat/inline - ref: ef8ebd51ac5e5d884a11dbaaa74b2c117c47acc9 + ref: 83b7ab7489976bb9aa19aead2e9e06ba2a965601 path: inline-public - name: Set build time diff --git a/docs/deployment.md b/docs/deployment.md index 7c587be..02c0049 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -30,11 +30,12 @@ docker compose pull Published images are built for `linux/amd64` and `linux/arm64`. Source builds are available with the build override. Build commands must run -from the `matrix-inline` repo root with the sibling `inline` repo available: +from the `matrix-inline` repo root with `inline-chat/inline` checked out as the +sibling `inline-public` directory: ```text inline-chat/ - inline/ + inline-public/ matrix-inline/ ```