add: questions, requests

This commit is contained in:
trisua 2025-04-12 22:25:54 -04:00
parent 24f67221ca
commit 7960484bf9
52 changed files with 1698 additions and 100 deletions

View file

@ -1,3 +1,4 @@
pub mod communities;
pub mod images;
pub mod posts;
pub mod questions;

View file

@ -19,25 +19,32 @@ pub async fn create_request(
None => return Json(Error::NotAllowed.into()),
};
match data
.create_post(Post::new(
req.content,
match req.community.parse::<usize>() {
Ok(x) => x,
let mut props = Post::new(
req.content,
match req.community.parse::<usize>() {
Ok(x) => x,
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
},
if let Some(rt) = req.replying_to {
match rt.parse::<usize>() {
Ok(x) => Some(x),
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
},
if let Some(rt) = req.replying_to {
match rt.parse::<usize>() {
Ok(x) => Some(x),
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
}
} else {
None
},
user.id,
))
.await
{
}
} else {
None
},
user.id,
);
if !req.answering.is_empty() {
// we're answering a question!
props.context.answering = match req.answering.parse::<usize>() {
Ok(x) => x,
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
};
}
match data.create_post(props).await {
Ok(id) => Json(ApiReturn {
ok: true,
message: "Post created".to_string(),

View file

@ -0,0 +1,65 @@
use axum::{Extension, Json, extract::Path, response::IntoResponse};
use axum_extra::extract::CookieJar;
use tetratto_core::model::{communities::Question, ApiReturn, Error};
use crate::{get_user_from_token, routes::api::v1::CreateQuestion, State};
pub async fn create_request(
jar: CookieJar,
Extension(data): Extension<State>,
Json(req): Json<CreateQuestion>,
) -> 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()),
};
let mut props = Question::new(
user.id,
match req.receiver.parse::<usize>() {
Ok(x) => x,
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
},
req.content,
req.is_global,
);
if !req.community.is_empty() {
props.is_global = true;
props.receiver = 0;
props.community = match req.community.parse::<usize>() {
Ok(x) => x,
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
}
}
match data.create_question(props).await {
Ok(id) => Json(ApiReturn {
ok: true,
message: "Question created".to_string(),
payload: Some(id.to_string()),
}),
Err(e) => Json(e.into()),
}
}
pub async fn delete_request(
jar: CookieJar,
Extension(data): Extension<State>,
Path(id): Path<usize>,
) -> 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()),
};
match data.delete_question(id, &user).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Question deleted".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),
}
}

View file

@ -3,6 +3,7 @@ pub mod communities;
pub mod notifications;
pub mod reactions;
pub mod reports;
pub mod requests;
pub mod util;
use axum::{
@ -93,6 +94,12 @@ pub fn routes() -> Router {
"/posts/{id}/context",
post(communities::posts::update_context_request),
)
// questions
.route("/questions", post(communities::questions::create_request))
.route(
"/questions/{id}",
delete(communities::questions::delete_request),
)
// auth
// global
.route("/auth/register", post(auth::register_request))
@ -201,6 +208,12 @@ pub fn routes() -> Router {
// reports
.route("/reports", post(reports::create_request))
.route("/reports/{id}", delete(reports::delete_request))
// requests
.route(
"/requests/{id}/{linked_asset}",
delete(requests::delete_request),
)
.route("/requests/my", delete(requests::delete_all_request))
}
#[derive(Deserialize)]
@ -255,6 +268,8 @@ pub struct CreatePost {
pub community: String,
#[serde(default)]
pub replying_to: Option<String>,
#[serde(default)]
pub answering: String,
}
#[derive(Deserialize)]
@ -337,3 +352,13 @@ pub struct DisableTotp {
pub struct CreateUserWarning {
pub content: String,
}
#[derive(Deserialize)]
pub struct CreateQuestion {
pub content: String,
pub is_global: bool,
#[serde(default)]
pub receiver: String,
#[serde(default)]
pub community: String,
}

View file

@ -0,0 +1,45 @@
use crate::{State, get_user_from_token};
use axum::{Extension, Json, extract::Path, response::IntoResponse};
use axum_extra::extract::CookieJar;
use tetratto_core::model::{ApiReturn, Error};
pub async fn delete_request(
jar: CookieJar,
Extension(data): Extension<State>,
Path((id, linked_asset)): Path<(usize, usize)>,
) -> 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()),
};
match data.delete_request(id, linked_asset, &user).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Request deleted".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),
}
}
pub async fn delete_all_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()),
};
match data.delete_all_requests(&user).await {
Ok(_) => Json(ApiReturn {
ok: true,
message: "Requests cleared".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),
}
}