feat(im): Task 0 - IM 模型定义 + 数据库表迁移 + 常量定义

- 新增 3 个 GORM 模型:Conversation / ConversationMember / Message
- 新增 IM 常量:会话类型、消息类型、消息状态、撤回时限
- main.go 添加 IM 表 AutoMigrate
- init.sql 追加 IM 模块 DDL(含索引和全文搜索 GIN 索引)

Made-with: Cursor
This commit is contained in:
bujinyuan
2026-03-03 10:41:42 +08:00
parent 1f390fce56
commit 6ab04fb902
6 changed files with 227 additions and 6 deletions

View File

@@ -0,0 +1,48 @@
package constants
// 会话类型
const (
ConversationTypePrivate = 1 // 单聊
ConversationTypeGroup = 2 // 群聊Phase 2c 预留)
)
// ConversationTypeMap 会话类型中文映射
var ConversationTypeMap = map[int]string{
ConversationTypePrivate: "单聊",
ConversationTypeGroup: "群聊",
}
// 消息类型
const (
MessageTypeText = 1 // 文本消息
MessageTypeImage = 2 // 图片消息(预留)
MessageTypeVoice = 3 // 语音消息(预留)
MessageTypeVideo = 4 // 视频消息(预留)
MessageTypeFile = 5 // 文件消息(预留)
)
// MessageTypeMap 消息类型中文映射
var MessageTypeMap = map[int]string{
MessageTypeText: "文本",
MessageTypeImage: "图片",
MessageTypeVoice: "语音",
MessageTypeVideo: "视频",
MessageTypeFile: "文件",
}
// 消息状态
const (
MessageStatusNormal = 1 // 正常
MessageStatusRecalled = 2 // 已撤回
MessageStatusDeleted = 3 // 已删除
)
// MessageStatusMap 消息状态中文映射
var MessageStatusMap = map[int]string{
MessageStatusNormal: "正常",
MessageStatusRecalled: "已撤回",
MessageStatusDeleted: "已删除",
}
// MessageRecallTimeLimit 消息撤回时限(秒),超过此时间不允许撤回
const MessageRecallTimeLimit = 2 * 60

View File

@@ -0,0 +1,23 @@
// Package model 定义 IM 模块的数据库模型
package model
import "time"
// Conversation 会话模型,对应 im_conversations 表
// Type: 1=单聊(预留 2=群聊)
type Conversation struct {
ID int64 `json:"id" gorm:"primaryKey;autoIncrement"` // 会话唯一标识,自增主键
Type int `json:"type" gorm:"not null;default:1"` // 会话类型1=单聊2=群聊(预留)
CreatorID int64 `json:"creator_id" gorm:"not null"` // 会话创建者用户 ID
LastMessageID *int64 `json:"last_message_id"` // 最后一条消息 ID用于快速定位
LastMsgContent string `json:"last_msg_content" gorm:"type:text;default:''"` // 最后消息预览文本,用于会话列表展示
LastMsgTime *time.Time `json:"last_msg_time" gorm:"type:timestamp(0)"` // 最后消息时间,用于排序
LastMsgSenderID *int64 `json:"last_msg_sender_id"` // 最后消息发送者 ID
CreatedAt time.Time `json:"created_at" gorm:"not null;autoCreateTime;type:timestamp(0)"` // 会话创建时间
UpdatedAt time.Time `json:"updated_at" gorm:"not null;autoUpdateTime;type:timestamp(0)"` // 会话更新时间
}
// TableName 指定数据库表名
func (Conversation) TableName() string {
return "im_conversations"
}

View File

@@ -0,0 +1,22 @@
package model
import "time"
// ConversationMember 会话成员模型,对应 im_conversation_members 表
// 每个会话的每个成员一条记录,存储置顶/未读/软删除等个人视图
type ConversationMember struct {
ID int64 `json:"id" gorm:"primaryKey;autoIncrement"` // 记录唯一标识
ConversationID int64 `json:"conversation_id" gorm:"not null;uniqueIndex:idx_conv_user"` // 所属会话 ID
UserID int64 `json:"user_id" gorm:"not null;uniqueIndex:idx_conv_user"` // 成员用户 ID
IsPinned bool `json:"is_pinned" gorm:"default:false"` // 是否置顶该会话
IsDeleted bool `json:"is_deleted" gorm:"default:false"` // 是否删除该会话(软删除,不影响对方)
UnreadCount int `json:"unread_count" gorm:"default:0"` // 该成员在此会话中的未读消息数
LastReadMsgID int64 `json:"last_read_msg_id" gorm:"default:0"` // 该成员最后已读消息 ID
CreatedAt time.Time `json:"created_at" gorm:"not null;autoCreateTime;type:timestamp(0)"` // 加入会话时间
UpdatedAt time.Time `json:"updated_at" gorm:"not null;autoUpdateTime;type:timestamp(0)"` // 最后更新时间
}
// TableName 指定数据库表名
func (ConversationMember) TableName() string {
return "im_conversation_members"
}

View File

@@ -0,0 +1,23 @@
package model
import "time"
// Message 消息模型,对应 im_messages 表
// Type: 1=文本(预留 2=图片 3=语音 4=视频 5=文件)
// Status: 1=正常 2=已撤回 3=已删除
type Message struct {
ID int64 `json:"id" gorm:"primaryKey;autoIncrement"` // 消息唯一标识,自增主键
ConversationID int64 `json:"conversation_id" gorm:"not null;index:idx_msg_conv_time;index:idx_msg_conv_id"` // 所属会话 ID
SenderID int64 `json:"sender_id" gorm:"not null"` // 发送者用户 ID
Type int `json:"type" gorm:"not null;default:1"` // 消息类型1=文本(预留扩展)
Content string `json:"content" gorm:"type:text;not null"` // 消息内容
Extra *string `json:"extra" gorm:"type:jsonb"` // 扩展数据JSON 格式,预留图片/语音等元信息)
Status int `json:"status" gorm:"not null;default:1"` // 消息状态1=正常2=已撤回3=已删除
ClientMsgID string `json:"client_msg_id" gorm:"size:64;default:''"` // 客户端消息唯一 ID用于幂等去重
CreatedAt time.Time `json:"created_at" gorm:"not null;autoCreateTime;type:timestamp(0)"` // 消息发送时间
}
// TableName 指定数据库表名
func (Message) TableName() string {
return "im_messages"
}