store lowercase twitch username

This commit is contained in:
Brandon Thomas committed 2022-01-18 22:42:05 -05:00
1 parent ab90af892f
commit 0cb8cbd802
3 files changed
+23 -9

No files matched your search

@@ -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)
@@ -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<String>,
scope_twitch_user_id: Option<u32>,
scope_twitch_username: Option<String>,
scope_twitch_username_lowercase: Option<String>,
}
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,
@@ -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(),