- Introduce interfaces for all major services (JWT, PostAI, Comment, Message, Notification, QRCodeLogin, Upload, Vote, etc.) to support dependency inversion. - Move query parameters from `internal/dto` to a new `internal/query` package to separate request payloads from data transfer objects. - Refactor `internal/router` to use embedded `RouterDeps` for cleaner dependency management. - Decouple handlers from repositories by injecting services instead of direct repository access, ensuring proper layering. - Improve database initialization by moving it from `internal/model` to `internal/database`. - Optimize message decryption by implementing a more efficient `BatchDecrypt` method in `MessageEncryptor` using a worker pool. - Enhance error handling and security by implementing fail-fast checks for encryption key length during startup. - Clean up unused code, including the `avatar` package and several unused DTOs.
361 lines
11 KiB
Go
361 lines
11 KiB
Go
package model
|
||
|
||
import (
|
||
"database/sql/driver"
|
||
"encoding/json"
|
||
"time"
|
||
|
||
"gorm.io/gorm"
|
||
|
||
"with_you/internal/pkg/crypto"
|
||
)
|
||
|
||
// 系统消息相关常量
|
||
const (
|
||
// SystemSenderID 系统消息发送者ID (类似QQ 10000)
|
||
SystemSenderID int64 = 10000
|
||
|
||
// SystemSenderIDStr 系统消息发送者ID字符串版本
|
||
SystemSenderIDStr string = "10000"
|
||
|
||
// SystemConversationID 系统通知会话ID (string类型)
|
||
SystemConversationID string = "9999999999"
|
||
)
|
||
|
||
// ContentType 消息内容类型
|
||
type ContentType string
|
||
|
||
const (
|
||
ContentTypeText ContentType = "text"
|
||
ContentTypeImage ContentType = "image"
|
||
ContentTypeVideo ContentType = "video"
|
||
ContentTypeAudio ContentType = "audio"
|
||
ContentTypeFile ContentType = "file"
|
||
)
|
||
|
||
// MessageStatus 消息状态
|
||
type MessageStatus string
|
||
|
||
const (
|
||
MessageStatusNormal MessageStatus = "normal" // 正常
|
||
MessageStatusRecalled MessageStatus = "recalled" // 已撤回
|
||
MessageStatusDeleted MessageStatus = "deleted" // 已删除
|
||
)
|
||
|
||
// MessageCategory 消息类别
|
||
type MessageCategory string
|
||
|
||
const (
|
||
CategoryChat MessageCategory = "chat" // 普通聊天
|
||
CategoryNotification MessageCategory = "notification" // 通知类消息
|
||
CategoryAnnouncement MessageCategory = "announcement" // 系统公告
|
||
CategoryMarketing MessageCategory = "marketing" // 营销消息
|
||
)
|
||
|
||
// SystemMessageType 系统消息类型 (对应原NotificationType)
|
||
type SystemMessageType string
|
||
|
||
const (
|
||
// 互动通知
|
||
SystemTypeLikePost SystemMessageType = "like_post" // 点赞帖子
|
||
SystemTypeLikeComment SystemMessageType = "like_comment" // 点赞评论
|
||
SystemTypeComment SystemMessageType = "comment" // 评论
|
||
SystemTypeReply SystemMessageType = "reply" // 回复
|
||
SystemTypeFollow SystemMessageType = "follow" // 关注
|
||
SystemTypeMention SystemMessageType = "mention" // @提及
|
||
|
||
// 系统消息
|
||
SystemTypeSystem SystemMessageType = "system" // 系统通知
|
||
SystemTypeAnnounce SystemMessageType = "announce" // 系统公告
|
||
)
|
||
|
||
// ExtraData 消息额外数据,用于存储系统消息的相关信息
|
||
type ExtraData struct {
|
||
// 操作者信息
|
||
ActorID int64 `json:"actor_id,omitempty"` // 操作者ID (数字格式,兼容旧数据)
|
||
ActorIDStr string `json:"actor_id_str,omitempty"` // 操作者ID (UUID字符串格式)
|
||
ActorName string `json:"actor_name,omitempty"` // 操作者名称
|
||
AvatarURL string `json:"avatar_url,omitempty"` // 操作者头像
|
||
|
||
// 目标信息
|
||
TargetID int64 `json:"target_id,omitempty"` // 目标ID(帖子ID、评论ID等)
|
||
TargetTitle string `json:"target_title,omitempty"` // 目标标题
|
||
TargetType string `json:"target_type,omitempty"` // 目标类型(post/comment等)
|
||
|
||
// 其他信息
|
||
ActionURL string `json:"action_url,omitempty"` // 跳转链接
|
||
ActionTime string `json:"action_time,omitempty"` // 操作时间
|
||
}
|
||
|
||
// Value 实现driver.Valuer接口,用于数据库存储
|
||
func (e ExtraData) Value() (driver.Value, error) {
|
||
return ValueJSON(e)
|
||
}
|
||
|
||
// Scan 实现sql.Scanner接口,用于数据库读取
|
||
func (e *ExtraData) Scan(value any) error {
|
||
return ScanJSON(e, value)
|
||
}
|
||
|
||
// MessageSegmentData 单个消息段的数据
|
||
type MessageSegmentData map[string]any
|
||
|
||
// MessageSegment 消息段
|
||
type MessageSegment struct {
|
||
Type string `json:"type"`
|
||
Data MessageSegmentData `json:"data"`
|
||
}
|
||
|
||
// MessageSegments 消息链类型
|
||
type MessageSegments []MessageSegment
|
||
|
||
// Value 实现driver.Valuer接口,用于数据库存储
|
||
func (s MessageSegments) Value() (driver.Value, error) {
|
||
return ValueJSON(s)
|
||
}
|
||
|
||
// Scan 实现sql.Scanner接口,用于数据库读取
|
||
func (s *MessageSegments) Scan(value any) error {
|
||
if value == nil {
|
||
*s = nil
|
||
return nil
|
||
}
|
||
return ScanJSON(s, value)
|
||
}
|
||
|
||
// Message 消息实体
|
||
// 使用雪花算法ID(string类型)和seq机制实现消息排序和增量同步
|
||
// 支持消息内容加密存储
|
||
type Message struct {
|
||
ID string `gorm:"primaryKey;size:20" json:"id"` // 雪花算法ID(string类型)
|
||
ConversationID string `gorm:"not null;size:20;index:idx_msg_conversation_seq,priority:1" json:"conversation_id"` // 会话ID(string类型)
|
||
SenderID string `gorm:"column:sender_id;type:varchar(50);index;not null" json:"sender_id"` // 发送者ID (UUID格式)
|
||
Seq int64 `gorm:"not null;index:idx_msg_conversation_seq,priority:2" json:"seq"` // 会话内序号,用于排序和增量同步
|
||
|
||
// 消息内容字段
|
||
Segments MessageSegments `gorm:"-" json:"segments"` // 消息链(内存中使用,不映射到数据库)
|
||
SegmentsEncrypted string `gorm:"column:segments;type:text" json:"-"` // 加密后的消息链(存储在数据库)
|
||
SegmentsKeyVersion int `gorm:"column:segments_key_version;default:0" json:"segments_key_version"` // 密钥版本,用于密钥轮换
|
||
|
||
ReplyToID *string `json:"reply_to_id,omitempty"` // 回复的消息ID(string类型)
|
||
Status MessageStatus `gorm:"type:varchar(20);default:'normal'" json:"status"` // 消息状态
|
||
|
||
// 新增字段:消息分类和系统消息类型
|
||
Category MessageCategory `gorm:"type:varchar(20);default:'chat'" json:"category"` // 消息分类
|
||
SystemType SystemMessageType `gorm:"type:varchar(30)" json:"system_type,omitempty"` // 系统消息类型
|
||
ExtraData *ExtraData `gorm:"type:json" json:"extra_data,omitempty"` // 额外数据(JSON格式)
|
||
|
||
// 软删除
|
||
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
|
||
|
||
// 时间戳
|
||
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
|
||
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
|
||
|
||
// 内部标记,表示是否已解密
|
||
decrypted bool `gorm:"-"`
|
||
}
|
||
|
||
// SenderIDStr 返回发送者ID字符串(保持兼容性)
|
||
func (m *Message) SenderIDStr() string {
|
||
return m.SenderID
|
||
}
|
||
|
||
// BeforeCreate 创建前生成雪花算法ID并加密消息内容
|
||
func (m *Message) BeforeCreate(tx *gorm.DB) error {
|
||
if err := SetSnowflakeStringID(&m.ID); err != nil {
|
||
return err
|
||
}
|
||
|
||
// 加密消息内容
|
||
return m.encryptSegments()
|
||
}
|
||
|
||
// BeforeUpdate 更新前加密消息内容
|
||
func (m *Message) BeforeUpdate(tx *gorm.DB) error {
|
||
// 只有当 Segments 被修改时才重新加密
|
||
if tx.Statement.Changed("Segments") || len(m.Segments) > 0 {
|
||
return m.encryptSegments()
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// AfterFind 查询后标记需要解密(实际解密延迟到批量处理时)
|
||
// 这样可以支持批量并行解密优化
|
||
func (m *Message) AfterFind(tx *gorm.DB) error {
|
||
// 标记为未解密状态,等待批量解密
|
||
// 如果是单条查询,AfterFind 后会立即使用,需要解密
|
||
m.decrypted = false
|
||
return nil
|
||
}
|
||
|
||
// Decrypt 解密单条消息(显式调用)
|
||
func (m *Message) Decrypt() error {
|
||
return m.decryptSegments()
|
||
}
|
||
|
||
// AfterCreate 创建后解密消息内容(保持内存中的数据可读)
|
||
func (m *Message) AfterCreate(tx *gorm.DB) error {
|
||
m.decrypted = true
|
||
return nil
|
||
}
|
||
|
||
// encryptSegments 加密消息段
|
||
func (m *Message) encryptSegments() error {
|
||
// 如果没有消息内容或加密器未初始化,跳过加密
|
||
if len(m.Segments) == 0 {
|
||
return nil
|
||
}
|
||
|
||
encryptor := crypto.GetMessageEncryptor()
|
||
if encryptor == nil {
|
||
// 加密器未初始化时,直接存储JSON(兼容模式)
|
||
data, err := json.Marshal(m.Segments)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
m.SegmentsEncrypted = string(data)
|
||
m.SegmentsKeyVersion = 0
|
||
return nil
|
||
}
|
||
|
||
// 序列化消息段
|
||
plaintext, err := json.Marshal(m.Segments)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
// 加密
|
||
ciphertext, err := encryptor.Encrypt(plaintext)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
m.SegmentsEncrypted = ciphertext
|
||
m.SegmentsKeyVersion = encryptor.GetKeyVersion()
|
||
return nil
|
||
}
|
||
|
||
// decryptSegments 解密消息段
|
||
func (m *Message) decryptSegments() error {
|
||
// 如果已经解密过,跳过
|
||
if m.decrypted {
|
||
return nil
|
||
}
|
||
|
||
// 如果没有加密内容,跳过
|
||
if m.SegmentsEncrypted == "" {
|
||
m.decrypted = true
|
||
return nil
|
||
}
|
||
|
||
encryptor := crypto.GetMessageEncryptor()
|
||
if encryptor == nil {
|
||
// 加密器未初始化,尝试直接解析JSON(兼容旧数据)
|
||
if err := json.Unmarshal([]byte(m.SegmentsEncrypted), &m.Segments); err != nil {
|
||
return err
|
||
}
|
||
m.decrypted = true
|
||
return nil
|
||
}
|
||
|
||
// 尝试解密
|
||
plaintext, err := encryptor.Decrypt(m.SegmentsEncrypted)
|
||
if err != nil {
|
||
// 解密失败,可能是未加密的旧数据,尝试直接解析
|
||
if parseErr := json.Unmarshal([]byte(m.SegmentsEncrypted), &m.Segments); parseErr != nil {
|
||
return err // 返回解密错误
|
||
}
|
||
m.decrypted = true
|
||
return nil
|
||
}
|
||
|
||
// 解析解密后的数据
|
||
if err := json.Unmarshal(plaintext, &m.Segments); err != nil {
|
||
return err
|
||
}
|
||
|
||
m.decrypted = true
|
||
return nil
|
||
}
|
||
|
||
func (Message) TableName() string {
|
||
return "messages"
|
||
}
|
||
|
||
// IsSystemMessage 判断是否为系统消息
|
||
func (m *Message) IsSystemMessage() bool {
|
||
return m.SenderID == SystemSenderIDStr || m.Category == CategoryNotification || m.Category == CategoryAnnouncement
|
||
}
|
||
|
||
// IsInteractionNotification 判断是否为互动通知
|
||
func (m *Message) IsInteractionNotification() bool {
|
||
if m.Category != CategoryNotification {
|
||
return false
|
||
}
|
||
switch m.SystemType {
|
||
case SystemTypeLikePost, SystemTypeLikeComment, SystemTypeComment,
|
||
SystemTypeReply, SystemTypeFollow, SystemTypeMention:
|
||
return true
|
||
default:
|
||
return false
|
||
}
|
||
}
|
||
|
||
// BatchDecryptMessagesParallel 批量解密消息段。
|
||
// 并发逻辑收敛在 crypto.MessageEncryptor.BatchDecrypt(worker pool),
|
||
// 本函数仅负责「提取密文 → 回填明文到 Message.Segments」,不再自起 goroutine。
|
||
//
|
||
// 行为保持与历史版本一致:
|
||
// - 加密器未初始化时,降级为明文 JSON 直接解析(兼容旧数据);
|
||
// - 单条解密失败时回退尝试直接 JSON 解析(兼容未加密旧数据);
|
||
// - 已解密或无加密内容的消息跳过。
|
||
func BatchDecryptMessagesParallel(messages []*Message) {
|
||
if len(messages) == 0 {
|
||
return
|
||
}
|
||
|
||
encryptor := crypto.GetMessageEncryptor()
|
||
if encryptor == nil {
|
||
// 加密器未初始化,串行解析为明文 JSON(兼容模式)
|
||
for _, m := range messages {
|
||
if !m.decrypted && m.SegmentsEncrypted != "" {
|
||
_ = json.Unmarshal([]byte(m.SegmentsEncrypted), &m.Segments)
|
||
m.decrypted = true
|
||
}
|
||
}
|
||
return
|
||
}
|
||
|
||
// 提取待解密的密文索引与值
|
||
indices := make([]int, 0, len(messages))
|
||
ciphertexts := make([]string, 0, len(messages))
|
||
for i, m := range messages {
|
||
if m.decrypted || m.SegmentsEncrypted == "" {
|
||
m.decrypted = true
|
||
continue
|
||
}
|
||
indices = append(indices, i)
|
||
ciphertexts = append(ciphertexts, m.SegmentsEncrypted)
|
||
}
|
||
if len(ciphertexts) == 0 {
|
||
return
|
||
}
|
||
|
||
// 并发解密(worker pool 由 crypto.BatchDecrypt 内部管理)
|
||
plaintexts := encryptor.BatchDecrypt(ciphertexts, 0)
|
||
|
||
// 回填明文;解密失败的位置回退直接 JSON 解析
|
||
for j, idx := range indices {
|
||
m := messages[idx]
|
||
pt := plaintexts[j]
|
||
if pt == nil {
|
||
// 解密失败,兼容未加密的旧数据
|
||
_ = json.Unmarshal([]byte(m.SegmentsEncrypted), &m.Segments)
|
||
} else {
|
||
_ = json.Unmarshal(pt, &m.Segments)
|
||
}
|
||
m.decrypted = true
|
||
}
|
||
}
|