From 36cc8b039338442512f0e86d9487d951b5f2c6e3 Mon Sep 17 00:00:00 2001 From: Samuel Hand Date: Fri, 10 Aug 2018 00:45:22 +0100 Subject: Code cleanup - move long database queries to their own functions and improve variable names --- plugins/omemo/src/database.vala | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) (limited to 'plugins/omemo/src/database.vala') diff --git a/plugins/omemo/src/database.vala b/plugins/omemo/src/database.vala index 05864e38..8f8cb44a 100644 --- a/plugins/omemo/src/database.vala +++ b/plugins/omemo/src/database.vala @@ -64,6 +64,34 @@ public class Database : Qlite.Database { .value(this.identity_key_public_base64, Base64.encode(bundle.identity_key.serialize())) .value(this.trust_level, trust).perform(); } + + public QueryBuilder get_trusted_devices(int identity_id, string address_name) { + return this.with_address(identity_id, address_name) + .with(this.trust_level, "!=", TrustLevel.UNTRUSTED) + .with(this.now_active, "=", true); + } + + public QueryBuilder get_known_devices(int identity_id, string address_name) { + return this.with_address(identity_id, address_name) + .with(this.trust_level, "!=", TrustLevel.UNKNOWN) + .without_null(this.identity_key_public_base64); + } + + public QueryBuilder get_unknown_devices(int identity_id, string address_name) { + return this.with_address(identity_id, address_name) + .with_null(this.identity_key_public_base64); + } + + public QueryBuilder get_new_devices(int identity_id, string address_name) { + return this.with_address(identity_id, address_name) + .with(this.trust_level, "=", TrustLevel.UNKNOWN) + .without_null(this.identity_key_public_base64); + } + + public Row? get_device(int identity_id, string address_name, int device_id) { + return this.with_address(identity_id, address_name) + .with(this.device_id, "=", device_id).single().row().inner; + } } @@ -160,10 +188,12 @@ public class Database : Qlite.Database { } public override void migrate(long oldVersion) { - exec("DROP INDEX identity_meta_idx"); - exec("DROP INDEX identity_meta_list_idx"); - exec("CREATE UNIQUE INDEX identity_meta_idx ON identity_meta (identity_id, address_name, device_id)"); - exec("CREATE INDEX identity_meta_list_idx ON identity_meta (identity_id, address_name)"); + if(oldVersion == 1) { + exec("DROP INDEX identity_meta_idx"); + exec("DROP INDEX identity_meta_list_idx"); + exec("CREATE UNIQUE INDEX identity_meta_idx ON identity_meta (identity_id, address_name, device_id)"); + exec("CREATE INDEX identity_meta_list_idx ON identity_meta (identity_id, address_name)"); + } } } -- cgit v1.2.3-54-g00ecf