Files
backend/internal/model/conversation.go
lan ee78071d4d
All checks were successful
Build Backend / build (push) Successful in 3m2s
Build Backend / build-docker (push) Successful in 2m44s
refactor: improve system stability, performance, and code structure
This commit introduces several architectural improvements and optimizations across the codebase:

- **Performance & Reliability**:
  - Implemented Redis pipelining in `ConversationCache.CacheMessage` to reduce network round-trips.
  - Added a circuit breaker to the JPush client to prevent cascading failures.
  - Introduced batch deletion and batch member addition capabilities in repositories.
  - Added message idempotency support using `client_msg_id` and a Redis-based cache.
  - Optimized WebSocket handling with connection limits (total and per-user) and improved error logging.

- **Code Refactoring**:
  - Refactored `Router` to use a `RouterDeps` struct, simplifying the constructor and improving maintainability.
  - Unified model ID generation logic using new `id_helper.go` (supporting UUID and Snowflake).
  - Standardized JSON serialization/deserialization in models using `json_helper.go`.
  - Refactored DTO conversion logic, specifically for `UserResponse` (using functional options) and `Report` responses.
  - Removed redundant/deprecated DTOs like `PostDetailResponse` and `TradeItemDetailResponse`.

- **Cache Improvements**:
  - Enhanced `LayeredCache` with `SetRaw` to avoid double-encoding when promoting values from Redis to local cache.
  - Added `DeleteBatch` support to the cache interface.

- **Other Changes**:
  - Cleaned up `config.go` by removing redundant default values and explicit environment variable overrides.
  - Improved WebSocket registration flow to handle connection limits gracefully.
2026-05-04 13:07:03 +08:00

59 lines
3.7 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 (
"time"
"gorm.io/gorm"
)
// 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 {
return SetSnowflakeStringID(&c.ID)
}
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位置
NotificationMuted bool `gorm:"column:muted;default:false" json:"notification_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"
}