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

@@ -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) {