feat: 后台增加开放消息开关

关闭后前台隐藏私聊/群聊入口并由后端拦截;同步去掉公告页重复计数胶囊。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-09-17 04:16:10 +08:00
parent 9a55c2f6f2
commit 4b93739468
18 changed files with 275 additions and 91 deletions

View File

@@ -279,7 +279,12 @@ func (h *Handlers) Me(c *gin.Context) {
if n, err := h.Notification.UnreadCount(claims.ID); err == nil {
unread = n
}
chatUnread := h.Chat.TotalUnread(claims.ID)
chatUnread := int64(0)
if h.Setting != nil && h.Chat != nil {
if allow, err := h.Setting.AllowMessages(); err == nil && allow {
chatUnread = h.Chat.TotalUnread(claims.ID)
}
}
// 板块管理员带出授权板块,供前端菜单/按钮按板块范围渲染
var boardIDs []uint
if user.Role == model.RoleBoardAdmin {

View File

@@ -47,15 +47,33 @@ func (h *Handlers) chatOversee(userID uint) bool {
return service.CanOverseeChat(actor.Role)
}
// RequireMessagesOpen 前台群聊/私聊闸门;关闭后一律 403。管理端 /api/admin/chat 不受影响。
func (h *Handlers) RequireMessagesOpen(c *gin.Context) {
if h.Setting == nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "消息暂时不可用"})
return
}
allow, err := h.Setting.AllowMessages()
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "消息暂时不可用"})
return
}
if !allow {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": "本站不支持消息"})
return
}
c.Next()
}
// CreateChatRoom 建群(可公开/私密,私密可带初始成员)
func (h *Handlers) CreateChatRoom(c *gin.Context) {
claims := middleware.CurrentUser(c)
var req struct {
Name string `json:"name"`
Description string `json:"description"`
IsPrivate bool `json:"is_private"`
MemberIDs []uint `json:"member_ids"`
MemberUsernames []string `json:"member_usernames"`
Name string `json:"name"`
Description string `json:"description"`
IsPrivate bool `json:"is_private"`
MemberIDs []uint `json:"member_ids"`
MemberUsernames []string `json:"member_usernames"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数无效"})

View File

@@ -27,6 +27,7 @@ type updateSettingsRequest struct {
SiteDescription *string `json:"site_description"`
AllowRegister *bool `json:"allow_register"`
AllowComments *bool `json:"allow_comments"`
AllowMessages *bool `json:"allow_messages"`
PostCooldownHours *int `json:"post_cooldown_hours"`
CodeBlockAutoFold *bool `json:"code_block_auto_fold"`
CodeBlockFoldLines *int `json:"code_block_fold_lines"`
@@ -45,6 +46,7 @@ func settingsPayload(saved service.PublicSiteSettings) gin.H {
"site_description": saved.SiteDescription,
"allow_register": saved.AllowRegister,
"allow_comments": saved.AllowComments,
"allow_messages": saved.AllowMessages,
"post_cooldown_hours": saved.PostCooldownHours,
"code_block_auto_fold": saved.CodeBlockAutoFold,
"code_block_fold_lines": saved.CodeBlockFoldLines,
@@ -65,6 +67,7 @@ func (h *Handlers) UpdateSettings(c *gin.Context) {
}
if req.Accent == nil && req.TrustReviewedPublish == nil && req.SiteName == nil &&
req.SiteDescription == nil && req.AllowRegister == nil && req.AllowComments == nil &&
req.AllowMessages == nil &&
req.PostCooldownHours == nil && req.CodeBlockAutoFold == nil && req.CodeBlockFoldLines == nil &&
req.UIAnimations == nil && req.AnimCodeFold == nil && req.AnimSmoothScroll == nil &&
req.AnimChrome == nil && req.PostLinkNewTab == nil {
@@ -120,6 +123,12 @@ func (h *Handlers) UpdateSettings(c *gin.Context) {
return
}
}
if req.AllowMessages != nil {
if err := h.Setting.SetAllowMessages(*req.AllowMessages); 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

@@ -179,25 +179,28 @@ func Setup(cfg *config.Config) (*gin.Engine, error) {
api.GET("/my/media", h.MyMedia)
api.DELETE("/my/attachments/:id", h.DeleteAttachment)
// 群聊(二期):建群/成员/消息/未读
api.GET("/chat/rooms", h.ListChatRooms)
api.POST("/chat/rooms", h.CreateChatRoom)
api.POST("/chat/direct", h.OpenDirectChat)
api.GET("/chat/users", h.SearchChatUsers)
api.GET("/chat/rooms/:id", h.GetChatRoom)
api.PUT("/chat/rooms/:id", h.UpdateChatRoom)
api.PUT("/chat/rooms/:id/pin", h.PinChatRoom)
api.DELETE("/chat/rooms/:id", h.DissolveChatRoom)
api.POST("/chat/rooms/:id/join", h.JoinChatRoom)
api.POST("/chat/rooms/:id/leave", h.LeaveChatRoom)
api.GET("/chat/rooms/:id/members", h.ListChatMembers)
api.POST("/chat/rooms/:id/members", h.InviteChatMembers)
api.DELETE("/chat/rooms/:id/members/:uid", h.KickChatMember)
api.GET("/chat/rooms/:id/messages", h.ListChatMessages)
api.POST("/chat/rooms/:id/messages", middleware.RateLimitMiddleware(limiter, service.RateChat), h.SendChatMessage)
api.DELETE("/chat/rooms/:id/messages/:mid", h.RecallChatMessage)
api.PUT("/chat/rooms/:id/read", h.MarkChatRead)
api.GET("/chat/unread-summary", h.ChatUnreadSummary)
// 群聊(二期):建群/成员/消息/未读;全站关闭消息时前台一律 403
chatAPI := api.Group("/chat", h.RequireMessagesOpen)
{
chatAPI.GET("/rooms", h.ListChatRooms)
chatAPI.POST("/rooms", h.CreateChatRoom)
chatAPI.POST("/direct", h.OpenDirectChat)
chatAPI.GET("/users", h.SearchChatUsers)
chatAPI.GET("/rooms/:id", h.GetChatRoom)
chatAPI.PUT("/rooms/:id", h.UpdateChatRoom)
chatAPI.PUT("/rooms/:id/pin", h.PinChatRoom)
chatAPI.DELETE("/rooms/:id", h.DissolveChatRoom)
chatAPI.POST("/rooms/:id/join", h.JoinChatRoom)
chatAPI.POST("/rooms/:id/leave", h.LeaveChatRoom)
chatAPI.GET("/rooms/:id/members", h.ListChatMembers)
chatAPI.POST("/rooms/:id/members", h.InviteChatMembers)
chatAPI.DELETE("/rooms/:id/members/:uid", h.KickChatMember)
chatAPI.GET("/rooms/:id/messages", h.ListChatMessages)
chatAPI.POST("/rooms/:id/messages", middleware.RateLimitMiddleware(limiter, service.RateChat), h.SendChatMessage)
chatAPI.DELETE("/rooms/:id/messages/:mid", h.RecallChatMessage)
chatAPI.PUT("/rooms/:id/read", h.MarkChatRead)
chatAPI.GET("/unread-summary", h.ChatUnreadSummary)
}
}
// 管理后台 API:RequireStaff 基础鉴权(板块管理员及以上,Actor 实时从 DB 现取),

View File

@@ -21,7 +21,9 @@ const (
SettingKeySiteDescription = "site_description"
SettingKeyAllowRegister = "allow_register"
// SettingKeyAllowComments 全站开放评论;缺行视为开启(默认开)
SettingKeyAllowComments = "allow_comments"
SettingKeyAllowComments = "allow_comments"
// SettingKeyAllowMessages 全站开放私聊/群聊;缺行视为开启(默认开)
SettingKeyAllowMessages = "allow_messages"
SettingKeyPostCooldownHours = "post_cooldown_hours"
// SettingKeyCodeBlockAutoFold 代码块自动折叠;缺行视为开启
SettingKeyCodeBlockAutoFold = "code_block_auto_fold"
@@ -61,20 +63,21 @@ var accentHexRe = regexp.MustCompile(`^#[0-9a-fA-F]{6}$`)
// PublicSiteSettings 对前台公开的站点配置(SEO / 注册入口 / 发帖冷静期提示)
type PublicSiteSettings struct {
Accent string `json:"accent"`
TrustReviewedPublish bool `json:"trust_reviewed_publish"`
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"`
UIAnimations bool `json:"ui_animations"`
AnimCodeFold bool `json:"anim_code_fold"`
AnimSmoothScroll bool `json:"anim_smooth_scroll"`
AnimChrome bool `json:"anim_chrome"`
PostLinkNewTab bool `json:"post_link_new_tab"`
Accent string `json:"accent"`
TrustReviewedPublish bool `json:"trust_reviewed_publish"`
SiteName string `json:"site_name"`
SiteDescription string `json:"site_description"`
AllowRegister bool `json:"allow_register"`
AllowComments bool `json:"allow_comments"`
AllowMessages bool `json:"allow_messages"`
PostCooldownHours int `json:"post_cooldown_hours"`
CodeBlockAutoFold bool `json:"code_block_auto_fold"`
CodeBlockFoldLines int `json:"code_block_fold_lines"`
UIAnimations bool `json:"ui_animations"`
AnimCodeFold bool `json:"anim_code_fold"`
AnimSmoothScroll bool `json:"anim_smooth_scroll"`
AnimChrome bool `json:"anim_chrome"`
PostLinkNewTab bool `json:"post_link_new_tab"`
}
// SettingService 站点级键值设置
@@ -135,6 +138,7 @@ func (s *SettingService) Public() (PublicSiteSettings, error) {
TrustReviewedPublish: true,
AllowRegister: true,
AllowComments: true,
AllowMessages: true,
PostCooldownHours: DefaultCooldownHours,
CodeBlockAutoFold: true,
CodeBlockFoldLines: DefaultCodeFoldLines,
@@ -187,6 +191,12 @@ func (s *SettingService) Public() (PublicSiteSettings, error) {
}
out.AllowComments = comments
messages, err := s.AllowMessages()
if err != nil {
return out, err
}
out.AllowMessages = messages
hours, err := s.PostCooldownHours()
if err != nil {
return out, err
@@ -320,6 +330,20 @@ func (s *SettingService) SetAllowComments(on bool) error {
return s.setBoolDefaultTrue(SettingKeyAllowComments, on)
}
// AllowMessages 是否允许全站私聊/群聊。缺行视为开启。
func (s *SettingService) AllowMessages() (bool, error) {
v, found, err := s.getValue(SettingKeyAllowMessages)
if err != nil {
return true, err
}
return parseBoolDefaultTrue(v, found), nil
}
// SetAllowMessages 持久化消息开关;开启时删键保持「缺行=默认开」。
func (s *SettingService) SetAllowMessages(on bool) error {
return s.setBoolDefaultTrue(SettingKeyAllowMessages, on)
}
// PostCooldownHours 新用户发帖冷静期(小时)。缺行=24;0 表示关闭。
func (s *SettingService) PostCooldownHours() (int, error) {
v, found, err := s.getValue(SettingKeyPostCooldownHours)