From 9c0c74f547f5088de3e74cf1dc01f58538922e16 Mon Sep 17 00:00:00 2001 From: Paul Beziau Date: Tue, 18 Oct 2022 09:15:07 +0000 Subject: [PATCH 1/3] Migrate database to use correct rule id in pushrules. it convert : - ".m.rules.call" to ".m.rule.call" - ".m.rules.room_one_to_one" to ".m.rule.room_one_to_one" - ".m.rules.encrypted_room_one_to_one" to ".m.rule.encrypted_room_one_to_one" - ".m.rules.message" to ".m.rule.message" - ".m.rules.encrypted" to ".m.rule.encrypted" related to issue #264 --- src/database/mod.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/src/database/mod.rs b/src/database/mod.rs index 15ee1373..dce4eff6 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -7,7 +7,7 @@ use directories::ProjectDirs; use lru_cache::LruCache; use ruma::{ events::{ - push_rules::PushRulesEventContent, room::message::RoomMessageEventContent, + push_rules::{PushRulesEventContent, PushRulesEvent}, room::message::RoomMessageEventContent, GlobalAccountDataEvent, GlobalAccountDataEventType, StateEventType, }, push::Ruleset, @@ -801,6 +801,72 @@ impl KeyValueDatabase { warn!("Migration: 10 -> 11 finished"); } + if services().globals.database_version()? < 12 { + + for username in services().users.list_local_users().unwrap() { + + let user = UserId::parse_with_server_name(username, services().globals.server_name()) + .unwrap(); + + + let raw_rules_list = services().account_data + .get( + None, + &user, + GlobalAccountDataEventType::PushRules.to_string().into()) + .unwrap() + .expect("Username is invalid"); + + let mut account_data = serde_json::from_str::(raw_rules_list.get()).unwrap(); + let rules_list = &mut account_data.content.global; + + //content rule + { + let content_rule_transformation = + [".m.rules.contains_user_name", ".m.rule.contains_user_name"]; + + let rule = rules_list.content.get(content_rule_transformation[0]); + if rule.is_some() { + let mut rule = rule.unwrap().clone(); + rule.rule_id = content_rule_transformation[1].to_string(); + rules_list.content.remove(content_rule_transformation[0]); + rules_list.content.insert(rule); + } + } + + //underride rules + { + let underride_rule_transformation = + [[".m.rules.call", ".m.rule.call"], + [".m.rules.room_one_to_one", ".m.rule.room_one_to_one"], + [".m.rules.encrypted_room_one_to_one", ".m.rule.encrypted_room_one_to_one"], + [".m.rules.message", ".m.rule.message"], + [".m.rules.encrypted", ".m.rule.encrypted"]]; + + for transformation in underride_rule_transformation { + let rule = rules_list.underride.get(transformation[0]); + if rule.is_some() { + let mut rule = rule.unwrap().clone(); + rule.rule_id = transformation[1].to_string(); + rules_list.underride.remove(transformation[0]); + rules_list.underride.insert(rule); + } + } + } + + services().account_data.update( + None, + &user, + GlobalAccountDataEventType::PushRules.to_string().into(), + &serde_json::to_value(account_data).expect("to json value always works"), + )?; + } + + services().globals.bump_database_version(12)?; + + warn!("Migration: 11 -> 12 finished"); + } + assert_eq!(11, latest_database_version); info!( From d47c1a8ba678bf58b1ff8bdb65438aaaddc80188 Mon Sep 17 00:00:00 2001 From: Paul Beziau Date: Fri, 21 Oct 2022 12:27:11 +0000 Subject: [PATCH 2/3] Fix database version check & code formating --- src/database/mod.rs | 127 ++++++++++++++++++++++++-------------------- 1 file changed, 68 insertions(+), 59 deletions(-) diff --git a/src/database/mod.rs b/src/database/mod.rs index dce4eff6..ccc81774 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -7,7 +7,8 @@ use directories::ProjectDirs; use lru_cache::LruCache; use ruma::{ events::{ - push_rules::{PushRulesEventContent, PushRulesEvent}, room::message::RoomMessageEventContent, + push_rules::{PushRulesEvent, PushRulesEventContent}, + room::message::RoomMessageEventContent, GlobalAccountDataEvent, GlobalAccountDataEventType, StateEventType, }, push::Ruleset, @@ -405,7 +406,7 @@ impl KeyValueDatabase { } // If the database has any data, perform data migrations before starting - let latest_database_version = 11; + let latest_database_version = 12; if services().users.count()? > 0 { // MIGRATIONS @@ -801,73 +802,81 @@ impl KeyValueDatabase { warn!("Migration: 10 -> 11 finished"); } - if services().globals.database_version()? < 12 { + if services().globals.database_version()? < 12 { + for username in services().users.list_local_users().unwrap() { + let user = + UserId::parse_with_server_name(username, services().globals.server_name()) + .unwrap(); - for username in services().users.list_local_users().unwrap() { + let raw_rules_list = services() + .account_data + .get( + None, + &user, + GlobalAccountDataEventType::PushRules.to_string().into(), + ) + .unwrap() + .expect("Username is invalid"); - let user = UserId::parse_with_server_name(username, services().globals.server_name()) - .unwrap(); + let mut account_data = + serde_json::from_str::(raw_rules_list.get()).unwrap(); + let rules_list = &mut account_data.content.global; + //content rule + { + let content_rule_transformation = + [".m.rules.contains_user_name", ".m.rule.contains_user_name"]; - let raw_rules_list = services().account_data - .get( - None, - &user, - GlobalAccountDataEventType::PushRules.to_string().into()) - .unwrap() - .expect("Username is invalid"); + let rule = rules_list.content.get(content_rule_transformation[0]); + if rule.is_some() { + let mut rule = rule.unwrap().clone(); + rule.rule_id = content_rule_transformation[1].to_string(); + rules_list.content.remove(content_rule_transformation[0]); + rules_list.content.insert(rule); + } + } - let mut account_data = serde_json::from_str::(raw_rules_list.get()).unwrap(); - let rules_list = &mut account_data.content.global; + //underride rules + { + let underride_rule_transformation = [ + [".m.rules.call", ".m.rule.call"], + [".m.rules.room_one_to_one", ".m.rule.room_one_to_one"], + [ + ".m.rules.encrypted_room_one_to_one", + ".m.rule.encrypted_room_one_to_one", + ], + [".m.rules.message", ".m.rule.message"], + [".m.rules.encrypted", ".m.rule.encrypted"], + ]; - //content rule - { - let content_rule_transformation = - [".m.rules.contains_user_name", ".m.rule.contains_user_name"]; + for transformation in underride_rule_transformation { + let rule = rules_list.underride.get(transformation[0]); + if rule.is_some() { + let mut rule = rule.unwrap().clone(); + rule.rule_id = transformation[1].to_string(); + rules_list.underride.remove(transformation[0]); + rules_list.underride.insert(rule); + } + } + } - let rule = rules_list.content.get(content_rule_transformation[0]); - if rule.is_some() { - let mut rule = rule.unwrap().clone(); - rule.rule_id = content_rule_transformation[1].to_string(); - rules_list.content.remove(content_rule_transformation[0]); - rules_list.content.insert(rule); - } - } + services().account_data.update( + None, + &user, + GlobalAccountDataEventType::PushRules.to_string().into(), + &serde_json::to_value(account_data).expect("to json value always works"), + )?; + } - //underride rules - { - let underride_rule_transformation = - [[".m.rules.call", ".m.rule.call"], - [".m.rules.room_one_to_one", ".m.rule.room_one_to_one"], - [".m.rules.encrypted_room_one_to_one", ".m.rule.encrypted_room_one_to_one"], - [".m.rules.message", ".m.rule.message"], - [".m.rules.encrypted", ".m.rule.encrypted"]]; + services().globals.bump_database_version(12)?; - for transformation in underride_rule_transformation { - let rule = rules_list.underride.get(transformation[0]); - if rule.is_some() { - let mut rule = rule.unwrap().clone(); - rule.rule_id = transformation[1].to_string(); - rules_list.underride.remove(transformation[0]); - rules_list.underride.insert(rule); - } - } - } + warn!("Migration: 11 -> 12 finished"); + } - services().account_data.update( - None, - &user, - GlobalAccountDataEventType::PushRules.to_string().into(), - &serde_json::to_value(account_data).expect("to json value always works"), - )?; - } - - services().globals.bump_database_version(12)?; - - warn!("Migration: 11 -> 12 finished"); - } - - assert_eq!(11, latest_database_version); + assert_eq!( + services().globals.database_version().unwrap(), + latest_database_version + ); info!( "Loaded {} database with version {}", From a2d8aec1e3a1578872f1caaee719d3acfa5227bc Mon Sep 17 00:00:00 2001 From: Paul Beziau Date: Thu, 3 Nov 2022 13:12:53 +0000 Subject: [PATCH 3/3] Moving the unwraping of a variable Moving the unwraping of the variable "rule" inside the condition instead of the if body, for the migration of the database from version 11 to 12. --- src/database/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/database/mod.rs b/src/database/mod.rs index ccc81774..f6a76c68 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -851,8 +851,8 @@ impl KeyValueDatabase { for transformation in underride_rule_transformation { let rule = rules_list.underride.get(transformation[0]); - if rule.is_some() { - let mut rule = rule.unwrap().clone(); + if let Some(rule) = rule { + let mut rule = rule.clone(); rule.rule_id = transformation[1].to_string(); rules_list.underride.remove(transformation[0]); rules_list.underride.insert(rule);