Allow sending messages offline

This commit is contained in:
Roman Volosovskyi 2021-10-21 15:39:19 +03:00
parent 87d7c48e2a
commit 53bbfb3f08
No known key found for this signature in database
GPG Key ID: 0238A4B5ECEE70DE
3 changed files with 49 additions and 9 deletions

View File

@ -1 +1 @@
0.89.19 0.89.20

View File

@ -59,11 +59,14 @@ type MessageEvent struct {
type MessageSender struct { type MessageSender struct {
identity *ecdsa.PrivateKey identity *ecdsa.PrivateKey
datasync *datasync.DataSync datasync *datasync.DataSync
database *sql.DB
protocol *encryption.Protocol protocol *encryption.Protocol
transport *transport.Transport transport *transport.Transport
logger *zap.Logger logger *zap.Logger
persistence *RawMessagesPersistence persistence *RawMessagesPersistence
datasyncEnabled bool
// ephemeralKeys is a map that contains the ephemeral keys of the client, used // ephemeralKeys is a map that contains the ephemeral keys of the client, used
// to decrypt messages // to decrypt messages
ephemeralKeys map[string]*ecdsa.PrivateKey ephemeralKeys map[string]*ecdsa.PrivateKey
@ -101,14 +104,16 @@ func NewMessageSender(
ds := datasync.New(dataSyncNode, dataSyncTransport, features.Datasync, logger) ds := datasync.New(dataSyncNode, dataSyncTransport, features.Datasync, logger)
p := &MessageSender{ p := &MessageSender{
identity: identity, identity: identity,
datasync: ds, datasyncEnabled: features.Datasync,
protocol: enc, datasync: ds,
persistence: NewRawMessagesPersistence(database), protocol: enc,
transport: transport, database: database,
logger: logger, persistence: NewRawMessagesPersistence(database),
ephemeralKeys: make(map[string]*ecdsa.PrivateKey), transport: transport,
featureFlags: features, logger: logger,
ephemeralKeys: make(map[string]*ecdsa.PrivateKey),
featureFlags: features,
} }
// Initializing DataSync is required to encrypt and send messages. // Initializing DataSync is required to encrypt and send messages.
@ -838,3 +843,30 @@ func calculatePoW(payload []byte) float64 {
} }
return whisperDefaultPoW return whisperDefaultPoW
} }
func (s *MessageSender) StopDatasync() {
s.datasync.Stop()
}
func (s *MessageSender) StartDatasync() {
dataSyncTransport := datasync.NewNodeTransport()
dataSyncNode, err := datasyncnode.NewPersistentNode(
s.database,
dataSyncTransport,
datasyncpeer.PublicKeyToPeerID(s.identity.PublicKey),
datasyncnode.BATCH,
datasync.CalculateSendTime,
s.logger,
)
if err != nil {
return
}
ds := datasync.New(dataSyncNode, dataSyncTransport, true, s.logger)
if s.datasyncEnabled {
ds.Init(s.sendDataSync, s.transport.MaxMessageSize()/4*3, s.logger)
ds.Start(datasync.DatasyncTicker)
}
s.datasync = ds
}

View File

@ -547,5 +547,13 @@ func (m *Messenger) RemoveFilters(filters []*transport.Filter) error {
} }
func (m *Messenger) ConnectionChanged(state connection.State) { func (m *Messenger) ConnectionChanged(state connection.State) {
if !m.connectionState.Offline && state.Offline {
m.sender.StopDatasync()
}
if m.connectionState.Offline && !state.Offline {
m.sender.StartDatasync()
}
m.connectionState = state m.connectionState = state
} }