2025-04-26 19:23:30 -04:00
|
|
|
pub mod last_fm;
|
2025-04-26 16:27:18 -04:00
|
|
|
pub mod spotify;
|
2025-05-05 19:38:01 -04:00
|
|
|
pub mod stripe;
|
2025-04-26 16:27:18 -04:00
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use axum::{extract::Path, response::IntoResponse, Extension, Json};
|
|
|
|
use axum_extra::extract::CookieJar;
|
|
|
|
use serde::Deserialize;
|
|
|
|
use tetratto_core::model::{
|
|
|
|
auth::{ConnectionService, ExternalConnectionData},
|
|
|
|
ApiReturn, Error,
|
|
|
|
};
|
|
|
|
use crate::{get_user_from_token, State};
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct UpdateConnectionInfo {
|
|
|
|
pub connection: ConnectionService,
|
|
|
|
pub data: HashMap<String, String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct UpdateConnectionState {
|
|
|
|
pub connection: ConnectionService,
|
|
|
|
pub data: ExternalConnectionData,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct UpdateConnectionShownOnProfile {
|
|
|
|
pub connection: ConnectionService,
|
|
|
|
pub shown: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn update_info_data_request(
|
|
|
|
jar: CookieJar,
|
|
|
|
Extension(data): Extension<State>,
|
|
|
|
Json(props): Json<UpdateConnectionInfo>,
|
|
|
|
) -> 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 mut con = match user.connections.get(&props.connection) {
|
|
|
|
Some(c) => c.to_owned(),
|
|
|
|
None => return Json(Error::NotAllowed.into()),
|
|
|
|
};
|
|
|
|
|
|
|
|
con.0.data = props.data;
|
|
|
|
user.connections.insert(props.connection, con);
|
|
|
|
|
|
|
|
if let Err(e) = data
|
|
|
|
.update_user_connections(user.id, user.connections)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
return Json(e.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
Json(ApiReturn {
|
|
|
|
ok: true,
|
|
|
|
message: "Updated connection".to_string(),
|
|
|
|
payload: (),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn update_state_request(
|
|
|
|
jar: CookieJar,
|
|
|
|
Extension(data): Extension<State>,
|
|
|
|
Json(props): Json<UpdateConnectionState>,
|
|
|
|
) -> 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 mut con = match user.connections.get(&props.connection) {
|
|
|
|
Some(c) => c.to_owned(),
|
|
|
|
None => return Json(Error::NotAllowed.into()),
|
|
|
|
};
|
|
|
|
|
|
|
|
con.1 = props.data;
|
|
|
|
user.connections.insert(props.connection, con);
|
|
|
|
|
|
|
|
if let Err(e) = data
|
|
|
|
.update_user_connections(user.id, user.connections)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
return Json(e.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
Json(ApiReturn {
|
|
|
|
ok: true,
|
|
|
|
message: "Updated connection".to_string(),
|
|
|
|
payload: (),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn update_shown_on_profile_request(
|
|
|
|
jar: CookieJar,
|
|
|
|
Extension(data): Extension<State>,
|
|
|
|
Json(props): Json<UpdateConnectionShownOnProfile>,
|
|
|
|
) -> 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 mut con = match user.connections.get(&props.connection) {
|
|
|
|
Some(c) => c.to_owned(),
|
|
|
|
None => return Json(Error::NotAllowed.into()),
|
|
|
|
};
|
|
|
|
|
|
|
|
con.0.show_on_profile = props.shown;
|
|
|
|
user.connections.insert(props.connection, con);
|
|
|
|
|
|
|
|
if let Err(e) = data
|
|
|
|
.update_user_connections(user.id, user.connections)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
return Json(e.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
Json(ApiReturn {
|
|
|
|
ok: true,
|
|
|
|
message: "Updated connection".to_string(),
|
|
|
|
payload: (),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn delete_request(
|
|
|
|
jar: CookieJar,
|
|
|
|
Extension(data): Extension<State>,
|
|
|
|
Path(service): Path<ConnectionService>,
|
|
|
|
) -> 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()),
|
|
|
|
};
|
|
|
|
|
|
|
|
user.connections.remove(&service);
|
|
|
|
|
|
|
|
if let Err(e) = data
|
|
|
|
.update_user_connections(user.id, user.connections)
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
return Json(e.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
Json(ApiReturn {
|
|
|
|
ok: true,
|
|
|
|
message: "Connection removed".to_string(),
|
|
|
|
payload: (),
|
|
|
|
})
|
|
|
|
}
|