fix: one time keys are never removed

This commit is contained in:
timokoesters 2020-05-23 19:17:08 +02:00
parent 5d6542a8a6
commit 1014388a9c
No known key found for this signature in database
GPG key ID: 24DA7517711A2BA4
5 changed files with 54 additions and 102 deletions

5
Cargo.lock generated
View file

@ -1296,8 +1296,9 @@ dependencies = [
[[package]] [[package]]
name = "ruma-client-api" name = "ruma-client-api"
version = "0.8.0" version = "0.9.0"
source = "git+https://github.com/ruma/ruma-client-api.git#3a3ccabbf22c34da5c9de7cac54d9fbd3e571dcf" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "082913ad135ca55ee06a55d295bea954982f2ac5e0150adc09024f5cbb8cb6cf"
dependencies = [ dependencies = [
"http", "http",
"js_int", "js_int",

View file

@ -14,7 +14,7 @@ edition = "2018"
[dependencies] [dependencies]
rocket = { git = "https://github.com/SergioBenitez/Rocket.git", branch = "async", features = ["tls"] } rocket = { git = "https://github.com/SergioBenitez/Rocket.git", branch = "async", features = ["tls"] }
http = "0.2.1" 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-identifiers = { version = "0.16.1", features = ["rand"] }
ruma-api = "0.16.1" ruma-api = "0.16.1"
ruma-events = "0.21.2" ruma-events = "0.21.2"

View file

@ -185,14 +185,20 @@ pub fn register_route(
content: ruma_events::push_rules::PushRulesEventContent { content: ruma_events::push_rules::PushRulesEventContent {
global: ruma_events::push_rules::Ruleset { global: ruma_events::push_rules::Ruleset {
content: vec![], 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![], room: vec![],
sender: vec![], sender: vec![],
underride: vec![ruma_events::push_rules::ConditionalPushRule { underride: vec![ruma_events::push_rules::ConditionalPushRule {
actions: vec![ actions: vec![
ruma_events::push_rules::Action::Notify, ruma_events::push_rules::Action::Notify,
ruma_events::push_rules::Action::SetTweak( ruma_events::push_rules::Action::SetTweak(
ruma_common::push::Tweak::Highlight(false), ruma_common::push::Tweak::Sound("default".to_owned()),
), ),
], ],
default: true, default: true,
@ -320,28 +326,27 @@ pub fn get_capabilities_route() -> MatrixResult<get_capabilities::Response> {
})) }))
} }
#[get("/_matrix/client/r0/pushrules")] #[get("/_matrix/client/r0/pushrules", data = "<body>")]
pub fn get_pushrules_all_route() -> MatrixResult<get_pushrules_all::Response> { pub fn get_pushrules_all_route(
// TODO db: State<'_, Database>,
let mut global = BTreeMap::new(); body: Ruma<get_pushrules_all::Request>,
global.insert( ) -> MatrixResult<get_pushrules_all::Response> {
push::RuleKind::Underride, let user_id = body.user_id.as_ref().expect("user is authenticated");
vec![push::PushRule { if let Some(EduEvent::PushRules(pushrules)) = db
actions: vec![ .account_data
push::Action::Notify, .get(None, &user_id, &EventType::PushRules)
push::Action::SetTweak(ruma_common::push::Tweak::Highlight(false)), .unwrap().map(|edu| edu.deserialize().expect("PushRules event in db is valid"))
], {
default: true, MatrixResult(Ok(get_pushrules_all::Response {
enabled: true, global: BTreeMap::new(),
rule_id: ".m.rule.message".to_owned(), }))
conditions: Some(vec![push::PushCondition::EventMatch { } else {
key: "type".to_owned(), MatrixResult(Err(Error {
pattern: "m.room.message".to_owned(), kind: ErrorKind::NotFound,
}]), message: "PushRules event not found.".to_owned(),
pattern: None, status_code: http::StatusCode::BAD_REQUEST,
}], }))
); }
MatrixResult(Ok(get_pushrules_all::Response { global }))
} }
#[put( #[put(
@ -356,46 +361,7 @@ pub fn set_pushrule_route(
_rule_id: String, _rule_id: String,
) -> MatrixResult<set_pushrule::Response> { ) -> MatrixResult<set_pushrule::Response> {
// TODO // TODO
let user_id = body.user_id.as_ref().expect("user is authenticated"); warn!("TODO: set_pushrule_route");
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();
MatrixResult(Ok(set_pushrule::Response)) MatrixResult(Ok(set_pushrule::Response))
} }
@ -406,6 +372,7 @@ pub fn set_pushrule_enabled_route(
_rule_id: String, _rule_id: String,
) -> MatrixResult<set_pushrule_enabled::Response> { ) -> MatrixResult<set_pushrule_enabled::Response> {
// TODO // TODO
warn!("TODO: set_pushrule_enabled_route");
MatrixResult(Ok(set_pushrule_enabled::Response)) 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())) .set_avatar_url(&user_id, Some(body.avatar_url.clone()))
.unwrap(); .unwrap();
let mut json = serde_json::Map::new(); let mut json = serde_json::Map::new();
json.insert("membership".to_owned(), "join".into()); json.insert("membership".to_owned(), "join".into());
json.insert("avatar_url".to_owned(), (*body.avatar_url).into()); json.insert("avatar_url".to_owned(), (*body.avatar_url).into());
@ -962,12 +928,7 @@ pub fn create_room_route(
.unwrap(); .unwrap();
db.rooms db.rooms
.join( .join(&room_id, &user_id, &db.users, &db.globals)
&room_id,
&user_id,
&db.users,
&db.globals,
)
.unwrap(); .unwrap();
db.rooms db.rooms
@ -1069,12 +1030,7 @@ pub fn join_room_by_id_route(
if db if db
.rooms .rooms
.join( .join(&body.room_id, &user_id, &db.users, &db.globals)
&body.room_id,
&user_id,
&db.users,
&db.globals,
)
.is_ok() .is_ok()
{ {
MatrixResult(Ok(join_room_by_id::Response { MatrixResult(Ok(join_room_by_id::Response {
@ -1116,12 +1072,7 @@ pub fn join_room_by_id_or_alias_route(
if db if db
.rooms .rooms
.join( .join(&room_id, &user_id, &db.users, &db.globals)
&room_id,
&user_id,
&db.users,
&db.globals,
)
.is_ok() .is_ok()
{ {
MatrixResult(Ok(join_room_by_id_or_alias::Response { room_id })) MatrixResult(Ok(join_room_by_id_or_alias::Response { room_id }))
@ -1545,7 +1496,6 @@ pub fn sync_route(
None None
}; };
// They /sync response doesn't always return all messages, so we say the output is // They /sync response doesn't always return all messages, so we say the output is
// limited unless there are enough events // limited unless there are enough events
let mut limited = true; let mut limited = true;
@ -1617,7 +1567,7 @@ pub fn sync_route(
}, },
}, },
unread_notifications: sync_events::UnreadNotificationsCount { unread_notifications: sync_events::UnreadNotificationsCount {
highlight_count: None, highlight_count: notification_count,
notification_count, notification_count,
}, },
timeline: sync_events::Timeline { timeline: sync_events::Timeline {
@ -1736,8 +1686,8 @@ pub fn sync_route(
changed: db changed: db
.users .users
.device_keys_changed(since) .device_keys_changed(since)
.map(|u| u.unwrap().to_string()) .map(|u| u.unwrap())
.collect(), // TODO: use userids when ruma changes .collect(),
left: Vec::new(), // TODO left: Vec::new(), // TODO
}) })
} else { } else {
@ -1843,8 +1793,7 @@ pub fn send_event_to_device_route(
to_device::DeviceIdOrAllDevices::AllDevices => { to_device::DeviceIdOrAllDevices::AllDevices => {
for target_device_id in db.users.all_device_ids(&target_user_id) { for target_device_id in db.users.all_device_ids(&target_user_id) {
db db.users
.users
.add_to_device_event( .add_to_device_event(
user_id, user_id,
&target_user_id, &target_user_id,

View file

@ -209,6 +209,8 @@ impl Users {
.next() .next()
.map(|r| { .map(|r| {
let (key, value) = r?; let (key, value) = r?;
self.onetimekeyid_onetimekeys.remove(&key)?;
Ok(( Ok((
serde_json::from_slice( serde_json::from_slice(
&*key &*key

View file

@ -20,7 +20,7 @@ pub fn increment(old: Option<&[u8]>) -> Option<Vec<u8>> {
let number = u64::from_be_bytes(array); let number = u64::from_be_bytes(array);
number + 1 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()) Some(number.to_be_bytes().to_vec())