69 lines
3.9 KiB
Go
69 lines
3.9 KiB
Go
|
|
package model
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"strconv"
|
|||
|
|
"time"
|
|||
|
|
|
|||
|
|
"gorm.io/gorm"
|
|||
|
|
|
|||
|
|
"carrot_bbs/internal/pkg/utils"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// ConversationType 会话类型
|
|||
|
|
type ConversationType string
|
|||
|
|
|
|||
|
|
const (
|
|||
|
|
ConversationTypePrivate ConversationType = "private" // 私聊
|
|||
|
|
ConversationTypeGroup ConversationType = "group" // 群聊
|
|||
|
|
ConversationTypeSystem ConversationType = "system" // 系统通知会话
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// Conversation 会话实体
|
|||
|
|
// 使用雪花算法ID(作为string存储)和seq机制实现消息排序和增量同步
|
|||
|
|
type Conversation struct {
|
|||
|
|
ID string `gorm:"primaryKey;size:20" json:"id"` // 雪花算法ID(转为string避免精度丢失)
|
|||
|
|
Type ConversationType `gorm:"type:varchar(20);default:'private'" json:"type"` // 会话类型
|
|||
|
|
GroupID *string `gorm:"index" json:"group_id,omitempty"` // 关联的群组ID(群聊时使用,string类型避免JS精度丢失);使用指针支持NULL值
|
|||
|
|
LastSeq int64 `gorm:"default:0" json:"last_seq"` // 最后一条消息的seq
|
|||
|
|
LastMsgTime *time.Time `json:"last_msg_time,omitempty"` // 最后消息时间
|
|||
|
|
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
|
|||
|
|
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
|
|||
|
|
|
|||
|
|
// 关联 - 使用 polymorphic 模式避免外键约束问题
|
|||
|
|
Participants []ConversationParticipant `gorm:"foreignKey:ConversationID" json:"participants,omitempty"`
|
|||
|
|
Group *Group `gorm:"foreignKey:GroupID;references:ID" json:"group,omitempty"`
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// BeforeCreate 创建前生成雪花算法ID
|
|||
|
|
func (c *Conversation) BeforeCreate(tx *gorm.DB) error {
|
|||
|
|
if c.ID == "" {
|
|||
|
|
id, err := utils.GetSnowflake().GenerateID()
|
|||
|
|
if err != nil {
|
|||
|
|
return err
|
|||
|
|
}
|
|||
|
|
c.ID = strconv.FormatInt(id, 10)
|
|||
|
|
}
|
|||
|
|
return nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (Conversation) TableName() string {
|
|||
|
|
return "conversations"
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ConversationParticipant 会话参与者
|
|||
|
|
type ConversationParticipant struct {
|
|||
|
|
ID uint `gorm:"primaryKey" json:"id"`
|
|||
|
|
ConversationID string `gorm:"not null;size:20;uniqueIndex:idx_conversation_user,priority:1;index:idx_cp_conversation_user,priority:1" json:"conversation_id"` // 雪花算法ID(string类型)
|
|||
|
|
UserID string `gorm:"column:user_id;type:varchar(50);not null;uniqueIndex:idx_conversation_user,priority:2;index:idx_cp_conversation_user,priority:2;index:idx_cp_user_hidden_pinned_updated,priority:1" json:"user_id"` // UUID格式,与JWT中user_id保持一致
|
|||
|
|
LastReadSeq int64 `gorm:"default:0" json:"last_read_seq"` // 已读到的seq位置
|
|||
|
|
Muted bool `gorm:"default:false" json:"muted"` // 是否免打扰
|
|||
|
|
IsPinned bool `gorm:"default:false;index:idx_cp_user_hidden_pinned_updated,priority:3" json:"is_pinned"` // 是否置顶会话(用户维度)
|
|||
|
|
HiddenAt *time.Time `gorm:"index:idx_cp_user_hidden_pinned_updated,priority:2" json:"hidden_at,omitempty"` // 仅自己删除会话时使用,收到新消息后自动恢复
|
|||
|
|
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
|
|||
|
|
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime;index:idx_cp_user_hidden_pinned_updated,priority:4,sort:desc"`
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (ConversationParticipant) TableName() string {
|
|||
|
|
return "conversation_participants"
|
|||
|
|
}
|