2019-11-21 16:19:22 +00:00
|
|
|
package protocol
|
2019-07-17 22:25:42 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
2019-07-30 06:14:13 +00:00
|
|
|
"database/sql"
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2019-12-02 15:34:05 +00:00
|
|
|
"math/rand"
|
|
|
|
"sync"
|
2019-07-17 22:25:42 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-09-02 09:29:06 +00:00
|
|
|
"go.uber.org/zap"
|
2019-07-17 22:25:42 +00:00
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
enstypes "github.com/status-im/status-go/eth-node/types/ens"
|
2019-11-21 16:19:22 +00:00
|
|
|
"github.com/status-im/status-go/protocol/encryption"
|
|
|
|
"github.com/status-im/status-go/protocol/encryption/multidevice"
|
|
|
|
"github.com/status-im/status-go/protocol/encryption/sharedsecret"
|
|
|
|
"github.com/status-im/status-go/protocol/identity/alias"
|
|
|
|
"github.com/status-im/status-go/protocol/identity/identicon"
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2019-11-21 16:19:22 +00:00
|
|
|
"github.com/status-im/status-go/protocol/sqlite"
|
|
|
|
transport "github.com/status-im/status-go/protocol/transport/whisper"
|
|
|
|
v1protocol "github.com/status-im/status-go/protocol/v1"
|
2019-07-17 22:25:42 +00:00
|
|
|
)
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
const PubKeyStringLength = 132
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
var (
|
|
|
|
ErrChatIDEmpty = errors.New("chat ID is empty")
|
|
|
|
ErrNotImplemented = errors.New("not implemented")
|
2019-09-26 07:01:17 +00:00
|
|
|
|
|
|
|
errChatNotFound = errors.New("chat not found")
|
2019-07-17 22:25:42 +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 {
|
2019-12-02 15:34:05 +00:00
|
|
|
node types.Node
|
|
|
|
identity *ecdsa.PrivateKey
|
|
|
|
persistence *sqlitePersistence
|
|
|
|
transport *transport.WhisperServiceTransport
|
|
|
|
encryptor *encryption.Protocol
|
|
|
|
processor *messageProcessor
|
|
|
|
logger *zap.Logger
|
2019-07-17 22:25:42 +00:00
|
|
|
featureFlags featureFlags
|
|
|
|
messagesPersistenceEnabled bool
|
2019-07-26 07:17:29 +00:00
|
|
|
shutdownTasks []func() error
|
2019-12-02 15:34:05 +00:00
|
|
|
systemMessagesTranslations map[protobuf.MembershipUpdateEvent_EventType]string
|
|
|
|
allChats map[string]*Chat
|
|
|
|
allContacts map[string]*Contact
|
|
|
|
mutex sync.Mutex
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
type RawResponse struct {
|
|
|
|
Filter *transport.Filter `json:"filter"`
|
|
|
|
Messages []*v1protocol.StatusMessage `json:"messages"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type MessengerResponse struct {
|
|
|
|
Chats []*Chat `json:"chats,omitEmpty"`
|
|
|
|
Messages []*Message `json:"messages,omitEmpty"`
|
|
|
|
Contacts []*Contact `json:"contacts,omitEmpty"`
|
|
|
|
// Raw unprocessed messages
|
|
|
|
RawMessages []*RawResponse `json:"rawMessages,omitEmpty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MessengerResponse) IsEmpty() bool {
|
|
|
|
return len(m.Chats) == 0 && len(m.Messages) == 0 && len(m.Contacts) == 0 && len(m.RawMessages) == 0
|
|
|
|
}
|
2019-07-26 07:17:29 +00:00
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
type featureFlags struct {
|
2019-07-30 18:39:16 +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-26 07:17:29 +00:00
|
|
|
datasync bool
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 06:14:13 +00:00
|
|
|
type dbConfig struct {
|
|
|
|
dbPath string
|
|
|
|
dbKey string
|
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
type config struct {
|
|
|
|
onNewInstallationsHandler func([]*multidevice.Installation)
|
2019-08-27 12:04:15 +00:00
|
|
|
// 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
|
|
|
|
onNegotiatedFilters func([]*transport.Filter)
|
2019-07-17 22:25:42 +00:00
|
|
|
// DEPRECATED: no need to expose it
|
|
|
|
onSendContactCodeHandler func(*encryption.ProtocolMessageSpec)
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
// systemMessagesTranslations holds translations for system-messages
|
|
|
|
systemMessagesTranslations map[protobuf.MembershipUpdateEvent_EventType]string
|
2019-08-09 07:03:10 +00:00
|
|
|
// Config for the envelopes monitor
|
|
|
|
envelopesMonitorConfig *transport.EnvelopesMonitorConfig
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
messagesPersistenceEnabled bool
|
|
|
|
featureFlags featureFlags
|
|
|
|
|
2019-07-30 06:14:13 +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-17 22:25:42 +00:00
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(*config) error
|
|
|
|
|
2019-07-30 06:14:13 +00:00
|
|
|
func WithOnNewInstallationsHandler(h func([]*multidevice.Installation)) Option {
|
2019-07-17 22:25:42 +00:00
|
|
|
return func(c *config) error {
|
|
|
|
c.onNewInstallationsHandler = h
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func WithSystemMessagesTranslations(t map[protobuf.MembershipUpdateEvent_EventType]string) Option {
|
|
|
|
return func(c *config) error {
|
|
|
|
c.systemMessagesTranslations = t
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 12:04:15 +00:00
|
|
|
func WithOnNegotiatedFilters(h func([]*transport.Filter)) Option {
|
2019-07-17 22:25:42 +00:00
|
|
|
return func(c *config) error {
|
2019-08-27 12:04:15 +00:00
|
|
|
c.onNegotiatedFilters = h
|
2019-07-17 22:25:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 06:14:13 +00:00
|
|
|
func WithCustomLogger(logger *zap.Logger) Option {
|
2019-07-17 22:25:42 +00:00
|
|
|
return func(c *config) error {
|
|
|
|
c.logger = logger
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 06:14:13 +00:00
|
|
|
func WithMessagesPersistenceEnabled() Option {
|
2019-07-17 22:25:42 +00:00
|
|
|
return func(c *config) error {
|
|
|
|
c.messagesPersistenceEnabled = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 06:14:13 +00:00
|
|
|
func WithDatabaseConfig(dbPath, dbKey string) Option {
|
2019-07-17 22:25:42 +00:00
|
|
|
return func(c *config) error {
|
2019-07-30 06:14:13 +00:00
|
|
|
c.dbConfig = dbConfig{dbPath: dbPath, dbKey: dbKey}
|
2019-07-17 22:25:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 06:14:13 +00:00
|
|
|
func WithDatabase(db *sql.DB) Option {
|
|
|
|
return func(c *config) error {
|
|
|
|
c.db = db
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-26 07:17:29 +00:00
|
|
|
func WithDatasync() func(c *config) error {
|
|
|
|
return func(c *config) error {
|
|
|
|
c.featureFlags.datasync = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 07:03:10 +00:00
|
|
|
func WithEnvelopesMonitorConfig(emc *transport.EnvelopesMonitorConfig) Option {
|
|
|
|
return func(c *config) error {
|
|
|
|
c.envelopesMonitorConfig = emc
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
func NewMessenger(
|
|
|
|
identity *ecdsa.PrivateKey,
|
2019-11-23 17:57:05 +00:00
|
|
|
node types.Node,
|
2019-07-17 22:25:42 +00:00
|
|
|
installationID string,
|
|
|
|
opts ...Option,
|
|
|
|
) (*Messenger, error) {
|
|
|
|
var messenger *Messenger
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
shh, err := node.GetWhisper(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set default config fields.
|
|
|
|
if c.onNewInstallationsHandler == nil {
|
|
|
|
c.onNewInstallationsHandler = func(installations []*multidevice.Installation) {
|
|
|
|
sugar := logger.Sugar().With("site", "onNewInstallationsHandler")
|
|
|
|
for _, installation := range installations {
|
|
|
|
sugar.Infow(
|
|
|
|
"received a new installation",
|
|
|
|
"identity", installation.Identity,
|
|
|
|
"id", installation.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-27 12:04:15 +00:00
|
|
|
onNewSharedSecretHandler := func(secrets []*sharedsecret.Secret) {
|
|
|
|
filters, err := messenger.handleSharedSecrets(secrets)
|
|
|
|
if err != nil {
|
|
|
|
slogger := logger.With(zap.String("site", "onNewSharedSecretHandler"))
|
|
|
|
slogger.Warn("failed to process secrets", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.onNegotiatedFilters != nil {
|
|
|
|
c.onNegotiatedFilters(filters)
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.onSendContactCodeHandler == nil {
|
|
|
|
c.onSendContactCodeHandler = func(messageSpec *encryption.ProtocolMessageSpec) {
|
|
|
|
slogger := logger.With(zap.String("site", "onSendContactCodeHandler"))
|
2019-12-02 15:34:05 +00:00
|
|
|
slogger.Debug("received a SendContactCode request")
|
2019-09-02 09:29:06 +00:00
|
|
|
|
|
|
|
newMessage, err := messageSpecToWhisper(messageSpec)
|
|
|
|
if err != nil {
|
|
|
|
slogger.Warn("failed to convert spec to Whisper message", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
2019-09-02 09:29:06 +00:00
|
|
|
chatName := transport.ContactCodeTopic(&messenger.identity.PublicKey)
|
2019-10-09 14:22:53 +00:00
|
|
|
_, err = messenger.transport.SendPublic(ctx, newMessage, chatName)
|
2019-07-26 07:17:29 +00:00
|
|
|
if err != nil {
|
|
|
|
slogger.Warn("failed to send a contact code", zap.Error(err))
|
|
|
|
}
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
if c.systemMessagesTranslations == nil {
|
|
|
|
c.systemMessagesTranslations = defaultSystemMessagesTranslations
|
|
|
|
}
|
|
|
|
|
2019-07-30 06:14:13 +00:00
|
|
|
// Configure the database.
|
|
|
|
database := c.db
|
2019-09-26 09:26:33 +00:00
|
|
|
if c.db == nil && c.dbConfig == (dbConfig{}) {
|
|
|
|
return nil, errors.New("database instance or database path needs to be provided")
|
|
|
|
}
|
|
|
|
if c.db == nil {
|
2019-07-30 06:14:13 +00:00
|
|
|
logger.Info("opening a database", zap.String("dbPath", c.dbConfig.dbPath))
|
2019-09-26 09:26:33 +00:00
|
|
|
var err error
|
2019-07-30 06:14:13 +00:00
|
|
|
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-09-26 09:26:33 +00:00
|
|
|
|
2019-07-30 06:14:13 +00:00
|
|
|
// Apply migrations for all components.
|
2019-11-23 17:57:05 +00:00
|
|
|
err = sqlite.Migrate(database)
|
2019-07-30 06:14:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to apply migrations")
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 06:14:13 +00:00
|
|
|
// Initialize transport layer.
|
2019-07-17 22:25:42 +00:00
|
|
|
t, err := transport.NewWhisperServiceTransport(
|
|
|
|
shh,
|
|
|
|
identity,
|
2019-07-30 06:14:13 +00:00
|
|
|
database,
|
2019-07-17 22:25:42 +00:00
|
|
|
nil,
|
2019-08-09 07:03:10 +00:00
|
|
|
c.envelopesMonitorConfig,
|
2019-07-17 22:25:42 +00:00
|
|
|
logger,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to create a WhisperServiceTransport")
|
|
|
|
}
|
|
|
|
|
2019-07-30 06:14:13 +00:00
|
|
|
// Initialize encryption layer.
|
|
|
|
encryptionProtocol := encryption.New(
|
|
|
|
database,
|
2019-07-17 22:25:42 +00:00
|
|
|
installationID,
|
|
|
|
c.onNewInstallationsHandler,
|
2019-08-27 12:04:15 +00:00
|
|
|
onNewSharedSecretHandler,
|
2019-07-17 22:25:42 +00:00
|
|
|
c.onSendContactCodeHandler,
|
|
|
|
logger,
|
|
|
|
)
|
2019-07-26 07:17:29 +00:00
|
|
|
|
2019-09-02 09:29:06 +00:00
|
|
|
processor, err := newMessageProcessor(
|
|
|
|
identity,
|
2019-08-27 12:04:15 +00:00
|
|
|
database,
|
2019-09-02 09:29:06 +00:00
|
|
|
encryptionProtocol,
|
|
|
|
t,
|
2019-08-29 06:33:46 +00:00
|
|
|
logger,
|
2019-09-02 09:29:06 +00:00
|
|
|
c.featureFlags,
|
2019-07-26 07:17:29 +00:00
|
|
|
)
|
2019-08-27 12:04:15 +00:00
|
|
|
if err != nil {
|
2019-09-02 09:29:06 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to create messageProcessor")
|
2019-08-27 12:04:15 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
messenger = &Messenger{
|
2019-11-23 17:57:05 +00:00
|
|
|
node: node,
|
2019-07-17 22:25:42 +00:00
|
|
|
identity: identity,
|
2019-07-30 06:14:13 +00:00
|
|
|
persistence: &sqlitePersistence{db: database},
|
2019-09-02 09:29:06 +00:00
|
|
|
transport: t,
|
2019-07-17 22:25:42 +00:00
|
|
|
encryptor: encryptionProtocol,
|
2019-09-02 09:29:06 +00:00
|
|
|
processor: processor,
|
2019-07-17 22:25:42 +00:00
|
|
|
featureFlags: c.featureFlags,
|
2019-12-02 15:34:05 +00:00
|
|
|
systemMessagesTranslations: c.systemMessagesTranslations,
|
|
|
|
allChats: make(map[string]*Chat),
|
|
|
|
allContacts: make(map[string]*Contact),
|
2019-07-17 22:25:42 +00:00
|
|
|
messagesPersistenceEnabled: c.messagesPersistenceEnabled,
|
|
|
|
shutdownTasks: []func() error{
|
2019-07-30 06:14:13 +00:00
|
|
|
database.Close,
|
2019-09-02 09:29:06 +00:00
|
|
|
t.Reset,
|
|
|
|
t.Stop,
|
|
|
|
func() error { processor.Stop(); return nil },
|
2019-07-17 22:25:42 +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 },
|
|
|
|
},
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start all services immediately.
|
|
|
|
// TODO: consider removing identity as an argument to Start().
|
|
|
|
if err := encryptionProtocol.Start(identity); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debug("messages persistence", zap.Bool("enabled", c.messagesPersistenceEnabled))
|
|
|
|
|
|
|
|
return messenger, nil
|
|
|
|
}
|
|
|
|
|
2019-08-29 06:33:46 +00:00
|
|
|
// Init analyzes chats and contacts in order to setup filters
|
|
|
|
// which are responsible for retrieving messages.
|
|
|
|
func (m *Messenger) Init() error {
|
2019-12-02 15:34:05 +00:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
// Seed the for color generation
|
|
|
|
rand.Seed(time.Now().Unix())
|
|
|
|
|
2019-08-29 06:33:46 +00:00
|
|
|
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.
|
2019-12-02 15:34:05 +00:00
|
|
|
chats, err := m.persistence.Chats()
|
2019-08-29 06:33:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, chat := range chats {
|
2019-12-02 15:34:05 +00:00
|
|
|
m.allChats[chat.ID] = chat
|
2019-08-29 06:33:46 +00:00
|
|
|
if !chat.Active {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch chat.ChatType {
|
|
|
|
case ChatTypePublic:
|
|
|
|
publicChatIDs = append(publicChatIDs, chat.ID)
|
|
|
|
case ChatTypeOneToOne:
|
2019-12-02 15:34:05 +00:00
|
|
|
pk, err := chat.PublicKey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
publicKeys = append(publicKeys, pk)
|
2019-08-29 06:33:46 +00:00
|
|
|
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.
|
2019-12-02 15:34:05 +00:00
|
|
|
contacts, err := m.persistence.Contacts()
|
2019-08-29 06:33:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, contact := range contacts {
|
2019-12-02 15:34:05 +00:00
|
|
|
m.allContacts[contact.ID] = contact
|
2019-08-29 06:33:46 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2019-09-02 09:29:06 +00:00
|
|
|
_, err = m.transport.InitFilters(publicChatIDs, publicKeys)
|
2019-08-29 06:33:46 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
// Shutdown takes care of ensuring a clean shutdown of Messenger
|
|
|
|
func (m *Messenger) Shutdown() (err error) {
|
|
|
|
for _, task := range m.shutdownTasks {
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-27 12:04:15 +00:00
|
|
|
func (m *Messenger) handleSharedSecrets(secrets []*sharedsecret.Secret) ([]*transport.Filter, error) {
|
2019-09-02 09:29:06 +00:00
|
|
|
logger := m.logger.With(zap.String("site", "handleSharedSecrets"))
|
|
|
|
var result []*transport.Filter
|
|
|
|
for _, secret := range secrets {
|
|
|
|
logger.Debug("received shared secret", zap.Binary("identity", crypto.FromECDSAPub(secret.Identity)))
|
2019-11-23 17:57:05 +00:00
|
|
|
fSecret := types.NegotiatedSecret{
|
2019-09-02 09:29:06 +00:00
|
|
|
PublicKey: secret.Identity,
|
|
|
|
Key: secret.Key,
|
|
|
|
}
|
|
|
|
filter, err := m.transport.ProcessNegotiatedSecret(fSecret)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result = append(result, filter)
|
|
|
|
}
|
|
|
|
return result, nil
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) EnableInstallation(id string) error {
|
|
|
|
return m.encryptor.EnableInstallation(&m.identity.PublicKey, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) DisableInstallation(id string) error {
|
|
|
|
return m.encryptor.DisableInstallation(&m.identity.PublicKey, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) Installations() ([]*multidevice.Installation, error) {
|
|
|
|
return m.encryptor.GetOurInstallations(&m.identity.PublicKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) SetInstallationMetadata(id string, data *multidevice.InstallationMetadata) error {
|
|
|
|
return m.encryptor.SetInstallationMetadata(&m.identity.PublicKey, id, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOT IMPLEMENTED
|
|
|
|
func (m *Messenger) SelectMailserver(id string) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOT IMPLEMENTED
|
|
|
|
func (m *Messenger) AddMailserver(enode string) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOT IMPLEMENTED
|
|
|
|
func (m *Messenger) RemoveMailserver(id string) error {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOT IMPLEMENTED
|
|
|
|
func (m *Messenger) Mailservers() ([]string, error) {
|
|
|
|
return nil, ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) Join(chat Chat) error {
|
2019-10-14 14:10:48 +00:00
|
|
|
switch chat.ChatType {
|
|
|
|
case ChatTypeOneToOne:
|
2019-12-02 15:34:05 +00:00
|
|
|
pk, err := chat.PublicKey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.transport.JoinPrivate(pk)
|
2019-10-14 14:10:48 +00:00
|
|
|
case ChatTypePrivateGroupChat:
|
|
|
|
members, err := chat.MembersAsPublicKeys()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return m.transport.JoinGroup(members)
|
|
|
|
case ChatTypePublic:
|
2019-09-02 09:29:06 +00:00
|
|
|
return m.transport.JoinPublic(chat.Name)
|
2019-10-14 14:10:48 +00:00
|
|
|
default:
|
|
|
|
return errors.New("chat is neither public nor private")
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
// This is not accurate, it should not leave transport on removal of chat/group
|
|
|
|
// only once there is no more: Group chat with that member, one-to-one chat, contact added by us
|
2019-07-17 22:25:42 +00:00
|
|
|
func (m *Messenger) Leave(chat Chat) error {
|
2019-12-02 15:34:05 +00:00
|
|
|
switch chat.ChatType {
|
|
|
|
case ChatTypeOneToOne:
|
|
|
|
pk, err := chat.PublicKey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return m.transport.LeavePrivate(pk)
|
|
|
|
case ChatTypePrivateGroupChat:
|
|
|
|
members, err := chat.MembersAsPublicKeys()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return m.transport.LeaveGroup(members)
|
|
|
|
case ChatTypePublic:
|
2019-09-02 09:29:06 +00:00
|
|
|
return m.transport.LeavePublic(chat.Name)
|
2019-12-02 15:34:05 +00:00
|
|
|
default:
|
|
|
|
return errors.New("chat is neither public nor private")
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func (m *Messenger) CreateGroupChatWithMembers(ctx context.Context, name string, members []string) (*MessengerResponse, error) {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
var response MessengerResponse
|
|
|
|
logger := m.logger.With(zap.String("site", "CreateGroupChatWithMembers"))
|
|
|
|
logger.Info("Creating group chat", zap.String("name", name), zap.Any("members", members))
|
2019-10-14 14:10:48 +00:00
|
|
|
chat := createGroupChat()
|
2019-11-21 16:19:22 +00:00
|
|
|
group, err := v1protocol.NewGroupWithCreator(name, m.identity)
|
2019-10-14 14:10:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
chat.updateChatFromProtocolGroup(group)
|
2019-12-02 15:34:05 +00:00
|
|
|
|
|
|
|
// Add members
|
|
|
|
event := v1protocol.NewMembersAddedEvent(members, group.NextClockValue())
|
|
|
|
event.ChatID = chat.ID
|
|
|
|
err = event.Sign(m.identity)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = group.ProcessEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
recipients, err := stringSliceToPublicKeys(group.Members(), true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := m.propagateMembershipUpdates(ctx, group, recipients, nil); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
chat.updateChatFromProtocolGroup(group)
|
|
|
|
|
|
|
|
response.Chats = []*Chat{&chat}
|
|
|
|
response.Messages = buildSystemMessages(chat.MembershipUpdates, m.systemMessagesTranslations)
|
|
|
|
return &response, m.saveChat(&chat)
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func (m *Messenger) RemoveMemberFromGroupChat(ctx context.Context, chatID string, member string) (*MessengerResponse, error) {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
var response MessengerResponse
|
|
|
|
logger := m.logger.With(zap.String("site", "RemoveMemberFromGroupChat"))
|
|
|
|
logger.Info("Removing member form group chat", zap.String("chatID", chatID), zap.String("member", member))
|
|
|
|
chat, ok := m.allChats[chatID]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("can't find chat")
|
|
|
|
}
|
|
|
|
|
2019-10-14 14:10:48 +00:00
|
|
|
group, err := newProtocolGroupFromChat(chat)
|
|
|
|
if err != nil {
|
2019-12-02 15:34:05 +00:00
|
|
|
return nil, err
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
|
|
|
|
// We save the initial recipients as we want to send updates to also
|
|
|
|
// the members kicked out
|
|
|
|
oldRecipients, err := stringSliceToPublicKeys(group.Members(), true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
|
|
|
|
// Remove member
|
|
|
|
event := v1protocol.NewMemberRemovedEvent(member, group.NextClockValue())
|
|
|
|
event.ChatID = chat.ID
|
|
|
|
err = event.Sign(m.identity)
|
2019-10-14 14:10:48 +00:00
|
|
|
if err != nil {
|
2019-12-02 15:34:05 +00:00
|
|
|
return nil, err
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
|
|
|
|
err = group.ProcessEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, err := m.propagateMembershipUpdates(ctx, group, oldRecipients, nil); err != nil {
|
|
|
|
return nil, err
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
|
|
|
chat.updateChatFromProtocolGroup(group)
|
2019-12-02 15:34:05 +00:00
|
|
|
response.Chats = []*Chat{chat}
|
|
|
|
response.Messages = buildSystemMessages(chat.MembershipUpdates, m.systemMessagesTranslations)
|
|
|
|
return &response, m.saveChat(chat)
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func (m *Messenger) AddMembersToGroupChat(ctx context.Context, chatID string, members []string) (*MessengerResponse, error) {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
var response MessengerResponse
|
|
|
|
logger := m.logger.With(zap.String("site", "AddMembersFromGroupChat"))
|
|
|
|
logger.Info("Adding members form group chat", zap.String("chatID", chatID), zap.Any("members", members))
|
|
|
|
chat, ok := m.allChats[chatID]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("can't find chat")
|
|
|
|
}
|
|
|
|
|
2019-10-14 14:10:48 +00:00
|
|
|
group, err := newProtocolGroupFromChat(chat)
|
|
|
|
if err != nil {
|
2019-12-02 15:34:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add members
|
|
|
|
event := v1protocol.NewMembersAddedEvent(members, group.NextClockValue())
|
|
|
|
event.ChatID = chat.ID
|
|
|
|
err = event.Sign(m.identity)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = group.ProcessEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
recipients, err := stringSliceToPublicKeys(group.Members(), true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := m.propagateMembershipUpdates(ctx, group, recipients, nil); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
chat.updateChatFromProtocolGroup(group)
|
|
|
|
|
|
|
|
response.Chats = []*Chat{chat}
|
|
|
|
response.Messages = buildSystemMessages([]v1protocol.MembershipUpdateEvent{event}, m.systemMessagesTranslations)
|
|
|
|
|
|
|
|
return &response, m.saveChat(chat)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) AddAdminsToGroupChat(ctx context.Context, chatID string, members []string) (*MessengerResponse, error) {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
var response MessengerResponse
|
|
|
|
logger := m.logger.With(zap.String("site", "AddAdminsToGroupChat"))
|
|
|
|
logger.Info("Add admins to group chat", zap.String("chatID", chatID), zap.Any("members", members))
|
|
|
|
|
|
|
|
chat, ok := m.allChats[chatID]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("can't find chat")
|
|
|
|
}
|
|
|
|
|
|
|
|
group, err := newProtocolGroupFromChat(chat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add members
|
|
|
|
event := v1protocol.NewAdminsAddedEvent(members, group.NextClockValue())
|
|
|
|
event.ChatID = chat.ID
|
|
|
|
err = event.Sign(m.identity)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = group.ProcessEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
recipients, err := stringSliceToPublicKeys(group.Members(), true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := m.propagateMembershipUpdates(ctx, group, recipients, nil); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
chat.updateChatFromProtocolGroup(group)
|
|
|
|
|
|
|
|
response.Chats = []*Chat{chat}
|
|
|
|
response.Messages = buildSystemMessages([]v1protocol.MembershipUpdateEvent{event}, m.systemMessagesTranslations)
|
|
|
|
|
|
|
|
return &response, m.saveChat(chat)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) ConfirmJoiningGroup(ctx context.Context, chatID string) (*MessengerResponse, error) {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
var response MessengerResponse
|
|
|
|
|
|
|
|
chat, ok := m.allChats[chatID]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("can't find chat")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := m.Join(*chat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
group, err := newProtocolGroupFromChat(chat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
2019-11-21 16:19:22 +00:00
|
|
|
event := v1protocol.NewMemberJoinedEvent(
|
2019-10-14 14:10:48 +00:00
|
|
|
group.NextClockValue(),
|
|
|
|
)
|
2019-12-02 15:34:05 +00:00
|
|
|
event.ChatID = chat.ID
|
|
|
|
err = event.Sign(m.identity)
|
2019-10-14 14:10:48 +00:00
|
|
|
if err != nil {
|
2019-12-02 15:34:05 +00:00
|
|
|
return nil, err
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
|
|
|
|
err = group.ProcessEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
recipients, err := stringSliceToPublicKeys(group.Members(), true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := m.propagateMembershipUpdates(ctx, group, recipients, nil); err != nil {
|
|
|
|
return nil, err
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
|
|
|
chat.updateChatFromProtocolGroup(group)
|
2019-12-02 15:34:05 +00:00
|
|
|
|
|
|
|
response.Chats = []*Chat{chat}
|
|
|
|
response.Messages = buildSystemMessages([]v1protocol.MembershipUpdateEvent{event}, m.systemMessagesTranslations)
|
|
|
|
|
|
|
|
return &response, m.saveChat(chat)
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func (m *Messenger) LeaveGroupChat(ctx context.Context, chatID string) (*MessengerResponse, error) {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
var response MessengerResponse
|
|
|
|
|
|
|
|
chat, ok := m.allChats[chatID]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("can't find chat")
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
|
|
|
|
err := m.Leave(*chat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
|
|
|
|
group, err := newProtocolGroupFromChat(chat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
event := v1protocol.NewMemberRemovedEvent(
|
|
|
|
types.EncodeHex(crypto.FromECDSAPub(&m.identity.PublicKey)),
|
|
|
|
group.NextClockValue(),
|
|
|
|
)
|
|
|
|
event.ChatID = chat.ID
|
|
|
|
err = event.Sign(m.identity)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = group.ProcessEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-14 14:10:48 +00:00
|
|
|
recipients, err := stringSliceToPublicKeys(group.Members(), true)
|
|
|
|
if err != nil {
|
2019-12-02 15:34:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := m.propagateMembershipUpdates(ctx, group, recipients, nil); err != nil {
|
|
|
|
return nil, err
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
chat.updateChatFromProtocolGroup(group)
|
|
|
|
chat.Active = false
|
|
|
|
|
|
|
|
response.Chats = []*Chat{chat}
|
|
|
|
response.Messages = buildSystemMessages([]v1protocol.MembershipUpdateEvent{event}, m.systemMessagesTranslations)
|
|
|
|
return &response, m.saveChat(chat)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) propagateMembershipUpdates(ctx context.Context, group *v1protocol.Group, recipients []*ecdsa.PublicKey, chatMessage *protobuf.ChatMessage) ([]byte, error) {
|
|
|
|
hasPairedDevices, err := m.hasPairedDevices()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !hasPairedDevices {
|
|
|
|
// Filter out my key from the recipients
|
|
|
|
n := 0
|
|
|
|
for _, recipient := range recipients {
|
|
|
|
if !isPubKeyEqual(recipient, &m.identity.PublicKey) {
|
|
|
|
recipients[n] = recipient
|
|
|
|
n++
|
|
|
|
}
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
recipients = recipients[:n]
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
|
|
|
// Finally send membership updates to all recipients.
|
2019-12-02 15:34:05 +00:00
|
|
|
return m.processor.SendMembershipUpdate(
|
2019-10-14 14:10:48 +00:00
|
|
|
ctx,
|
|
|
|
recipients,
|
2019-12-02 15:34:05 +00:00
|
|
|
group,
|
|
|
|
chatMessage,
|
2019-10-14 14:10:48 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func (m *Messenger) saveChat(chat *Chat) error {
|
|
|
|
err := m.persistence.SaveChat(*chat)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.allChats[chat.ID] = chat
|
2019-07-30 18:39:16 +00:00
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
return nil
|
2019-07-30 18:39:16 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func (m *Messenger) saveChats(chats []*Chat) error {
|
|
|
|
err := m.persistence.SaveChats(chats)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, chat := range chats {
|
|
|
|
m.allChats[chat.ID] = chat
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
2019-07-30 18:39:16 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func (m *Messenger) SaveChat(chat *Chat) error {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
err := m.saveChat(chat)
|
2019-09-26 07:01:17 +00:00
|
|
|
if err != nil {
|
2019-12-02 15:34:05 +00:00
|
|
|
return err
|
2019-09-26 07:01:17 +00:00
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) Chats() []*Chat {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
var chats []*Chat
|
|
|
|
|
|
|
|
for _, c := range m.allChats {
|
|
|
|
chats = append(chats, c)
|
2019-09-26 07:01:17 +00:00
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
|
|
|
|
return chats
|
2019-09-26 07:01:17 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func (m *Messenger) DeleteChat(chatID string) error {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
err := m.persistence.DeleteChat(chatID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
delete(m.allChats, chatID)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) SaveContact(contact *Contact) error {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
2019-09-26 07:01:17 +00:00
|
|
|
identicon, err := identicon.GenerateBase64(contact.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
contact.Identicon = identicon
|
|
|
|
|
|
|
|
name, err := alias.GenerateFromPublicKeyString(contact.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
contact.Alias = name
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
err = m.persistence.SaveContact(contact, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.allContacts[contact.ID] = contact
|
|
|
|
return nil
|
2019-08-20 11:20:25 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func (m *Messenger) BlockContact(contact *Contact) ([]*Chat, error) {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
chats, err := m.persistence.BlockContact(contact)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m.allContacts[contact.ID] = contact
|
|
|
|
for _, chat := range chats {
|
|
|
|
m.allChats[chat.ID] = chat
|
|
|
|
}
|
|
|
|
delete(m.allChats, contact.ID)
|
|
|
|
return chats, nil
|
2019-07-30 18:39:16 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func (m *Messenger) Contacts() []*Contact {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
var contacts []*Contact
|
|
|
|
for _, contact := range m.allContacts {
|
|
|
|
contacts = append(contacts, contact)
|
|
|
|
}
|
|
|
|
|
|
|
|
return contacts
|
2019-07-30 18:39:16 +00:00
|
|
|
}
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
func timestampInMs() uint64 {
|
|
|
|
return uint64(time.Now().UnixNano() / int64(time.Millisecond))
|
|
|
|
}
|
2019-09-02 09:29:06 +00:00
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
// ReSendChatMessage pulls a message from the database and sends it again
|
|
|
|
func (m *Messenger) ReSendChatMessage(ctx context.Context, messageID string) (*MessengerResponse, error) {
|
2019-12-02 15:34:05 +00:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
logger := m.logger.With(zap.String("site", "ReSendChatMessage"))
|
|
|
|
var response MessengerResponse
|
|
|
|
message, err := m.persistence.MessageByID(messageID)
|
2019-09-26 07:01:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
if message == nil {
|
|
|
|
return nil, errors.New("message not found")
|
|
|
|
}
|
|
|
|
if message.RawPayload == nil {
|
|
|
|
return nil, errors.New("message payload not found, can't resend message")
|
|
|
|
}
|
2019-07-17 22:25:42 +00:00
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
chat, ok := m.allChats[message.LocalChatID]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("chat not found")
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
2019-10-14 14:10:48 +00:00
|
|
|
switch chat.ChatType {
|
|
|
|
case ChatTypeOneToOne:
|
2019-12-02 15:34:05 +00:00
|
|
|
publicKey, err := chat.PublicKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
logger.Debug("re-sending private message")
|
|
|
|
id, err := m.processor.SendPrivateRaw(ctx, publicKey, message.RawPayload, protobuf.ApplicationMetadataMessage_CHAT_MESSAGE)
|
2019-07-17 22:25:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
message.ID = "0x" + hex.EncodeToString(id)
|
2019-12-02 15:34:05 +00:00
|
|
|
err = m.sendToPairedDevices(ctx, message.RawPayload, protobuf.ApplicationMetadataMessage_CHAT_MESSAGE)
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
if err != nil {
|
2019-10-14 14:10:48 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
|
2019-10-14 14:10:48 +00:00
|
|
|
case ChatTypePublic:
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
logger.Debug("re-sending public message", zap.String("chatName", chat.Name))
|
2019-12-02 15:34:05 +00:00
|
|
|
id, err := m.processor.SendPublicRaw(ctx, chat.ID, message.RawPayload, protobuf.ApplicationMetadataMessage_CHAT_MESSAGE)
|
|
|
|
|
2019-10-14 14:10:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
message.ID = "0x" + hex.EncodeToString(id)
|
2019-10-14 14:10:48 +00:00
|
|
|
case ChatTypePrivateGroupChat:
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
logger.Debug("re-sending group message", zap.String("chatName", chat.Name))
|
2019-10-14 14:10:48 +00:00
|
|
|
recipients, err := chat.MembersAsPublicKeys()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
|
2019-10-14 14:10:48 +00:00
|
|
|
n := 0
|
|
|
|
for _, item := range recipients {
|
|
|
|
if !isPubKeyEqual(item, &m.identity.PublicKey) {
|
|
|
|
recipients[n] = item
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
id, err := m.processor.SendGroupRaw(ctx, recipients[:n], message.RawPayload, protobuf.ApplicationMetadataMessage_CHAT_MESSAGE)
|
|
|
|
|
2019-10-14 14:10:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
|
|
|
|
message.ID = "0x" + hex.EncodeToString(id)
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
err = m.sendToPairedDevices(ctx, message.RawPayload, protobuf.ApplicationMetadataMessage_CHAT_MESSAGE)
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
|
2019-10-14 14:10:48 +00:00
|
|
|
default:
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
return nil, errors.New("chat type not supported")
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
2019-07-17 22:25:42 +00:00
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
response.Messages = []*Message{message}
|
|
|
|
response.Chats = []*Chat{chat}
|
|
|
|
return &response, nil
|
|
|
|
}
|
2019-07-17 22:25:42 +00:00
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func (m *Messenger) hasPairedDevices() (bool, error) {
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
activeInstallations, err := m.encryptor.GetOurActiveInstallations(&m.identity.PublicKey)
|
2019-12-02 15:34:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return len(activeInstallations) > 1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendToPairedDevices will check if we have any paired devices and send to them if necessary
|
|
|
|
func (m *Messenger) sendToPairedDevices(ctx context.Context, payload []byte, messageType protobuf.ApplicationMetadataMessage_Type) error {
|
|
|
|
hasPairedDevices, err := m.hasPairedDevices()
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// We send a message to any paired device
|
2019-12-02 15:34:05 +00:00
|
|
|
if hasPairedDevices {
|
|
|
|
_, err := m.processor.SendPrivateRaw(ctx, &m.identity.PublicKey, payload, messageType)
|
2019-10-14 14:10:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
2019-10-14 14:10:48 +00:00
|
|
|
return nil
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
// SendChatMessage takes a minimal message and sends it based on the corresponding chat
|
|
|
|
func (m *Messenger) SendChatMessage(ctx context.Context, message *Message) (*MessengerResponse, error) {
|
2019-12-02 15:34:05 +00:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
logger := m.logger.With(zap.String("site", "Send"), zap.String("chatID", message.ChatId))
|
|
|
|
var response MessengerResponse
|
|
|
|
|
|
|
|
// A valid added chat is required.
|
2019-12-02 15:34:05 +00:00
|
|
|
chat, ok := m.allChats[message.ChatId]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("Chat not found")
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
clock := chat.LastClockValue
|
|
|
|
timestamp := timestampInMs()
|
|
|
|
if clock == 0 || clock < timestamp {
|
|
|
|
clock = timestamp
|
|
|
|
} else {
|
|
|
|
clock = clock + 1
|
|
|
|
}
|
2019-07-17 22:25:42 +00:00
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
message.LocalChatID = chat.ID
|
|
|
|
message.Clock = clock
|
|
|
|
message.Timestamp = timestamp
|
|
|
|
message.From = "0x" + hex.EncodeToString(crypto.FromECDSAPub(&m.identity.PublicKey))
|
|
|
|
message.SigPubKey = &m.identity.PublicKey
|
|
|
|
message.WhisperTimestamp = timestamp
|
|
|
|
message.Seen = true
|
|
|
|
message.OutgoingStatus = OutgoingStatusSending
|
2019-07-17 22:25:42 +00:00
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
identicon, err := identicon.GenerateBase64(message.From)
|
2019-07-26 07:17:29 +00:00
|
|
|
if err != nil {
|
2019-09-26 07:01:17 +00:00
|
|
|
return nil, err
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
message.Identicon = identicon
|
2019-07-17 22:25:42 +00:00
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
alias, err := alias.GenerateFromPublicKeyString(message.From)
|
2019-07-26 07:17:29 +00:00
|
|
|
if err != nil {
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
return nil, err
|
2019-07-26 07:17:29 +00:00
|
|
|
}
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
message.Alias = alias
|
2019-07-17 22:25:42 +00:00
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
switch chat.ChatType {
|
|
|
|
case ChatTypeOneToOne:
|
2019-12-02 15:34:05 +00:00
|
|
|
publicKey, err := chat.PublicKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
logger.Debug("sending private message")
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
message.MessageType = protobuf.ChatMessage_ONE_TO_ONE
|
|
|
|
encodedMessage, err := proto.Marshal(message)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
message.RawPayload = encodedMessage
|
2019-07-17 22:25:42 +00:00
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
id, err := m.processor.SendPrivateRaw(ctx, publicKey, encodedMessage, protobuf.ApplicationMetadataMessage_CHAT_MESSAGE)
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
message.ID = "0x" + hex.EncodeToString(id)
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
err = m.sendToPairedDevices(ctx, encodedMessage, protobuf.ApplicationMetadataMessage_CHAT_MESSAGE)
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
case ChatTypePublic:
|
|
|
|
logger.Debug("sending public message", zap.String("chatName", chat.Name))
|
|
|
|
message.MessageType = protobuf.ChatMessage_PUBLIC_GROUP
|
|
|
|
encodedMessage, err := proto.Marshal(message)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
message.RawPayload = encodedMessage
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
id, err := m.processor.SendPublicRaw(ctx, chat.ID, encodedMessage, protobuf.ApplicationMetadataMessage_CHAT_MESSAGE)
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
message.ID = "0x" + hex.EncodeToString(id)
|
|
|
|
case ChatTypePrivateGroupChat:
|
|
|
|
logger.Debug("sending public message", zap.String("chatName", chat.Name))
|
|
|
|
message.MessageType = protobuf.ChatMessage_PRIVATE_GROUP
|
|
|
|
logger.Debug("sending group message", zap.String("chatName", chat.Name))
|
|
|
|
recipients, err := chat.MembersAsPublicKeys()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
group, err := newProtocolGroupFromChat(chat)
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-26 07:01:17 +00:00
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
encodedMessage, err := m.propagateMembershipUpdates(ctx, group, recipients, &message.ChatMessage)
|
2019-09-26 07:01:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
id := v1protocol.MessageID(&m.identity.PublicKey, encodedMessage)
|
|
|
|
message.ID = "0x" + hex.EncodeToString(id)
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
default:
|
|
|
|
return nil, errors.New("chat type not supported")
|
2019-09-26 07:01:17 +00:00
|
|
|
}
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
err = message.PrepareContent()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
jsonMessage, err := json.Marshal(message)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
chat.LastClockValue = clock
|
|
|
|
chat.LastMessage = jsonMessage
|
|
|
|
chat.Timestamp = int64(timestamp)
|
2019-07-17 22:25:42 +00:00
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
err = m.persistence.SaveMessagesLegacy([]*Message{message})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Chats = []*Chat{chat}
|
|
|
|
response.Messages = []*Message{message}
|
2019-12-02 15:34:05 +00:00
|
|
|
return &response, m.saveChat(chat)
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
// SendRaw takes encoded data, encrypts it and sends through the wire.
|
2019-07-17 22:25:42 +00:00
|
|
|
// DEPRECATED
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
func (m *Messenger) SendRaw(ctx context.Context, chat Chat, data []byte) ([]byte, error) {
|
2019-12-02 15:34:05 +00:00
|
|
|
publicKey, err := chat.PublicKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if publicKey != nil {
|
|
|
|
return m.processor.SendPrivateRaw(ctx, publicKey, data, protobuf.ApplicationMetadataMessage_UNKNOWN)
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
} else if chat.Name != "" {
|
2019-12-02 15:34:05 +00:00
|
|
|
return m.processor.SendPublicRaw(ctx, chat.Name, data, protobuf.ApplicationMetadataMessage_UNKNOWN)
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
}
|
|
|
|
return nil, errors.New("chat is neither public nor private")
|
|
|
|
}
|
|
|
|
|
|
|
|
// RetrieveAll retrieves messages from all filters, processes them and returns a
|
|
|
|
// MessengerResponse to the client
|
|
|
|
func (m *Messenger) RetrieveAll() (*MessengerResponse, error) {
|
2019-09-02 09:29:06 +00:00
|
|
|
chatWithMessages, err := m.transport.RetrieveRawAll()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
return m.handleRetrievedMessages(chatWithMessages)
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
type ReceivedMessageState struct {
|
|
|
|
// Message is the protobuf message received
|
|
|
|
Message protobuf.ChatMessage
|
|
|
|
// MessageID is the ID of the message
|
|
|
|
MessageID string
|
|
|
|
// WhisperTimestamp is the whisper timestamp of the message
|
|
|
|
WhisperTimestamp uint64
|
|
|
|
// Contact is the contact associated with the author of the message
|
|
|
|
Contact *Contact
|
|
|
|
// PublicKey is the public key of the author of the message
|
|
|
|
PublicKey *ecdsa.PublicKey
|
|
|
|
// List of chats modified
|
|
|
|
ModifiedChats map[string]bool
|
|
|
|
PostProcessor *postProcessor
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) handleChatMessage(state *ReceivedMessageState) (*Message, error) {
|
|
|
|
logger := m.logger.With(zap.String("site", "handleChatMessage"))
|
|
|
|
if err := ValidateReceivedChatMessage(&state.Message); err != nil {
|
|
|
|
logger.Warn("failed to validate message", zap.Error(err))
|
|
|
|
return nil, err
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
receivedMessage := &Message{
|
|
|
|
ID: state.MessageID,
|
|
|
|
ChatMessage: state.Message,
|
|
|
|
From: state.Contact.ID,
|
|
|
|
Alias: state.Contact.Alias,
|
|
|
|
SigPubKey: state.PublicKey,
|
|
|
|
Identicon: state.Contact.Identicon,
|
|
|
|
WhisperTimestamp: state.WhisperTimestamp,
|
|
|
|
}
|
|
|
|
receivedMessage.PrepareContent()
|
|
|
|
chat, err := state.PostProcessor.matchMessage(receivedMessage, m.allChats)
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
// If deleted-at is greater, ignore message
|
|
|
|
if chat.DeletedAtClockValue >= receivedMessage.Clock {
|
|
|
|
return nil, nil
|
|
|
|
}
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
// Set the LocalChatID for the message
|
|
|
|
receivedMessage.LocalChatID = chat.ID
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
if c, ok := m.allChats[chat.ID]; ok {
|
|
|
|
chat = c
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
// Set the LocalChatID for the message
|
|
|
|
receivedMessage.LocalChatID = chat.ID
|
|
|
|
|
|
|
|
// Increase unviewed count
|
|
|
|
if !isPubKeyEqual(receivedMessage.SigPubKey, &m.identity.PublicKey) {
|
|
|
|
chat.UnviewedMessagesCount++
|
|
|
|
} else {
|
|
|
|
// Our own message, mark as sent
|
|
|
|
receivedMessage.OutgoingStatus = OutgoingStatusSent
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update chat timestamp
|
|
|
|
chat.Timestamp = int64(timestampInMs())
|
|
|
|
// Update last clock value
|
|
|
|
if chat.LastClockValue <= receivedMessage.Clock {
|
|
|
|
chat.LastClockValue = receivedMessage.Clock
|
|
|
|
encodedLastMessage, err := json.Marshal(receivedMessage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
chat.LastMessage = encodedLastMessage
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
// Set chat active
|
|
|
|
chat.Active = true
|
|
|
|
// Set in the modified maps chat
|
|
|
|
state.ModifiedChats[chat.ID] = true
|
|
|
|
m.allChats[chat.ID] = chat
|
|
|
|
|
|
|
|
return receivedMessage, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) messageExists(messageID string, existingMessagesMap map[string]bool) (bool, error) {
|
|
|
|
if _, ok := existingMessagesMap[messageID]; ok {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
existingMessagesMap[messageID] = true
|
|
|
|
|
|
|
|
// Check against the database, this is probably a bit slow for
|
|
|
|
// each message, but for now might do, we'll make it faster later
|
|
|
|
existingMessage, err := m.persistence.MessageByID(messageID)
|
|
|
|
if err != nil && err != errRecordNotFound {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if existingMessage != nil {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) handleRetrievedMessages(chatWithMessages map[transport.Filter][]*types.Message) (*MessengerResponse, error) {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
response := &MessengerResponse{
|
|
|
|
Chats: []*Chat{},
|
|
|
|
Messages: []*Message{},
|
|
|
|
}
|
|
|
|
|
|
|
|
postProcessor := newPostProcessor(m, postProcessorConfig{MatchChat: true})
|
|
|
|
|
|
|
|
logger := m.logger.With(zap.String("site", "RetrieveAll"))
|
|
|
|
rawMessages := make(map[transport.Filter][]*v1protocol.StatusMessage)
|
|
|
|
|
|
|
|
modifiedChats := make(map[string]bool)
|
|
|
|
modifiedContacts := make(map[string]bool)
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
existingMessagesMap := make(map[string]bool)
|
2019-09-02 09:29:06 +00:00
|
|
|
|
|
|
|
for chat, messages := range chatWithMessages {
|
2019-10-09 14:22:53 +00:00
|
|
|
for _, shhMessage := range messages {
|
2019-09-02 09:29:06 +00:00
|
|
|
// TODO: fix this to use an exported method.
|
2019-11-06 16:23:11 +00:00
|
|
|
statusMessages, err := m.processor.handleMessages(shhMessage, true)
|
2019-09-02 09:29:06 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Info("failed to decode messages", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
2019-09-26 07:01:17 +00:00
|
|
|
|
2019-11-15 08:52:28 +00:00
|
|
|
for _, msg := range statusMessages {
|
2019-12-02 15:34:05 +00:00
|
|
|
publicKey := msg.SigPubKey()
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
// Check for messages from blocked users
|
2019-12-02 15:34:05 +00:00
|
|
|
senderID := types.EncodeHex(crypto.FromECDSAPub(publicKey))
|
|
|
|
if _, ok := m.allContacts[senderID]; ok && m.allContacts[senderID].IsBlocked() {
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Don't process duplicates
|
2019-12-02 15:34:05 +00:00
|
|
|
messageID := types.EncodeHex(msg.ID)
|
|
|
|
exists, err := m.messageExists(messageID, existingMessagesMap)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn("failed to check message exists", zap.Error(err))
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
if exists {
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var contact *Contact
|
2019-12-02 15:34:05 +00:00
|
|
|
if c, ok := m.allContacts[senderID]; ok {
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
contact = c
|
|
|
|
} else {
|
|
|
|
c, err := buildContact(publicKey)
|
|
|
|
if err != nil {
|
|
|
|
logger.Info("failed to build contact", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
contact = c
|
2019-12-02 15:34:05 +00:00
|
|
|
m.allContacts[senderID] = c
|
|
|
|
modifiedContacts[contact.ID] = true
|
|
|
|
}
|
|
|
|
messageState := &ReceivedMessageState{
|
|
|
|
MessageID: messageID,
|
|
|
|
WhisperTimestamp: uint64(msg.TransportMessage.Timestamp) * 1000,
|
|
|
|
Contact: contact,
|
|
|
|
PublicKey: publicKey,
|
|
|
|
ModifiedChats: modifiedChats,
|
|
|
|
PostProcessor: postProcessor,
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 08:52:28 +00:00
|
|
|
if msg.ParsedMessage != nil {
|
2019-12-02 15:34:05 +00:00
|
|
|
switch msg.ParsedMessage.(type) {
|
|
|
|
case protobuf.MembershipUpdateMessage:
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
rawMembershipUpdate := msg.ParsedMessage.(protobuf.MembershipUpdateMessage)
|
|
|
|
membershipUpdate, err := v1protocol.MembershipUpdateMessageFromProtobuf(&rawMembershipUpdate)
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
if err != nil {
|
2019-12-02 15:34:05 +00:00
|
|
|
logger.Warn("failed to process membership update", zap.Error(err))
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
continue
|
2019-12-02 15:34:05 +00:00
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
chat, systemMessages, err := HandleMembershipUpdate(m.allChats[membershipUpdate.ChatID], membershipUpdate, types.EncodeHex(crypto.FromECDSAPub(&m.identity.PublicKey)), m.systemMessagesTranslations)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn("failed to process membership update", zap.Error(err))
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
for _, message := range systemMessages {
|
|
|
|
messageID := message.ID
|
|
|
|
exists, err := m.messageExists(messageID, existingMessagesMap)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn("failed to check message exists", zap.Error(err))
|
|
|
|
}
|
|
|
|
if exists {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
response.Messages = append(response.Messages, message)
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
// Store in chats map as it might be a new one
|
|
|
|
m.allChats[chat.ID] = chat
|
|
|
|
// Set in the map
|
|
|
|
modifiedChats[chat.ID] = true
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
if rawMembershipUpdate.Message != nil {
|
|
|
|
messageState.Message = *rawMembershipUpdate.Message
|
|
|
|
receivedMessage, err := m.handleChatMessage(messageState)
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
if err != nil {
|
2019-12-02 15:34:05 +00:00
|
|
|
logger.Warn("failed to process message", zap.Error(err))
|
|
|
|
continue
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
// Add to response
|
|
|
|
if receivedMessage != nil {
|
|
|
|
response.Messages = append(response.Messages, receivedMessage)
|
|
|
|
}
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
case protobuf.ChatMessage:
|
|
|
|
messageState.Message = msg.ParsedMessage.(protobuf.ChatMessage)
|
|
|
|
receivedMessage, err := m.handleChatMessage(messageState)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn("failed to process message", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
// Add to response
|
2019-12-02 15:34:05 +00:00
|
|
|
if receivedMessage != nil {
|
|
|
|
response.Messages = append(response.Messages, receivedMessage)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// RawMessage, not processed here, pass straight to the client
|
|
|
|
rawMessages[chat] = append(rawMessages[chat], msg)
|
|
|
|
|
2019-11-15 08:52:28 +00:00
|
|
|
}
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
} else {
|
|
|
|
rawMessages[chat] = append(rawMessages[chat], msg)
|
2019-11-15 08:52:28 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-02 09:29:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
for id, _ := range modifiedChats {
|
|
|
|
response.Chats = append(response.Chats, m.allChats[id])
|
|
|
|
}
|
|
|
|
|
|
|
|
for id, _ := range modifiedContacts {
|
|
|
|
response.Contacts = append(response.Contacts, m.allContacts[id])
|
2019-09-26 07:01:17 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
var err error
|
|
|
|
if len(response.Chats) > 0 {
|
|
|
|
err = m.saveChats(response.Chats)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(response.Messages) > 0 {
|
|
|
|
err = m.SaveMessages(response.Messages)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
}
|
2019-09-26 07:01:17 +00:00
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
if len(response.Contacts) > 0 {
|
|
|
|
err = m.persistence.SaveContacts(response.Contacts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2019-09-26 07:01:17 +00:00
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
for filter, messages := range rawMessages {
|
|
|
|
response.RawMessages = append(response.RawMessages, &RawResponse{Filter: &filter, Messages: messages})
|
2019-09-26 07:01:17 +00:00
|
|
|
}
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
return response, nil
|
2019-09-26 07:01:17 +00:00
|
|
|
}
|
|
|
|
|
2019-11-06 16:23:11 +00:00
|
|
|
func (m *Messenger) RequestHistoricMessages(
|
|
|
|
ctx context.Context,
|
|
|
|
peer []byte, // should be removed after mailserver logic is ported
|
|
|
|
from, to uint32,
|
|
|
|
cursor []byte,
|
|
|
|
) ([]byte, error) {
|
|
|
|
return m.transport.SendMessagesRequest(ctx, peer, from, to, cursor)
|
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
// DEPRECATED
|
2019-08-29 06:33:46 +00:00
|
|
|
func (m *Messenger) LoadFilters(filters []*transport.Filter) ([]*transport.Filter, error) {
|
2019-09-02 09:29:06 +00:00
|
|
|
return m.transport.LoadFilters(filters)
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED
|
2019-08-29 06:33:46 +00:00
|
|
|
func (m *Messenger) RemoveFilters(filters []*transport.Filter) error {
|
2019-09-02 09:29:06 +00:00
|
|
|
return m.transport.RemoveFilters(filters)
|
2019-07-17 22:25:42 +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-08-06 21:50:13 +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-20 11:20:25 +00:00
|
|
|
func (m *Messenger) MessagesExist(ids []string) (map[string]bool, error) {
|
|
|
|
return m.persistence.MessagesExist(ids)
|
2019-08-06 21:50:13 +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-20 11:20:25 +00:00
|
|
|
func (m *Messenger) SaveMessages(messages []*Message) error {
|
|
|
|
return m.persistence.SaveMessagesLegacy(messages)
|
2019-08-06 21:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED: required by status-react.
|
2019-08-20 11:20:25 +00:00
|
|
|
func (m *Messenger) DeleteMessage(id string) error {
|
|
|
|
return m.persistence.DeleteMessage(id)
|
2019-08-06 21:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED: required by status-react.
|
2019-08-20 11:20:25 +00:00
|
|
|
func (m *Messenger) DeleteMessagesByChatID(id string) error {
|
|
|
|
return m.persistence.DeleteMessagesByChatID(id)
|
2019-08-06 21:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED: required by status-react.
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
func (m *Messenger) MarkMessagesSeen(chatID string, ids []string) error {
|
2019-12-02 15:34:05 +00:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
err := m.persistence.MarkMessagesSeen(chatID, ids)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
chat, err := m.persistence.Chat(chatID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.allChats[chatID] = chat
|
|
|
|
return nil
|
2019-08-06 21:50:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED: required by status-react.
|
|
|
|
func (m *Messenger) UpdateMessageOutgoingStatus(id, newOutgoingStatus string) error {
|
|
|
|
return m.persistence.UpdateMessageOutgoingStatus(id, newOutgoingStatus)
|
|
|
|
}
|
2019-09-26 07:01:17 +00:00
|
|
|
|
|
|
|
// postProcessor performs a set of actions on newly retrieved messages.
|
|
|
|
// If persist is true, it saves the messages into the database.
|
|
|
|
// If matchChat is true, it matches each messages against a Chat instance.
|
|
|
|
type postProcessor struct {
|
|
|
|
myPublicKey *ecdsa.PublicKey
|
|
|
|
persistence *sqlitePersistence
|
|
|
|
logger *zap.Logger
|
|
|
|
|
|
|
|
config postProcessorConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
type postProcessorConfig struct {
|
|
|
|
MatchChat bool // match each messages to a chat; may result in a new chat creation
|
|
|
|
Persist bool // if true, all sent and received user messages will be persisted
|
2019-11-15 08:52:28 +00:00
|
|
|
Parse bool // if true, it will parse the content
|
2019-09-26 07:01:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newPostProcessor(m *Messenger, config postProcessorConfig) *postProcessor {
|
|
|
|
return &postProcessor{
|
|
|
|
myPublicKey: &m.identity.PublicKey,
|
|
|
|
persistence: m.persistence,
|
|
|
|
logger: m.logger,
|
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func (p *postProcessor) matchMessage(message *Message, chats map[string]*Chat) (*Chat, error) {
|
2019-11-04 10:08:22 +00:00
|
|
|
if message.SigPubKey == nil {
|
|
|
|
p.logger.Error("public key can't be empty")
|
|
|
|
return nil, errors.New("received a message with empty public key")
|
|
|
|
}
|
|
|
|
|
2019-09-26 07:01:17 +00:00
|
|
|
switch {
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
case message.MessageType == protobuf.ChatMessage_PUBLIC_GROUP:
|
2019-09-26 07:01:17 +00:00
|
|
|
// For public messages, all outgoing and incoming messages have the same chatID
|
|
|
|
// equal to a public chat name.
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
chatID := message.ChatId
|
2019-12-02 15:34:05 +00:00
|
|
|
chat := chats[chatID]
|
2019-09-26 07:01:17 +00:00
|
|
|
if chat == nil {
|
|
|
|
return nil, errors.New("received a public message from non-existing chat")
|
|
|
|
}
|
|
|
|
return chat, nil
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
case message.MessageType == protobuf.ChatMessage_ONE_TO_ONE && isPubKeyEqual(message.SigPubKey, p.myPublicKey):
|
|
|
|
// It's a private message coming from us so we rely on Message.ChatId
|
2019-09-26 07:01:17 +00:00
|
|
|
// If chat does not exist, it should be created to support multidevice synchronization.
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
chatID := message.ChatId
|
2019-12-02 15:34:05 +00:00
|
|
|
chat := chats[chatID]
|
2019-09-26 07:01:17 +00:00
|
|
|
if chat == nil {
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
if len(chatID) != PubKeyStringLength {
|
|
|
|
return nil, errors.New("invalid pubkey length")
|
|
|
|
}
|
|
|
|
bytePubKey, err := hex.DecodeString(chatID[2:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to decode hex chatID")
|
|
|
|
}
|
|
|
|
|
|
|
|
pubKey, err := crypto.UnmarshalPubkey(bytePubKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to decode pubkey")
|
|
|
|
}
|
|
|
|
|
|
|
|
newChat := CreateOneToOneChat(chatID[:8], pubKey)
|
2019-09-26 07:01:17 +00:00
|
|
|
chat = &newChat
|
|
|
|
}
|
|
|
|
return chat, nil
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
case message.MessageType == protobuf.ChatMessage_ONE_TO_ONE:
|
2019-09-26 07:01:17 +00:00
|
|
|
// It's an incoming private message. ChatID is calculated from the signature.
|
|
|
|
// If a chat does not exist, a new one is created and saved.
|
2019-11-23 17:57:05 +00:00
|
|
|
chatID := types.EncodeHex(crypto.FromECDSAPub(message.SigPubKey))
|
2019-12-02 15:34:05 +00:00
|
|
|
chat := chats[chatID]
|
2019-09-26 07:01:17 +00:00
|
|
|
if chat == nil {
|
|
|
|
// TODO: this should be a three-word name used in the mobile client
|
|
|
|
newChat := CreateOneToOneChat(chatID[:8], message.SigPubKey)
|
|
|
|
chat = &newChat
|
|
|
|
}
|
|
|
|
return chat, nil
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
case message.MessageType == protobuf.ChatMessage_PRIVATE_GROUP:
|
2019-09-26 07:01:17 +00:00
|
|
|
// In the case of a group message, ChatID is the same for all messages belonging to a group.
|
|
|
|
// It needs to be verified if the signature public key belongs to the chat.
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
chatID := message.ChatId
|
2019-12-02 15:34:05 +00:00
|
|
|
chat := chats[chatID]
|
2019-11-04 10:08:22 +00:00
|
|
|
if chat == nil {
|
|
|
|
return nil, errors.New("received group chat message for non-existing chat")
|
|
|
|
}
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
theirKeyHex := types.EncodeHex(crypto.FromECDSAPub(message.SigPubKey))
|
|
|
|
myKeyHex := types.EncodeHex(crypto.FromECDSAPub(p.myPublicKey))
|
|
|
|
var theyJoined bool
|
|
|
|
var iJoined bool
|
2019-09-26 07:01:17 +00:00
|
|
|
for _, member := range chat.Members {
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
if member.ID == theirKeyHex && member.Joined {
|
|
|
|
theyJoined = true
|
2019-09-26 07:01:17 +00:00
|
|
|
}
|
|
|
|
}
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
for _, member := range chat.Members {
|
|
|
|
if member.ID == myKeyHex && member.Joined {
|
|
|
|
iJoined = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if theyJoined && iJoined {
|
|
|
|
return chat, nil
|
|
|
|
}
|
|
|
|
|
2019-09-26 07:01:17 +00:00
|
|
|
return nil, errors.New("did not find a matching group chat")
|
|
|
|
default:
|
|
|
|
return nil, errors.New("can not match a chat because there is no valid case")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Identicon returns an identicon based on the input string
|
|
|
|
func Identicon(id string) (string, error) {
|
|
|
|
return identicon.GenerateBase64(id)
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
// VerifyENSNames verifies that a registered ENS name matches the expected public key
|
|
|
|
func (m *Messenger) VerifyENSNames(rpcEndpoint, contractAddress string, ensDetails []enstypes.ENSDetails) (map[string]enstypes.ENSResponse, error) {
|
2019-12-02 15:34:05 +00:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
2019-11-23 17:57:05 +00:00
|
|
|
verifier := m.node.NewENSVerifier(m.logger)
|
2019-11-04 10:08:22 +00:00
|
|
|
|
|
|
|
ensResponse, err := verifier.CheckBatch(ensDetails, rpcEndpoint, contractAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update contacts
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
var contacts []*Contact
|
2019-11-04 10:08:22 +00:00
|
|
|
for _, details := range ensResponse {
|
|
|
|
if details.Error == nil {
|
2019-12-02 15:34:05 +00:00
|
|
|
contact, ok := m.allContacts[details.PublicKeyString]
|
|
|
|
if !ok {
|
|
|
|
contact, err = buildContact(details.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-04 10:08:22 +00:00
|
|
|
}
|
|
|
|
contact.ENSVerified = details.Verified
|
|
|
|
contact.ENSVerifiedAt = details.VerifiedAt
|
|
|
|
contact.Name = details.Name
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
contacts = append(contacts, contact)
|
2019-11-04 10:08:22 +00:00
|
|
|
} else {
|
|
|
|
m.logger.Warn("Failed to resolve ens name",
|
|
|
|
zap.String("name", details.Name),
|
|
|
|
zap.String("publicKey", details.PublicKeyString),
|
|
|
|
zap.Error(details.Error),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(contacts) != 0 {
|
2019-12-02 15:34:05 +00:00
|
|
|
err = m.persistence.SaveContacts(contacts)
|
2019-11-04 10:08:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ensResponse, nil
|
|
|
|
}
|
|
|
|
|
2019-09-26 07:01:17 +00:00
|
|
|
// GenerateAlias name returns the generated name given a public key hex encoded prefixed with 0x
|
|
|
|
func GenerateAlias(id string) (string, error) {
|
|
|
|
return alias.GenerateFromPublicKeyString(id)
|
|
|
|
}
|