From 246e38a45b7ad110a303a79449d3b3466d3ce170 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Fri, 15 Dec 2023 05:34:17 -0500 Subject: [PATCH] Making login case-insensitive. Fixes #4272 (#4273) * Making login case-insensitive. Fixes #4272 * Removing redundant lowercase. --- crates/db_schema/src/impls/instance.rs | 11 +++++++---- crates/db_schema/src/impls/post.rs | 2 +- crates/db_views/src/local_user_view.rs | 17 +++++++++++++---- crates/db_views/src/post_view.rs | 16 ++++++++++++---- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/crates/db_schema/src/impls/instance.rs b/crates/db_schema/src/impls/instance.rs index 7da4ce354..7d63c9ffa 100644 --- a/crates/db_schema/src/impls/instance.rs +++ b/crates/db_schema/src/impls/instance.rs @@ -13,12 +13,17 @@ use crate::{ federation_queue_state::FederationQueueState, instance::{Instance, InstanceForm}, }, - utils::{functions::lower, get_conn, naive_now, now, DbPool}, + utils::{ + functions::{coalesce, lower}, + get_conn, + naive_now, + now, + DbPool, + }, }; use diesel::{ dsl::{count_star, insert_into}, result::Error, - sql_types::{Nullable, Timestamptz}, ExpressionMethods, NullableExpressionMethods, QueryDsl, @@ -157,5 +162,3 @@ impl Instance { .await } } - -sql_function! { fn coalesce(x: Nullable, y: Timestamptz) -> Timestamptz; } diff --git a/crates/db_schema/src/impls/post.rs b/crates/db_schema/src/impls/post.rs index d65fd2398..4f2f88cb2 100644 --- a/crates/db_schema/src/impls/post.rs +++ b/crates/db_schema/src/impls/post.rs @@ -1,4 +1,3 @@ -use super::instance::coalesce; use crate::{ newtypes::{CommunityId, DbUrl, PersonId, PostId}, schema::post::dsl::{ @@ -30,6 +29,7 @@ use crate::{ }, traits::{Crud, Likeable, Saveable}, utils::{ + functions::coalesce, get_conn, naive_now, DbPool, diff --git a/crates/db_views/src/local_user_view.rs b/crates/db_views/src/local_user_view.rs index dd0e3631b..cb9ab86c4 100644 --- a/crates/db_views/src/local_user_view.rs +++ b/crates/db_views/src/local_user_view.rs @@ -5,7 +5,14 @@ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ newtypes::{LocalUserId, PersonId}, schema::{local_user, person, person_aggregates}, - utils::{functions::lower, DbConn, DbPool, ListFn, Queries, ReadFn}, + utils::{ + functions::{coalesce, lower}, + DbConn, + DbPool, + ListFn, + Queries, + ReadFn, + }, }; use lemmy_utils::error::{LemmyError, LemmyErrorType}; use std::future::{ready, Ready}; @@ -34,7 +41,9 @@ fn queries<'a>( let mut query = local_user::table.into_boxed(); query = match search { ReadBy::Id(local_user_id) => query.filter(local_user::id.eq(local_user_id)), - ReadBy::Email(from_email) => query.filter(local_user::email.eq(from_email)), + ReadBy::Email(from_email) => { + query.filter(lower(coalesce(local_user::email, "")).eq(from_email.to_lowercase())) + } _ => query, }; let mut query = query.inner_join(person::table); @@ -43,8 +52,8 @@ fn queries<'a>( ReadBy::Name(name) => query.filter(lower(person::name).eq(name.to_lowercase())), ReadBy::NameOrEmail(name_or_email) => query.filter( lower(person::name) - .eq(lower(name_or_email)) - .or(local_user::email.eq(name_or_email)), + .eq(lower(name_or_email.to_lowercase())) + .or(lower(coalesce(local_user::email, "")).eq(name_or_email.to_lowercase())), ), _ => query, }; diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index 1d15c7c41..745e32c18 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -4,7 +4,6 @@ use diesel::{ dsl::{exists, not, IntervalDsl}, pg::Pg, result::Error, - sql_function, sql_types, BoolExpressionMethods, BoxableExpression, @@ -38,14 +37,23 @@ use lemmy_db_schema::{ post_read, post_saved, }, - utils::{fuzzy_search, get_conn, limit_and_offset, now, DbConn, DbPool, ListFn, Queries, ReadFn}, + utils::{ + functions::coalesce, + fuzzy_search, + get_conn, + limit_and_offset, + now, + DbConn, + DbPool, + ListFn, + Queries, + ReadFn, + }, ListingType, SortType, }; use tracing::debug; -sql_function!(fn coalesce(x: sql_types::Nullable, y: sql_types::BigInt) -> sql_types::BigInt); - #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum Ord { Desc,