2019-07-08 09:21:21 +00:00
|
|
|
package statusproto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
2019-07-27 12:27:31 +00:00
|
|
|
"database/sql"
|
2019-07-08 09:21:21 +00:00
|
|
|
"time"
|
|
|
|
|
2019-07-18 13:41:48 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2019-07-08 09:21:21 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-07-16 10:43:07 +00:00
|
|
|
whisper "github.com/status-im/whisper/whisperv6"
|
|
|
|
|
2019-07-17 08:45:14 +00:00
|
|
|
"github.com/status-im/status-protocol-go/datasync"
|
|
|
|
datasyncpeer "github.com/status-im/status-protocol-go/datasync/peer"
|
2019-07-16 10:43:07 +00:00
|
|
|
"github.com/status-im/status-protocol-go/encryption"
|
|
|
|
"github.com/status-im/status-protocol-go/encryption/multidevice"
|
|
|
|
"github.com/status-im/status-protocol-go/encryption/sharedsecret"
|
|
|
|
"github.com/status-im/status-protocol-go/sqlite"
|
|
|
|
transport "github.com/status-im/status-protocol-go/transport/whisper"
|
2019-07-08 09:21:21 +00:00
|
|
|
protocol "github.com/status-im/status-protocol-go/v1"
|
2019-07-17 08:45:14 +00:00
|
|
|
datasyncnode "github.com/vacp2p/mvds/node"
|
|
|
|
datasyncpeers "github.com/vacp2p/mvds/peers"
|
|
|
|
datasyncstate "github.com/vacp2p/mvds/state"
|
|
|
|
datasyncstore "github.com/vacp2p/mvds/store"
|
2019-07-23 08:33:57 +00:00
|
|
|
)
|
|
|
|
|
2019-07-16 10:43:07 +00:00
|
|
|
var (
|
|
|
|
ErrChatIDEmpty = errors.New("chat ID is empty")
|
|
|
|
ErrNotImplemented = errors.New("not implemented")
|
|
|
|
)
|
2019-07-08 09:21:21 +00:00
|
|
|
|
2019-07-16 10:43:07 +00:00
|
|
|
// Messenger is a entity managing chats and messages.
|
|
|
|
// It acts as a bridge between the application and encryption
|
|
|
|
// layers.
|
|
|
|
// It needs to expose an interface to manage installations
|
|
|
|
// because installations are managed by the user.
|
|
|
|
// Similarly, it needs to expose an interface to manage
|
|
|
|
// mailservers because they can also be managed by the user.
|
|
|
|
type Messenger struct {
|
|
|
|
identity *ecdsa.PrivateKey
|
2019-07-27 12:27:31 +00:00
|
|
|
persistence *sqlitePersistence
|
2019-07-16 10:43:07 +00:00
|
|
|
adapter *whisperAdapter
|
|
|
|
encryptor *encryption.Protocol
|
2019-07-26 06:19:03 +00:00
|
|
|
logger *zap.Logger
|
2019-07-08 09:21:21 +00:00
|
|
|
|
2019-07-23 08:33:57 +00:00
|
|
|
ownMessages map[string][]*protocol.Message
|
|
|
|
featureFlags featureFlags
|
|
|
|
messagesPersistenceEnabled bool
|
2019-07-26 06:19:03 +00:00
|
|
|
shutdownTasks []func() error
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 14:50:09 +00:00
|
|
|
type featureFlags struct {
|
|
|
|
genericDiscoveryTopicEnabled bool
|
2019-07-16 15:16:35 +00:00
|
|
|
// sendV1Messages indicates whether we should send
|
|
|
|
// messages compatible only with V1 and later.
|
|
|
|
// V1 messages adds additional wrapping
|
|
|
|
// which contains a signature and payload.
|
|
|
|
sendV1Messages bool
|
2019-07-17 08:45:14 +00:00
|
|
|
|
2019-07-29 18:09:56 +00:00
|
|
|
// datasync indicates whether direct messages should be sent exclusively
|
|
|
|
// using datasync, breaking change for non-v1 clients. Public messages
|
|
|
|
// are not impacted
|
2019-07-17 08:45:14 +00:00
|
|
|
datasync bool
|
2019-07-17 14:50:09 +00:00
|
|
|
}
|
|
|
|
|
2019-07-27 12:27:31 +00:00
|
|
|
type dbConfig struct {
|
|
|
|
dbPath string
|
|
|
|
dbKey string
|
|
|
|
}
|
|
|
|
|
2019-07-16 10:43:07 +00:00
|
|
|
type config struct {
|
|
|
|
onNewInstallationsHandler func([]*multidevice.Installation)
|
2019-07-23 08:33:57 +00:00
|
|
|
// DEPRECATED: no need to expose it
|
|
|
|
onNewSharedSecretHandler func([]*sharedsecret.Secret)
|
|
|
|
// DEPRECATED: no need to expose it
|
|
|
|
onSendContactCodeHandler func(*encryption.ProtocolMessageSpec)
|
2019-07-08 09:21:21 +00:00
|
|
|
|
2019-08-05 08:10:13 +00:00
|
|
|
// Config for the envelopes monitor
|
|
|
|
envelopesMonitorConfig *transport.EnvelopesMonitorConfig
|
|
|
|
|
2019-07-23 08:33:57 +00:00
|
|
|
messagesPersistenceEnabled bool
|
|
|
|
featureFlags featureFlags
|
2019-07-18 13:41:48 +00:00
|
|
|
|
2019-07-27 12:27:31 +00:00
|
|
|
// A path to a database or a database instance is required.
|
|
|
|
// The database instance has a higher priority.
|
|
|
|
dbConfig dbConfig
|
|
|
|
db *sql.DB
|
|
|
|
|
2019-07-18 13:41:48 +00:00
|
|
|
logger *zap.Logger
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 10:43:07 +00:00
|
|
|
type Option func(*config) error
|
2019-07-08 09:21:21 +00:00
|
|
|
|
2019-07-27 12:27:31 +00:00
|
|
|
func WithOnNewInstallationsHandler(h func([]*multidevice.Installation)) Option {
|
2019-07-16 10:43:07 +00:00
|
|
|
return func(c *config) error {
|
|
|
|
c.onNewInstallationsHandler = h
|
|
|
|
return nil
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
2019-07-16 10:43:07 +00:00
|
|
|
}
|
2019-07-08 09:21:21 +00:00
|
|
|
|
2019-07-27 12:27:31 +00:00
|
|
|
func WithOnNewSharedSecret(h func([]*sharedsecret.Secret)) Option {
|
2019-07-16 10:43:07 +00:00
|
|
|
return func(c *config) error {
|
|
|
|
c.onNewSharedSecretHandler = h
|
|
|
|
return nil
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-27 12:27:31 +00:00
|
|
|
func WithCustomLogger(logger *zap.Logger) Option {
|
2019-07-16 10:43:07 +00:00
|
|
|
return func(c *config) error {
|
2019-07-23 08:33:57 +00:00
|
|
|
c.logger = logger
|
2019-07-16 10:43:07 +00:00
|
|
|
return nil
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-27 12:27:31 +00:00
|
|
|
func WithGenericDiscoveryTopicSupport() Option {
|
2019-07-18 13:41:48 +00:00
|
|
|
return func(c *config) error {
|
2019-07-23 08:33:57 +00:00
|
|
|
c.featureFlags.genericDiscoveryTopicEnabled = true
|
2019-07-18 13:41:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-27 12:27:31 +00:00
|
|
|
func WithMessagesPersistenceEnabled() Option {
|
2019-07-17 14:50:09 +00:00
|
|
|
return func(c *config) error {
|
2019-07-23 08:33:57 +00:00
|
|
|
c.messagesPersistenceEnabled = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-27 12:27:31 +00:00
|
|
|
func WithDatabaseConfig(dbPath, dbKey string) Option {
|
|
|
|
return func(c *config) error {
|
|
|
|
c.dbConfig = dbConfig{dbPath: dbPath, dbKey: dbKey}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithDatabase(db *sql.DB) Option {
|
2019-07-23 08:33:57 +00:00
|
|
|
return func(c *config) error {
|
2019-07-27 12:27:31 +00:00
|
|
|
c.db = db
|
2019-07-17 14:50:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-27 12:27:31 +00:00
|
|
|
func WithSendV1Messages() Option {
|
2019-07-16 15:16:35 +00:00
|
|
|
return func(c *config) error {
|
|
|
|
c.featureFlags.sendV1Messages = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-17 08:45:14 +00:00
|
|
|
func WithDatasync() func(c *config) error {
|
|
|
|
return func(c *config) error {
|
|
|
|
c.featureFlags.datasync = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 08:10:13 +00:00
|
|
|
func WithEnvelopesMonitorConfig(emc *transport.EnvelopesMonitorConfig) Option {
|
|
|
|
return func(c *config) error {
|
|
|
|
c.envelopesMonitorConfig = emc
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 10:43:07 +00:00
|
|
|
func NewMessenger(
|
|
|
|
identity *ecdsa.PrivateKey,
|
|
|
|
shh *whisper.Whisper,
|
|
|
|
installationID string,
|
|
|
|
opts ...Option,
|
|
|
|
) (*Messenger, error) {
|
|
|
|
var messenger *Messenger
|
|
|
|
|
2019-07-18 13:41:48 +00:00
|
|
|
c := config{}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
if err := opt(&c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logger := c.logger
|
|
|
|
if c.logger == nil {
|
|
|
|
var err error
|
|
|
|
if logger, err = zap.NewDevelopment(); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to create a logger")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 10:43:07 +00:00
|
|
|
// Set default config fields.
|
2019-07-18 13:41:48 +00:00
|
|
|
if c.onNewInstallationsHandler == nil {
|
|
|
|
c.onNewInstallationsHandler = func(installations []*multidevice.Installation) {
|
|
|
|
sugar := logger.Sugar().With("site", "onNewInstallationsHandler")
|
2019-07-16 10:43:07 +00:00
|
|
|
for _, installation := range installations {
|
2019-07-18 13:41:48 +00:00
|
|
|
sugar.Infow(
|
|
|
|
"received a new installation",
|
|
|
|
"identity", installation.Identity,
|
|
|
|
"id", installation.ID)
|
2019-07-16 10:43:07 +00:00
|
|
|
}
|
2019-07-18 13:41:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.onNewSharedSecretHandler == nil {
|
|
|
|
c.onNewSharedSecretHandler = func(secrets []*sharedsecret.Secret) {
|
2019-07-16 10:43:07 +00:00
|
|
|
if err := messenger.handleSharedSecrets(secrets); err != nil {
|
2019-07-18 13:41:48 +00:00
|
|
|
slogger := logger.With(zap.String("site", "onNewSharedSecretHandler"))
|
|
|
|
slogger.Warn("failed to process secrets", zap.Error(err))
|
2019-07-16 10:43:07 +00:00
|
|
|
}
|
2019-07-18 13:41:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.onSendContactCodeHandler == nil {
|
|
|
|
c.onSendContactCodeHandler = func(messageSpec *encryption.ProtocolMessageSpec) {
|
|
|
|
slogger := logger.With(zap.String("site", "onSendContactCodeHandler"))
|
|
|
|
slogger.Info("received a SendContactCode request")
|
2019-07-16 10:43:07 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
_, err := messenger.adapter.SendContactCode(ctx, messageSpec)
|
2019-07-23 18:38:50 +00:00
|
|
|
if err != nil {
|
|
|
|
slogger.Warn("failed to send a contact code", zap.Error(err))
|
|
|
|
}
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
2019-07-16 10:43:07 +00:00
|
|
|
}
|
2019-07-08 09:21:21 +00:00
|
|
|
|
2019-07-27 12:27:31 +00:00
|
|
|
// Configure the database.
|
|
|
|
database := c.db
|
|
|
|
if c.db == nil && c.dbConfig == (dbConfig{}) {
|
|
|
|
return nil, errors.New("database instance or database path needs to be provided")
|
2019-07-23 08:33:57 +00:00
|
|
|
}
|
2019-07-27 12:27:31 +00:00
|
|
|
if c.db == nil {
|
|
|
|
logger.Info("opening a database", zap.String("dbPath", c.dbConfig.dbPath))
|
|
|
|
var err error
|
|
|
|
database, err = sqlite.Open(c.dbConfig.dbPath, c.dbConfig.dbKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to initialize database from the db config")
|
|
|
|
}
|
2019-07-23 08:33:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-27 12:27:31 +00:00
|
|
|
// Apply migrations for all components.
|
|
|
|
migrationNames, migrationGetter, err := prepareMigrations(defaultMigrations)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to prepare migrations")
|
|
|
|
}
|
|
|
|
err = sqlite.ApplyMigrations(database, migrationNames, migrationGetter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to apply migrations")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize transport layer.
|
2019-07-16 10:43:07 +00:00
|
|
|
t, err := transport.NewWhisperServiceTransport(
|
|
|
|
shh,
|
|
|
|
identity,
|
2019-07-27 12:27:31 +00:00
|
|
|
database,
|
2019-07-16 10:43:07 +00:00
|
|
|
nil,
|
2019-08-05 08:10:13 +00:00
|
|
|
c.envelopesMonitorConfig,
|
2019-07-18 13:41:48 +00:00
|
|
|
logger,
|
2019-08-25 15:48:37 +00:00
|
|
|
transport.SetGenericDiscoveryTopicSupport(c.featureFlags.genericDiscoveryTopicEnabled),
|
2019-07-16 10:43:07 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to create a WhisperServiceTransport")
|
|
|
|
}
|
2019-07-08 09:21:21 +00:00
|
|
|
|
2019-07-27 12:27:31 +00:00
|
|
|
// Initialize encryption layer.
|
|
|
|
encryptionProtocol := encryption.New(
|
|
|
|
database,
|
2019-07-16 10:43:07 +00:00
|
|
|
installationID,
|
|
|
|
c.onNewInstallationsHandler,
|
|
|
|
c.onNewSharedSecretHandler,
|
|
|
|
c.onSendContactCodeHandler,
|
2019-07-18 13:41:48 +00:00
|
|
|
logger,
|
2019-07-16 10:43:07 +00:00
|
|
|
)
|
|
|
|
|
2019-07-27 12:27:31 +00:00
|
|
|
// Initialize data sync.
|
2019-07-17 08:45:14 +00:00
|
|
|
dataSyncTransport := datasync.NewDataSyncNodeTransport()
|
|
|
|
dataSyncStore := datasyncstore.NewDummyStore()
|
|
|
|
dataSyncNode := datasyncnode.NewNode(
|
|
|
|
&dataSyncStore,
|
|
|
|
dataSyncTransport,
|
|
|
|
datasyncstate.NewSyncState(), // @todo sqlite syncstate
|
|
|
|
datasync.CalculateSendTime,
|
|
|
|
0,
|
|
|
|
datasyncpeer.PublicKeyToPeerID(identity.PublicKey),
|
|
|
|
datasyncnode.BATCH,
|
|
|
|
datasyncpeers.NewMemoryPersistence(),
|
|
|
|
)
|
2019-07-26 14:38:27 +00:00
|
|
|
datasync := datasync.New(dataSyncNode, dataSyncTransport, c.featureFlags.datasync, logger)
|
2019-07-08 09:21:21 +00:00
|
|
|
|
2019-07-17 08:45:14 +00:00
|
|
|
adapter := newWhisperAdapter(identity, t, encryptionProtocol, datasync, c.featureFlags, logger)
|
2019-07-27 12:27:31 +00:00
|
|
|
|
2019-07-16 10:43:07 +00:00
|
|
|
messenger = &Messenger{
|
2019-07-23 08:33:57 +00:00
|
|
|
identity: identity,
|
2019-07-27 12:27:31 +00:00
|
|
|
persistence: &sqlitePersistence{db: database},
|
2019-07-23 08:33:57 +00:00
|
|
|
adapter: adapter,
|
|
|
|
encryptor: encryptionProtocol,
|
|
|
|
ownMessages: make(map[string][]*protocol.Message),
|
|
|
|
featureFlags: c.featureFlags,
|
|
|
|
messagesPersistenceEnabled: c.messagesPersistenceEnabled,
|
|
|
|
shutdownTasks: []func() error{
|
2019-07-27 12:27:31 +00:00
|
|
|
database.Close,
|
2019-07-23 08:33:57 +00:00
|
|
|
adapter.transport.Reset,
|
2019-07-26 14:38:27 +00:00
|
|
|
func() error { datasync.Stop(); return nil },
|
2019-07-24 06:53:51 +00:00
|
|
|
// Currently this often fails, seems like it's safe to ignore them
|
|
|
|
// https://github.com/uber-go/zap/issues/328
|
|
|
|
func() error { _ = logger.Sync; return nil },
|
2019-08-05 08:10:13 +00:00
|
|
|
func() error { adapter.Stop(); return nil },
|
2019-07-18 13:41:48 +00:00
|
|
|
},
|
2019-07-23 08:33:57 +00:00
|
|
|
logger: logger,
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 13:14:16 +00:00
|
|
|
// Start all services immediately.
|
|
|
|
// TODO: consider removing identity as an argument to Start().
|
|
|
|
if err := encryptionProtocol.Start(identity); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-17 08:45:14 +00:00
|
|
|
if c.featureFlags.datasync {
|
|
|
|
dataSyncNode.Start(300 * time.Millisecond)
|
|
|
|
}
|
2019-07-17 13:14:16 +00:00
|
|
|
|
2019-07-23 08:33:57 +00:00
|
|
|
logger.Debug("messages persistence", zap.Bool("enabled", c.messagesPersistenceEnabled))
|
|
|
|
|
2019-07-16 10:43:07 +00:00
|
|
|
return messenger, nil
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
|
|
|
|
2019-08-25 15:48:37 +00:00
|
|
|
// Init analyzes chats and contacts in order to setup filters
|
|
|
|
// which are responsible for retrieving messages.
|
|
|
|
func (m *Messenger) Init() error {
|
|
|
|
logger := m.logger.With(zap.String("site", "Init"))
|
|
|
|
|
|
|
|
var (
|
|
|
|
publicChatIDs []string
|
|
|
|
publicKeys []*ecdsa.PublicKey
|
|
|
|
)
|
|
|
|
|
|
|
|
// Get chat IDs and public keys from the existing chats.
|
|
|
|
// TODO: Get only active chats by the query.
|
|
|
|
chats, err := m.Chats()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, chat := range chats {
|
|
|
|
if !chat.Active {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch chat.ChatType {
|
|
|
|
case ChatTypePublic:
|
|
|
|
publicChatIDs = append(publicChatIDs, chat.ID)
|
|
|
|
case ChatTypeOneToOne:
|
|
|
|
publicKeys = append(publicKeys, chat.PublicKey)
|
|
|
|
case ChatTypePrivateGroupChat:
|
|
|
|
for _, member := range chat.Members {
|
|
|
|
publicKey, err := member.PublicKey()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "invalid public key for member %s in chat %s", member.ID, chat.Name)
|
|
|
|
}
|
|
|
|
publicKeys = append(publicKeys, publicKey)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return errors.New("invalid chat type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get chat IDs and public keys from the contacts.
|
|
|
|
contacts, err := m.Contacts()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, contact := range contacts {
|
|
|
|
// We only need filters for contacts added by us and not blocked.
|
|
|
|
if !contact.IsAdded() || contact.IsBlocked() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
publicKey, err := contact.PublicKey()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("failed to get contact's public key", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
publicKeys = append(publicKeys, publicKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = m.adapter.transport.InitFilters(publicChatIDs, publicKeys)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-18 13:41:48 +00:00
|
|
|
// Shutdown takes care of ensuring a clean shutdown of Messenger
|
2019-07-23 08:33:57 +00:00
|
|
|
func (m *Messenger) Shutdown() (err error) {
|
2019-07-18 13:41:48 +00:00
|
|
|
for _, task := range m.shutdownTasks {
|
2019-07-23 08:33:57 +00:00
|
|
|
if tErr := task(); tErr != nil {
|
|
|
|
if err == nil {
|
|
|
|
// First error appeared.
|
|
|
|
err = tErr
|
|
|
|
} else {
|
|
|
|
// We return all errors. They will be concatenated in the order of occurrence,
|
|
|
|
// however, they will also be returned as a single error.
|
|
|
|
err = errors.Wrap(err, tErr.Error())
|
|
|
|
}
|
|
|
|
}
|
2019-07-18 13:41:48 +00:00
|
|
|
}
|
2019-07-23 08:33:57 +00:00
|
|
|
return
|
2019-07-18 13:41:48 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 10:43:07 +00:00
|
|
|
func (m *Messenger) handleSharedSecrets(secrets []*sharedsecret.Secret) error {
|
|
|
|
return m.adapter.handleSharedSecrets(secrets)
|
|
|
|
}
|
2019-07-08 09:21:21 +00:00
|
|
|
|
2019-07-16 10:43:07 +00:00
|
|
|
func (m *Messenger) EnableInstallation(id string) error {
|
|
|
|
return m.encryptor.EnableInstallation(&m.identity.PublicKey, id)
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 10:43:07 +00:00
|
|
|
func (m *Messenger) DisableInstallation(id string) error {
|
|
|
|
return m.encryptor.DisableInstallation(&m.identity.PublicKey, id)
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 10:43:07 +00:00
|
|
|
func (m *Messenger) Installations() ([]*multidevice.Installation, error) {
|
|
|
|
return m.encryptor.GetOurInstallations(&m.identity.PublicKey)
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 10:43:07 +00:00
|
|
|
func (m *Messenger) SetInstallationMetadata(id string, data *multidevice.InstallationMetadata) error {
|
|
|
|
return m.encryptor.SetInstallationMetadata(&m.identity.PublicKey, id, data)
|
|
|
|
}
|
2019-07-08 09:21:21 +00:00
|
|
|
|
2019-07-23 08:33:57 +00:00
|
|
|
// NOT IMPLEMENTED
|
2019-07-16 10:43:07 +00:00
|
|
|
func (m *Messenger) SelectMailserver(id string) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
2019-07-08 09:21:21 +00:00
|
|
|
|
2019-07-23 08:33:57 +00:00
|
|
|
// NOT IMPLEMENTED
|
2019-07-16 10:43:07 +00:00
|
|
|
func (m *Messenger) AddMailserver(enode string) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
2019-07-08 09:21:21 +00:00
|
|
|
|
2019-07-23 08:33:57 +00:00
|
|
|
// NOT IMPLEMENTED
|
2019-07-16 10:43:07 +00:00
|
|
|
func (m *Messenger) RemoveMailserver(id string) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
2019-07-08 09:21:21 +00:00
|
|
|
|
2019-07-23 08:33:57 +00:00
|
|
|
// NOT IMPLEMENTED
|
2019-07-16 10:43:07 +00:00
|
|
|
func (m *Messenger) Mailservers() ([]string, error) {
|
|
|
|
return nil, ErrNotImplemented
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 10:43:07 +00:00
|
|
|
func (m *Messenger) Join(chat Chat) error {
|
2019-07-30 16:02:45 +00:00
|
|
|
if chat.PublicKey != nil {
|
|
|
|
return m.adapter.JoinPrivate(chat.PublicKey)
|
|
|
|
} else if chat.Name != "" {
|
|
|
|
return m.adapter.JoinPublic(chat.Name)
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
2019-07-16 10:43:07 +00:00
|
|
|
return errors.New("chat is neither public nor private")
|
|
|
|
}
|
2019-07-08 09:21:21 +00:00
|
|
|
|
2019-07-16 10:43:07 +00:00
|
|
|
func (m *Messenger) Leave(chat Chat) error {
|
2019-07-30 16:02:45 +00:00
|
|
|
if chat.PublicKey != nil {
|
|
|
|
return m.adapter.LeavePrivate(chat.PublicKey)
|
|
|
|
} else if chat.Name != "" {
|
|
|
|
return m.adapter.LeavePublic(chat.Name)
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
2019-07-16 10:43:07 +00:00
|
|
|
return errors.New("chat is neither public nor private")
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 16:02:45 +00:00
|
|
|
func (m *Messenger) SaveChat(chat Chat) error {
|
|
|
|
return m.persistence.SaveChat(chat)
|
|
|
|
}
|
|
|
|
|
2019-08-25 15:48:37 +00:00
|
|
|
func (m *Messenger) Chats() ([]*Chat, error) {
|
|
|
|
return m.persistence.Chats()
|
2019-07-30 16:02:45 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 13:09:46 +00:00
|
|
|
func (m *Messenger) DeleteChat(chatID string) error {
|
|
|
|
return m.persistence.DeleteChat(chatID)
|
2019-08-02 07:51:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) SaveContact(contact Contact) error {
|
2019-08-13 13:09:46 +00:00
|
|
|
return m.persistence.SaveContact(contact, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) BlockContact(contact Contact) ([]*Chat, error) {
|
|
|
|
return m.persistence.BlockContact(contact)
|
2019-08-02 07:51:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) Contacts() ([]*Contact, error) {
|
|
|
|
return m.persistence.Contacts()
|
|
|
|
}
|
|
|
|
|
2019-07-16 10:43:07 +00:00
|
|
|
func (m *Messenger) Send(ctx context.Context, chat Chat, data []byte) ([]byte, error) {
|
2019-07-30 16:02:45 +00:00
|
|
|
chatID := chat.ID
|
2019-07-16 10:43:07 +00:00
|
|
|
if chatID == "" {
|
|
|
|
return nil, ErrChatIDEmpty
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 16:02:45 +00:00
|
|
|
clock, err := m.persistence.LastMessageClock(chat.ID)
|
2019-07-08 09:21:21 +00:00
|
|
|
if err != nil {
|
2019-07-16 10:43:07 +00:00
|
|
|
return nil, err
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 16:02:45 +00:00
|
|
|
if chat.PublicKey != nil {
|
|
|
|
hash, message, err := m.adapter.SendPrivate(ctx, chat.PublicKey, chat.ID, data, clock)
|
2019-07-16 10:43:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-08 09:21:21 +00:00
|
|
|
|
2019-07-16 10:43:07 +00:00
|
|
|
// Save our message because it won't be received from the transport layer.
|
|
|
|
message.ID = hash // a Message need ID to be properly stored in the db
|
|
|
|
message.SigPubKey = &m.identity.PublicKey
|
2019-07-23 08:33:57 +00:00
|
|
|
|
|
|
|
if m.messagesPersistenceEnabled {
|
2019-07-30 16:02:45 +00:00
|
|
|
_, err = m.persistence.SaveMessages(chat.ID, []*protocol.Message{message})
|
2019-07-23 08:33:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-16 10:43:07 +00:00
|
|
|
}
|
2019-07-08 09:21:21 +00:00
|
|
|
|
2019-07-16 10:43:07 +00:00
|
|
|
// Cache it to be returned in Retrieve().
|
|
|
|
m.ownMessages[chatID] = append(m.ownMessages[chatID], message)
|
2019-07-08 09:21:21 +00:00
|
|
|
|
2019-07-16 10:43:07 +00:00
|
|
|
return hash, nil
|
2019-07-30 16:02:45 +00:00
|
|
|
} else if chat.Name != "" {
|
|
|
|
return m.adapter.SendPublic(ctx, chat.Name, chat.ID, data, clock)
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
2019-07-16 10:43:07 +00:00
|
|
|
return nil, errors.New("chat is neither public nor private")
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 08:33:57 +00:00
|
|
|
// SendRaw takes encoded data, encrypts it and sends through the wire.
|
|
|
|
// DEPRECATED
|
2019-08-05 08:10:13 +00:00
|
|
|
func (m *Messenger) SendRaw(ctx context.Context, chat Chat, data []byte) ([]byte, error) {
|
2019-07-30 16:02:45 +00:00
|
|
|
if chat.PublicKey != nil {
|
|
|
|
return m.adapter.SendPrivateRaw(ctx, chat.PublicKey, data)
|
|
|
|
} else if chat.Name != "" {
|
|
|
|
return m.adapter.SendPublicRaw(ctx, chat.Name, data)
|
2019-07-23 08:33:57 +00:00
|
|
|
}
|
2019-08-05 08:10:13 +00:00
|
|
|
return nil, errors.New("chat is neither public nor private")
|
2019-07-23 08:33:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 10:43:07 +00:00
|
|
|
type RetrieveConfig struct {
|
|
|
|
From time.Time
|
|
|
|
To time.Time
|
|
|
|
latest bool
|
|
|
|
last24Hours bool
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 10:43:07 +00:00
|
|
|
var (
|
|
|
|
RetrieveLatest = RetrieveConfig{latest: true}
|
|
|
|
RetrieveLastDay = RetrieveConfig{latest: true, last24Hours: true}
|
|
|
|
)
|
2019-07-08 09:21:21 +00:00
|
|
|
|
2019-07-26 06:19:03 +00:00
|
|
|
// RetrieveAll retrieves all previously fetched messages
|
|
|
|
func (m *Messenger) RetrieveAll(ctx context.Context, c RetrieveConfig) (allMessages []*protocol.Message, err error) {
|
|
|
|
latest, err := m.adapter.RetrieveAllMessages()
|
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "failed to retrieve messages")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, messages := range latest {
|
|
|
|
chatID := messages.ChatID
|
|
|
|
|
|
|
|
_, err = m.persistence.SaveMessages(chatID, messages.Messages)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to save messages")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !messages.Public {
|
|
|
|
// Return any own messages for this chat as well.
|
|
|
|
if ownMessages, ok := m.ownMessages[chatID]; ok {
|
|
|
|
messages.Messages = append(messages.Messages, ownMessages...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
retrievedMessages, err := m.retrieveSaved(ctx, chatID, c, messages.Messages)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to get saved messages")
|
|
|
|
}
|
|
|
|
|
|
|
|
allMessages = append(allMessages, retrievedMessages...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete own messages as they were added to the result.
|
|
|
|
for _, messages := range latest {
|
|
|
|
if !messages.Public {
|
|
|
|
delete(m.ownMessages, messages.ChatID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-16 10:43:07 +00:00
|
|
|
func (m *Messenger) Retrieve(ctx context.Context, chat Chat, c RetrieveConfig) (messages []*protocol.Message, err error) {
|
2019-07-23 08:33:57 +00:00
|
|
|
var (
|
|
|
|
latest []*protocol.Message
|
|
|
|
ownLatest []*protocol.Message
|
|
|
|
)
|
2019-07-08 09:21:21 +00:00
|
|
|
|
2019-07-30 16:02:45 +00:00
|
|
|
if chat.PublicKey != nil {
|
|
|
|
latest, err = m.adapter.RetrievePrivateMessages(chat.PublicKey)
|
2019-07-16 10:43:07 +00:00
|
|
|
// Return any own messages for this chat as well.
|
2019-07-30 16:02:45 +00:00
|
|
|
if ownMessages, ok := m.ownMessages[chat.ID]; ok {
|
2019-07-23 08:33:57 +00:00
|
|
|
ownLatest = ownMessages
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
2019-07-30 16:02:45 +00:00
|
|
|
} else if chat.Name != "" {
|
|
|
|
latest, err = m.adapter.RetrievePublicMessages(chat.Name)
|
2019-07-16 10:43:07 +00:00
|
|
|
} else {
|
|
|
|
return nil, errors.New("chat is neither public nor private")
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 10:43:07 +00:00
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "failed to retrieve messages")
|
|
|
|
return
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 08:33:57 +00:00
|
|
|
if m.messagesPersistenceEnabled {
|
2019-07-30 16:02:45 +00:00
|
|
|
_, err = m.persistence.SaveMessages(chat.ID, latest)
|
2019-07-23 08:33:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to save latest messages")
|
|
|
|
}
|
2019-07-16 10:43:07 +00:00
|
|
|
}
|
2019-07-08 09:21:21 +00:00
|
|
|
|
2019-07-23 08:33:57 +00:00
|
|
|
// Confirm received and decrypted messages.
|
2019-07-30 16:02:45 +00:00
|
|
|
if m.messagesPersistenceEnabled && chat.PublicKey != nil {
|
2019-07-23 08:33:57 +00:00
|
|
|
for _, message := range latest {
|
|
|
|
// Confirm received and decrypted messages.
|
|
|
|
if err := m.encryptor.ConfirmMessageProcessed(message.ID); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to confirm message being processed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-26 06:19:03 +00:00
|
|
|
// We may need to add more messages from the past.
|
2019-07-30 16:02:45 +00:00
|
|
|
result, err := m.retrieveSaved(ctx, chat.ID, c, append(latest, ownLatest...))
|
2019-07-26 06:19:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-23 08:33:57 +00:00
|
|
|
// When our messages are returned, we can delete them.
|
2019-07-30 16:02:45 +00:00
|
|
|
delete(m.ownMessages, chat.ID)
|
2019-07-23 08:33:57 +00:00
|
|
|
|
2019-07-26 06:19:03 +00:00
|
|
|
return result, nil
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 06:19:03 +00:00
|
|
|
func (m *Messenger) retrieveSaved(ctx context.Context, chatID string, c RetrieveConfig, latest []*protocol.Message) (messages []*protocol.Message, err error) {
|
2019-07-23 08:33:57 +00:00
|
|
|
if !m.messagesPersistenceEnabled {
|
|
|
|
return latest, nil
|
|
|
|
}
|
|
|
|
|
2019-07-16 10:43:07 +00:00
|
|
|
if !c.latest {
|
2019-07-26 06:19:03 +00:00
|
|
|
return m.persistence.Messages(chatID, c.From, c.To)
|
2019-07-16 10:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.last24Hours {
|
|
|
|
to := time.Now()
|
|
|
|
from := to.Add(-time.Hour * 24)
|
2019-07-26 06:19:03 +00:00
|
|
|
return m.persistence.Messages(chatID, from, to)
|
2019-07-16 10:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return latest, nil
|
2019-07-08 09:21:21 +00:00
|
|
|
}
|
2019-07-23 08:33:57 +00:00
|
|
|
|
|
|
|
// DEPRECATED
|
2019-08-06 15:33:32 +00:00
|
|
|
func (m *Messenger) RetrieveRawAll() (map[transport.Filter][]*protocol.StatusMessage, error) {
|
2019-07-23 08:33:57 +00:00
|
|
|
return m.adapter.RetrieveRawAll()
|
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED
|
2019-08-25 15:48:37 +00:00
|
|
|
func (m *Messenger) LoadFilters(filters []*transport.Filter) ([]*transport.Filter, error) {
|
|
|
|
return m.adapter.transport.LoadFilters(filters)
|
2019-07-23 08:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED
|
2019-08-25 15:48:37 +00:00
|
|
|
func (m *Messenger) RemoveFilters(filters []*transport.Filter) error {
|
|
|
|
return m.adapter.transport.RemoveFilters(filters)
|
2019-07-23 08:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED
|
|
|
|
func (m *Messenger) ConfirmMessagesProcessed(messageIDs [][]byte) error {
|
|
|
|
for _, id := range messageIDs {
|
|
|
|
if err := m.encryptor.ConfirmMessageProcessed(id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-31 07:50:08 +00:00
|
|
|
|
|
|
|
// DEPRECATED: required by status-react.
|
|
|
|
func (m *Messenger) MessageByID(id string) (*Message, error) {
|
|
|
|
return m.persistence.MessageByID(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED: required by status-react.
|
2019-08-13 13:09:46 +00:00
|
|
|
func (m *Messenger) MessagesExist(ids []string) (map[string]bool, error) {
|
|
|
|
return m.persistence.MessagesExist(ids)
|
2019-07-31 07:50:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED: required by status-react.
|
|
|
|
func (m *Messenger) MessageByChatID(chatID, cursor string, limit int) ([]*Message, string, error) {
|
|
|
|
return m.persistence.MessageByChatID(chatID, cursor, limit)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED: required by status-react.
|
2019-08-13 13:09:46 +00:00
|
|
|
func (m *Messenger) SaveMessages(messages []*Message) error {
|
|
|
|
return m.persistence.SaveMessagesLegacy(messages)
|
2019-07-31 07:50:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED: required by status-react.
|
2019-08-13 13:09:46 +00:00
|
|
|
func (m *Messenger) DeleteMessage(id string) error {
|
|
|
|
return m.persistence.DeleteMessage(id)
|
2019-07-31 07:50:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED: required by status-react.
|
2019-08-13 13:09:46 +00:00
|
|
|
func (m *Messenger) DeleteMessagesByChatID(id string) error {
|
|
|
|
return m.persistence.DeleteMessagesByChatID(id)
|
2019-07-31 07:50:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED: required by status-react.
|
|
|
|
func (m *Messenger) MarkMessagesSeen(ids ...string) error {
|
|
|
|
return m.persistence.MarkMessagesSeen(ids...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED: required by status-react.
|
|
|
|
func (m *Messenger) UpdateMessageOutgoingStatus(id, newOutgoingStatus string) error {
|
|
|
|
return m.persistence.UpdateMessageOutgoingStatus(id, newOutgoingStatus)
|
|
|
|
}
|