From 18bf67748c14c2ded3bd7112e38317153d93a577 Mon Sep 17 00:00:00 2001 From: timokoesters Date: Tue, 26 May 2020 10:27:51 +0200 Subject: [PATCH] feat: redaction --- Cargo.lock | 126 ++++++++++++++++++------------------------ Cargo.toml | 11 ++-- src/client_server.rs | 82 ++++++++++++++++++++------- src/database/rooms.rs | 55 +++++++++++++++--- src/pdu.rs | 33 ++++++++++- src/server_server.rs | 4 +- 6 files changed, 204 insertions(+), 107 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dc47816..fc9840f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,9 +30,9 @@ version = "0.1.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26c4f3195085c36ea8d24d32b2f828d23296a9370a28aa39d111f6f16bef9f3b" dependencies = [ - "proc-macro2 1.0.13", + "proc-macro2 1.0.17", "quote 1.0.6", - "syn 1.0.23", + "syn 1.0.25", ] [[package]] @@ -277,9 +277,9 @@ version = "0.3.0" source = "git+https://github.com/SergioBenitez/Devise.git?rev=e58b3ac9a#e58b3ac9afc3b6ff10a8aaf02a3e768a8f530089" dependencies = [ "bitflags", - "proc-macro2 1.0.13", + "proc-macro2 1.0.17", "quote 1.0.6", - "syn 1.0.23", + "syn 1.0.25", ] [[package]] @@ -421,9 +421,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0b5a30a4328ab5473878237c447333c093297bded83a4983d10f4deea240d39" dependencies = [ "proc-macro-hack", - "proc-macro2 1.0.13", + "proc-macro2 1.0.17", "quote 1.0.6", - "syn 1.0.23", + "syn 1.0.25", ] [[package]] @@ -918,9 +918,9 @@ checksum = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de" [[package]] name = "openssl-sys" -version = "0.9.56" +version = "0.9.57" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f02309a7f127000ed50594f0b50ecc69e7c654e16d41b4e8156d1b3df8e0b52e" +checksum = "7410fef80af8ac071d4f63755c0ab89ac3df0fd1ea91f1d1f37cf5cec4395990" dependencies = [ "autocfg", "cc", @@ -1002,9 +1002,9 @@ version = "0.4.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e58db2081ba5b4c93bd6be09c40fd36cb9193a8336c384f3b40012e531aa7e40" dependencies = [ - "proc-macro2 1.0.13", + "proc-macro2 1.0.17", "quote 1.0.6", - "syn 1.0.23", + "syn 1.0.25", ] [[package]] @@ -1045,9 +1045,9 @@ checksum = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea" [[package]] name = "proc-macro-hack" -version = "0.5.15" +version = "0.5.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d659fe7c6d27f25e9d80a1a094c223f5246f6a6596453e09d7229bf42750b63" +checksum = "7e0456befd48169b9f13ef0f0ad46d492cf9d2dbb918bcf38e01eed4ce3ec5e4" [[package]] name = "proc-macro-nested" @@ -1066,9 +1066,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.13" +version = "1.0.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53f5ffe53a6b28e37c9c1ce74893477864d64f74778a93a4beb43c8fa167f639" +checksum = "1502d12e458c49a4c9cbff560d0fe0060c252bc29799ed94ca2ed4bb665a0101" dependencies = [ "unicode-xid 0.2.0", ] @@ -1088,7 +1088,7 @@ version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "54a21852a652ad6f610c9510194f398ff6f8692e334fd1145fed931f7fbe44ea" dependencies = [ - "proc-macro2 1.0.13", + "proc-macro2 1.0.17", ] [[package]] @@ -1229,7 +1229,7 @@ dependencies = [ "time", "tokio", "toml", - "version_check 0.9.1", + "version_check 0.9.2", "yansi 0.5.0", ] @@ -1242,7 +1242,7 @@ dependencies = [ "indexmap", "quote 1.0.6", "rocket_http", - "version_check 0.9.1", + "version_check 0.9.2", "yansi 0.5.0", ] @@ -1277,7 +1277,7 @@ dependencies = [ "percent-encoding 2.1.0", "ruma-api-macros", "ruma-identifiers", - "ruma-serde 0.2.1", + "ruma-serde", "serde", "serde_json", "strum", @@ -1289,16 +1289,15 @@ version = "0.16.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "52b82b4567b9af9b40a86f7778821c016ea961f55e4fee255f8f24bb28ee7452" dependencies = [ - "proc-macro2 1.0.13", + "proc-macro2 1.0.17", "quote 1.0.6", - "syn 1.0.23", + "syn 1.0.25", ] [[package]] name = "ruma-client-api" version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "082913ad135ca55ee06a55d295bea954982f2ac5e0150adc09024f5cbb8cb6cf" +source = "git+https://github.com/ruma/ruma-client-api.git#b064daf23dbf970933e83ce3b84a2563c5e646e7" dependencies = [ "http", "js_int", @@ -1306,7 +1305,7 @@ dependencies = [ "ruma-common", "ruma-events", "ruma-identifiers", - "ruma-serde 0.2.1", + "ruma-serde", "serde", "serde_json", "strum", @@ -1319,7 +1318,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "253416d67b4bde281f2781424232a58a946a4f1c451d5f857a8d0705d58eaf2a" dependencies = [ "matches", - "ruma-serde 0.2.1", + "ruma-serde", "serde", "serde_json", ] @@ -1327,14 +1326,13 @@ dependencies = [ [[package]] name = "ruma-events" version = "0.21.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0afc6d4da07d10213478d32dc42b6222df0a1ea319e9ced9f8a341617952d909" +source = "git+https://github.com/ruma/ruma-events.git#a17714669da1db4aa7bf10948463bb964cf5058a" dependencies = [ "js_int", "ruma-common", "ruma-events-macros", "ruma-identifiers", - "ruma-serde 0.2.1", + "ruma-serde", "serde", "serde_json", ] @@ -1342,24 +1340,24 @@ dependencies = [ [[package]] name = "ruma-events-macros" version = "0.21.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc706c4a53cc54c3a198cfbcd7dfff20448599d84f90e636d96034d0df5a9ac9" +source = "git+https://github.com/ruma/ruma-events.git#a17714669da1db4aa7bf10948463bb964cf5058a" dependencies = [ - "proc-macro2 1.0.13", + "proc-macro2 1.0.17", "quote 1.0.6", - "syn 1.0.23", + "syn 1.0.25", ] [[package]] name = "ruma-federation-api" version = "0.0.1" -source = "git+https://github.com/ruma/ruma-federation-api.git?rev=ccbf216f39bbbaa59131cc200eae5bd18aa1947c#ccbf216f39bbbaa59131cc200eae5bd18aa1947c" +source = "git+https://github.com/ruma/ruma-federation-api.git?rev=4cf4aa6ef74b25ad8c14d99d7774129f023df163#4cf4aa6ef74b25ad8c14d99d7774129f023df163" dependencies = [ "js_int", + "matches", "ruma-api", "ruma-events", "ruma-identifiers", - "ruma-serde 0.1.3", + "ruma-serde", "serde", "serde_json", ] @@ -1376,23 +1374,9 @@ dependencies = [ [[package]] name = "ruma-serde" -version = "0.1.3" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e14edc0e2f5177c419e3b89060b1e94fb3af81b2f253783ac6967f14a7ec3911" -dependencies = [ - "dtoa", - "itoa", - "js_int", - "serde", - "serde_json", - "url", -] - -[[package]] -name = "ruma-serde" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0dd3d04c6755bae23101dec7426d044b773ef517932f23d5a6254c4caa1cfce3" +checksum = "8c71dabb8e2709ca4f59201cb72d7fe8d590e7e3f55feb348e851c18354938af" dependencies = [ "dtoa", "itoa", @@ -1520,9 +1504,9 @@ version = "1.0.110" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "818fbf6bfa9a42d3bfcaca148547aa00c7b915bec71d1757aa2d44ca68771984" dependencies = [ - "proc-macro2 1.0.13", + "proc-macro2 1.0.17", "quote 1.0.6", - "syn 1.0.23", + "syn 1.0.25", ] [[package]] @@ -1614,9 +1598,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "87c85aa3f8ea653bfd3ddf25f7ee357ee4d204731f6aa9ad04002306f6e2774c" dependencies = [ "heck", - "proc-macro2 1.0.13", + "proc-macro2 1.0.17", "quote 1.0.6", - "syn 1.0.23", + "syn 1.0.25", ] [[package]] @@ -1632,11 +1616,11 @@ dependencies = [ [[package]] name = "syn" -version = "1.0.23" +version = "1.0.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95b5f192649e48a5302a13f2feb224df883b98933222369e4b3b0fe2a5447269" +checksum = "f14a640819f79b72a710c0be059dce779f9339ae046c8bef12c361d56702146f" dependencies = [ - "proc-macro2 1.0.13", + "proc-macro2 1.0.17", "quote 1.0.6", "unicode-xid 0.2.0", ] @@ -1657,22 +1641,22 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.18" +version = "1.0.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5976891d6950b4f68477850b5b9e5aa64d955961466f9e174363f573e54e8ca7" +checksum = "b13f926965ad00595dd129fa12823b04bbf866e9085ab0a5f2b05b850fbfc344" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.18" +version = "1.0.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab81dbd1cd69cd2ce22ecfbdd3bdb73334ba25350649408cc6c085f46d89573d" +checksum = "893582086c2f98cde18f906265a65b5030a074b1046c674ae898be6519a7f479" dependencies = [ - "proc-macro2 1.0.13", + "proc-macro2 1.0.17", "quote 1.0.6", - "syn 1.0.23", + "syn 1.0.25", ] [[package]] @@ -1714,9 +1698,9 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0c3acc6aa564495a0f2e1d59fab677cd7f81a19994cfc7f3ad0e64301560389" dependencies = [ - "proc-macro2 1.0.13", + "proc-macro2 1.0.17", "quote 1.0.6", - "syn 1.0.23", + "syn 1.0.25", ] [[package]] @@ -1782,7 +1766,7 @@ version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" dependencies = [ - "version_check 0.9.1", + "version_check 0.9.2", ] [[package]] @@ -1852,9 +1836,9 @@ checksum = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" [[package]] name = "version_check" -version = "0.9.1" +version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "078775d0255232fb988e6fccf26ddc9d1ac274299aaedcedce21c6f72cc533ce" +checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" [[package]] name = "want" @@ -1893,9 +1877,9 @@ dependencies = [ "bumpalo", "lazy_static", "log", - "proc-macro2 1.0.13", + "proc-macro2 1.0.17", "quote 1.0.6", - "syn 1.0.23", + "syn 1.0.25", "wasm-bindgen-shared", ] @@ -1927,9 +1911,9 @@ version = "0.2.62" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8eb197bd3a47553334907ffd2f16507b4f4f01bbec3ac921a7719e0decdfe72a" dependencies = [ - "proc-macro2 1.0.13", + "proc-macro2 1.0.17", "quote 1.0.6", - "syn 1.0.23", + "syn 1.0.25", "wasm-bindgen-backend", "wasm-bindgen-shared", ] diff --git a/Cargo.toml b/Cargo.toml index 5a068f5..c864508 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,12 +14,12 @@ edition = "2018" [dependencies] rocket = { git = "https://github.com/SergioBenitez/Rocket.git", branch = "async", features = ["tls"] } http = "0.2.1" -ruma-client-api = "0.9.0" +ruma-client-api = { git = "https://github.com/ruma/ruma-client-api.git" } ruma-identifiers = { version = "0.16.1", features = ["rand"] } ruma-api = "0.16.1" -ruma-events = "0.21.2" +ruma-events = { git = "https://github.com/ruma/ruma-events.git" } ruma-signatures = { git = "https://github.com/ruma/ruma-signatures.git", rev = "1ca545cba8dfd43e0fc8e3c18e1311fb73390a97" } -ruma-federation-api = { git = "https://github.com/ruma/ruma-federation-api.git", rev = "ccbf216f39bbbaa59131cc200eae5bd18aa1947c" } +ruma-federation-api = { git = "https://github.com/ruma/ruma-federation-api.git", rev = "4cf4aa6ef74b25ad8c14d99d7774129f023df163" } log = "0.4.8" sled = "0.31.0" directories = "2.0.2" @@ -31,6 +31,9 @@ rand = "0.7.3" rust-argon2 = "0.8.2" reqwest = "0.10.4" base64 = "0.12.1" -thiserror = "1.0.18" +thiserror = "1.0.19" ruma-common = "0.1.2" image = { version = "0.23.4", default-features = false, features = ["jpeg", "png", "gif"] } + +[patch.crates-io] +ruma-events = { git = "https://github.com/ruma/ruma-events.git" } diff --git a/src/client_server.rs b/src/client_server.rs index 6812fdd..0701be1 100644 --- a/src/client_server.rs +++ b/src/client_server.rs @@ -39,13 +39,13 @@ use ruma_client_api::{ to_device::{self, send_event_to_device}, typing::create_typing_event, uiaa::{AuthFlow, UiaaInfo, UiaaResponse}, - user_directory::search_users, + user_directory::search_users, redact::redact_event, }, unversioned::get_supported_versions, }; use ruma_events::{ collections::only::Event as EduEvent, - room::{guest_access, history_visibility, join_rules, member}, + room::{guest_access, history_visibility, join_rules, member, redaction}, EventJson, EventType, }; use ruma_identifiers::{RoomId, RoomVersionId, UserId}; @@ -517,6 +517,7 @@ pub fn set_displayname_route( .unwrap(), None, Some(user_id.to_string()), + None, &db.globals, ) .unwrap(); @@ -564,7 +565,7 @@ pub fn set_avatar_url_route( ) -> MatrixResult { let user_id = body.user_id.as_ref().expect("user is authenticated"); - if let avatar_url = &body.avatar_url { + if let Some(avatar_url) = &body.avatar_url { if !avatar_url.starts_with("mxc://") { debug!("Request contains an invalid avatar_url."); return MatrixResult(Err(Error { @@ -579,7 +580,7 @@ pub fn set_avatar_url_route( } db.users - .set_avatar_url(&user_id, Some(body.avatar_url.clone())) + .set_avatar_url(&user_id, body.avatar_url.clone()) .unwrap(); // Send a new membership event into all joined rooms @@ -591,7 +592,7 @@ pub fn set_avatar_url_route( user_id.clone(), EventType::RoomMember, serde_json::to_value(ruma_events::room::member::MemberEventContent { - avatar_url: Some(body.avatar_url.clone()), + avatar_url: body.avatar_url.clone(), ..serde_json::from_value::>( db.rooms .room_state(&room_id) @@ -608,6 +609,7 @@ pub fn set_avatar_url_route( .unwrap(), None, Some(user_id.to_string()), + None, &db.globals, ) .unwrap(); @@ -917,14 +919,15 @@ pub fn create_room_route( creator: user_id.clone(), federate: body .creation_content - .and_then(|c| c.federate) - .unwrap_or(true), - predecessor: None, // TODO: Check creation_content.predecessor once ruma has that + .as_ref() + .map_or(true, |c| c.federate), + predecessor: body.creation_content.as_ref().and_then(|c| c.predecessor.clone()), room_version: RoomVersionId::version_5(), }) .unwrap(), None, Some("".to_owned()), + None, &db.globals, ) .unwrap(); @@ -945,6 +948,7 @@ pub fn create_room_route( .unwrap(), None, Some(user_id.to_string()), + None, &db.globals, ) .unwrap(); @@ -968,18 +972,8 @@ pub fn create_room_route( .expect("TODO: handle. we hope the client sends a valid power levels json") } else { serde_json::to_value(ruma_events::room::power_levels::PowerLevelsEventContent { - ban: 50.into(), - events: BTreeMap::new(), - events_default: 0.into(), - invite: 50.into(), - kick: 50.into(), - redact: 50.into(), - state_default: 50.into(), users, - users_default: 0.into(), - notifications: ruma_events::room::power_levels::NotificationPowerLevels { - room: 50.into(), - }, + ..Default::default() }) .unwrap() }; @@ -991,6 +985,7 @@ pub fn create_room_route( power_levels_content, None, Some("".to_owned()), + None, &db.globals, ) .unwrap(); @@ -1016,6 +1011,7 @@ pub fn create_room_route( }, None, Some("".to_owned()), + None, &db.globals, ) .unwrap(); @@ -1032,6 +1028,7 @@ pub fn create_room_route( .unwrap(), None, Some("".to_owned()), + None, &db.globals, ) .unwrap(); @@ -1056,6 +1053,7 @@ pub fn create_room_route( }, None, Some("".to_owned()), + None, &db.globals, ) .unwrap(); @@ -1071,10 +1069,11 @@ pub fn create_room_route( .append_pdu( room_id.clone(), user_id.clone(), - EventType::from(event_type), + event_type.clone(), serde_json::from_str(content.get()).unwrap(), None, state_key.clone(), + None, &db.globals, ) .unwrap(); @@ -1093,6 +1092,7 @@ pub fn create_room_route( .unwrap(), None, Some("".to_owned()), + None, &db.globals, ) .unwrap(); @@ -1110,6 +1110,7 @@ pub fn create_room_route( .unwrap(), None, Some("".to_owned()), + None, &db.globals, ) .unwrap(); @@ -1132,6 +1133,7 @@ pub fn create_room_route( .unwrap(), None, Some(user.to_string()), + None, &db.globals, ) .unwrap(); @@ -1140,6 +1142,38 @@ pub fn create_room_route( MatrixResult(Ok(create_room::Response { room_id })) } +#[put("/_matrix/client/r0/rooms/<_room_id>/redact/<_event_id>/<_txn_id>", data = "")] +pub fn redact_event_route( + db: State<'_, Database>, + body: Ruma, + _room_id: String, + _event_id: String, + _txn_id: String, +) -> MatrixResult { + let user_id = body.user_id.as_ref().expect("user is authenticated"); + + if let Ok(event_id) = db.rooms.append_pdu( + body.room_id.clone(), + user_id.clone(), + EventType::RoomRedaction, + serde_json::to_value(redaction::RedactionEventContent { + reason: body.reason.clone(), + }).unwrap(), + None, + None, + Some(body.event_id.clone()), + &db.globals, + ) { + MatrixResult(Ok(redact_event::Response { event_id })) + } else { + MatrixResult(Err(Error { + kind: ErrorKind::Unknown, + message: "Failed to redact event.".to_owned(), + status_code: http::StatusCode::BAD_REQUEST, + })) + } +} + #[get("/_matrix/client/r0/directory/room/<_room_alias>", data = "")] pub fn get_alias_route( db: State<'_, Database>, @@ -1220,6 +1254,7 @@ pub fn join_room_by_id_route( serde_json::to_value(event).unwrap(), None, Some(user_id.to_string()), + None, &db.globals, ) .unwrap(); @@ -1285,6 +1320,7 @@ pub fn leave_room_route( json!({"membership": "leave"}), None, Some(user_id.to_string()), + None, &db.globals, ) .unwrap(); @@ -1328,6 +1364,7 @@ pub fn invite_user_route( serde_json::to_value(event).unwrap(), None, Some(user_id.to_string()), + None, &db.globals, ) .unwrap(); @@ -1414,7 +1451,7 @@ pub async fn get_public_rooms_filtered_route( .deserialize() .unwrap() .alias - }).map(|a| a.to_string()), + }), name: state.get(&(EventType::RoomName, "".to_owned())).map(|s| { serde_json::from_value::>( s.content.clone(), @@ -1565,6 +1602,7 @@ pub fn create_message_event_route( serde_json::from_str(body.json_body.unwrap().get()).unwrap(), Some(unsigned), None, + None, &db.globals, ) { MatrixResult(Ok(create_message_event::Response { event_id })) @@ -1598,6 +1636,7 @@ pub fn create_state_event_for_key_route( serde_json::from_str(body.json_body.clone().unwrap().get()).unwrap(), None, Some(body.state_key.clone()), + None, &db.globals, ) { MatrixResult(Ok(create_state_event_for_key::Response { event_id })) @@ -1630,6 +1669,7 @@ pub fn create_state_event_for_empty_key_route( serde_json::from_str(body.json_body.unwrap().get()).unwrap(), None, Some("".to_owned()), + None, &db.globals, ) { MatrixResult(Ok(create_state_event_for_empty_key::Response { event_id })) diff --git a/src/database/rooms.rs b/src/database/rooms.rs index 87f3429..8e626cb 100644 --- a/src/database/rooms.rs +++ b/src/database/rooms.rs @@ -12,7 +12,7 @@ use ruma_events::{ EventJson, EventType, }; use ruma_identifiers::{EventId, RoomId, UserId}; - +use sled::IVec; use std::{ collections::{BTreeMap, HashMap}, convert::{TryFrom, TryInto}, @@ -110,28 +110,50 @@ impl Rooms { self.eventid_pduid .get(event_id.to_string().as_bytes())? .map_or(Ok(None), |pdu_id| { - Ok(serde_json::from_slice( + Ok(Some(serde_json::from_slice( &self.pduid_pdu.get(pdu_id)?.ok_or(Error::BadDatabase( "eventid_pduid points to nonexistent pdu", ))?, - )?) - .map(Some) + )?)) }) } + /// Returns the pdu's id. + pub fn get_pdu_id(&self, event_id: &EventId) -> Result> { + self.eventid_pduid + .get(event_id.to_string().as_bytes())? + .map_or(Ok(None), |pdu_id| Ok(Some(pdu_id))) + } + /// Returns the pdu. pub fn get_pdu(&self, event_id: &EventId) -> Result> { self.eventid_pduid .get(event_id.to_string().as_bytes())? .map_or(Ok(None), |pdu_id| { - Ok(serde_json::from_slice( + Ok(Some(serde_json::from_slice( &self.pduid_pdu.get(pdu_id)?.ok_or(Error::BadDatabase( "eventid_pduid points to nonexistent pdu", ))?, - )?) - .map(Some) + )?)) }) } + /// Returns the pdu. + pub fn get_pdu_from_id(&self, pdu_id: &IVec) -> Result> { + self.pduid_pdu + .get(pdu_id)? + .map_or(Ok(None), |pdu| Ok(Some(serde_json::from_slice(&pdu)?))) + } + + /// Returns the pdu. + pub fn replace_pdu(&self, pdu_id: &IVec, pdu: &PduEvent) -> Result<()> { + if self.pduid_pdu.get(&pdu_id)?.is_some() { + self.pduid_pdu + .insert(&pdu_id, &*serde_json::to_string(pdu)?)?; + Ok(()) + } else { + Err(Error::BadRequest("pdu does not exist")) + } + } /// Returns the leaf pdus of a room. pub fn get_pdu_leaves(&self, room_id: &RoomId) -> Result> { @@ -177,6 +199,7 @@ impl Rooms { content: serde_json::Value, unsigned: Option>, state_key: Option, + redacts: Option, globals: &super::globals::Globals, ) -> Result { // TODO: Make sure this isn't called twice in parallel @@ -435,7 +458,7 @@ impl Rooms { .try_into() .expect("depth can overflow and should be deprecated..."), auth_events: Vec::new(), - redacts: None, + redacts, unsigned, hashes: ruma_federation_api::EventHash { sha256: "aaa".to_owned(), @@ -555,7 +578,21 @@ impl Rooms { .map(|(_, v)| Ok(serde_json::from_slice(&v)?)) } - /// Makes a user join a room. Only call this if the membership is Join already + /// Replace a PDU with the redacted form. + pub fn redact_pdu(&self, event_id: &EventId) -> Result<()> { + if let Some(pdu_id) = self.get_pdu_id(event_id)? { + let mut pdu = self + .get_pdu_from_id(&pdu_id)? + .ok_or(Error::BadDatabase("pduid points to invalid pdu"))?; + pdu.redact(); + self.replace_pdu(&pdu_id, &pdu)?; + Ok(()) + } else { + Err(Error::BadRequest("eventid does not exist")) + } + } + + /// Update current membership data. fn update_membership( &self, room_id: &RoomId, diff --git a/src/pdu.rs b/src/pdu.rs index 0e1b3de..3e1ac0a 100644 --- a/src/pdu.rs +++ b/src/pdu.rs @@ -33,6 +33,38 @@ pub struct PduEvent { } impl PduEvent { + pub fn redact(&mut self) { + self.unsigned.clear(); + let allowed = match self.kind { + EventType::RoomMember => vec!["membership"], + EventType::RoomCreate => vec!["creator"], + EventType::RoomJoinRules => vec!["join_rule"], + EventType::RoomPowerLevels => vec![ + "ban", + "events", + "events_default", + "kick", + "redact", + "state_default", + "users", + "users_default", + ], + EventType::RoomHistoryVisibility => vec!["history_visibility"], + _ => vec![], + }; + + let old_content = self.content.as_object_mut().unwrap(); // TODO error + let mut new_content = serde_json::Map::new(); + + for key in allowed { + if let Some(value) = old_content.remove(key) { + new_content.insert(key.to_owned(), value); + } + } + + self.content = new_content.into(); + } + pub fn to_room_event(&self) -> EventJson { // Can only fail in rare circumstances that won't ever happen here, see // https://docs.rs/serde_json/1.0.50/serde_json/fn.to_string.html @@ -40,7 +72,6 @@ impl PduEvent { // EventJson's deserialize implementation always returns `Ok(...)` serde_json::from_str::>(&json).unwrap() } - pub fn to_state_event(&self) -> EventJson { let json = serde_json::to_string(&self).unwrap(); serde_json::from_str::>(&json).unwrap() diff --git a/src/server_server.rs b/src/server_server.rs index 84ca5cc..6aa1e99 100644 --- a/src/server_server.rs +++ b/src/server_server.rs @@ -4,7 +4,9 @@ use log::error; use rocket::{get, response::content::Json, State}; use ruma_api::Endpoint; use ruma_client_api::error::Error; -use ruma_federation_api::{v1::get_server_version, v2::get_server_keys}; +use ruma_federation_api::discovery::{ + get_server_keys::v2 as get_server_keys, get_server_version::v1 as get_server_version, +}; use serde_json::json; use std::{ collections::BTreeMap,