Send image on update

This commit changes the behavior so that when the image is updated it
will be published on the contact code topic.
If that does not happen because we are offline, it will be scheduled for
the next time we are online.
This commit is contained in:
Andrea Maria Piana 2020-12-21 09:41:50 +01:00
parent bf703254ba
commit 4185420897
3 changed files with 62 additions and 13 deletions

View File

@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"github.com/ethereum/go-ethereum/log"
"github.com/status-im/status-go/images"
"github.com/status-im/status-go/multiaccounts/migrations"
"github.com/status-im/status-go/sqlite"
@ -24,7 +25,8 @@ type MultiAccountMarshaller interface {
}
type Database struct {
db *sql.DB
db *sql.DB
identityImageSubscriptions []chan struct{}
}
// InitializeDB creates db file at a given path and applies migrations.
@ -209,9 +211,28 @@ func (db *Database) StoreIdentityImages(keyUID string, iis []*images.IdentityIma
}
}
db.publishOnIdentityImageSubscriptions()
return nil
}
func (db *Database) SubscribeToIdentityImageChanges() chan struct{} {
s := make(chan struct{}, 100)
db.identityImageSubscriptions = append(db.identityImageSubscriptions, s)
return s
}
func (db *Database) publishOnIdentityImageSubscriptions() {
// Publish on channels, drop if buffer is full
for _, s := range db.identityImageSubscriptions {
select {
case s <- struct{}{}:
default:
log.Warn("subscription channel full, dropping message")
}
}
}
func (db *Database) DeleteIdentityImage(keyUID string) error {
_, err := db.db.Exec(`DELETE FROM identity_images WHERE key_uid = ?`, keyUID)
return err

View File

@ -79,8 +79,8 @@ type Messenger struct {
logger *zap.Logger
verifyTransactionClient EthClient
featureFlags common.FeatureFlags
messagesPersistenceEnabled bool
shutdownTasks []func() error
shouldPublishContactCode bool
systemMessagesTranslations map[protobuf.MembershipUpdateEvent_EventType]string
allChats map[string]*Chat
allContacts map[string]*Contact
@ -287,7 +287,6 @@ func NewMessenger(
allInstallations: make(map[string]*multidevice.Installation),
installationID: installationID,
modifiedInstallations: make(map[string]bool),
messagesPersistenceEnabled: c.messagesPersistenceEnabled,
verifyTransactionClient: c.verifyTransactionClient,
database: database,
multiAccounts: c.multiAccount,
@ -315,7 +314,6 @@ func NewMessenger(
}
}
logger.Debug("messages persistence", zap.Bool("enabled", c.messagesPersistenceEnabled))
return messenger, nil
}
@ -421,6 +419,7 @@ func (m *Messenger) Start() error {
m.handleConnectionChange(m.online())
m.watchConnectionChange()
m.watchExpiredEmojis()
m.watchIdentityImageChanges()
return nil
}
@ -430,6 +429,15 @@ func (m *Messenger) handleConnectionChange(online bool) {
if m.pushNotificationClient != nil {
m.pushNotificationClient.Online()
}
if m.shouldPublishContactCode {
if err := m.handleSendContactCode(); err != nil {
m.logger.Error("could not publish on contact code", zap.Error(err))
return
}
m.shouldPublishContactCode = false
}
} else {
if m.pushNotificationClient != nil {
m.pushNotificationClient.Offline()
@ -757,6 +765,34 @@ func (m *Messenger) watchExpiredEmojis() {
}()
}
// watchIdentityImageChanges checks for identity images changes and publishes to the contact code when it happens
func (m *Messenger) watchIdentityImageChanges() {
m.logger.Debug("watching identity image changes")
if m.multiAccounts == nil {
return
}
channel := m.multiAccounts.SubscribeToIdentityImageChanges()
go func() {
for {
select {
case <-channel:
if m.online() {
if err := m.handleSendContactCode(); err != nil {
m.logger.Error("failed to publish contact code", zap.Error(err))
}
} else {
m.shouldPublishContactCode = true
}
case <-m.quit:
return
}
}
}()
}
// handlePushNotificationClientRegistration handles registration events
func (m *Messenger) handlePushNotificationClientRegistrations(c chan struct{}) {
go func() {

View File

@ -24,8 +24,7 @@ type config struct {
// Config for the envelopes monitor
envelopesMonitorConfig *transport.EnvelopesMonitorConfig
messagesPersistenceEnabled bool
featureFlags common.FeatureFlags
featureFlags common.FeatureFlags
// A path to a database or a database instance is required.
// The database instance has a higher priority.
@ -67,13 +66,6 @@ func WithCustomLogger(logger *zap.Logger) Option {
}
}
func WithMessagesPersistenceEnabled() Option {
return func(c *config) error {
c.messagesPersistenceEnabled = true
return nil
}
}
func WithDatabaseConfig(dbPath, dbKey string) Option {
return func(c *config) error {
c.dbConfig = dbConfig{dbPath: dbPath, dbKey: dbKey}