Browse Source

fix: bug with merge

pull/254/head
almogbaku 1 year ago
parent
commit
f475b7b8f0
No known key found for this signature in database GPG Key ID: 66C92B1C5B475512
  1. 18
      src/cmd/root.go
  2. 47
      src/pkg/whatsapp/init.go

18
src/cmd/root.go

@ -3,8 +3,6 @@ package cmd
import (
"embed"
"fmt"
pkgError "github.com/aldinokemal/go-whatsapp-web-multidevice/pkg/error"
waLog "go.mau.fi/whatsmeow/util/log"
"log"
"net/http"
"os"
@ -241,20 +239,8 @@ func runRest(_ *cobra.Command, _ []string) {
}))
}
waLogger := waLog.Stdout("Main", config.WhatsappLogLevel, true)
dbLogger := waLog.Stdout("Database", config.WhatsappLogLevel, true)
db, err := whatsapp.InitDatabase(config.DBURI, dbLogger)
if err != nil {
waLogger.Errorf("Database initialization error: %v", err)
panic(pkgError.InternalServerError(fmt.Sprintf("Database initialization error: %v", err)))
}
dbKeys, err := whatsapp.InitDatabase(config.DBKeysURI, dbLogger)
if err != nil {
waLogger.Errorf("Database initialization error: %v", err)
panic(pkgError.InternalServerError(fmt.Sprintf("Database initialization error: %v", err)))
}
db := whatsapp.InitWaDB(config.DBURI)
dbKeys := whatsapp.InitWaDB(config.DBKeysURI)
cli := whatsapp.InitWaCLI(db, dbKeys)
// Service

47
src/pkg/whatsapp/init.go

@ -11,6 +11,7 @@ import (
"github.com/aldinokemal/go-whatsapp-web-multidevice/config"
"github.com/aldinokemal/go-whatsapp-web-multidevice/internal/websocket"
pkgError "github.com/aldinokemal/go-whatsapp-web-multidevice/pkg/error"
"github.com/aldinokemal/go-whatsapp-web-multidevice/pkg/utils"
"github.com/sirupsen/logrus"
"go.mau.fi/whatsmeow"
@ -52,15 +53,29 @@ var (
startupTime = time.Now().Unix()
)
// InitDatabase creates and returns a database store container based on the configured URI
func InitDatabase(dbUri string, dbLog waLog.Logger) (*sqlstore.Container, error) {
if strings.HasPrefix(dbUri, "file:") {
return sqlstore.New("sqlite3", dbUri, dbLog)
} else if strings.HasPrefix(dbUri, "postgres:") {
return sqlstore.New("postgres", dbUri, dbLog)
// InitWaDB initializes the WhatsApp database connection
func InitWaDB(DBURI string) *sqlstore.Container {
log = waLog.Stdout("Main", config.WhatsappLogLevel, true)
dbLog := waLog.Stdout("Database", config.WhatsappLogLevel, true)
storeContainer, err := initDatabase(dbLog, DBURI)
if err != nil {
log.Errorf("Database initialization error: %v", err)
panic(pkgError.InternalServerError(fmt.Sprintf("Database initialization error: %v", err)))
}
return nil, fmt.Errorf("unknown database type: %s. Currently only sqlite3(file:) and postgres are supported", dbUri)
return storeContainer
}
// initDatabase creates and returns a database store container based on the configured URI
func initDatabase(dbLog waLog.Logger, DBURI string) (*sqlstore.Container, error) {
if strings.HasPrefix(DBURI, "file:") {
return sqlstore.New("sqlite3", DBURI, dbLog)
} else if strings.HasPrefix(DBURI, "postgres:") {
return sqlstore.New("postgres", DBURI, dbLog)
}
return nil, fmt.Errorf("unknown database type: %s. Currently only sqlite3(file:) and postgres are supported", DBURI)
}
// InitWaCLI initializes the WhatsApp client
@ -82,15 +97,15 @@ func InitWaCLI(storeContainer, keysStoreContainer *sqlstore.Container) *whatsmeo
store.DeviceProps.Os = &osName
// Configure a separated database for accelerating encryption caching
if keysStoreContainer != nil && device.ID != nil {
innerStore := sqlstore.NewSQLStore(keysStoreContainer, *device.ID)
device.Identities = innerStore
device.Sessions = innerStore
device.PreKeys = innerStore
device.SenderKeys = innerStore
device.MsgSecrets = innerStore
device.PrivacyTokens = innerStore
}
//if keysStoreContainer != nil && device.ID != nil {
// innerStore := sqlstore.NewSQLStore(keysStoreContainer, *device.ID)
// device.Identities = innerStore
// device.Sessions = innerStore
// device.PreKeys = innerStore
// device.SenderKeys = innerStore
// device.MsgSecrets = innerStore
// device.PrivacyTokens = innerStore
//}
// Create and configure the client
cli = whatsmeow.NewClient(device, waLog.Stdout("Client", config.WhatsappLogLevel, true))

Loading…
Cancel
Save