Endpoint for refreshing all users at once

Should maybe be gated by auth somehow?
There's no option to force here,
so we aren't at risk of being rate limited.
This commit is contained in:
Emilia Allison 2023-07-12 13:21:04 -04:00
parent 4ccd7981df
commit 4acb7301f1
Signed by: emilia
GPG Key ID: 7A3F8997BFE894E0
2 changed files with 26 additions and 1 deletions

View File

@ -11,7 +11,7 @@ mod joint_err;
mod refresh;
use spotify_auth::callback_manager;
use refresh::refresh_manager;
use refresh::{refresh_manager, refresh_all_manager};
use actix_web::{App, HttpServer, HttpResponse, get, Responder, web};
use dotenvy::dotenv;
@ -66,6 +66,7 @@ async fn main() -> std::io::Result<()> {
.service(succeeded)
.service(failed)
.service(refresh_manager)
.service(refresh_all_manager)
})
.bind((address, port))?
.run()

View File

@ -39,6 +39,30 @@ pub async fn refresh_manager(
}
}
#[allow(non_snake_case)]
#[get("/refresh_all")]
pub async fn refresh_all_manager(state: web::Data<crate::state::AppState>)
-> impl Responder {
let pool = &state.pool;
let users = sqlx::query!("
SELECT SpotifyId FROM Users
WHERE LastRefreshed < CURDATE() - INTERVAL 7 DAY
")
.fetch_all(pool)
.await;
if let Ok(users) = users {
for user in users {
if let Err(_) = refresh(&user.SpotifyId, pool).await {
log::error!("Tried to refresh for {}, failed", &user.SpotifyId);
}
}
return HttpResponse::Ok().body("yeah ok");
} else {
return HttpResponse::InternalServerError().body("where the users at")
}
}
async fn refresh(spotify_id: &str, pool: &MySqlPool) -> Result<(), JointErr> {
let token = spotify_auth::refresh_token_for_id(spotify_id, pool).await?;
let internal_id = sql_users::get_internal_id_by_id(spotify_id, pool).await?;