diff --git a/.env.example b/.env.example index 9a5df51..c73e767 100644 --- a/.env.example +++ b/.env.example @@ -5,3 +5,5 @@ REDIS_PASSWORD= DATABASE_PATH=.zapmeow/zapmeow.db STORAGE_PATH=.zapmeow/storage WEBHOOK_URL=http://localhost:3000/api/whatsapp/message +HISTORY_SYNC=true +MAX_MESSAGE_SYNC=10 diff --git a/api/service/whatsapp_service.go b/api/service/whatsapp_service.go index ad09e22..2adb41e 100644 --- a/api/service/whatsapp_service.go +++ b/api/service/whatsapp_service.go @@ -240,6 +240,9 @@ func (w *whatsAppService) eventHandler(instanceID string, rawEvt interface{}) { } func (w *whatsAppService) handleHistorySync(instanceID string, evt *events.HistorySync) { + if !w.app.Config.HistorySync { + return + } history, _ := proto.Marshal(evt.Data) q := queue.NewHistorySyncQueue(w.app) diff --git a/cmd/server/main.go b/cmd/server/main.go index 7e0d524..af9f0a6 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -118,7 +118,9 @@ func main() { } }() - go historySyncWorker.ProcessQueue() + if cfg.HistorySync { + go historySyncWorker.ProcessQueue() + } <-*app.StopCh app.Wg.Wait() diff --git a/config/config.go b/config/config.go index 746f88d..bebbe3d 100644 --- a/config/config.go +++ b/config/config.go @@ -1,7 +1,9 @@ package config import ( + "log" "os" + "strconv" ) type Environment = uint @@ -12,36 +14,50 @@ const ( ) type Config struct { - Environment Environment - StoragePath string - WebhookURL string - DatabaseURL string - RedisAddr string - RedisPassword string - Port string - HistorySyncQueueName string - MaxMessagesForChatSync int + Environment Environment + StoragePath string + WebhookURL string + DatabaseURL string + RedisAddr string + RedisPassword string + Port string + HistorySyncQueueName string + HistorySync bool + MaxMessageSync int } func Load() Config { - storagePath := os.Getenv("STORAGE_PATH") - webhookURL := os.Getenv("WEBHOOK_URL") - databaseURL := os.Getenv("DATABASE_PATH") - redisAddr := os.Getenv("REDIS_ADDR") - redisPassword := os.Getenv("REDIS_PASSWORD") - port := os.Getenv("PORT") + storagePathEnv := os.Getenv("STORAGE_PATH") + webhookURLEnv := os.Getenv("WEBHOOK_URL") + databaseURLEnv := os.Getenv("DATABASE_PATH") + redisAddrEnv := os.Getenv("REDIS_ADDR") + redisPasswordEnv := os.Getenv("REDIS_PASSWORD") + portEnv := os.Getenv("PORT") + historySyncEnv := os.Getenv("HISTORY_SYNC") + maxMessageSyncEnv := os.Getenv("MAX_MESSAGE_SYNC") environment := getEnvironment() + maxMessageSync, err := strconv.Atoi(maxMessageSyncEnv) + if err != nil { + maxMessageSync = 10 + } + + historySync, err := strconv.ParseBool(historySyncEnv) + if err != nil { + log.Fatal(err) + } + return Config{ - Environment: environment, - StoragePath: storagePath, - WebhookURL: webhookURL, - DatabaseURL: databaseURL, - RedisAddr: redisAddr, - RedisPassword: redisPassword, - Port: port, - HistorySyncQueueName: "queue:history-sync", - MaxMessagesForChatSync: 10, + Environment: environment, + StoragePath: storagePathEnv, + WebhookURL: webhookURLEnv, + DatabaseURL: databaseURLEnv, + RedisAddr: redisAddrEnv, + RedisPassword: redisPasswordEnv, + Port: portEnv, + HistorySyncQueueName: "queue:history-sync", + HistorySync: historySync, + MaxMessageSync: maxMessageSync, } } diff --git a/worker/history_sync_worker.go b/worker/history_sync_worker.go index 774bf14..0de65ed 100644 --- a/worker/history_sync_worker.go +++ b/worker/history_sync_worker.go @@ -127,7 +127,7 @@ func (q *historySyncWorker) processMessages(evt *waProto.HistorySync, account *m return nil, err } - if count > int64(q.app.Config.MaxMessagesForChatSync) { + if count > int64(q.app.Config.MaxMessageSync) { continue } @@ -145,7 +145,7 @@ func (q *historySyncWorker) processMessages(evt *waProto.HistorySync, account *m return eventsMessage[i].Info.Timestamp.After(eventsMessage[j].Info.Timestamp) }) - maxMessages := helper.Min(q.app.Config.MaxMessagesForChatSync, len(eventsMessage)) + maxMessages := helper.Min(q.app.Config.MaxMessageSync, len(eventsMessage)) slice := eventsMessage[:maxMessages] for _, evtMessage := range slice {