Files
backend/internal/model/conversation.go
lafay 2f584c1f2c
All checks were successful
Build Backend / build (push) Successful in 5m10s
Build Backend / build-docker (push) Successful in 2m22s
This is a breaking change that renames the entire project from "Carrot BBS" to "WithYou".
The Go module name has been changed from `carrot_bbs` to `with_you`, which requires
all import paths to be updated throughout the codebase and by external consumers.

BREAKING CHANGE: Module name changed from carrot_bbs to with_you. All imports
and dependencies must be updated from carrot_bbs/* to with_you/*.
2026-04-22 16:01:59 +08:00

69 lines
3.9 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 model
import (
"strconv"
"time"
"gorm.io/gorm"
"with_you/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"` // 雪花算法IDstring类型
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"
}