From 0cb8cbd80205ed4e4f96f97181df0021d3f1e15f Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Tue, 18 Jan 2022 22:42:05 -0500 Subject: [PATCH] store lowercase twitch username --- .../up.sql | 8 ++++++-- .../database_queries/src/twitch_oauth/find.rs | 20 ++++++++++++------- .../src/twitch_oauth/insert.rs | 4 ++++ 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/_migrations/2022-01-11-042235_twitch_oauth_tokens/up.sql b/_migrations/2022-01-11-042235_twitch_oauth_tokens/up.sql index d9f3497fb5..ac837ff3a3 100644 --- a/_migrations/2022-01-11-042235_twitch_oauth_tokens/up.sql +++ b/_migrations/2022-01-11-042235_twitch_oauth_tokens/up.sql @@ -26,10 +26,13 @@ CREATE TABLE twitch_oauth_tokens( -- https://discuss.dev.twitch.tv/t/bug-v5-api-returns--id-as-string-for-featured-channels/10310 twitch_user_id VARCHAR(64) NOT NULL, - -- Usernames are between 4 and 25 characters. + -- Twitch usernames are between 4 and 25 characters. -- This is returned in the oauth flow twitch_username VARCHAR(32) NOT NULL, + -- Twitch username, but lowercase for lookup. + twitch_username_lowercase VARCHAR(32) NOT NULL, + -- ========== OAUTH TOKEN DETAILS ========== -- The ever important OAuth access token. @@ -103,7 +106,8 @@ CREATE TABLE twitch_oauth_tokens( PRIMARY KEY (id), KEY fk_maybe_user_token (maybe_user_token), KEY index_twitch_user_id (twitch_user_id), - KEY index_twitch_username (twitch_username), + -- KEY index_twitch_username (twitch_username), + KEY index_twitch_username_lowercase (twitch_username_lowercase), KEY index_expires_at (expires_at), KEY index_user_deleted_at (user_deleted_at), KEY index_mod_deleted_at (mod_deleted_at) diff --git a/crates/lib/database_queries/src/twitch_oauth/find.rs b/crates/lib/database_queries/src/twitch_oauth/find.rs index df2c45e88b..dce52a05e6 100644 --- a/crates/lib/database_queries/src/twitch_oauth/find.rs +++ b/crates/lib/database_queries/src/twitch_oauth/find.rs @@ -18,6 +18,7 @@ pub struct TwitchOauthTokenRecord { /// Twitch user pub twitch_user_id: String, pub twitch_username: String, + pub twitch_username_lowercase: String, /// Token details pub access_token: String, @@ -43,7 +44,7 @@ pub struct TwitchOauthTokenFinder { scope_user_token: Option, scope_twitch_user_id: Option, - scope_twitch_username: Option, + scope_twitch_username_lowercase: Option, } impl TwitchOauthTokenFinder { @@ -51,7 +52,7 @@ impl TwitchOauthTokenFinder { Self { scope_user_token: None, scope_twitch_user_id: None, - scope_twitch_username: None + scope_twitch_username_lowercase: None } } @@ -65,8 +66,10 @@ impl TwitchOauthTokenFinder { self } + /// This will automatically look up against a lowercased username pub fn scope_twitch_username(mut self, twitch_username: Option<&str>) -> Self { - self.scope_twitch_username = twitch_username.map(|t| t.to_string()); + self.scope_twitch_username_lowercase = twitch_username + .map(|t| t.to_string().to_lowercase()); self } @@ -82,6 +85,7 @@ impl TwitchOauthTokenFinder { maybe_user_gravatar_hash: record.maybe_user_gravatar_hash.clone(), twitch_user_id: record.twitch_user_id.clone(), twitch_username: record.twitch_username.clone(), + twitch_username_lowercase: record.twitch_username_lowercase.clone(), access_token: record.access_token.clone(), maybe_refresh_token: record.maybe_refresh_token.clone(), token_type: record.token_type.clone(), @@ -117,7 +121,7 @@ impl TwitchOauthTokenFinder { query = query.bind(user_id); } - if let Some(twitch_username) = self.scope_twitch_username.as_deref() { + if let Some(twitch_username) = self.scope_twitch_username_lowercase.as_deref() { query = query.bind(twitch_username); } @@ -148,6 +152,7 @@ SELECT twitch_oauth_tokens.twitch_user_id, twitch_oauth_tokens.twitch_username, + twitch_oauth_tokens.twitch_username_lowercase, twitch_oauth_tokens.access_token, twitch_oauth_tokens.maybe_refresh_token, @@ -197,12 +202,12 @@ LEFT OUTER JOIN users } } - if let Some(username) = self.scope_twitch_username.as_deref() { + if let Some(username) = self.scope_twitch_username_lowercase.as_deref() { if !first_predicate_added { - query.push_str(" WHERE twitch_oauth_tokens.twitch_username = ?"); + query.push_str(" WHERE twitch_oauth_tokens.twitch_username_lowercase = ?"); first_predicate_added = true; } else { - query.push_str(" AND twitch_oauth_tokens.twitch_username = ?"); + query.push_str(" AND twitch_oauth_tokens.twitch_username_lowercase = ?"); } } @@ -234,6 +239,7 @@ pub struct TwitchOauthTokenRecordInternal { /// Twitch user pub twitch_user_id: String, pub twitch_username: String, + pub twitch_username_lowercase: String, /// Token details pub access_token: String, diff --git a/crates/lib/database_queries/src/twitch_oauth/insert.rs b/crates/lib/database_queries/src/twitch_oauth/insert.rs index 70ca12d97a..85340cc875 100644 --- a/crates/lib/database_queries/src/twitch_oauth/insert.rs +++ b/crates/lib/database_queries/src/twitch_oauth/insert.rs @@ -115,6 +115,7 @@ SET maybe_user_token = ?, twitch_user_id = ?, twitch_username = ?, + twitch_username_lowercase = ?, access_token = ?, maybe_refresh_token = ?, token_type = ?, @@ -129,6 +130,7 @@ SET self.maybe_user_token.clone(), self.twitch_user_id.clone(), self.twitch_username.clone(), + self.twitch_username.clone().to_lowercase(), self.access_token.clone(), self.maybe_refresh_token.clone(), self.token_type.clone(), @@ -148,6 +150,7 @@ SET maybe_user_token = ?, twitch_user_id = ?, twitch_username = ?, + twitch_username_lowercase = ?, access_token = ?, maybe_refresh_token = ?, token_type = ?, @@ -162,6 +165,7 @@ SET self.maybe_user_token.clone(), self.twitch_user_id.clone(), self.twitch_username.clone(), + self.twitch_username.clone().to_lowercase(), self.access_token.clone(), self.maybe_refresh_token.clone(), self.token_type.clone(),