//! The Discussions switch on the repo settings page. `POST /{owner}/{repo}/settings/discussions` //! with `enabled=1` or `enabled=0`. Admins only. Turning discussions off hides the tab and makes //! the discussion pages 404; the discussions themselves are kept. use forge_core::AuditAction; use serde::Deserialize; use topcoat::Result; use topcoat::context::{Cx, app_context}; use topcoat::router::content::Form; use topcoat::router::error::{SeeOther, forbidden, see_other}; use topcoat::router::{path_param, route}; use topcoat::view::{View, component, view}; use crate::audit::{self, Actor, Target}; use crate::auth::require_csrf; use crate::i18n::Lang; use crate::i18n::community::{self as g, K}; use crate::pages::repo_ctx::load_repo; use crate::state::AppState; use crate::views::repo_href; path_param!(owner); path_param!(repo); /// The settings card with the on/off button. `enabled` is the current state. #[component] pub(crate) async fn discussions_card( lang: Lang, action: String, csrf: String, enabled: bool, ) -> Result { Ok(view! {

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

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

if enabled { } else { }
}) } #[derive(Deserialize, Default)] struct ToggleForm { #[serde(default)] csrf: String, #[serde(default)] enabled: String, } #[route(POST "/{owner}/{repo}/settings/discussions")] async fn settings_discussions_save(cx: &Cx, Form(form): Form) -> Result { let ctx = load_repo(cx, path_param::(cx), path_param::(cx)).await?; let (user, _) = require_csrf(cx, &form.csrf).await?; if !ctx.can_admin() { return Err(forbidden().into()); } let enabled = form.enabled == "1"; let state = app_context::(cx); state .store .set_discussions_enabled(&ctx.repo.id, enabled) .await?; let name = format!("{}/{}", ctx.repo.owner_name, ctx.repo.name); let action = if enabled { AuditAction::DiscussionsEnabled } else { AuditAction::DiscussionsDisabled }; audit::record( &state.store, Some(cx), Actor::User(&user), action, Target { kind: "repo", id: &ctx.repo.id, name: &name, }, serde_json::Value::Null, ) .await; let back = repo_href(&ctx.repo.owner_name, &ctx.repo.name, "settings", ""); Ok(see_other(format!("{back}#discussions"))) }