pub async fn find_or_create(
        conn: ConnectionPool,
        given_username: String,
        given_name: Option<String>,
        given_nick: Option<String>,
        given_email: Option<String>,
        given_external_profile_url: Option<reqwest::Url>,
        given_picture_url: Option<reqwest::Url>,
    ) -> Result<User> {
        use crate::schema::users::dsl::{
            email, external_profile_url, name, nick, picture_url, username, uuid,
        };

        tokio::task::spawn_blocking(move || {
            let conn = conn.get()?;

            let user: Option<User> = crate::schema::users::table
                .filter(username.eq(&given_username))
                .get_result(&conn)
                .optional()?;

            if let Some(user) = user {
                return Ok(user);
            }

            diesel::insert_into(users::table)
                .values((
                    username.eq(&given_username),
                    uuid.eq(SqlUuid::random()),
                    name.eq(&given_name),
                    nick.eq(&given_nick),
                    email.eq(&given_email),
                    external_profile_url.eq(given_external_profile_url.map(|v| v.to_string())),
                    picture_url.eq(given_picture_url.map(|v| v.to_string())),
                ))
                .execute(&conn)?;

            Ok(crate::schema::users::table
                .filter(username.eq(given_username))
                .get_result(&conn)?)
        })
        .await?
    }
/// Lookup the user in the database by username, or create the user if it doesn't yet
/// exist. The user will be created with no password so it cannot be logged into using
/// standard `password` auth, and must be logged into using OAuth.
Lookup the user in the database by username, or create the user if it doesn't yet
exist. The user will be created with no password so it cannot be logged into using
standard `password` auth, and must be logged into using OAuth.
Lookup the user in the database by username or create the user if it doesn ' t yet exist . The user will be created with no password so it cannot be logged into using standard ` password ` auth and must be logged into using OAuth .
pub async fn find_or_create(
        conn: ConnectionPool,
        given_username: String,
        given_name: Option<String>,
        given_nick: Option<String>,
        given_email: Option<String>,
        given_external_profile_url: Option<reqwest::Url>,
        given_picture_url: Option<reqwest::Url>,
    ) -> Result<User> {
        use crate::schema::users::dsl::{
            email, external_profile_url, name, nick, picture_url, username, uuid,
        };
        tokio::task::spawn_blocking(move || {
            let conn = conn.get()?;
            let user: Option<User> = crate::schema::users::table
                .filter(username.eq(&given_username))
                .get_result(&conn)
                .optional()?;
            if let Some(user) = user {
                return Ok(user);
            }
            diesel::insert_into(users::table)
                .values((
                    username.eq(&given_username),
                    uuid.eq(SqlUuid::random()),
                    name.eq(&given_name),
                    nick.eq(&given_nick),
                    email.eq(&given_email),
                    external_profile_url.eq(given_external_profile_url.map(|v| v.to_string())),
                    picture_url.eq(given_picture_url.map(|v| v.to_string())),
                ))
                .execute(&conn)?;
            Ok(crate::schema::users::table
                .filter(username.eq(given_username))
                .get_result(&conn)?)
        })
        .await?
    }
pub async fn find_or_create ( conn : ConnectionPool , given_username : String , given_name : Option < String > , given_nick : Option < String > , given_email : Option < String > , given_external_profile_url : Option < reqwest :: Url > , given_picture_url : Option < reqwest :: Url > , ) -> Result < User > { use crate :: schema :: users :: dsl :: { email , external_profile_url , name , nick , picture_url , username , uuid , } ; tokio :: task :: spawn_blocking ( move | | { let conn = conn . get ( ) ? ; let user : Option < User > = crate :: schema :: users :: table . filter ( username . eq ( & given_username ) ) . get_result ( & conn ) . optional ( ) ? ; if let Some ( user ) = user { return Ok ( user ) ; } diesel :: insert_into ( users :: table ) . values ( ( username . eq ( & given_username ) , uuid . eq ( SqlUuid :: random ( ) ) , name . eq ( & given_name ) , nick . eq ( & given_nick ) , email . eq ( & given_email ) , external_profile_url . eq ( given_external_profile_url . map ( | v | v . to_string ( ) ) ) , picture_url . eq ( given_picture_url . map ( | v | v . to_string ( ) ) ) , ) ) . execute ( & conn ) ? ; Ok ( crate :: schema :: users :: table . filter ( username . eq ( given_username ) ) . get_result ( & conn ) ? ) } ) . await ? }
Lookup the user in the database by username, or create the user if it doesn't yet
exist.
Lookup the user in the database by username or create the user if it doesn ' t yet exist .
conn
ConnectionPool
given_username
String
given_name
Option<String>
given_nick
Option<String>
given_email
Option<String>
given_external_profile_url
Option<reqwest::Url>
given_picture_url
Option<reqwest::Url>
conn
ConnectionPool
given_username
String
given_name
Option<String>
given_nick
Option<String>
given_email
Option<String>
given_external_profile_url
Option<reqwest::Url>
given_picture_url
Option<reqwest::Url>