From 1014388a9c0f917dd92ce85fb300c8d9abb50d5a Mon Sep 17 00:00:00 2001 From: timokoesters Date: Sat, 23 May 2020 19:17:08 +0200 Subject: [PATCH] fix: one time keys are never removed --- Cargo.lock | 5 +- Cargo.toml | 2 +- src/client_server.rs | 145 ++++++++++++++---------------------------- src/database/users.rs | 2 + src/utils.rs | 2 +- 5 files changed, 54 insertions(+), 102 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 234372b..dc47816 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1296,8 +1296,9 @@ dependencies = [ [[package]] name = "ruma-client-api" -version = "0.8.0" -source = "git+https://github.com/ruma/ruma-client-api.git#3a3ccabbf22c34da5c9de7cac54d9fbd3e571dcf" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "082913ad135ca55ee06a55d295bea954982f2ac5e0150adc09024f5cbb8cb6cf" dependencies = [ "http", "js_int", diff --git a/Cargo.toml b/Cargo.toml index 6edb8e3..5a068f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ edition = "2018" [dependencies] rocket = { git = "https://github.com/SergioBenitez/Rocket.git", branch = "async", features = ["tls"] } http = "0.2.1" -ruma-client-api = { git = "https://github.com/ruma/ruma-client-api.git" } +ruma-client-api = "0.9.0" ruma-identifiers = { version = "0.16.1", features = ["rand"] } ruma-api = "0.16.1" ruma-events = "0.21.2" diff --git a/src/client_server.rs b/src/client_server.rs index a47dfae..a2045d8 100644 --- a/src/client_server.rs +++ b/src/client_server.rs @@ -185,14 +185,20 @@ pub fn register_route( content: ruma_events::push_rules::PushRulesEventContent { global: ruma_events::push_rules::Ruleset { content: vec![], - override_rules: vec![], + override_rules: vec![ruma_events::push_rules::ConditionalPushRule { + actions: vec![ruma_events::push_rules::Action::DontNotify], + default: true, + enabled: false, + rule_id: ".m.rule.master".to_owned(), + conditions: vec![], + }], room: vec![], sender: vec![], underride: vec![ruma_events::push_rules::ConditionalPushRule { actions: vec![ ruma_events::push_rules::Action::Notify, ruma_events::push_rules::Action::SetTweak( - ruma_common::push::Tweak::Highlight(false), + ruma_common::push::Tweak::Sound("default".to_owned()), ), ], default: true, @@ -320,28 +326,27 @@ pub fn get_capabilities_route() -> MatrixResult { })) } -#[get("/_matrix/client/r0/pushrules")] -pub fn get_pushrules_all_route() -> MatrixResult { - // TODO - let mut global = BTreeMap::new(); - global.insert( - push::RuleKind::Underride, - vec![push::PushRule { - actions: vec![ - push::Action::Notify, - push::Action::SetTweak(ruma_common::push::Tweak::Highlight(false)), - ], - default: true, - enabled: true, - rule_id: ".m.rule.message".to_owned(), - conditions: Some(vec![push::PushCondition::EventMatch { - key: "type".to_owned(), - pattern: "m.room.message".to_owned(), - }]), - pattern: None, - }], - ); - MatrixResult(Ok(get_pushrules_all::Response { global })) +#[get("/_matrix/client/r0/pushrules", data = "")] +pub fn get_pushrules_all_route( + db: State<'_, Database>, + body: Ruma, +) -> MatrixResult { + let user_id = body.user_id.as_ref().expect("user is authenticated"); + if let Some(EduEvent::PushRules(pushrules)) = db + .account_data + .get(None, &user_id, &EventType::PushRules) + .unwrap().map(|edu| edu.deserialize().expect("PushRules event in db is valid")) + { + MatrixResult(Ok(get_pushrules_all::Response { + global: BTreeMap::new(), + })) + } else { + MatrixResult(Err(Error { + kind: ErrorKind::NotFound, + message: "PushRules event not found.".to_owned(), + status_code: http::StatusCode::BAD_REQUEST, + })) + } } #[put( @@ -356,46 +361,7 @@ pub fn set_pushrule_route( _rule_id: String, ) -> MatrixResult { // TODO - let user_id = body.user_id.as_ref().expect("user is authenticated"); - db.account_data - .update( - None, - &user_id, - &EventType::PushRules, - serde_json::to_value(ruma_events::push_rules::PushRulesEvent { - content: ruma_events::push_rules::PushRulesEventContent { - global: ruma_events::push_rules::Ruleset { - content: vec![], - override_rules: vec![], - room: vec![], - sender: vec![], - underride: vec![ruma_events::push_rules::ConditionalPushRule { - actions: vec![ - ruma_events::push_rules::Action::Notify, - ruma_events::push_rules::Action::SetTweak( - ruma_common::push::Tweak::Highlight(false), - ), - ], - default: true, - enabled: true, - rule_id: ".m.rule.message".to_owned(), - conditions: vec![ruma_events::push_rules::PushCondition::EventMatch( - ruma_events::push_rules::EventMatchCondition { - key: "type".to_owned(), - pattern: "m.room.message".to_owned(), - }, - )], - }], - }, - }, - }) - .unwrap() - .as_object_mut() - .unwrap(), - &db.globals, - ) - .unwrap(); - + warn!("TODO: set_pushrule_route"); MatrixResult(Ok(set_pushrule::Response)) } @@ -406,6 +372,7 @@ pub fn set_pushrule_enabled_route( _rule_id: String, ) -> MatrixResult { // TODO + warn!("TODO: set_pushrule_enabled_route"); MatrixResult(Ok(set_pushrule_enabled::Response)) } @@ -617,7 +584,6 @@ pub fn set_avatar_url_route( .set_avatar_url(&user_id, Some(body.avatar_url.clone())) .unwrap(); - let mut json = serde_json::Map::new(); json.insert("membership".to_owned(), "join".into()); json.insert("avatar_url".to_owned(), (*body.avatar_url).into()); @@ -962,12 +928,7 @@ pub fn create_room_route( .unwrap(); db.rooms - .join( - &room_id, - &user_id, - &db.users, - &db.globals, - ) + .join(&room_id, &user_id, &db.users, &db.globals) .unwrap(); db.rooms @@ -1069,12 +1030,7 @@ pub fn join_room_by_id_route( if db .rooms - .join( - &body.room_id, - &user_id, - &db.users, - &db.globals, - ) + .join(&body.room_id, &user_id, &db.users, &db.globals) .is_ok() { MatrixResult(Ok(join_room_by_id::Response { @@ -1116,12 +1072,7 @@ pub fn join_room_by_id_or_alias_route( if db .rooms - .join( - &room_id, - &user_id, - &db.users, - &db.globals, - ) + .join(&room_id, &user_id, &db.users, &db.globals) .is_ok() { MatrixResult(Ok(join_room_by_id_or_alias::Response { room_id })) @@ -1545,7 +1496,6 @@ pub fn sync_route( None }; - // They /sync response doesn't always return all messages, so we say the output is // limited unless there are enough events let mut limited = true; @@ -1617,7 +1567,7 @@ pub fn sync_route( }, }, unread_notifications: sync_events::UnreadNotificationsCount { - highlight_count: None, + highlight_count: notification_count, notification_count, }, timeline: sync_events::Timeline { @@ -1736,8 +1686,8 @@ pub fn sync_route( changed: db .users .device_keys_changed(since) - .map(|u| u.unwrap().to_string()) - .collect(), // TODO: use userids when ruma changes + .map(|u| u.unwrap()) + .collect(), left: Vec::new(), // TODO }) } else { @@ -1843,17 +1793,16 @@ pub fn send_event_to_device_route( to_device::DeviceIdOrAllDevices::AllDevices => { for target_device_id in db.users.all_device_ids(&target_user_id) { - db - .users - .add_to_device_event( - user_id, - &target_user_id, - &target_device_id.unwrap(), - &body.event_type, - serde_json::from_str(event.get()).unwrap(), - &db.globals, - ) - .unwrap(); + db.users + .add_to_device_event( + user_id, + &target_user_id, + &target_device_id.unwrap(), + &body.event_type, + serde_json::from_str(event.get()).unwrap(), + &db.globals, + ) + .unwrap(); } } } diff --git a/src/database/users.rs b/src/database/users.rs index 1b56066..bf1f214 100644 --- a/src/database/users.rs +++ b/src/database/users.rs @@ -209,6 +209,8 @@ impl Users { .next() .map(|r| { let (key, value) = r?; + self.onetimekeyid_onetimekeys.remove(&key)?; + Ok(( serde_json::from_slice( &*key diff --git a/src/utils.rs b/src/utils.rs index 45b9b08..5b41bd4 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -20,7 +20,7 @@ pub fn increment(old: Option<&[u8]>) -> Option> { let number = u64::from_be_bytes(array); number + 1 } - None => 0, + None => 1, // Start at one. since 0 should return the first event in the db }; Some(number.to_be_bytes().to_vec())