Files
jiang13-forum/service/settings.go
freefire 1d273066b0 升级帖子编辑与 Feed 体验:TipTap 富文本、修订历史、排序与编辑时限。
将 Markdown 编辑器替换为 TipTap WYSIWYG,新增帖子修订记录与 diff 展示;首页支持最新/回复排序与本地缓存;后台可配置编辑时限与锁定帖子;侧边栏整合板块导航并优化 Feed 布局。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-16 03:05:45 +08:00

71 lines
1.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 (
"strconv"
"sync"
"git.iioio.com/freefire/jiang13-forum/model"
)
const (
SettingPostEditWindowHours = "post_edit_window_hours"
defaultEditWindowHours = 24
)
// ForumSettingsService 论坛全局设置
type ForumSettingsService struct {
mu sync.RWMutex
}
func NewForumSettingsService() *ForumSettingsService {
s := &ForumSettingsService{}
s.ensureDefaults()
return s
}
func (s *ForumSettingsService) ensureDefaults() {
var count int64
model.DB.Model(&model.ForumSetting{}).Where("`key` = ?", SettingPostEditWindowHours).Count(&count)
if count == 0 {
model.DB.Create(&model.ForumSetting{
Key: SettingPostEditWindowHours,
Value: strconv.Itoa(defaultEditWindowHours),
})
}
}
// PostEditWindowHours 返回用户可编辑帖子的时限小时0 表示不限
func (s *ForumSettingsService) PostEditWindowHours() int {
s.mu.RLock()
defer s.mu.RUnlock()
var setting model.ForumSetting
if err := model.DB.First(&setting, "`key` = ?", SettingPostEditWindowHours).Error; err != nil {
return defaultEditWindowHours
}
h, err := strconv.Atoi(setting.Value)
if err != nil || h < 0 {
return defaultEditWindowHours
}
return h
}
// SetPostEditWindowHours 设置编辑时限小时0 表示不限
func (s *ForumSettingsService) SetPostEditWindowHours(hours int) error {
if hours < 0 {
return ErrInvalidSetting
}
s.mu.Lock()
defer s.mu.Unlock()
return model.DB.Save(&model.ForumSetting{
Key: SettingPostEditWindowHours,
Value: strconv.Itoa(hours),
}).Error
}
// ForumSettings 返回所有可配置的论坛设置
func (s *ForumSettingsService) ForumSettings() map[string]int {
return map[string]int{
"post_edit_window_hours": s.PostEditWindowHours(),
}
}