add: polls backend
TODO: polls frontend
This commit is contained in:
parent
b5e060e8ae
commit
4dfa09207e
18 changed files with 574 additions and 17 deletions
|
@ -22,7 +22,6 @@ use tetratto_core::{
|
|||
use tetratto_l10n::LangFile;
|
||||
use tetratto_shared::hash::salt;
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
use crate::{create_dir_if_not_exists, write_if_track, write_template};
|
||||
|
||||
// images
|
||||
|
|
|
@ -111,7 +111,7 @@
|
|||
("style" "display: contents")
|
||||
(text "{{ self::post(post=post, owner=owner, secondary=secondary, community=community, show_community=show_community, can_manage_post=can_manage_post, repost=repost, expect_repost=true) }}"))
|
||||
|
||||
(text "{%- endmacro %} {% macro post(post, owner, question=false, secondary=false, community=false, show_community=true, can_manage_post=false, repost=false, expect_repost=false) -%} {% if community and show_community and community.id != config.town_square or question %}")
|
||||
(text "{%- endmacro %} {% macro post(post, owner, question=false, secondary=false, community=false, show_community=true, can_manage_post=false, repost=false, expect_repost=false, poll=false) -%} {% if community and show_community and community.id != config.town_square or question %}")
|
||||
(div
|
||||
("class" "card-nest")
|
||||
(text "{% if question -%} {{ self::question(question=question[0], owner=question[1], profile=owner) }} {% else %}")
|
||||
|
|
|
@ -522,9 +522,9 @@
|
|||
("class" "card flex flex-col gap-2")
|
||||
(text "{% if is_supporter -%}")
|
||||
(p
|
||||
(text "You")
|
||||
(text "You ")
|
||||
(b
|
||||
(text "are"))
|
||||
(text "are "))
|
||||
(text "a supporter! Thank you for all
|
||||
that you do. You can manage your billing
|
||||
information below.")
|
||||
|
@ -539,9 +539,9 @@
|
|||
(text "Manage billing"))
|
||||
(text "{% else %}")
|
||||
(p
|
||||
(text "You're")
|
||||
(text "You're ")
|
||||
(b
|
||||
(text "not"))
|
||||
(text "not "))
|
||||
(text "currently a supporter! No
|
||||
pressure, but it helps us do some pretty cool
|
||||
things! As a supporter, you'll get:"))
|
||||
|
|
|
@ -30,6 +30,6 @@
|
|||
(text "{%- endif %}")
|
||||
(div
|
||||
("class" "card w-full flex flex-col gap-2")
|
||||
(text "{% for post in list %} {% if post[2].read_access == \"Everybody\" -%} {% if post[0].context.repost and post[0].context.repost.reposting -%} {{ components::repost(repost=post[3], post=post[0], owner=post[1], secondary=true, community=post[2], show_community=true) }} {% else %} {{ components::post(post=post[0], owner=post[1], question=post[4], secondary=true, community=post[2]) }} {%- endif %} {%- endif %} {% endfor %} {{ components::pagination(page=page, items=list|length) }}")))
|
||||
(text "{% for post in list %} {% if post[2].read_access == \"Everybody\" -%} {% if post[0].context.repost and post[0].context.repost.reposting -%} {{ components::repost(repost=post[3], post=post[0], owner=post[1], secondary=true, community=post[2], show_community=true) }} {% else %} {{ components::post(post=post[0], owner=post[1], question=post[4], secondary=true, community=post[2], poll=post[3]) }} {%- endif %} {%- endif %} {% endfor %} {{ components::pagination(page=page, items=list|length) }}")))
|
||||
|
||||
(text "{% endblock %}")
|
||||
|
|
|
@ -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()),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue