Skip to content

Commit

Permalink
add setting for history sync
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielolivrp committed Jan 24, 2024
1 parent 629bdc3 commit 9037ceb
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions api/service/whatsapp_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ func main() {
}
}()

go historySyncWorker.ProcessQueue()
if cfg.HistorySync {
go historySyncWorker.ProcessQueue()
}

<-*app.StopCh
app.Wg.Wait()
Expand Down
64 changes: 40 additions & 24 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package config

import (
"log"
"os"
"strconv"
)

type Environment = uint
Expand All @@ -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,
}
}

Expand Down
4 changes: 2 additions & 2 deletions worker/history_sync_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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 {
Expand Down

0 comments on commit 9037ceb

Please sign in to comment.