Fix database version check & code formating

This commit is contained in:
Paul Beziau 2022-10-21 12:27:11 +00:00
parent 9c0c74f547
commit d47c1a8ba6

View file

@ -7,7 +7,8 @@ use directories::ProjectDirs;
use lru_cache::LruCache; use lru_cache::LruCache;
use ruma::{ use ruma::{
events::{ events::{
push_rules::{PushRulesEventContent, PushRulesEvent}, room::message::RoomMessageEventContent, push_rules::{PushRulesEvent, PushRulesEventContent},
room::message::RoomMessageEventContent,
GlobalAccountDataEvent, GlobalAccountDataEventType, StateEventType, GlobalAccountDataEvent, GlobalAccountDataEventType, StateEventType,
}, },
push::Ruleset, push::Ruleset,
@ -405,7 +406,7 @@ impl KeyValueDatabase {
} }
// If the database has any data, perform data migrations before starting // 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 { if services().users.count()? > 0 {
// MIGRATIONS // MIGRATIONS
@ -801,73 +802,81 @@ impl KeyValueDatabase {
warn!("Migration: 10 -> 11 finished"); 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()) let mut account_data =
.unwrap(); serde_json::from_str::<PushRulesEvent>(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 let rule = rules_list.content.get(content_rule_transformation[0]);
.get( if rule.is_some() {
None, let mut rule = rule.unwrap().clone();
&user, rule.rule_id = content_rule_transformation[1].to_string();
GlobalAccountDataEventType::PushRules.to_string().into()) rules_list.content.remove(content_rule_transformation[0]);
.unwrap() rules_list.content.insert(rule);
.expect("Username is invalid"); }
}
let mut account_data = serde_json::from_str::<PushRulesEvent>(raw_rules_list.get()).unwrap(); //underride rules
let rules_list = &mut account_data.content.global; {
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 for transformation in underride_rule_transformation {
{ let rule = rules_list.underride.get(transformation[0]);
let content_rule_transformation = if rule.is_some() {
[".m.rules.contains_user_name", ".m.rule.contains_user_name"]; 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]); services().account_data.update(
if rule.is_some() { None,
let mut rule = rule.unwrap().clone(); &user,
rule.rule_id = content_rule_transformation[1].to_string(); GlobalAccountDataEventType::PushRules.to_string().into(),
rules_list.content.remove(content_rule_transformation[0]); &serde_json::to_value(account_data).expect("to json value always works"),
rules_list.content.insert(rule); )?;
} }
}
//underride rules services().globals.bump_database_version(12)?;
{
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 { warn!("Migration: 11 -> 12 finished");
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( assert_eq!(
None, services().globals.database_version().unwrap(),
&user, latest_database_version
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!( info!(
"Loaded {} database with version {}", "Loaded {} database with version {}",