github.com/status-im/status-go@v1.1.0/protocol/messenger_config.go (about)

     1  package protocol
     2  
     3  import (
     4  	"database/sql"
     5  	"encoding/json"
     6  	"time"
     7  
     8  	"github.com/ethereum/go-ethereum/event"
     9  
    10  	"github.com/status-im/status-go/account"
    11  	"github.com/status-im/status-go/rpc"
    12  	"github.com/status-im/status-go/server"
    13  	"github.com/status-im/status-go/services/browsers"
    14  	"github.com/status-im/status-go/wakuv2"
    15  
    16  	"go.uber.org/zap"
    17  
    18  	"github.com/status-im/status-go/appdatabase/migrations"
    19  	"github.com/status-im/status-go/multiaccounts"
    20  	"github.com/status-im/status-go/multiaccounts/accounts"
    21  	"github.com/status-im/status-go/multiaccounts/settings"
    22  	"github.com/status-im/status-go/params"
    23  	"github.com/status-im/status-go/protocol/anonmetrics"
    24  	"github.com/status-im/status-go/protocol/common"
    25  	"github.com/status-im/status-go/protocol/communities"
    26  	"github.com/status-im/status-go/protocol/discord"
    27  	"github.com/status-im/status-go/protocol/protobuf"
    28  	"github.com/status-im/status-go/protocol/pushnotificationclient"
    29  	"github.com/status-im/status-go/protocol/pushnotificationserver"
    30  	"github.com/status-im/status-go/protocol/transport"
    31  	"github.com/status-im/status-go/protocol/wakusync"
    32  	"github.com/status-im/status-go/services/mailservers"
    33  	"github.com/status-im/status-go/services/wallet"
    34  )
    35  
    36  type MessageDeliveredHandler func(string, string)
    37  
    38  type MessengerSignalsHandler interface {
    39  	MessageDelivered(chatID string, messageID string)
    40  	CommunityInfoFound(community *communities.Community)
    41  	MessengerResponse(response *MessengerResponse)
    42  	HistoryRequestStarted(numBatches int)
    43  	HistoryRequestCompleted()
    44  
    45  	BackupPerformed(uint64)
    46  	HistoryArchivesProtocolEnabled()
    47  	HistoryArchivesProtocolDisabled()
    48  	CreatingHistoryArchives(communityID string)
    49  	NoHistoryArchivesCreated(communityID string, from int, to int)
    50  	HistoryArchivesCreated(communityID string, from int, to int)
    51  	HistoryArchivesSeeding(communityID string)
    52  	HistoryArchivesUnseeded(communityID string)
    53  	HistoryArchiveDownloaded(communityID string, from int, to int)
    54  	DownloadingHistoryArchivesStarted(communityID string)
    55  	DownloadingHistoryArchivesFinished(communityID string)
    56  	ImportingHistoryArchiveMessages(communityID string)
    57  	StatusUpdatesTimedOut(statusUpdates *[]UserStatus)
    58  	DiscordCategoriesAndChannelsExtracted(categories []*discord.Category, channels []*discord.Channel, oldestMessageTimestamp int64, errors map[string]*discord.ImportError)
    59  	DiscordCommunityImportProgress(importProgress *discord.ImportProgress)
    60  	DiscordCommunityImportFinished(communityID string)
    61  	DiscordCommunityImportCancelled(communityID string)
    62  	DiscordCommunityImportCleanedUp(communityID string)
    63  	DiscordChannelImportProgress(importProgress *discord.ImportProgress)
    64  	DiscordChannelImportFinished(communityID string, channelID string)
    65  	DiscordChannelImportCancelled(channelID string)
    66  	SendWakuFetchingBackupProgress(response *wakusync.WakuBackedUpDataResponse)
    67  	SendWakuBackedUpProfile(response *wakusync.WakuBackedUpDataResponse)
    68  	SendWakuBackedUpSettings(response *wakusync.WakuBackedUpDataResponse)
    69  	SendWakuBackedUpKeypair(response *wakusync.WakuBackedUpDataResponse)
    70  	SendWakuBackedUpWatchOnlyAccount(response *wakusync.WakuBackedUpDataResponse)
    71  	SendCuratedCommunitiesUpdate(response *communities.KnownCommunitiesResponse)
    72  }
    73  
    74  type config struct {
    75  	// systemMessagesTranslations holds translations for system-messages
    76  	systemMessagesTranslations *systemMessageTranslationsMap
    77  	// Config for the envelopes monitor
    78  	envelopesMonitorConfig *transport.EnvelopesMonitorConfig
    79  
    80  	featureFlags     common.FeatureFlags
    81  	codeControlFlags common.CodeControlFlags
    82  
    83  	appDb                  *sql.DB
    84  	walletDb               *sql.DB
    85  	afterDbCreatedHooks    []Option
    86  	multiAccount           *multiaccounts.Database
    87  	mailserversDatabase    *mailservers.Database
    88  	account                *multiaccounts.Account
    89  	clusterConfig          params.ClusterConfig
    90  	browserDatabase        *browsers.Database
    91  	torrentConfig          *params.TorrentConfig
    92  	walletConfig           *params.WalletConfig
    93  	walletService          *wallet.Service
    94  	communityTokensService communities.CommunityTokensServiceInterface
    95  	httpServer             *server.MediaServer
    96  	rpcClient              *rpc.Client
    97  	tokenManager           communities.TokenManager
    98  	collectiblesManager    communities.CollectiblesManager
    99  	accountsManager        account.Manager
   100  
   101  	verifyTransactionClient  EthClient
   102  	verifyENSURL             string
   103  	verifyENSContractAddress string
   104  
   105  	anonMetricsClientConfig *anonmetrics.ClientConfig
   106  	anonMetricsServerConfig *anonmetrics.ServerConfig
   107  
   108  	pushNotificationServerConfig *pushnotificationserver.Config
   109  	pushNotificationClientConfig *pushnotificationclient.Config
   110  
   111  	logger *zap.Logger
   112  
   113  	outputMessagesCSV bool
   114  
   115  	messengerSignalsHandler MessengerSignalsHandler
   116  
   117  	telemetryServerURL  string
   118  	telemetrySendPeriod time.Duration
   119  	wakuService         *wakuv2.Waku
   120  
   121  	messageResendMinDelay time.Duration
   122  	messageResendMaxCount int
   123  
   124  	communityManagerOptions []communities.ManagerOption
   125  
   126  	accountsFeed *event.Feed
   127  }
   128  
   129  func messengerDefaultConfig() config {
   130  	c := config{
   131  		messageResendMinDelay: 30 * time.Second,
   132  		messageResendMaxCount: 3,
   133  	}
   134  
   135  	c.codeControlFlags.AutoRequestHistoricMessages = true
   136  	c.codeControlFlags.CuratedCommunitiesUpdateLoopEnabled = true
   137  	return c
   138  }
   139  
   140  type Option func(*config) error
   141  
   142  // WithSystemMessagesTranslations is required for Group Chats which are currently disabled.
   143  // nolint: unused
   144  func WithSystemMessagesTranslations(t map[protobuf.MembershipUpdateEvent_EventType]string) Option {
   145  	return func(c *config) error {
   146  		c.systemMessagesTranslations.Init(t)
   147  		return nil
   148  	}
   149  }
   150  
   151  func WithCustomLogger(logger *zap.Logger) Option {
   152  	return func(c *config) error {
   153  		c.logger = logger
   154  		return nil
   155  	}
   156  }
   157  
   158  func WithVerifyTransactionClient(client EthClient) Option {
   159  	return func(c *config) error {
   160  		c.verifyTransactionClient = client
   161  		return nil
   162  	}
   163  }
   164  
   165  func WithResendParams(minDelay time.Duration, maxCount int) Option {
   166  	return func(c *config) error {
   167  		c.messageResendMinDelay = minDelay
   168  		c.messageResendMaxCount = maxCount
   169  		return nil
   170  	}
   171  }
   172  
   173  func WithDatabase(db *sql.DB) Option {
   174  	return func(c *config) error {
   175  		c.appDb = db
   176  		return nil
   177  	}
   178  }
   179  
   180  func WithWalletDatabase(db *sql.DB) Option {
   181  	return func(c *config) error {
   182  		c.walletDb = db
   183  		return nil
   184  	}
   185  }
   186  
   187  func WithToplevelDatabaseMigrations() Option {
   188  	return func(c *config) error {
   189  		c.afterDbCreatedHooks = append(c.afterDbCreatedHooks, func(c *config) error {
   190  			return migrations.Migrate(c.appDb, nil)
   191  		})
   192  		return nil
   193  	}
   194  }
   195  
   196  func WithAppSettings(s settings.Settings, nc params.NodeConfig) Option {
   197  	return func(c *config) error {
   198  		c.afterDbCreatedHooks = append(c.afterDbCreatedHooks, func(c *config) error {
   199  			if s.Networks == nil {
   200  				networks := new(json.RawMessage)
   201  				if err := networks.UnmarshalJSON([]byte("net")); err != nil {
   202  					return err
   203  				}
   204  
   205  				s.Networks = networks
   206  			}
   207  
   208  			sDB, err := accounts.NewDB(c.appDb)
   209  			if err != nil {
   210  				return err
   211  			}
   212  			return sDB.CreateSettings(s, nc)
   213  		})
   214  		return nil
   215  	}
   216  }
   217  
   218  func WithMultiAccounts(ma *multiaccounts.Database) Option {
   219  	return func(c *config) error {
   220  		c.multiAccount = ma
   221  		return nil
   222  	}
   223  }
   224  
   225  func WithMailserversDatabase(ma *mailservers.Database) Option {
   226  	return func(c *config) error {
   227  		c.mailserversDatabase = ma
   228  		return nil
   229  	}
   230  }
   231  
   232  func WithAccount(acc *multiaccounts.Account) Option {
   233  	return func(c *config) error {
   234  		c.account = acc
   235  		return nil
   236  	}
   237  }
   238  
   239  func WithBrowserDatabase(bd *browsers.Database) Option {
   240  	return func(c *config) error {
   241  		c.browserDatabase = bd
   242  		if c.browserDatabase == nil {
   243  			c.afterDbCreatedHooks = append(c.afterDbCreatedHooks, func(c *config) error {
   244  				c.browserDatabase = browsers.NewDB(c.appDb)
   245  				return nil
   246  			})
   247  		}
   248  		return nil
   249  	}
   250  }
   251  
   252  func WithAnonMetricsClientConfig(anonMetricsClientConfig *anonmetrics.ClientConfig) Option {
   253  	return func(c *config) error {
   254  		c.anonMetricsClientConfig = anonMetricsClientConfig
   255  		return nil
   256  	}
   257  }
   258  
   259  func WithAnonMetricsServerConfig(anonMetricsServerConfig *anonmetrics.ServerConfig) Option {
   260  	return func(c *config) error {
   261  		c.anonMetricsServerConfig = anonMetricsServerConfig
   262  		return nil
   263  	}
   264  }
   265  
   266  func WithTelemetry(serverURL string, sendPeriod time.Duration) Option {
   267  	return func(c *config) error {
   268  		c.telemetryServerURL = serverURL
   269  		c.telemetrySendPeriod = sendPeriod
   270  		return nil
   271  	}
   272  }
   273  
   274  func WithPushNotificationServerConfig(pushNotificationServerConfig *pushnotificationserver.Config) Option {
   275  	return func(c *config) error {
   276  		c.pushNotificationServerConfig = pushNotificationServerConfig
   277  		return nil
   278  	}
   279  }
   280  
   281  func WithPushNotificationClientConfig(pushNotificationClientConfig *pushnotificationclient.Config) Option {
   282  	return func(c *config) error {
   283  		c.pushNotificationClientConfig = pushNotificationClientConfig
   284  		return nil
   285  	}
   286  }
   287  
   288  func WithDatasync() func(c *config) error {
   289  	return func(c *config) error {
   290  		c.featureFlags.Datasync = true
   291  		return nil
   292  	}
   293  }
   294  
   295  func WithPushNotifications() func(c *config) error {
   296  	return func(c *config) error {
   297  		c.featureFlags.PushNotifications = true
   298  		return nil
   299  	}
   300  }
   301  
   302  func WithCheckingForBackupDisabled() func(c *config) error {
   303  	return func(c *config) error {
   304  		c.featureFlags.DisableCheckingForBackup = true
   305  		return nil
   306  	}
   307  }
   308  
   309  func WithAutoMessageDisabled() func(c *config) error {
   310  	return func(c *config) error {
   311  		c.featureFlags.DisableAutoMessageLoop = true
   312  		return nil
   313  	}
   314  }
   315  
   316  func WithEnvelopesMonitorConfig(emc *transport.EnvelopesMonitorConfig) Option {
   317  	return func(c *config) error {
   318  		c.envelopesMonitorConfig = emc
   319  		return nil
   320  	}
   321  }
   322  
   323  func WithSignalsHandler(h MessengerSignalsHandler) Option {
   324  	return func(c *config) error {
   325  		c.messengerSignalsHandler = h
   326  		return nil
   327  	}
   328  }
   329  
   330  func WithENSVerificationConfig(url, address string) Option {
   331  	return func(c *config) error {
   332  		c.verifyENSURL = url
   333  		c.verifyENSContractAddress = address
   334  		return nil
   335  	}
   336  }
   337  
   338  func WithClusterConfig(cc params.ClusterConfig) Option {
   339  	return func(c *config) error {
   340  		c.clusterConfig = cc
   341  		return nil
   342  	}
   343  }
   344  
   345  func WithTorrentConfig(tc *params.TorrentConfig) Option {
   346  	return func(c *config) error {
   347  		c.torrentConfig = tc
   348  		return nil
   349  	}
   350  }
   351  
   352  func WithHTTPServer(s *server.MediaServer) Option {
   353  	return func(c *config) error {
   354  		c.httpServer = s
   355  		return nil
   356  	}
   357  }
   358  
   359  func WithRPCClient(r *rpc.Client) Option {
   360  	return func(c *config) error {
   361  		c.rpcClient = r
   362  		return nil
   363  	}
   364  }
   365  
   366  func WithWalletConfig(wc *params.WalletConfig) Option {
   367  	return func(c *config) error {
   368  		c.walletConfig = wc
   369  		return nil
   370  	}
   371  }
   372  
   373  func WithMessageCSV(enabled bool) Option {
   374  	return func(c *config) error {
   375  		c.outputMessagesCSV = enabled
   376  		return nil
   377  	}
   378  }
   379  
   380  func WithWalletService(s *wallet.Service) Option {
   381  	return func(c *config) error {
   382  		c.walletService = s
   383  		return nil
   384  	}
   385  }
   386  
   387  func WithCommunityTokensService(s communities.CommunityTokensServiceInterface) Option {
   388  	return func(c *config) error {
   389  		c.communityTokensService = s
   390  		return nil
   391  	}
   392  }
   393  
   394  func WithWakuService(s *wakuv2.Waku) Option {
   395  	return func(c *config) error {
   396  		c.wakuService = s
   397  		return nil
   398  	}
   399  }
   400  
   401  func WithTokenManager(tokenManager communities.TokenManager) Option {
   402  	return func(c *config) error {
   403  		c.tokenManager = tokenManager
   404  		return nil
   405  	}
   406  }
   407  
   408  func WithCollectiblesManager(collectiblesManager communities.CollectiblesManager) Option {
   409  	return func(c *config) error {
   410  		c.collectiblesManager = collectiblesManager
   411  		return nil
   412  	}
   413  }
   414  
   415  func WithAccountManager(accountManager account.Manager) Option {
   416  	return func(c *config) error {
   417  		c.accountsManager = accountManager
   418  		return nil
   419  	}
   420  }
   421  
   422  func WithAccountsFeed(feed *event.Feed) Option {
   423  	return func(c *config) error {
   424  		c.accountsFeed = feed
   425  		return nil
   426  	}
   427  }