add: profile connections, spotify connection
This commit is contained in:
parent
a5c2356940
commit
33ba576d4a
31 changed files with 931 additions and 19 deletions
156
crates/app/src/routes/api/v1/auth/connections/mod.rs
Normal file
156
crates/app/src/routes/api/v1/auth/connections/mod.rs
Normal file
|
@ -0,0 +1,156 @@
|
|||
pub mod spotify;
|
||||
|
||||
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: (),
|
||||
})
|
||||
}
|
42
crates/app/src/routes/api/v1/auth/connections/spotify.rs
Normal file
42
crates/app/src/routes/api/v1/auth/connections/spotify.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
use axum::{response::IntoResponse, Extension, Json};
|
||||
use axum_extra::extract::CookieJar;
|
||||
use tetratto_core::{
|
||||
database::connections::spotify::SpotifyConnection,
|
||||
model::{
|
||||
auth::{ConnectionService, ExternalConnectionData},
|
||||
ApiReturn, Error,
|
||||
},
|
||||
};
|
||||
use crate::{get_user_from_token, State};
|
||||
|
||||
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 = (
|
||||
SpotifyConnection::connection(),
|
||||
ExternalConnectionData::default(),
|
||||
);
|
||||
|
||||
user.connections
|
||||
.insert(ConnectionService::Spotify, 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),
|
||||
})
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
pub mod connections;
|
||||
pub mod images;
|
||||
pub mod ipbans;
|
||||
pub mod profile;
|
||||
|
|
|
@ -59,6 +59,20 @@ pub async fn redirect_from_ip(
|
|||
}
|
||||
}
|
||||
|
||||
pub async fn me_request(jar: CookieJar, Extension(data): Extension<State>) -> 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()),
|
||||
};
|
||||
|
||||
return Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "User exists".to_string(),
|
||||
payload: Some(user),
|
||||
});
|
||||
}
|
||||
|
||||
/// Update the settings of the given user.
|
||||
pub async fn update_user_settings_request(
|
||||
jar: CookieJar,
|
||||
|
|
|
@ -124,6 +124,7 @@ pub fn routes() -> Router {
|
|||
post(auth::images::upload_banner_request),
|
||||
)
|
||||
// profile
|
||||
.route("/auth/user/me", get(auth::profile::me_request))
|
||||
.route("/auth/user/{id}/avatar", get(auth::images::avatar_request))
|
||||
.route("/auth/user/{id}/banner", get(auth::images::banner_request))
|
||||
.route("/auth/user/{id}/follow", post(auth::social::follow_request))
|
||||
|
@ -232,6 +233,27 @@ pub fn routes() -> Router {
|
|||
delete(requests::delete_request),
|
||||
)
|
||||
.route("/requests/my", delete(requests::delete_all_request))
|
||||
// connections
|
||||
.route(
|
||||
"/auth/user/connections/_data",
|
||||
post(auth::connections::update_info_data_request),
|
||||
)
|
||||
.route(
|
||||
"/auth/user/connections/_state",
|
||||
post(auth::connections::update_state_request),
|
||||
)
|
||||
.route(
|
||||
"/auth/user/connections/_shown",
|
||||
post(auth::connections::update_shown_on_profile_request),
|
||||
)
|
||||
.route(
|
||||
"/auth/user/connections/{connection}",
|
||||
delete(auth::connections::delete_request),
|
||||
)
|
||||
.route(
|
||||
"/auth/user/connections/spotify",
|
||||
post(auth::connections::spotify::create_request),
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue