| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | use forge_core::{Reaction, ReactionTarget}; |
| 8 | use serde::Deserialize; |
| 9 | use topcoat::Result; |
| 10 | use topcoat::context::{Cx, app_context}; |
| 11 | use topcoat::router::content::Form; |
| 12 | use topcoat::router::error::{SeeOther, bad_request, forbidden, not_found, see_other}; |
| 13 | use topcoat::router::{path_param, route}; |
| 14 | |
| 15 | use crate::auth::require_csrf; |
| 16 | use crate::pages::issue_parts::{ItemKind, comment_for, item_for, may_converse, parse_number}; |
| 17 | use crate::pages::repo_ctx::load_repo; |
| 18 | use crate::state::AppState; |
| 19 | use crate::views::repo_href; |
| 20 | |
| 21 | path_param!(owner); |
| 22 | path_param!(repo); |
| 23 | path_param!(number); |
| 24 | |
| 25 | #[derive(Deserialize)] |
| 26 | struct ReactionForm { |
| 27 | #[serde(default)] |
| 28 | csrf: String, |
| 29 | #[serde(default)] |
| 30 | target_kind: String, |
| 31 | #[serde(default)] |
| 32 | target_id: String, |
| 33 | #[serde(default)] |
| 34 | reaction: String, |
| 35 | } |
| 36 | |
| 37 | #[route(POST "/{owner}/{repo}/issues/{number}/reactions")] |
| 38 | async fn issue_reaction(cx: &Cx, Form(form): Form<ReactionForm>) -> Result<SeeOther> { |
| 39 | react(cx, ItemKind::Issue, form).await |
| 40 | } |
| 41 | |
| 42 | #[route(POST "/{owner}/{repo}/pulls/{number}/reactions")] |
| 43 | async fn pull_reaction(cx: &Cx, Form(form): Form<ReactionForm>) -> Result<SeeOther> { |
| 44 | react(cx, ItemKind::Pull, form).await |
| 45 | } |
| 46 | |
| 47 | async fn react(cx: &Cx, kind: ItemKind, form: ReactionForm) -> Result<SeeOther> { |
| 48 | let number = parse_number(path_param::<Number>(cx))?; |
| 49 | let ctx = load_repo(cx, path_param::<Owner>(cx), path_param::<Repo>(cx)).await?; |
| 50 | let (user, _) = require_csrf(cx, &form.csrf).await?; |
| 51 | if ctx.repo.archived { |
| 52 | return Err(forbidden().into()); |
| 53 | } |
| 54 | let state = app_context::<AppState>(cx); |
| 55 | let item = item_for(state, &ctx, kind, number).await?; |
| 56 | if !may_converse(&ctx, &item) { |
| 57 | return Err(forbidden().into()); |
| 58 | } |
| 59 | let Some(reaction) = Reaction::parse(&form.reaction) else { |
| 60 | return Err(bad_request("unknown reaction").into()); |
| 61 | }; |
| 62 | let (target, anchor) = match form.target_kind.as_str() { |
| 63 | "issue" => { |
| 64 | if form.target_id != item.id { |
| 65 | return Err(not_found().into()); |
| 66 | } |
| 67 | let prefix = match kind { |
| 68 | ItemKind::Issue => "issue", |
| 69 | ItemKind::Pull => "pull", |
| 70 | }; |
| 71 | (ReactionTarget::Issue, format!("{prefix}-{number}")) |
| 72 | } |
| 73 | "comment" => { |
| 74 | let comment = comment_for(state, &item, &form.target_id).await?; |
| 75 | (ReactionTarget::Comment, format!("comment-{}", comment.id)) |
| 76 | } |
| 77 | _ => return Err(bad_request("unknown reaction target").into()), |
| 78 | }; |
| 79 | state |
| 80 | .store |
| 81 | .toggle_reaction(target, &form.target_id, &user.id, reaction) |
| 82 | .await?; |
| 83 | let href = repo_href( |
| 84 | &ctx.repo.owner_name, |
| 85 | &ctx.repo.name, |
| 86 | kind.segment(), |
| 87 | &number.to_string(), |
| 88 | ); |
| 89 | Ok(see_other(format!("{href}#{anchor}"))) |
| 90 | } |