//! Pinning an issue: `POST /{owner}/{repo}/issues/{n}/pin` with `csrf` and `pinned` (`1` pins, //! `0` unpins). Triage holders only, through the same guard as the other issue writes. Pinning a //! fourth issue re-renders the reason in an error box. A real change adds a `pinned` or `unpinned` //! event to the timeline. Also holds the sidebar box that posts here. use serde::Deserialize; use serde_json::Value as Json; use topcoat::Result; use topcoat::context::{Cx, app_context}; use topcoat::router::content::Form; use topcoat::router::error::{bad_request, see_other}; use topcoat::router::{page, path_param}; use topcoat::view::{View, component, view}; use crate::i18n::issues::{self as g, K}; use crate::i18n::{self, Lang}; use crate::pages::issue_meta::{ action_error_at, item_from_path, item_href, refusal_message, triage_guard, }; use crate::pages::issue_parts::ItemKind; use crate::pages::repo_ctx::load_repo; use crate::state::AppState; use crate::views::shell; path_param!(owner); path_param!(repo); path_param!(number); #[derive(Deserialize)] struct PinForm { #[serde(default)] csrf: String, #[serde(default)] pinned: String, } #[page(POST "/{owner}/{repo}/issues/{number}/pin")] async fn issue_pin_save(cx: &Cx, Form(form): Form) -> Result { let ctx = load_repo(cx, path_param::(cx), path_param::(cx)).await?; let issue = item_from_path(cx, &ctx, ItemKind::Issue, path_param::(cx)).await?; let user = triage_guard(cx, &ctx, &form.csrf).await?; let lang = i18n::lang(cx, ctx.viewer.as_ref().map(|(u, _)| u)); let pin = match form.pinned.as_str() { "1" => true, "0" => false, _ => return Err(bad_request("pinned must be 1 or 0").into()), }; let back = item_href(&ctx, ItemKind::Issue, issue.number); let state = app_context::(cx); let result = if pin { state.store.pin_issue(&ctx.repo.id, &issue.id).await } else { state.store.unpin_issue(&ctx.repo.id, &issue.id).await }; match result { Ok(changed) => { if changed { let kind = if pin { "pinned" } else { "unpinned" }; state .store .add_event(&issue.id, Some(&user.id), kind, Json::Null) .await?; } } Err(e) => { let message = refusal_message(e)?; return Ok(view! { cx => shell(title: g::t(lang, K::CouldNotSave), viewer: ctx.viewer.as_ref(), action_error_at(lang: lang, message: message, back: back)) }); } } Err(see_other(back).into()) } /// The sidebar box for pinning. Triage holders get the button, which posts the opposite of the /// current state; others see whether the issue is pinned. #[component] pub(crate) async fn pin_box( lang: Lang, write: bool, pinned: bool, action: String, csrf: String, ) -> Result { let next = if pinned { "0" } else { "1" }; let label = if pinned { g::t(lang, K::UnpinIssue) } else { g::t(lang, K::PinIssue) }; Ok(view! {

(g::t(lang, K::Pinned))

if write {
} else { if pinned {

(g::t(lang, K::PinnedTopNote))

} else {

(g::t(lang, K::NotPinned))

} }
}) }