89 lines
1.9 KiB
Go
89 lines
1.9 KiB
Go
package chat
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
"go.uber.org/zap"
|
|
"gorm.io/gorm"
|
|
"hfb_jysh/backend/internal/modules/chathub"
|
|
)
|
|
|
|
type Repository struct {
|
|
db *gorm.DB
|
|
hub *chathub.Hub
|
|
redis *redis.Client
|
|
logger *zap.Logger
|
|
ocrHTTPClient *http.Client
|
|
ocrGate *ocrRequestGate
|
|
ocrSleep func(context.Context, time.Duration) error
|
|
}
|
|
|
|
const (
|
|
defaultSupportRoleCode = "cs"
|
|
)
|
|
|
|
type RepositoryOption func(*Repository)
|
|
|
|
func WithLogger(logger *zap.Logger) RepositoryOption {
|
|
return func(repo *Repository) {
|
|
if logger != nil {
|
|
repo.logger = logger
|
|
}
|
|
}
|
|
}
|
|
|
|
func withOCRHTTPClient(client *http.Client) RepositoryOption {
|
|
return func(repo *Repository) {
|
|
if client != nil {
|
|
repo.ocrHTTPClient = client
|
|
}
|
|
}
|
|
}
|
|
|
|
func withOCRGate(gate *ocrRequestGate) RepositoryOption {
|
|
return func(repo *Repository) {
|
|
if gate != nil {
|
|
repo.ocrGate = gate
|
|
}
|
|
}
|
|
}
|
|
|
|
func withOCRSleep(sleep func(context.Context, time.Duration) error) RepositoryOption {
|
|
return func(repo *Repository) {
|
|
if sleep != nil {
|
|
repo.ocrSleep = sleep
|
|
}
|
|
}
|
|
}
|
|
|
|
func NewRepository(db *gorm.DB, hub *chathub.Hub, redis *redis.Client, options ...RepositoryOption) *Repository {
|
|
repo := &Repository{
|
|
db: db,
|
|
hub: hub,
|
|
redis: redis,
|
|
logger: zap.NewNop(),
|
|
ocrHTTPClient: ocrHTTPClient,
|
|
ocrGate: sharedOCRRequestGate,
|
|
ocrSleep: sleepWithContext,
|
|
}
|
|
for _, option := range options {
|
|
option(repo)
|
|
}
|
|
return repo
|
|
}
|
|
func (r *Repository) NotifyNewConversation(conversationID uint64) {
|
|
if r.hub == nil {
|
|
return
|
|
}
|
|
event := &chathub.ChatEvent{
|
|
Type: "conversation_updated",
|
|
ConversationID: conversationID,
|
|
}
|
|
r.hub.NotifyConversation(conversationID, event)
|
|
// 新会话(如未分配的平台咨询)需让所有在线客服的列表实时出现。
|
|
r.hub.NotifyAllAdmins(event)
|
|
}
|