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.
76 lines
2.3 KiB
Go
76 lines
2.3 KiB
Go
package repository
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"with_you/internal/model"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type GroupJoinRequestRepository interface {
|
|
Create(req *model.GroupJoinRequest) error
|
|
GetByFlag(flag string) (*model.GroupJoinRequest, error)
|
|
Update(req *model.GroupJoinRequest) error
|
|
GetPendingByGroupAndTarget(groupID, targetUserID string, reqType model.GroupJoinRequestType) (*model.GroupJoinRequest, error)
|
|
GetPendingByGroupAndTargets(groupID string, targetUserIDs []string, reqType model.GroupJoinRequestType) (map[string]bool, error)
|
|
}
|
|
|
|
type groupJoinRequestRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewGroupJoinRequestRepository(db *gorm.DB) GroupJoinRequestRepository {
|
|
return &groupJoinRequestRepository{db: db}
|
|
}
|
|
|
|
func (r *groupJoinRequestRepository) Create(req *model.GroupJoinRequest) error {
|
|
return r.db.Create(req).Error
|
|
}
|
|
|
|
func (r *groupJoinRequestRepository) GetByFlag(flag string) (*model.GroupJoinRequest, error) {
|
|
var req model.GroupJoinRequest
|
|
if err := r.db.Where("flag = ?", flag).First(&req).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &req, nil
|
|
}
|
|
|
|
func (r *groupJoinRequestRepository) Update(req *model.GroupJoinRequest) error {
|
|
return r.db.Save(req).Error
|
|
}
|
|
|
|
func (r *groupJoinRequestRepository) GetPendingByGroupAndTarget(groupID, targetUserID string, reqType model.GroupJoinRequestType) (*model.GroupJoinRequest, error) {
|
|
var req model.GroupJoinRequest
|
|
err := r.db.Where("group_id = ? AND target_user_id = ? AND request_type = ? AND status = ?",
|
|
groupID, targetUserID, reqType, model.GroupJoinRequestStatusPending).
|
|
Order("created_at DESC").
|
|
First(&req).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &req, nil
|
|
}
|
|
|
|
func (r *groupJoinRequestRepository) GetPendingByGroupAndTargets(groupID string, targetUserIDs []string, reqType model.GroupJoinRequestType) (map[string]bool, error) {
|
|
result := make(map[string]bool)
|
|
if len(targetUserIDs) == 0 {
|
|
return result, nil
|
|
}
|
|
var existingIDs []string
|
|
err := r.db.Model(&model.GroupJoinRequest{}).
|
|
Where("group_id = ? AND target_user_id IN ? AND request_type = ? AND status = ?",
|
|
groupID, targetUserIDs, reqType, model.GroupJoinRequestStatusPending).
|
|
Pluck("target_user_id", &existingIDs).Error
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return result, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
for _, id := range existingIDs {
|
|
result[id] = true
|
|
}
|
|
return result, nil
|
|
}
|