2020-07-03 10:08:47 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2021-02-17 23:14:48 +00:00
|
|
|
"encoding/json"
|
2020-07-22 07:41:40 +00:00
|
|
|
|
2022-01-17 03:42:11 +00:00
|
|
|
"github.com/status-im/status-go/services/browsers"
|
|
|
|
|
2020-07-22 07:41:40 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2021-02-15 23:18:08 +00:00
|
|
|
"github.com/status-im/status-go/appdatabase/migrations"
|
2020-11-24 23:16:19 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts"
|
2021-02-17 23:14:48 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
|
|
|
"github.com/status-im/status-go/params"
|
2021-09-01 12:02:18 +00:00
|
|
|
"github.com/status-im/status-go/protocol/anonmetrics"
|
2020-07-06 08:54:22 +00:00
|
|
|
"github.com/status-im/status-go/protocol/common"
|
2021-04-19 12:09:46 +00:00
|
|
|
"github.com/status-im/status-go/protocol/communities"
|
2020-07-03 10:08:47 +00:00
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2020-07-22 07:41:40 +00:00
|
|
|
"github.com/status-im/status-go/protocol/pushnotificationclient"
|
|
|
|
"github.com/status-im/status-go/protocol/pushnotificationserver"
|
2020-07-03 10:08:47 +00:00
|
|
|
"github.com/status-im/status-go/protocol/transport"
|
2021-01-14 22:15:13 +00:00
|
|
|
"github.com/status-im/status-go/services/mailservers"
|
2020-07-03 10:08:47 +00:00
|
|
|
)
|
|
|
|
|
2021-02-23 15:47:45 +00:00
|
|
|
type MessageDeliveredHandler func(string, string)
|
|
|
|
|
2021-04-19 12:09:46 +00:00
|
|
|
type MessengerSignalsHandler interface {
|
|
|
|
MessageDelivered(chatID string, messageID string)
|
|
|
|
CommunityInfoFound(community *communities.Community)
|
2021-03-25 15:15:22 +00:00
|
|
|
MessengerResponse(response *MessengerResponse)
|
2021-11-09 21:16:19 +00:00
|
|
|
HistoryRequestStarted(requestID string, numBatches int)
|
|
|
|
HistoryRequestBatchProcessed(requestID string, batchIndex int, batchNum int)
|
|
|
|
HistoryRequestCompleted(requestID string)
|
|
|
|
HistoryRequestFailed(requestID string, err error)
|
2021-10-11 15:39:52 +00:00
|
|
|
BackupPerformed(uint64)
|
2021-04-19 12:09:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 10:08:47 +00:00
|
|
|
type config struct {
|
|
|
|
// This needs to be exposed until we move here mailserver logic
|
|
|
|
// as otherwise the client is not notified of a new filter and
|
|
|
|
// won't be pulling messages from mailservers until it reloads the chats/filters
|
2021-01-11 10:32:51 +00:00
|
|
|
onContactENSVerified func(*MessengerResponse)
|
2020-07-03 10:08:47 +00:00
|
|
|
|
|
|
|
// systemMessagesTranslations holds translations for system-messages
|
2021-03-29 15:41:30 +00:00
|
|
|
systemMessagesTranslations *systemMessageTranslationsMap
|
2020-07-03 10:08:47 +00:00
|
|
|
// Config for the envelopes monitor
|
|
|
|
envelopesMonitorConfig *transport.EnvelopesMonitorConfig
|
|
|
|
|
2020-12-21 08:41:50 +00:00
|
|
|
featureFlags common.FeatureFlags
|
2020-07-03 10:08:47 +00:00
|
|
|
|
|
|
|
// A path to a database or a database instance is required.
|
|
|
|
// The database instance has a higher priority.
|
2021-01-14 22:15:13 +00:00
|
|
|
dbConfig dbConfig
|
|
|
|
db *sql.DB
|
2021-02-15 23:18:08 +00:00
|
|
|
afterDbCreatedHooks []Option
|
2021-01-14 22:15:13 +00:00
|
|
|
multiAccount *multiaccounts.Database
|
|
|
|
mailserversDatabase *mailservers.Database
|
|
|
|
account *multiaccounts.Account
|
2022-01-12 16:02:01 +00:00
|
|
|
clusterConfig params.ClusterConfig
|
2022-01-17 03:42:11 +00:00
|
|
|
browserDatabase *browsers.Database
|
2020-07-03 10:08:47 +00:00
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
verifyTransactionClient EthClient
|
|
|
|
verifyENSURL string
|
|
|
|
verifyENSContractAddress string
|
2020-07-03 10:08:47 +00:00
|
|
|
|
2021-09-01 12:02:18 +00:00
|
|
|
anonMetricsClientConfig *anonmetrics.ClientConfig
|
|
|
|
anonMetricsServerConfig *anonmetrics.ServerConfig
|
|
|
|
|
2020-07-22 07:41:40 +00:00
|
|
|
pushNotificationServerConfig *pushnotificationserver.Config
|
|
|
|
pushNotificationClientConfig *pushnotificationclient.Config
|
2020-07-03 10:08:47 +00:00
|
|
|
|
|
|
|
logger *zap.Logger
|
2021-02-23 15:47:45 +00:00
|
|
|
|
2021-04-19 12:09:46 +00:00
|
|
|
messengerSignalsHandler MessengerSignalsHandler
|
2021-11-03 12:38:37 +00:00
|
|
|
|
|
|
|
telemetryServerURL string
|
2020-07-03 10:08:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(*config) error
|
|
|
|
|
|
|
|
// WithSystemMessagesTranslations is required for Group Chats which are currently disabled.
|
|
|
|
// nolint: unused
|
|
|
|
func WithSystemMessagesTranslations(t map[protobuf.MembershipUpdateEvent_EventType]string) Option {
|
|
|
|
return func(c *config) error {
|
2021-03-29 15:41:30 +00:00
|
|
|
c.systemMessagesTranslations.Init(t)
|
2020-07-03 10:08:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithCustomLogger(logger *zap.Logger) Option {
|
|
|
|
return func(c *config) error {
|
|
|
|
c.logger = logger
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithDatabaseConfig(dbPath, dbKey string) Option {
|
|
|
|
return func(c *config) error {
|
|
|
|
c.dbConfig = dbConfig{dbPath: dbPath, dbKey: dbKey}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithVerifyTransactionClient(client EthClient) Option {
|
|
|
|
return func(c *config) error {
|
|
|
|
c.verifyTransactionClient = client
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithDatabase(db *sql.DB) Option {
|
|
|
|
return func(c *config) error {
|
|
|
|
c.db = db
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 23:18:08 +00:00
|
|
|
func WithToplevelDatabaseMigrations() Option {
|
|
|
|
return func(c *config) error {
|
|
|
|
c.afterDbCreatedHooks = append(c.afterDbCreatedHooks, func(c *config) error {
|
|
|
|
return migrations.Migrate(c.db)
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 23:14:48 +00:00
|
|
|
func WithAppSettings(s accounts.Settings, nc params.NodeConfig) Option {
|
|
|
|
return func(c *config) error {
|
|
|
|
c.afterDbCreatedHooks = append(c.afterDbCreatedHooks, func(c *config) error {
|
|
|
|
if s.Networks == nil {
|
|
|
|
networks := new(json.RawMessage)
|
|
|
|
if err := networks.UnmarshalJSON([]byte("net")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Networks = networks
|
|
|
|
}
|
|
|
|
|
|
|
|
sDB := accounts.NewDB(c.db)
|
|
|
|
return sDB.CreateSettings(s, nc)
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-24 13:13:46 +00:00
|
|
|
func WithMultiAccounts(ma *multiaccounts.Database) Option {
|
|
|
|
return func(c *config) error {
|
|
|
|
c.multiAccount = ma
|
|
|
|
return nil
|
2021-01-14 22:15:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithMailserversDatabase(ma *mailservers.Database) Option {
|
|
|
|
return func(c *config) error {
|
|
|
|
c.mailserversDatabase = ma
|
|
|
|
return nil
|
2020-11-24 13:13:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-09 14:03:43 +00:00
|
|
|
func WithAccount(acc *multiaccounts.Account) Option {
|
|
|
|
return func(c *config) error {
|
|
|
|
c.account = acc
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-17 03:42:11 +00:00
|
|
|
func WithBrowserDatabase(bd *browsers.Database) Option {
|
|
|
|
return func(c *config) error {
|
|
|
|
c.browserDatabase = bd
|
|
|
|
if c.browserDatabase == nil {
|
|
|
|
c.afterDbCreatedHooks = append(c.afterDbCreatedHooks, func(c *config) error {
|
|
|
|
c.browserDatabase = browsers.NewDB(c.db)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-01 12:02:18 +00:00
|
|
|
func WithAnonMetricsClientConfig(anonMetricsClientConfig *anonmetrics.ClientConfig) Option {
|
|
|
|
return func(c *config) error {
|
|
|
|
c.anonMetricsClientConfig = anonMetricsClientConfig
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithAnonMetricsServerConfig(anonMetricsServerConfig *anonmetrics.ServerConfig) Option {
|
|
|
|
return func(c *config) error {
|
|
|
|
c.anonMetricsServerConfig = anonMetricsServerConfig
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 12:38:37 +00:00
|
|
|
func WithTelemetry(serverURL string) Option {
|
|
|
|
return func(c *config) error {
|
|
|
|
c.telemetryServerURL = serverURL
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 07:41:40 +00:00
|
|
|
func WithPushNotificationServerConfig(pushNotificationServerConfig *pushnotificationserver.Config) Option {
|
2020-07-03 10:08:47 +00:00
|
|
|
return func(c *config) error {
|
|
|
|
c.pushNotificationServerConfig = pushNotificationServerConfig
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 07:41:40 +00:00
|
|
|
func WithPushNotificationClientConfig(pushNotificationClientConfig *pushnotificationclient.Config) Option {
|
2020-07-15 12:25:01 +00:00
|
|
|
return func(c *config) error {
|
|
|
|
c.pushNotificationClientConfig = pushNotificationClientConfig
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 10:08:47 +00:00
|
|
|
func WithDatasync() func(c *config) error {
|
|
|
|
return func(c *config) error {
|
2020-07-06 08:54:22 +00:00
|
|
|
c.featureFlags.Datasync = true
|
2020-07-03 10:08:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 15:20:16 +00:00
|
|
|
func WithPushNotifications() func(c *config) error {
|
|
|
|
return func(c *config) error {
|
|
|
|
c.featureFlags.PushNotifications = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 10:08:47 +00:00
|
|
|
func WithEnvelopesMonitorConfig(emc *transport.EnvelopesMonitorConfig) Option {
|
|
|
|
return func(c *config) error {
|
|
|
|
c.envelopesMonitorConfig = emc
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2021-02-23 15:47:45 +00:00
|
|
|
|
2021-04-19 12:09:46 +00:00
|
|
|
func WithSignalsHandler(h MessengerSignalsHandler) Option {
|
2021-02-23 15:47:45 +00:00
|
|
|
return func(c *config) error {
|
2021-04-19 12:09:46 +00:00
|
|
|
c.messengerSignalsHandler = h
|
2021-02-23 15:47:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2021-01-11 10:32:51 +00:00
|
|
|
|
|
|
|
func WithENSVerificationConfig(onENSVerified func(*MessengerResponse), url, address string) Option {
|
|
|
|
return func(c *config) error {
|
|
|
|
c.onContactENSVerified = onENSVerified
|
|
|
|
c.verifyENSURL = url
|
|
|
|
c.verifyENSContractAddress = address
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2022-01-12 16:02:01 +00:00
|
|
|
|
|
|
|
func WithClusterConfig(cc params.ClusterConfig) Option {
|
|
|
|
return func(c *config) error {
|
|
|
|
c.clusterConfig = cc
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithMailserverCycle() func(c *config) error {
|
|
|
|
return func(c *config) error {
|
|
|
|
c.featureFlags.MailserverCycle = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|