feat(settings): 后台可开关全站评论

关闭后禁止新评论与回复,已有内容仍可浏览;设置经 WS 同步到前台。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-09-16 07:01:46 +08:00
parent bcb4f6a603
commit 3e135542d3
5 changed files with 73 additions and 2 deletions

View File

@@ -26,6 +26,7 @@ type updateSettingsRequest struct {
SiteName *string `json:"site_name"`
SiteDescription *string `json:"site_description"`
AllowRegister *bool `json:"allow_register"`
AllowComments *bool `json:"allow_comments"`
PostCooldownHours *int `json:"post_cooldown_hours"`
CodeBlockAutoFold *bool `json:"code_block_auto_fold"`
CodeBlockFoldLines *int `json:"code_block_fold_lines"`
@@ -42,6 +43,7 @@ func settingsPayload(saved service.PublicSiteSettings) gin.H {
"site_name": saved.SiteName,
"site_description": saved.SiteDescription,
"allow_register": saved.AllowRegister,
"allow_comments": saved.AllowComments,
"post_cooldown_hours": saved.PostCooldownHours,
"code_block_auto_fold": saved.CodeBlockAutoFold,
"code_block_fold_lines": saved.CodeBlockFoldLines,
@@ -60,7 +62,7 @@ func (h *Handlers) UpdateSettings(c *gin.Context) {
return
}
if req.Accent == nil && req.TrustReviewedPublish == nil && req.SiteName == nil &&
req.SiteDescription == nil && req.AllowRegister == nil &&
req.SiteDescription == nil && req.AllowRegister == nil && req.AllowComments == nil &&
req.PostCooldownHours == nil && req.CodeBlockAutoFold == nil && req.CodeBlockFoldLines == nil &&
req.UIAnimations == nil && req.AnimCodeFold == nil && req.AnimSmoothScroll == nil &&
req.AnimChrome == nil {
@@ -110,6 +112,12 @@ func (h *Handlers) UpdateSettings(c *gin.Context) {
return
}
}
if req.AllowComments != nil {
if err := h.Setting.SetAllowComments(*req.AllowComments); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "保存站点设置失败"})
return
}
}
if req.PostCooldownHours != nil {
if err := h.Setting.SetPostCooldownHours(*req.PostCooldownHours); err != nil {
if errors.Is(err, service.ErrInvalidSiteSetting) {

View File

@@ -20,6 +20,8 @@ const (
SettingKeySiteName = "site_name"
SettingKeySiteDescription = "site_description"
SettingKeyAllowRegister = "allow_register"
// SettingKeyAllowComments 全站开放评论;缺行视为开启(默认开)
SettingKeyAllowComments = "allow_comments"
SettingKeyPostCooldownHours = "post_cooldown_hours"
// SettingKeyCodeBlockAutoFold 代码块自动折叠;缺行视为开启
SettingKeyCodeBlockAutoFold = "code_block_auto_fold"
@@ -62,6 +64,7 @@ type PublicSiteSettings struct {
SiteName string `json:"site_name"`
SiteDescription string `json:"site_description"`
AllowRegister bool `json:"allow_register"`
AllowComments bool `json:"allow_comments"`
PostCooldownHours int `json:"post_cooldown_hours"`
CodeBlockAutoFold bool `json:"code_block_auto_fold"`
CodeBlockFoldLines int `json:"code_block_fold_lines"`
@@ -128,6 +131,7 @@ func (s *SettingService) Public() (PublicSiteSettings, error) {
SiteDescription: DefaultSiteDescription,
TrustReviewedPublish: true,
AllowRegister: true,
AllowComments: true,
PostCooldownHours: DefaultCooldownHours,
CodeBlockAutoFold: true,
CodeBlockFoldLines: DefaultCodeFoldLines,
@@ -173,6 +177,12 @@ func (s *SettingService) Public() (PublicSiteSettings, error) {
}
out.AllowRegister = parseBoolDefaultTrue(reg, found)
comments, err := s.AllowComments()
if err != nil {
return out, err
}
out.AllowComments = comments
hours, err := s.PostCooldownHours()
if err != nil {
return out, err
@@ -286,6 +296,19 @@ func (s *SettingService) SetAllowRegister(on bool) error {
return s.setBoolDefaultTrue(SettingKeyAllowRegister, on)
}
// AllowComments 是否允许全站发表评论/回复。缺行视为开启。
func (s *SettingService) AllowComments() (bool, error) {
v, found, err := s.getValue(SettingKeyAllowComments)
if err != nil {
return true, err
}
return parseBoolDefaultTrue(v, found), nil
}
// SetAllowComments 持久化评论开关;开启时删键保持「缺行=默认开」。
func (s *SettingService) SetAllowComments(on bool) error {
return s.setBoolDefaultTrue(SettingKeyAllowComments, on)
}
// PostCooldownHours 新用户发帖冷静期(小时)。缺行=24;0 表示关闭。
func (s *SettingService) PostCooldownHours() (int, error) {