add: post comments
add: user follow api, user block api
This commit is contained in:
parent
559ce19932
commit
8580e34be2
18 changed files with 296 additions and 49 deletions
|
@ -1,4 +1,5 @@
|
|||
pub mod images;
|
||||
pub mod social;
|
||||
|
||||
use super::AuthProps;
|
||||
use crate::{
|
||||
|
|
93
crates/app/src/routes/api/v1/auth/social.rs
Normal file
93
crates/app/src/routes/api/v1/auth/social.rs
Normal file
|
@ -0,0 +1,93 @@
|
|||
use crate::{
|
||||
State, get_user_from_token,
|
||||
model::{ApiReturn, Error},
|
||||
};
|
||||
use axum::{Extension, Json, extract::Path, response::IntoResponse};
|
||||
use axum_extra::extract::CookieJar;
|
||||
use tetratto_core::model::auth::{UserBlock, UserFollow};
|
||||
|
||||
/// Toggle following on the given user.
|
||||
pub async fn follow_request(
|
||||
jar: CookieJar,
|
||||
Path(id): Path<usize>,
|
||||
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()),
|
||||
};
|
||||
|
||||
if let Ok(userfollow) = data.get_userfollow_by_initiator_receiver(user.id, id).await {
|
||||
// delete
|
||||
match data.delete_userfollow(userfollow.id, user).await {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "User unfollowed".to_string(),
|
||||
payload: (),
|
||||
}),
|
||||
Err(e) => return Json(e.into()),
|
||||
}
|
||||
} else {
|
||||
// create
|
||||
match data.create_userfollow(UserFollow::new(user.id, id)).await {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "User followed".to_string(),
|
||||
payload: (),
|
||||
}),
|
||||
Err(e) => return Json(e.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Toggle blocking on the given user.
|
||||
pub async fn block_request(
|
||||
jar: CookieJar,
|
||||
Path(id): Path<usize>,
|
||||
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()),
|
||||
};
|
||||
|
||||
if let Ok(userblock) = data.get_userblock_by_initiator_receiver(user.id, id).await {
|
||||
// delete
|
||||
match data.delete_userblock(userblock.id, user).await {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "User unblocked".to_string(),
|
||||
payload: (),
|
||||
}),
|
||||
Err(e) => return Json(e.into()),
|
||||
}
|
||||
} else {
|
||||
// create
|
||||
match data.create_userblock(UserBlock::new(user.id, id)).await {
|
||||
Ok(_) => {
|
||||
if let Ok(userfollow) = data.get_userfollow_by_initiator_receiver(user.id, id).await
|
||||
{
|
||||
// automatically unfollow
|
||||
match data.delete_userfollow(userfollow.id, user).await {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "User unfollowed".to_string(),
|
||||
payload: (),
|
||||
}),
|
||||
Err(e) => return Json(e.into()),
|
||||
}
|
||||
} else {
|
||||
// not following user, don't do anything else
|
||||
Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "User blocked".to_string(),
|
||||
payload: (),
|
||||
})
|
||||
}
|
||||
}
|
||||
Err(e) => return Json(e.into()),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -19,7 +19,12 @@ pub async fn create_request(
|
|||
};
|
||||
|
||||
match data
|
||||
.create_entry(JournalPost::new(req.content, req.journal, user.id))
|
||||
.create_post(JournalPost::new(
|
||||
req.content,
|
||||
req.journal,
|
||||
req.replying_to,
|
||||
user.id,
|
||||
))
|
||||
.await
|
||||
{
|
||||
Ok(_) => Json(ApiReturn {
|
||||
|
@ -42,7 +47,7 @@ pub async fn delete_request(
|
|||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
match data.delete_entry(id, user).await {
|
||||
match data.delete_post(id, user).await {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Entry deleted".to_string(),
|
||||
|
|
|
@ -70,6 +70,14 @@ pub fn routes() -> Router {
|
|||
"/auth/profile/{id}/banner",
|
||||
get(auth::images::banner_request),
|
||||
)
|
||||
.route(
|
||||
"/auth/profile/{id}/follow",
|
||||
post(auth::social::follow_request),
|
||||
)
|
||||
.route(
|
||||
"/auth/profile/{id}/block",
|
||||
post(auth::social::block_request),
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
@ -108,6 +116,8 @@ pub struct UpdateJournalWriteAccess {
|
|||
pub struct CreateJournalEntry {
|
||||
pub content: String,
|
||||
pub journal: usize,
|
||||
#[serde(default)]
|
||||
pub replying_to: Option<usize>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue