Register application on Mastodon domain

O01eg
Jul 31, 2024, 11:47 AM
KDKRTAYJBTOXY75T2U5B236L3SEHC7RPCDNYD2H3JZ3ZNQD3NSEAC

Dependencies

  • [2] BUW254XB Update toml dependency
  • [3] WM64YAQL Fix warnings
  • [4] BCMU6UYK Start login mastodon form
  • [5] ZQIIC7C3 Add field to store timestamp of joining game
  • [6] MUTHALNP Detect user and domain in Mastodon fediverse
  • [7] G4JCZ5F7 Store try to register on Mastodon domain
  • [*] FUCFD4UV Add log in and log out support
  • [*] IXY6NZLM Start to use migrations files for PostgreSQL database
  • [*] 4MZ4VIR7 Initial commit
  • [*] BCXEUKX6 Add config, static files and web server
  • [*] FVYTV3D2 Add custom notes to new games feed

Change contents

  • edit in src/pages/log_in.rs at line 143
    [4.108]
    [4.108]
    async fn register_in_mastodon(
    domain: &str,
    reg_sess_id: &Uuid,
    data: &web::Data<WebData<'_>>,
    ) -> Result<(String, String), ()> {
    log::info!(
    "Mastodon domain {} registration session {} started",
    domain,
    reg_sess_id
    );
  • edit in src/pages/log_in.rs at line 155
    [4.109]
    [4.109]
    let client = awc::Client::default();
    let website = format!("{}://{}/", data.base_proto, data.base_domain);
    match client
    .post(format!("https://{}/api/v1/apps", domain))
    .content_type("application/x-www-form-urlencoded")
    .send_body(format!(
    "client_name=FreeOrion%20Test%20Web&redirect_uris={}&scopes=read:accounts&website={}",
    pct_str::PctString::encode(website.chars(), pct_str::URIReserved).as_str(),
    pct_str::PctString::encode(website.chars(), pct_str::URIReserved).as_str()
    ))
    .await
    {
    Ok(mut resp) => match resp.json::<serde_json::Value>().await {
    Ok(serde_json::Value::Object(resp_value_obj)) => {
    let client_id_value = resp_value_obj.get("client_id");
    let client_secret_value = resp_value_obj.get("client_secret");
    if let (
    Some(serde_json::Value::String(client_id)),
    Some(serde_json::Value::String(client_secret)),
    ) = (client_id_value, client_secret_value)
    {
    Ok((client_id.clone(), client_secret.clone()))
    } else {
    log::error!(
    "Mastodon domain {} registration session {} missing fields JSON {:?}",
    domain,
    reg_sess_id,
    resp_value_obj
    );
    Err(())
    }
    }
    Ok(resp_value) => {
    log::error!(
    "Mastodon domain {} registration session {} non-object JSON {:?}",
    domain,
    reg_sess_id,
    resp_value
    );
    Err(())
    }
    Err(err) => {
    log::error!(
    "Mastodon domain {} registration session {} failed JSON {:?}",
    domain,
    reg_sess_id,
    err
    );
    Err(())
    }
    },
    Err(err) => {
    log::error!(
    "Mastodon domain {} registration session {} failed {:?}",
    domain,
    reg_sess_id,
    err
    );
    Err(())
    }
    }
    }
  • replacement in src/pages/log_in.rs at line 287
    [4.384][4.384:465]()
    "INSERT INTO auth.mastodon_apps (domain, refresh_ts) VALUES ($1, $2)
    [4.384]
    [4.465]
    "INSERT INTO auth.mastodon_apps (domain, refresh_ts, reg_sess_id) VALUES ($1, $2, $3)
  • replacement in src/pages/log_in.rs at line 289
    [4.528][4.528:578]()
    RETURNING client_id, client_secret;",
    [4.528]
    [4.578]
    RETURNING client_id, client_secret, reg_sess_id;",
  • replacement in src/pages/log_in.rs at line 300
    [4.865][4.865:940]()
    let rows = match dbclient_rw.query_opt(&stmt, &[&domain, &ts]).await {
    [4.865]
    [4.940]
    let reg_sess_id = Uuid::new_v4();
    log::info!(
    "Mastodon domain {} registration session {} created",
    domain,
    reg_sess_id
    );
    let rows = match dbclient_rw
    .query_opt(&stmt, &[&domain, &ts, &reg_sess_id])
    .await
    {
  • edit in src/pages/log_in.rs at line 323
    [4.1373]
    [4.1725]
    let opt_client_id = row.get::<_, Option<String>>(0);
    let opt_client_secret = row.get::<_, Option<String>>(1);
    let old_reg_sess_id = row.get::<_, uuid::Uuid>(2);
  • replacement in src/pages/log_in.rs at line 328
    [4.1726][3.526:642]()
    let _opt_client_id = row.get::<_, Option<&str>>(0);
    let _opt_client_secret = row.get::<_, Option<&str>>(1);
    [4.1726]
    [4.1488]
    let (client_id, _client_secret) =
    if let (Some(client_id), Some(client_secret)) = (opt_client_id, opt_client_secret) {
    (client_id, client_secret)
    } else {
    if reg_sess_id != old_reg_sess_id {
    // Some one else registering same domain, try again later
    log::warn!(
    "Mastodon domain {} registration session {} conflict with {}",
    domain,
    reg_sess_id,
    old_reg_sess_id
    );
    return HttpResponse::Found()
    .append_header((header::LOCATION, "index.html"))
    .finish();
    }
    // Try to register on Mastodon domain
    match register_in_mastodon(&domain, &reg_sess_id, &data).await {
    Ok(res) => res,
    Err(_) => {
    return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());
    }
    }
    };
  • replacement in src/pages/log_in.rs at line 354
    [4.1489][4.1489:1531]()
    // Try to register on Mastodon domain
    [4.1489]
    [4.1531]
    log::info!(
    "Mastodon domain {} registration session {} completed with client_id {}",
    domain,
    reg_sess_id,
    client_id
    );
  • file addition: 20240731_0_mastodon-apps-uuid.sql (----------)
    [10.15]
    -- Create field to store registartion session on Mastodon
    DO $$
    BEGIN
    IF NOT EXISTS (
    SELECT 1 FROM pg_class c
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    INNER JOIN pg_attribute a ON a.attrelid = c.oid
    WHERE c.relname = 'mastodon_apps' AND n.nspname = 'auth' AND a.attname = 'reg_sess_id'
    ) THEN
    ALTER TABLE auth.mastodon_apps ADD COLUMN reg_sess_id UUID NOT NULL;
    END IF;
    END
    $$;
  • edit in Cargo.toml at line 15
    [12.8410]
    [2.0]
    serde_json = "1.0"
  • edit in Cargo.lock at line 859
    [12.20545]
    [13.1353]
    "serde_json",