add: last.fm status integration
This commit is contained in:
parent
3e2bdceb99
commit
0765156697
17 changed files with 397 additions and 3 deletions
111
crates/app/src/routes/api/v1/auth/connections/last_fm.rs
Normal file
111
crates/app/src/routes/api/v1/auth/connections/last_fm.rs
Normal file
|
@ -0,0 +1,111 @@
|
|||
use std::collections::HashMap;
|
||||
use axum::{response::IntoResponse, Extension, Json};
|
||||
use axum_extra::extract::CookieJar;
|
||||
use tetratto_core::{
|
||||
database::connections::last_fm::LastFmConnection,
|
||||
model::{
|
||||
auth::{ConnectionService, ExternalConnectionData},
|
||||
ApiReturn, Error,
|
||||
},
|
||||
};
|
||||
use crate::{get_user_from_token, State};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct LastFmApiProxy {
|
||||
pub method: String,
|
||||
pub data: HashMap<String, String>,
|
||||
}
|
||||
|
||||
pub async fn create_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let mut user = match get_user_from_token!(jar, data) {
|
||||
Some(ua) => ua,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
let con = (
|
||||
LastFmConnection::connection(),
|
||||
ExternalConnectionData::default(),
|
||||
);
|
||||
|
||||
user.connections
|
||||
.insert(ConnectionService::LastFm, con.clone());
|
||||
|
||||
if let Err(e) = data
|
||||
.update_user_connections(user.id, user.connections)
|
||||
.await
|
||||
{
|
||||
return Json(e.into());
|
||||
}
|
||||
|
||||
Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Connection created".to_string(),
|
||||
payload: Some(con.0.data),
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn proxy_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Json(mut req): Json<LastFmApiProxy>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let user = match get_user_from_token!(jar, data) {
|
||||
Some(ua) => ua,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
if let None = user.connections.get(&ConnectionService::LastFm) {
|
||||
// connection doesn't exist
|
||||
return Json(Error::GeneralNotFound("connection".to_string()).into());
|
||||
};
|
||||
|
||||
req.data.insert("method".to_string(), req.method);
|
||||
req.data.insert("format".to_string(), "json".to_string());
|
||||
req.data.insert(
|
||||
"api_key".to_string(),
|
||||
data.0.connections.last_fm_key.as_ref().unwrap().to_string(),
|
||||
);
|
||||
req.data.insert(
|
||||
"api_sig".to_string(),
|
||||
LastFmConnection::signature(
|
||||
req.data.clone(),
|
||||
data.0
|
||||
.connections
|
||||
.last_fm_secret
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.to_string(),
|
||||
),
|
||||
);
|
||||
|
||||
// build url string
|
||||
let mut out: String = String::new();
|
||||
|
||||
for (i, v) in req.data.iter().enumerate() {
|
||||
if i == 0 {
|
||||
out.push_str(&format!("?{}={}", v.0, v.1));
|
||||
} else {
|
||||
out.push_str(&format!("&{}={}", v.0, v.1));
|
||||
}
|
||||
}
|
||||
|
||||
// ...
|
||||
let res = reqwest::get(format!("https://ws.audioscrobbler.com/2.0/{out}"))
|
||||
.await
|
||||
.unwrap()
|
||||
.text()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
Json(ApiReturn {
|
||||
ok: true,
|
||||
message: String::new(),
|
||||
payload: res,
|
||||
})
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
pub mod last_fm;
|
||||
pub mod spotify;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
|
|
@ -254,6 +254,14 @@ pub fn routes() -> Router {
|
|||
"/auth/user/connections/spotify",
|
||||
post(auth::connections::spotify::create_request),
|
||||
)
|
||||
.route(
|
||||
"/auth/user/connections/last_fm",
|
||||
post(auth::connections::last_fm::create_request),
|
||||
)
|
||||
.route(
|
||||
"/auth/user/connections/last_fm/api_proxy",
|
||||
post(auth::connections::last_fm::proxy_request),
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue