62 lines
1.9 KiB
Go
62 lines
1.9 KiB
Go
package automation
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"sync"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"github.com/gochat/gochat/internal/worker"
|
|
)
|
|
|
|
type macroExecutionJob struct {
|
|
AccountID uint `json:"account_id"`
|
|
MacroID uint `json:"macro_id"`
|
|
ConversationIDs []uint `json:"conversation_ids"`
|
|
UserID uint `json:"user_id"`
|
|
}
|
|
|
|
var macroExecutionRegistrations sync.Map
|
|
|
|
// RegisterMacroExecutionJobs wires Chatwoot's MacrosExecutionJob equivalent.
|
|
// The job resolves account-scoped conversation display IDs at perform time.
|
|
func RegisterMacroExecutionJobs(wp *worker.WorkerPool, db DBProvider) {
|
|
registerMacroExecutionJobs(wp, db, nil)
|
|
}
|
|
|
|
func RegisterMacroExecutionSearchIndexer(wp *worker.WorkerPool, db DBProvider, indexer ActionSearchIndexer) {
|
|
registerMacroExecutionJobs(wp, db, indexer)
|
|
}
|
|
|
|
func registerMacroExecutionJobs(wp *worker.WorkerPool, db DBProvider, indexer ActionSearchIndexer) {
|
|
if wp == nil || db == nil {
|
|
return
|
|
}
|
|
runner := ¯oExecutionJobRunner{db: db, worker: wp, searchIndexer: indexer}
|
|
actual, loaded := macroExecutionRegistrations.LoadOrStore(wp, runner)
|
|
if loaded {
|
|
if indexer != nil {
|
|
actual.(*macroExecutionJobRunner).searchIndexer = indexer
|
|
}
|
|
return
|
|
}
|
|
wp.Register(TaskTypeMacroExecution, runner.perform)
|
|
}
|
|
|
|
type macroExecutionJobRunner struct {
|
|
db DBProvider
|
|
worker *worker.WorkerPool
|
|
searchIndexer ActionSearchIndexer
|
|
}
|
|
|
|
func (r *macroExecutionJobRunner) perform(ctx context.Context, job *model.BackgroundJob) error {
|
|
var payload macroExecutionJob
|
|
if err := json.Unmarshal(job.Payload, &payload); err != nil {
|
|
return fmt.Errorf("unmarshal macro execution job: %w", err)
|
|
}
|
|
svc := NewMacroServiceWithWorker(r.db, r.worker)
|
|
svc.SetSearchIndexer(r.searchIndexer)
|
|
return svc.executeForDisplayIDsNow(ctx, payload.AccountID, payload.MacroID, payload.ConversationIDs, payload.UserID)
|
|
}
|