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.
104 lines
4.1 KiB
Go
104 lines
4.1 KiB
Go
package model
|
||
|
||
import (
|
||
"time"
|
||
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
// CallType 通话类型
|
||
type CallType string
|
||
|
||
const (
|
||
CallTypePrivate CallType = "private" // 私聊通话
|
||
CallTypeGroup CallType = "group" // 群聊通话
|
||
)
|
||
|
||
// CallStatus 通话状态
|
||
type CallStatus string
|
||
|
||
const (
|
||
CallStatusCalling CallStatus = "calling" // 呼叫中
|
||
CallStatusConnected CallStatus = "connected" // 已接通
|
||
CallStatusEnded CallStatus = "ended" // 已结束
|
||
CallStatusRejected CallStatus = "rejected" // 已拒绝
|
||
CallStatusMissed CallStatus = "missed" // 未接听
|
||
CallStatusCancelled CallStatus = "cancelled" // 已取消
|
||
)
|
||
|
||
// ParticipantStatus 参与者状态
|
||
type ParticipantStatus string
|
||
|
||
const (
|
||
ParticipantStatusInvited ParticipantStatus = "invited" // 已邀请
|
||
ParticipantStatusJoined ParticipantStatus = "joined" // 已加入
|
||
ParticipantStatusLeft ParticipantStatus = "left" // 已离开
|
||
ParticipantStatusRejected ParticipantStatus = "rejected" // 已拒绝
|
||
)
|
||
|
||
// CallSession 通话会话
|
||
type CallSession struct {
|
||
ID string `gorm:"primaryKey;size:20" json:"id"` // 雪花算法ID
|
||
ConversationID string `gorm:"not null;size:20;index" json:"conversation_id"` // 会话ID
|
||
GroupID *string `gorm:"index" json:"group_id,omitempty"` // 群组ID(群聊时使用)
|
||
CallerID string `gorm:"column:caller_id;type:varchar(50);index;not null" json:"caller_id"` // 发起者ID
|
||
CallType CallType `gorm:"type:varchar(20);not null" json:"call_type"` // 通话类型
|
||
Status CallStatus `gorm:"type:varchar(20);default:'calling'" json:"status"` // 通话状态
|
||
StartedAt *time.Time `json:"started_at,omitempty"` // 接通时间
|
||
EndedAt *time.Time `json:"ended_at,omitempty"` // 结束时间
|
||
Duration int64 `gorm:"default:0" json:"duration"` // 通话时长(秒)
|
||
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
|
||
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
|
||
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
|
||
|
||
// 关联
|
||
Participants []CallParticipant `gorm:"foreignKey:CallID" json:"participants,omitempty"`
|
||
}
|
||
|
||
// BeforeCreate 创建前生成雪花算法ID
|
||
func (c *CallSession) BeforeCreate(tx *gorm.DB) error {
|
||
return SetSnowflakeStringID(&c.ID)
|
||
}
|
||
|
||
func (CallSession) TableName() string {
|
||
return "call_sessions"
|
||
}
|
||
|
||
// CallParticipant 通话参与者
|
||
type CallParticipant struct {
|
||
ID uint `gorm:"primaryKey" json:"id"`
|
||
CallID string `gorm:"not null;size:20;index" json:"call_id"` // 通话ID
|
||
UserID string `gorm:"column:user_id;type:varchar(50);index;not null" json:"user_id"` // 用户ID
|
||
Status ParticipantStatus `gorm:"type:varchar(20);default:'invited'" json:"status"` // 参与状态
|
||
JoinedAt *time.Time `json:"joined_at,omitempty"` // 加入时间
|
||
LeftAt *time.Time `json:"left_at,omitempty"` // 离开时间
|
||
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
|
||
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
|
||
}
|
||
|
||
func (CallParticipant) TableName() string {
|
||
return "call_participants"
|
||
}
|
||
|
||
// IsPrivate 是否私聊通话
|
||
func (c *CallSession) IsPrivate() bool {
|
||
return c.CallType == CallTypePrivate
|
||
}
|
||
|
||
// IsGroup 是否群聊通话
|
||
func (c *CallSession) IsGroup() bool {
|
||
return c.CallType == CallTypeGroup
|
||
}
|
||
|
||
// IsActive 是否活跃通话
|
||
func (c *CallSession) IsActive() bool {
|
||
return c.Status == CallStatusCalling || c.Status == CallStatusConnected
|
||
}
|
||
|
||
// CalculateDuration 计算通话时长
|
||
func (c *CallSession) CalculateDuration() {
|
||
if c.StartedAt != nil && c.EndedAt != nil {
|
||
c.Duration = int64(c.EndedAt.Sub(*c.StartedAt).Seconds())
|
||
}
|
||
}
|