add: polls backend

TODO: polls frontend
This commit is contained in:
trisua 2025-06-03 23:35:34 -04:00
parent b5e060e8ae
commit 4dfa09207e
18 changed files with 574 additions and 17 deletions

View file

@ -7,7 +7,7 @@ use axum::{
use axum_extra::extract::CookieJar;
use tetratto_core::model::{
addr::RemoteAddr,
communities::Post,
communities::{Poll, PollVote, Post},
permissions::FinePermission,
uploads::{MediaType, MediaUpload},
ApiReturn, Error,
@ -15,7 +15,7 @@ use tetratto_core::model::{
use crate::{
get_user_from_token,
image::{save_webp_buffer, JsonMultipart},
routes::api::v1::{CreatePost, CreateRepost, UpdatePostContent, UpdatePostContext},
routes::api::v1::{CreatePost, CreateRepost, UpdatePostContent, UpdatePostContext, VoteInPoll},
State,
};
@ -66,6 +66,21 @@ pub async fn create_request(
return Json(Error::NotAllowed.into());
}
// create poll
let poll_id = if let Some(p) = req.poll {
match data
.create_poll(Poll::new(
user.id, 86400000, p.option_a, p.option_b, p.option_c, p.option_d,
))
.await
{
Ok(p) => p,
Err(e) => return Json(e.into()),
}
} else {
0
};
// ...
let mut props = Post::new(
req.content,
@ -82,6 +97,7 @@ pub async fn create_request(
None
},
user.id,
poll_id,
);
if !req.answering.is_empty() {
@ -308,3 +324,39 @@ pub async fn update_context_request(
Err(e) => Json(e.into()),
}
}
pub async fn vote_request(
jar: CookieJar,
Extension(data): Extension<State>,
Path(id): Path<usize>,
Json(req): Json<VoteInPoll>,
) -> 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 post = match data.get_post_by_id(id).await {
Ok(p) => p,
Err(e) => return Json(e.into()),
};
let poll = match data.get_poll_by_id(post.poll_id).await {
Ok(p) => p,
Err(e) => return Json(e.into()),
};
// ...
match data
.create_pollvote(PollVote::new(user.id, poll.id, req.option))
.await
{
Ok(_) => Json(ApiReturn {
ok: true,
message: "Vote cast".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),
}
}

View file

@ -19,7 +19,7 @@ use serde::Deserialize;
use tetratto_core::model::{
communities::{
CommunityContext, CommunityJoinAccess, CommunityReadAccess, CommunityWriteAccess,
PostContext,
PollOption, PostContext,
},
communities_permissions::CommunityPermission,
permissions::FinePermission,
@ -113,6 +113,10 @@ pub fn routes() -> Router {
"/posts/{id}/context",
post(communities::posts::update_context_request),
)
.route(
"/posts/{id}/poll_vote",
post(communities::posts::vote_request),
)
// drafts
.route("/drafts", post(communities::drafts::create_request))
.route("/drafts/{id}", delete(communities::drafts::delete_request))
@ -419,6 +423,14 @@ pub struct UpdateCommunityJoinAccess {
pub access: CommunityJoinAccess,
}
#[derive(Deserialize)]
pub struct CreatePostPoll {
pub option_a: String,
pub option_b: String,
pub option_c: String,
pub option_d: String,
}
#[derive(Deserialize)]
pub struct CreatePost {
pub content: String,
@ -427,6 +439,8 @@ pub struct CreatePost {
pub replying_to: Option<String>,
#[serde(default)]
pub answering: String,
#[serde(default)]
pub poll: Option<CreatePostPoll>,
}
#[derive(Deserialize)]
@ -597,3 +611,8 @@ pub struct UpdateEmojiName {
pub struct CreatePostDraft {
pub content: String,
}
#[derive(Deserialize)]
pub struct VoteInPoll {
pub option: PollOption,
}