From 64b3be68a03499342df3ceb3a97b825684ef2ab0 Mon Sep 17 00:00:00 2001 From: kpcyrd Date: Sun, 11 Aug 2019 10:17:38 +0200 Subject: [PATCH] Add misc fields --- docs/structs.rst | 8 +++ .../2019-08-11-073709_misc-fields/down.sql | 41 +++++++++++++++ .../2019-08-11-073709_misc-fields/up.sql | 4 ++ src/cmd/add_cmd.rs | 4 ++ src/models/account.rs | 50 ++++++++++++++++--- src/models/network.rs | 14 +++++- src/schema.rs | 4 ++ 7 files changed, 117 insertions(+), 8 deletions(-) create mode 100644 migrations/2019-08-11-073709_misc-fields/down.sql create mode 100644 migrations/2019-08-11-073709_misc-fields/up.sql diff --git a/docs/structs.rst b/docs/structs.rst index 88b2f0f..ce05abf 100644 --- a/docs/structs.rst +++ b/docs/structs.rst @@ -145,6 +145,8 @@ connected to. Latitude of the networks location. ``longitude`` Longitude of the networks location. +``description`` + A human readable description in case the value is a technical identifier. Accounts -------- @@ -166,6 +168,12 @@ A users account or profile on a webservice, like github or instagram. The url of the public profile if available. ``last_seen`` The last time this account has been active/online. +``birthday`` + The users birthday set on the account. +``phonenumber`` + The phonenumber associated with the account. +``profile_pic`` + The blob identifier of the users current profile picture. Breaches -------- diff --git a/migrations/2019-08-11-073709_misc-fields/down.sql b/migrations/2019-08-11-073709_misc-fields/down.sql new file mode 100644 index 0000000..9c6c052 --- /dev/null +++ b/migrations/2019-08-11-073709_misc-fields/down.sql @@ -0,0 +1,41 @@ +PRAGMA foreign_keys=off; + +-- accounts +CREATE TABLE _accounts_new ( + id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + value VARCHAR NOT NULL, + service VARCHAR NOT NULL, + username VARCHAR NOT NULL, + displayname VARCHAR, + email VARCHAR, + url VARCHAR, + last_seen DATETIME, + unscoped BOOLEAN DEFAULT 0 NOT NULL, + CONSTRAINT account_unique UNIQUE (value) +); + +INSERT INTO _accounts_new (id, value, service, username, displayname, email, url, last_seen, unscoped) + SELECT id, value, service, username, displayname, email, url, last_seen, unscoped + FROM accounts; + +DROP TABLE accounts; +ALTER TABLE _accounts_new RENAME TO accounts; + +-- networks +CREATE TABLE _networks_new ( + id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + value VARCHAR NOT NULL, + unscoped BOOLEAN DEFAULT 0 NOT NULL, + latitude FLOAT, + longitude FLOAT, + CONSTRAINT network_unique UNIQUE (value) +); + +INSERT INTO _networks_new (id, value, unscoped, latitude, longitude) + SELECT id, value, unscoped, latitude, longitude + FROM networks; + +DROP TABLE networks; +ALTER TABLE _networks_new RENAME TO networks; + +PRAGMA foreign_keys=on; diff --git a/migrations/2019-08-11-073709_misc-fields/up.sql b/migrations/2019-08-11-073709_misc-fields/up.sql new file mode 100644 index 0000000..7f463a9 --- /dev/null +++ b/migrations/2019-08-11-073709_misc-fields/up.sql @@ -0,0 +1,4 @@ +ALTER TABLE accounts ADD COLUMN phonenumber VARCHAR; +ALTER TABLE accounts ADD COLUMN profile_pic VARCHAR; +ALTER TABLE accounts ADD COLUMN birthday VARCHAR; +ALTER TABLE networks ADD COLUMN description VARCHAR; diff --git a/src/cmd/add_cmd.rs b/src/cmd/add_cmd.rs index f45dc0a..6a945e2 100644 --- a/src/cmd/add_cmd.rs +++ b/src/cmd/add_cmd.rs @@ -304,6 +304,7 @@ impl IntoInsert for AddNetwork { value: network, latitude, longitude, + description: None, unscoped: false, })) } @@ -342,6 +343,9 @@ impl IntoInsert for AddAccount { email: None, url: None, last_seen: None, + birthday: None, + phonenumber: None, + profile_pic: None, unscoped: false, })) } diff --git a/src/models/account.rs b/src/models/account.rs index ac02b5b..698bfd0 100644 --- a/src/models/account.rs +++ b/src/models/account.rs @@ -20,6 +20,9 @@ pub struct Account { pub url: Option, pub last_seen: Option, pub unscoped: bool, + pub birthday: Option, + pub phonenumber: Option, + pub profile_pic: Option, } impl Model for Account { @@ -84,29 +87,29 @@ impl Model for Account { fn by_id(db: &Database, my_id: i32) -> Result { use crate::schema::accounts::dsl::*; - let domain = accounts.filter(id.eq(my_id)) + let account = accounts.filter(id.eq(my_id)) .first::(db.db())?; - Ok(domain) + Ok(account) } fn get(db: &Database, query: &Self::ID) -> Result { use crate::schema::accounts::dsl::*; - let domain = accounts.filter(value.eq(query)) + let account = accounts.filter(value.eq(query)) .first::(db.db())?; - Ok(domain) + Ok(account) } fn get_opt(db: &Database, query: &Self::ID) -> Result> { use crate::schema::accounts::dsl::*; - let domain = accounts.filter(value.eq(query)) + let account = accounts.filter(value.eq(query)) .first::(db.db()) .optional()?; - Ok(domain) + Ok(account) } } @@ -160,6 +163,9 @@ pub struct DetailedAccount { url: Option, last_seen: Option, unscoped: bool, + birthday: Option, + phonenumber: Option, + profile_pic: Option, } impl DisplayableDetailed for DetailedAccount { @@ -178,6 +184,9 @@ impl DisplayableDetailed for DetailedAccount { w.opt_debug::(&self.email)?; w.opt_debug::(&self.url)?; w.opt_debug::(&self.last_seen)?; + w.opt_debug::(&self.birthday)?; + w.opt_debug::(&self.phonenumber)?; + w.opt_debug::(&self.profile_pic)?; w.end_group()?; Ok(()) @@ -203,6 +212,9 @@ impl Detailed for Account { url: self.url.clone(), last_seen: self.last_seen.clone(), unscoped: self.unscoped, + birthday: self.birthday.clone(), + phonenumber: self.phonenumber.clone(), + profile_pic: self.profile_pic.clone(), }) } } @@ -218,6 +230,9 @@ pub struct NewAccount { pub url: Option, pub last_seen: Option, pub unscoped: bool, + pub birthday: Option, + pub phonenumber: Option, + pub profile_pic: Option, } impl InsertableStruct for NewAccount { @@ -247,6 +262,9 @@ impl Upsertable for NewAccount { email: Self::upsert_opt(self.email, &existing.email), url: Self::upsert_opt(self.url, &existing.url), last_seen: Self::upsert_opt(self.last_seen, &existing.last_seen), + birthday: Self::upsert_opt(self.birthday, &existing.birthday), + phonenumber: Self::upsert_opt(self.phonenumber, &existing.phonenumber), + profile_pic: Self::upsert_opt(self.profile_pic, &existing.profile_pic), } } } @@ -267,6 +285,9 @@ pub struct InsertAccount { pub email: Option, pub url: Option, pub last_seen: Option, + pub birthday: Option, + pub phonenumber: Option, + pub profile_pic: Option, } impl LuaInsertToNew for InsertAccount { @@ -285,6 +306,9 @@ impl LuaInsertToNew for InsertAccount { email: self.email, url: self.url, last_seen: self.last_seen, + birthday: self.birthday, + phonenumber: self.phonenumber, + profile_pic: self.profile_pic, unscoped: false, }) } @@ -298,6 +322,9 @@ pub struct AccountUpdate { pub email: Option, pub url: Option, pub last_seen: Option, + pub birthday: Option, + pub phonenumber: Option, + pub profile_pic: Option, } impl Upsert for AccountUpdate { @@ -305,7 +332,10 @@ impl Upsert for AccountUpdate { self.displayname.is_some() || self.email.is_some() || self.url.is_some() || - self.last_seen.is_some() + self.last_seen.is_some() || + self.birthday.is_some() || + self.phonenumber.is_some() || + self.profile_pic.is_some() } fn generic(self) -> Update { @@ -323,6 +353,9 @@ impl Updateable for AccountUpdate { Self::clear_if_equal(&mut self.email, &existing.email); Self::clear_if_equal(&mut self.url, &existing.url); Self::clear_if_equal(&mut self.last_seen, &existing.last_seen); + Self::clear_if_equal(&mut self.birthday, &existing.birthday); + Self::clear_if_equal(&mut self.phonenumber, &existing.phonenumber); + Self::clear_if_equal(&mut self.profile_pic, &existing.profile_pic); } fn fmt(&self, updates: &mut Vec) { @@ -330,5 +363,8 @@ impl Updateable for AccountUpdate { Self::push_value(updates, "email", &self.email); Self::push_value(updates, "url", &self.url); Self::push_value(updates, "last_seen", &self.last_seen); + Self::push_value(updates, "birthday", &self.birthday); + Self::push_value(updates, "phonenumber", &self.phonenumber); + Self::push_value(updates, "profile_pic", &self.profile_pic); } } diff --git a/src/models/network.rs b/src/models/network.rs index b39b26a..59f85c8 100644 --- a/src/models/network.rs +++ b/src/models/network.rs @@ -15,6 +15,7 @@ pub struct Network { pub unscoped: bool, pub latitude: Option, pub longitude: Option, + pub description: Option, } impl Model for Network { @@ -153,6 +154,7 @@ pub struct DetailedNetwork { unscoped: bool, latitude: Option, longitude: Option, + description: Option, devices: Vec, } @@ -170,6 +172,7 @@ impl DisplayableDetailed for DetailedNetwork { w.start_group(); w.opt_debug::(&self.latitude)?; w.opt_debug::(&self.longitude)?; + w.opt_debug::(&self.description)?; w.end_group()?; Ok(()) @@ -200,6 +203,7 @@ impl Detailed for Network { unscoped: self.unscoped, latitude: self.latitude.clone(), longitude: self.longitude.clone(), + description: self.description.clone(), devices, }) } @@ -211,6 +215,7 @@ pub struct NewNetwork { pub value: String, pub latitude: Option, pub longitude: Option, + pub description: Option, pub unscoped: bool, } @@ -239,6 +244,7 @@ impl Upsertable for NewNetwork { id: existing.id, latitude: Self::upsert_opt(self.latitude, &existing.latitude), longitude: Self::upsert_opt(self.longitude, &existing.longitude), + description: Self::upsert_opt(self.description, &existing.description), } } } @@ -256,6 +262,7 @@ pub struct InsertNetwork { pub value: String, pub latitude: Option, pub longitude: Option, + pub description: Option, } impl LuaInsertToNew for InsertNetwork { @@ -266,6 +273,7 @@ impl LuaInsertToNew for InsertNetwork { value: self.value, latitude: self.latitude, longitude: self.longitude, + description: self.description, unscoped: false, }) @@ -278,12 +286,14 @@ pub struct NetworkUpdate { pub id: i32, pub latitude: Option, pub longitude: Option, + pub description: Option, } impl Upsert for NetworkUpdate { fn is_dirty(&self) -> bool { self.latitude.is_some() || - self.longitude.is_some() + self.longitude.is_some() || + self.description.is_some() } fn generic(self) -> Update { @@ -299,10 +309,12 @@ impl Updateable for NetworkUpdate { fn changeset(&mut self, existing: &Network) { Self::clear_if_equal(&mut self.latitude, &existing.latitude); Self::clear_if_equal(&mut self.longitude, &existing.longitude); + Self::clear_if_equal(&mut self.description, &existing.description); } fn fmt(&self, updates: &mut Vec) { Self::push_value(updates, "latitude", &self.latitude); Self::push_value(updates, "longitude", &self.longitude); + Self::push_value(updates, "description", &self.description); } } diff --git a/src/schema.rs b/src/schema.rs index e1cfce3..e483d01 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -9,6 +9,9 @@ table! { url -> Nullable, last_seen -> Nullable, unscoped -> Bool, + phonenumber -> Nullable, + profile_pic -> Nullable, + birthday -> Nullable, } } @@ -136,6 +139,7 @@ table! { unscoped -> Bool, latitude -> Nullable, longitude -> Nullable, + description -> Nullable, } }