- 新增关注系统:UserFollow 模型、FollowService(toggle/status/列表隐私控制) - User 新增 FollowersCount/FollowingCount/FollowListPublic/NotifyPrefs 字段 - Space 页面增加四态关注按钮(关注/已关注/回关/已互粉)+ 粉丝/关注数链接 - 新增 /space/:uid/followers 和 /space/:uid/following 列表页 - 新增 NotifyFollow/NotifyLikeAggregated 通知类型 - ReactionService 点赞时写入 daily_like_summary,访问时生成聚合通知 - FollowService 关注时触发 NotifyFollow 通知 - 消息中心侧边栏升级为分类 TAB(全部/系统通知/@艾特/点赞/关注) - NotificationService 新增 ListByCategory 按分类分页查询
87 lines
2.2 KiB
Go
87 lines
2.2 KiB
Go
package controller
|
||
|
||
import (
|
||
"net/http"
|
||
"strconv"
|
||
|
||
"metazone.cc/metalab/internal/common"
|
||
"metazone.cc/metalab/internal/model"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// notifProvider MessageController 所需的通知服务接口(ISP:5 个方法)
|
||
type notifProvider interface {
|
||
List(userID uint, page, pageSize int) (*model.NotificationListResult, error)
|
||
ListByCategory(userID uint, category string, page, pageSize int) (*model.NotificationListResult, error)
|
||
CountUnread(userID uint) (int64, error)
|
||
MarkRead(id, userID uint) error
|
||
MarkAllRead(userID uint) error
|
||
}
|
||
|
||
// MessageController 消息中心控制器
|
||
type MessageController struct {
|
||
notifService notifProvider
|
||
}
|
||
|
||
// NewMessageController 构造函数
|
||
func NewMessageController(notifService notifProvider) *MessageController {
|
||
return &MessageController{notifService: notifService}
|
||
}
|
||
|
||
// UnreadCount 获取未读消息数(API,供导航栏轮询)
|
||
func (mc *MessageController) UnreadCount(c *gin.Context) {
|
||
uidVal, exists := c.Get("uid")
|
||
if !exists {
|
||
common.Ok(c, gin.H{"unread": 0})
|
||
return
|
||
}
|
||
|
||
count, err := mc.notifService.CountUnread(uidVal.(uint))
|
||
if err != nil {
|
||
common.Ok(c, gin.H{"unread": 0})
|
||
return
|
||
}
|
||
|
||
common.Ok(c, gin.H{"unread": count})
|
||
}
|
||
|
||
// MarkRead 标记单条消息已读(API)
|
||
func (mc *MessageController) MarkRead(c *gin.Context) {
|
||
uidVal, exists := c.Get("uid")
|
||
if !exists {
|
||
common.Error(c, http.StatusUnauthorized, "请先登录")
|
||
return
|
||
}
|
||
|
||
idStr := c.Param("id")
|
||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||
if err != nil {
|
||
common.Error(c, http.StatusBadRequest, "消息 ID 无效")
|
||
return
|
||
}
|
||
|
||
if err := mc.notifService.MarkRead(uint(id), uidVal.(uint)); err != nil {
|
||
common.Error(c, http.StatusInternalServerError, "操作失败")
|
||
return
|
||
}
|
||
|
||
common.OkMessage(c, "已标记为已读")
|
||
}
|
||
|
||
// MarkAllRead 一键全部已读(API)
|
||
func (mc *MessageController) MarkAllRead(c *gin.Context) {
|
||
uidVal, exists := c.Get("uid")
|
||
if !exists {
|
||
common.Error(c, http.StatusUnauthorized, "请先登录")
|
||
return
|
||
}
|
||
|
||
if err := mc.notifService.MarkAllRead(uidVal.(uint)); err != nil {
|
||
common.Error(c, http.StatusInternalServerError, "操作失败")
|
||
return
|
||
}
|
||
|
||
common.OkMessage(c, "已全部标为已读")
|
||
}
|