Files
jiang13-bbs/backend/service/setting.go
freefire f3145b24ff feat: 就地通过评审,并为已过审用户提供默认可关的免审发布
帖/评管理入口可直接通过;站点默认信任已有 published 内容的用户后续直发。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-16 05:37:57 +08:00

90 lines
2.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package service
import (
"errors"
"regexp"
"strings"
"github.com/freefire/jiang13-bbs/model"
"gorm.io/gorm"
)
// 站点设置键
const (
// SettingKeyAccent 站点主题色(浅色基准,#rrggbb);为空表示使用内置默认色
SettingKeyAccent = "theme_accent"
// SettingKeyTrustReviewedPublish 已过审用户后续发帖/评论免审;缺行视为开启(默认开)
SettingKeyTrustReviewedPublish = "trust_reviewed_publish"
)
// ErrInvalidAccent 主题色格式非法
var ErrInvalidAccent = errors.New("主题色格式不正确,应为 #RRGGBB")
var accentHexRe = regexp.MustCompile(`^#[0-9a-fA-F]{6}$`)
// SettingService 站点级键值设置(主题色等外观配置)
type SettingService struct {
db *gorm.DB
}
func NewSettingService(db *gorm.DB) *SettingService {
return &SettingService{db: db}
}
// AccentColor 返回配置的主题色(小写 #rrggbb);未配置时返回空串,表示走前端默认色
func (s *SettingService) AccentColor() (string, error) {
var st model.SiteSetting
err := s.db.Where("key = ?", SettingKeyAccent).First(&st).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return "", nil
}
if err != nil {
return "", err
}
return strings.ToLower(st.Value), nil
}
// SetAccent 校验并保存主题色;hex 传空串表示恢复默认(删除该键)
func (s *SettingService) SetAccent(hex string) error {
hex = strings.TrimSpace(strings.ToLower(hex))
if hex == "" {
return s.db.Where("key = ?", SettingKeyAccent).Delete(&model.SiteSetting{}).Error
}
if !accentHexRe.MatchString(hex) {
return ErrInvalidAccent
}
// UPSERT:键冲突时更新值与时间戳
st := model.SiteSetting{Key: SettingKeyAccent, Value: hex}
return s.db.Save(&st).Error
}
// TrustReviewedPublish 是否对「已有过审内容」的用户免审。
// KV 缺行 / 空值视为开启(默认开);仅显式写入 false/0/off/no 为关闭。
// 不用 GORM bool default:true,避免零值省略陷阱。
func (s *SettingService) TrustReviewedPublish() (bool, error) {
var st model.SiteSetting
err := s.db.Where("key = ?", SettingKeyTrustReviewedPublish).First(&st).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return true, nil
}
if err != nil {
return true, err
}
v := strings.TrimSpace(strings.ToLower(st.Value))
switch v {
case "false", "0", "off", "no":
return false, nil
default:
return true, nil
}
}
// SetTrustReviewedPublish 持久化免审开关;开启时删除键以保持「缺行=默认开」语义
func (s *SettingService) SetTrustReviewedPublish(on bool) error {
if on {
return s.db.Where("key = ?", SettingKeyTrustReviewedPublish).Delete(&model.SiteSetting{}).Error
}
st := model.SiteSetting{Key: SettingKeyTrustReviewedPublish, Value: "false"}
return s.db.Save(&st).Error
}