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:
48
backend/go-service/app/constants/im.go
Normal file
48
backend/go-service/app/constants/im.go
Normal 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
|
||||
23
backend/go-service/app/im/model/conversation.go
Normal file
23
backend/go-service/app/im/model/conversation.go
Normal 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"
|
||||
}
|
||||
22
backend/go-service/app/im/model/conversation_member.go
Normal file
22
backend/go-service/app/im/model/conversation_member.go
Normal 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"
|
||||
}
|
||||
23
backend/go-service/app/im/model/message.go
Normal file
23
backend/go-service/app/im/model/message.go
Normal 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"
|
||||
}
|
||||
@@ -10,10 +10,11 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/echochat/backend/app/im/model"
|
||||
"github.com/echochat/backend/app/provider"
|
||||
"github.com/echochat/backend/config"
|
||||
"github.com/echochat/backend/pkg/logs"
|
||||
"github.com/echochat/backend/pkg/middleware"
|
||||
"github.com/echochat/backend/app/provider"
|
||||
"github.com/echochat/backend/router"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
@@ -50,13 +51,23 @@ func main() {
|
||||
logs.Fatal(ctx, "main", "初始化应用失败", zap.Error(err))
|
||||
}
|
||||
|
||||
// 4. 创建 Gin Engine
|
||||
// 4. IM 数据库表自动迁移(开发阶段使用,生产环境配合 init.sql)
|
||||
if err := app.DB.AutoMigrate(
|
||||
&model.Conversation{},
|
||||
&model.ConversationMember{},
|
||||
&model.Message{},
|
||||
); err != nil {
|
||||
logs.Fatal(ctx, "main", "IM 表迁移失败", zap.Error(err))
|
||||
}
|
||||
logs.Info(ctx, "main", "IM 表迁移完成")
|
||||
|
||||
// 5. 创建 Gin Engine
|
||||
if cfg.Server.Mode == "release" {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
engine := gin.New()
|
||||
|
||||
// 5. 注册中间件(顺序:Trace → Logger → CORS → Recovery)
|
||||
// 6. 注册中间件(顺序:Trace → Logger → CORS → Recovery)
|
||||
engine.Use(
|
||||
middleware.Trace(),
|
||||
middleware.Logger(),
|
||||
@@ -64,10 +75,10 @@ func main() {
|
||||
middleware.Recovery(),
|
||||
)
|
||||
|
||||
// 6. 注册路由(由 router.Setup 统一汇总各模块路由)
|
||||
// 7. 注册路由(由 router.Setup 统一汇总各模块路由)
|
||||
router.Setup(engine, app)
|
||||
|
||||
// 7. 启动 HTTP 服务(优雅关闭)
|
||||
// 8. 启动 HTTP 服务(优雅关闭)
|
||||
addr := fmt.Sprintf(":%d", cfg.Server.Port)
|
||||
srv := &http.Server{
|
||||
Addr: addr,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
-- ============================================================
|
||||
-- EchoChat 数据库初始化脚本
|
||||
-- 包含模块:auth(用户认证)、contact(联系人)
|
||||
-- 包含模块:auth(用户认证)、contact(联系人)、im(即时通讯)
|
||||
-- ============================================================
|
||||
|
||||
-- ============================================================
|
||||
@@ -143,3 +143,97 @@ COMMENT ON COLUMN contact_groups.sort_order IS '排序权重,数值越小越
|
||||
COMMENT ON COLUMN contact_groups.created_at IS '创建时间';
|
||||
|
||||
CREATE INDEX idx_contact_groups_user ON contact_groups (user_id);
|
||||
|
||||
-- ============================================================
|
||||
-- im_conversations: 会话表
|
||||
-- 存储单聊(type=1)和群聊(type=2,预留)会话
|
||||
-- 冗余 last_msg_* 字段以避免会话列表查询时 JOIN 消息表
|
||||
-- ============================================================
|
||||
CREATE TABLE im_conversations (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
type SMALLINT NOT NULL DEFAULT 1,
|
||||
creator_id BIGINT NOT NULL,
|
||||
last_message_id BIGINT DEFAULT NULL,
|
||||
last_msg_content TEXT DEFAULT '',
|
||||
last_msg_time TIMESTAMP(0) DEFAULT NULL,
|
||||
last_msg_sender_id BIGINT DEFAULT NULL,
|
||||
created_at TIMESTAMP(0) NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMP(0) NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
COMMENT ON TABLE im_conversations IS '会话表,支持单聊(type=1)和群聊(type=2,预留)';
|
||||
COMMENT ON COLUMN im_conversations.id IS '会话唯一标识,自增主键';
|
||||
COMMENT ON COLUMN im_conversations.type IS '会话类型:1=单聊,2=群聊(预留)';
|
||||
COMMENT ON COLUMN im_conversations.creator_id IS '会话创建者用户 ID';
|
||||
COMMENT ON COLUMN im_conversations.last_message_id IS '最后一条消息 ID,用于快速定位';
|
||||
COMMENT ON COLUMN im_conversations.last_msg_content IS '最后消息预览文本,用于会话列表展示';
|
||||
COMMENT ON COLUMN im_conversations.last_msg_time IS '最后消息时间,用于会话列表排序';
|
||||
COMMENT ON COLUMN im_conversations.last_msg_sender_id IS '最后消息发送者 ID';
|
||||
COMMENT ON COLUMN im_conversations.created_at IS '会话创建时间';
|
||||
COMMENT ON COLUMN im_conversations.updated_at IS '会话信息更新时间';
|
||||
|
||||
CREATE INDEX idx_im_conversations_updated ON im_conversations (updated_at DESC);
|
||||
|
||||
-- ============================================================
|
||||
-- im_conversation_members: 会话成员表
|
||||
-- 每个会话的每个成员一条记录,存储个人视图(置顶/未读/软删除)
|
||||
-- 单聊两条记录,群聊 N 条记录
|
||||
-- ============================================================
|
||||
CREATE TABLE im_conversation_members (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
conversation_id BIGINT NOT NULL REFERENCES im_conversations(id),
|
||||
user_id BIGINT NOT NULL,
|
||||
is_pinned BOOLEAN DEFAULT FALSE,
|
||||
is_deleted BOOLEAN DEFAULT FALSE,
|
||||
unread_count INT DEFAULT 0,
|
||||
last_read_msg_id BIGINT DEFAULT 0,
|
||||
created_at TIMESTAMP(0) NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMP(0) NOT NULL DEFAULT NOW(),
|
||||
UNIQUE (conversation_id, user_id)
|
||||
);
|
||||
|
||||
COMMENT ON TABLE im_conversation_members IS '会话成员表,存储每个成员对会话的个人视图';
|
||||
COMMENT ON COLUMN im_conversation_members.id IS '记录唯一标识';
|
||||
COMMENT ON COLUMN im_conversation_members.conversation_id IS '所属会话 ID';
|
||||
COMMENT ON COLUMN im_conversation_members.user_id IS '成员用户 ID';
|
||||
COMMENT ON COLUMN im_conversation_members.is_pinned IS '是否置顶该会话';
|
||||
COMMENT ON COLUMN im_conversation_members.is_deleted IS '是否删除该会话(软删除,不影响对方视图)';
|
||||
COMMENT ON COLUMN im_conversation_members.unread_count IS '该成员在此会话中的未读消息数';
|
||||
COMMENT ON COLUMN im_conversation_members.last_read_msg_id IS '该成员最后已读消息 ID';
|
||||
COMMENT ON COLUMN im_conversation_members.created_at IS '加入会话时间';
|
||||
COMMENT ON COLUMN im_conversation_members.updated_at IS '最后更新时间';
|
||||
|
||||
CREATE INDEX idx_im_conv_members_user ON im_conversation_members (user_id, is_deleted);
|
||||
CREATE INDEX idx_im_conv_members_conv ON im_conversation_members (conversation_id);
|
||||
|
||||
-- ============================================================
|
||||
-- im_messages: 消息表
|
||||
-- 存储所有聊天消息,通过 conversation_id 关联会话
|
||||
-- 游标分页查询核心索引:(conversation_id, created_at DESC)
|
||||
-- ============================================================
|
||||
CREATE TABLE im_messages (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
conversation_id BIGINT NOT NULL REFERENCES im_conversations(id),
|
||||
sender_id BIGINT NOT NULL,
|
||||
type SMALLINT NOT NULL DEFAULT 1,
|
||||
content TEXT NOT NULL,
|
||||
extra JSONB DEFAULT NULL,
|
||||
status SMALLINT NOT NULL DEFAULT 1,
|
||||
client_msg_id VARCHAR(64) DEFAULT '',
|
||||
created_at TIMESTAMP(0) NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
COMMENT ON TABLE im_messages IS '聊天消息表,存储所有消息内容';
|
||||
COMMENT ON COLUMN im_messages.id IS '消息唯一标识,自增主键';
|
||||
COMMENT ON COLUMN im_messages.conversation_id IS '所属会话 ID';
|
||||
COMMENT ON COLUMN im_messages.sender_id IS '发送者用户 ID';
|
||||
COMMENT ON COLUMN im_messages.type IS '消息类型:1=文本,2=图片(预留),3=语音(预留),4=视频(预留),5=文件(预留)';
|
||||
COMMENT ON COLUMN im_messages.content IS '消息内容(文本消息为纯文本,其他类型为 URL 或描述)';
|
||||
COMMENT ON COLUMN im_messages.extra IS '扩展数据(JSON 格式),预留图片尺寸、语音时长等元信息';
|
||||
COMMENT ON COLUMN im_messages.status IS '消息状态:1=正常,2=已撤回,3=已删除';
|
||||
COMMENT ON COLUMN im_messages.client_msg_id IS '客户端消息唯一 ID,用于幂等去重防止网络重试重复发送';
|
||||
COMMENT ON COLUMN im_messages.created_at IS '消息发送时间';
|
||||
|
||||
CREATE INDEX idx_im_messages_conv_time ON im_messages (conversation_id, created_at DESC);
|
||||
CREATE INDEX idx_im_messages_conv_id ON im_messages (conversation_id, id DESC);
|
||||
CREATE INDEX idx_im_messages_content_search ON im_messages USING gin(to_tsvector('simple', content));
|
||||
|
||||
Reference in New Issue
Block a user