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"
|
2020-05-13 13:16:17 +00:00
|
|
|
"io/ioutil"
|
2019-12-02 15:34:05 +00:00
|
|
|
"math/rand"
|
2020-05-13 13:16:17 +00:00
|
|
|
"os"
|
2019-12-02 15:34:05 +00:00
|
|
|
"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"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
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"
|
2020-05-13 13:16:17 +00:00
|
|
|
"github.com/status-im/status-go/protocol/images"
|
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"
|
2020-01-13 19:17:30 +00:00
|
|
|
"github.com/status-im/status-go/protocol/transport"
|
|
|
|
wakutransp "github.com/status-im/status-go/protocol/transport/waku"
|
|
|
|
shhtransp "github.com/status-im/status-go/protocol/transport/whisper"
|
2019-11-21 16:19:22 +00:00
|
|
|
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
|
|
|
|
|
2020-02-10 11:22:37 +00:00
|
|
|
const transactionSentTxt = "Transaction sent"
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
var (
|
|
|
|
ErrChatIDEmpty = errors.New("chat ID is empty")
|
2020-04-14 11:48:32 +00:00
|
|
|
ErrChatNotFound = errors.New("can't find chat")
|
2019-07-17 22:25:42 +00:00
|
|
|
ErrNotImplemented = errors.New("not implemented")
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2020-01-13 19:17:30 +00:00
|
|
|
transport transport.Transport
|
2019-12-02 15:34:05 +00:00
|
|
|
encryptor *encryption.Protocol
|
|
|
|
processor *messageProcessor
|
2020-01-10 18:59:01 +00:00
|
|
|
handler *MessageHandler
|
2019-12-02 15:34:05 +00:00
|
|
|
logger *zap.Logger
|
2020-01-10 18:59:01 +00:00
|
|
|
verifyTransactionClient EthClient
|
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
|
2020-01-10 18:59:01 +00:00
|
|
|
allInstallations map[string]*multidevice.Installation
|
|
|
|
modifiedInstallations map[string]bool
|
|
|
|
installationID string
|
|
|
|
|
|
|
|
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 {
|
2020-02-10 11:22:37 +00:00
|
|
|
Chats []*Chat `json:"chats,omitempty"`
|
|
|
|
Messages []*Message `json:"messages,omitempty"`
|
|
|
|
Contacts []*Contact `json:"contacts,omitempty"`
|
|
|
|
Installations []*multidevice.Installation `json:"installations,omitempty"`
|
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 *MessengerResponse) IsEmpty() bool {
|
2020-01-28 11:16:28 +00:00
|
|
|
return len(m.Chats) == 0 && len(m.Messages) == 0 && len(m.Contacts) == 0 && len(m.Installations) == 0
|
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-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 {
|
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
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
verifyTransactionClient EthClient
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(*config) error
|
|
|
|
|
2020-02-07 11:30:26 +00:00
|
|
|
// WithSystemMessagesTranslations is required for Group Chats which are currently disabled.
|
|
|
|
// nolint: unused
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
func WithVerifyTransactionClient(client EthClient) Option {
|
|
|
|
return func(c *config) error {
|
|
|
|
c.verifyTransactionClient = client
|
|
|
|
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
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
onNewInstallationsHandler := func(installations []*multidevice.Installation) {
|
|
|
|
|
|
|
|
for _, installation := range installations {
|
2020-01-15 07:25:09 +00:00
|
|
|
if installation.Identity == contactIDFromPublicKey(&messenger.identity.PublicKey) {
|
2020-01-10 18:59:01 +00:00
|
|
|
if _, ok := messenger.allInstallations[installation.ID]; !ok {
|
|
|
|
messenger.allInstallations[installation.ID] = installation
|
|
|
|
messenger.modifiedInstallations[installation.ID] = true
|
|
|
|
}
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
// Set default config fields.
|
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.
|
2020-01-13 19:17:30 +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.
|
2020-01-13 19:17:30 +00:00
|
|
|
var transp transport.Transport
|
2020-01-20 20:56:06 +00:00
|
|
|
if shh, err := node.GetWhisper(nil); err == nil && shh != nil {
|
2020-02-10 11:22:37 +00:00
|
|
|
transp, err = shhtransp.NewTransport(
|
2020-01-13 19:17:30 +00:00
|
|
|
shh,
|
|
|
|
identity,
|
|
|
|
database,
|
|
|
|
nil,
|
|
|
|
c.envelopesMonitorConfig,
|
|
|
|
logger,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2020-02-10 11:22:37 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to create Transport")
|
2020-01-13 19:17:30 +00:00
|
|
|
}
|
2020-01-20 20:56:06 +00:00
|
|
|
} else {
|
2020-01-13 19:17:30 +00:00
|
|
|
logger.Info("failed to find Whisper service; trying Waku", zap.Error(err))
|
|
|
|
waku, err := node.GetWaku(nil)
|
2020-01-20 20:56:06 +00:00
|
|
|
if err != nil || waku == nil {
|
2020-01-13 19:17:30 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to find Whisper and Waku services")
|
|
|
|
}
|
2020-02-10 11:22:37 +00:00
|
|
|
transp, err = wakutransp.NewTransport(
|
2020-01-13 19:17:30 +00:00
|
|
|
waku,
|
|
|
|
identity,
|
|
|
|
database,
|
|
|
|
nil,
|
|
|
|
c.envelopesMonitorConfig,
|
|
|
|
logger,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2020-02-10 11:22:37 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to create Transport")
|
2020-01-13 19:17:30 +00:00
|
|
|
}
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 06:14:13 +00:00
|
|
|
// Initialize encryption layer.
|
|
|
|
encryptionProtocol := encryption.New(
|
|
|
|
database,
|
2019-07-17 22:25:42 +00:00
|
|
|
installationID,
|
2020-01-10 18:59:01 +00:00
|
|
|
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,
|
2020-01-13 19:17:30 +00:00
|
|
|
transp,
|
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
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
handler := newMessageHandler(identity, logger, &sqlitePersistence{db: database})
|
|
|
|
|
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},
|
2020-01-13 19:17:30 +00:00
|
|
|
transport: transp,
|
2019-07-17 22:25:42 +00:00
|
|
|
encryptor: encryptionProtocol,
|
2019-09-02 09:29:06 +00:00
|
|
|
processor: processor,
|
2020-01-10 18:59:01 +00:00
|
|
|
handler: handler,
|
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),
|
2020-01-10 18:59:01 +00:00
|
|
|
allInstallations: make(map[string]*multidevice.Installation),
|
|
|
|
installationID: installationID,
|
|
|
|
modifiedInstallations: make(map[string]bool),
|
2019-07-17 22:25:42 +00:00
|
|
|
messagesPersistenceEnabled: c.messagesPersistenceEnabled,
|
2020-01-10 18:59:01 +00:00
|
|
|
verifyTransactionClient: c.verifyTransactionClient,
|
2019-07-17 22:25:42 +00:00
|
|
|
shutdownTasks: []func() error{
|
2019-07-30 06:14:13 +00:00
|
|
|
database.Close,
|
2020-01-13 19:17:30 +00:00
|
|
|
transp.ResetFilters,
|
|
|
|
transp.Stop,
|
2019-09-02 09:29:06 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debug("messages persistence", zap.Bool("enabled", c.messagesPersistenceEnabled))
|
|
|
|
|
|
|
|
return messenger, nil
|
|
|
|
}
|
|
|
|
|
2020-01-29 19:40:06 +00:00
|
|
|
func (m *Messenger) Start() error {
|
|
|
|
return m.encryptor.Start(m.identity)
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-02-07 11:56:30 +00:00
|
|
|
if err := chat.Validate(); err != nil {
|
|
|
|
logger.Warn("failed to validate chat", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
installations, err := m.encryptor.GetOurInstallations(&m.identity.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, installation := range installations {
|
|
|
|
m.allInstallations[installation.ID] = installation
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-01-10 18:59:01 +00:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
installation, ok := m.allInstallations[id]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("no installation found")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := m.encryptor.EnableInstallation(&m.identity.PublicKey, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
installation.Enabled = true
|
|
|
|
m.allInstallations[id] = installation
|
|
|
|
return nil
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) DisableInstallation(id string) error {
|
2020-01-10 18:59:01 +00:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
installation, ok := m.allInstallations[id]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("no installation found")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := m.encryptor.DisableInstallation(&m.identity.PublicKey, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
installation.Enabled = false
|
|
|
|
m.allInstallations[id] = installation
|
|
|
|
return nil
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
func (m *Messenger) Installations() []*multidevice.Installation {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
installations := make([]*multidevice.Installation, len(m.allInstallations))
|
|
|
|
|
|
|
|
var i = 0
|
|
|
|
for _, installation := range m.allInstallations {
|
|
|
|
installations[i] = installation
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return installations
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
func (m *Messenger) setInstallationMetadata(id string, data *multidevice.InstallationMetadata) error {
|
|
|
|
installation, ok := m.allInstallations[id]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("no installation found")
|
|
|
|
}
|
|
|
|
|
|
|
|
installation.InstallationMetadata = data
|
2019-07-17 22:25:42 +00:00
|
|
|
return m.encryptor.SetInstallationMetadata(&m.identity.PublicKey, id, data)
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
func (m *Messenger) SetInstallationMetadata(id string, data *multidevice.InstallationMetadata) error {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
return m.setInstallationMetadata(id, data)
|
|
|
|
}
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
// 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:
|
2020-01-15 07:25:09 +00:00
|
|
|
return m.transport.JoinPublic(chat.ID)
|
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))
|
2020-02-07 11:30:26 +00:00
|
|
|
chat := CreateGroupChat(m.getTimesource())
|
2020-02-29 08:26:08 +00:00
|
|
|
|
|
|
|
clock, _ := chat.NextClockAndTimestamp(m.getTimesource())
|
|
|
|
|
|
|
|
group, err := v1protocol.NewGroupWithCreator(name, clock, m.identity)
|
2019-10-14 14:10:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-29 08:26:08 +00:00
|
|
|
chat.LastClockValue = clock
|
2020-04-22 12:58:28 +00:00
|
|
|
|
|
|
|
chat.updateChatFromGroupMembershipChanges(contactIDFromPublicKey(&m.identity.PublicKey), group)
|
2019-12-02 15:34:05 +00:00
|
|
|
|
2020-02-29 08:26:08 +00:00
|
|
|
clock, _ = chat.NextClockAndTimestamp(m.getTimesource())
|
2019-12-02 15:34:05 +00:00
|
|
|
// Add members
|
2020-01-15 07:25:09 +00:00
|
|
|
event := v1protocol.NewMembersAddedEvent(members, clock)
|
2019-12-02 15:34:05 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
encodedMessage, err := m.processor.EncodeMembershipUpdate(group, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m.allChats[chat.ID] = &chat
|
|
|
|
|
|
|
|
_, err = m.dispatchMessage(ctx, &RawMessage{
|
|
|
|
LocalChatID: chat.ID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_MEMBERSHIP_UPDATE_MESSAGE,
|
|
|
|
Recipients: recipients,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2019-12-02 15:34:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
|
2020-04-22 12:58:28 +00:00
|
|
|
chat.updateChatFromGroupMembershipChanges(contactIDFromPublicKey(&m.identity.PublicKey), group)
|
2019-12-02 15:34:05 +00:00
|
|
|
|
|
|
|
response.Chats = []*Chat{&chat}
|
|
|
|
response.Messages = buildSystemMessages(chat.MembershipUpdates, m.systemMessagesTranslations)
|
2020-05-20 12:16:12 +00:00
|
|
|
err = m.persistence.SaveMessages(response.Messages)
|
2020-01-17 12:39:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
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 {
|
2020-04-14 11:48:32 +00:00
|
|
|
return nil, ErrChatNotFound
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
clock, _ := chat.NextClockAndTimestamp(m.getTimesource())
|
2019-12-02 15:34:05 +00:00
|
|
|
// Remove member
|
2020-01-15 07:25:09 +00:00
|
|
|
event := v1protocol.NewMemberRemovedEvent(member, clock)
|
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
|
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
encodedMessage, err := m.processor.EncodeMembershipUpdate(group, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = m.dispatchMessage(ctx, &RawMessage{
|
|
|
|
LocalChatID: chat.ID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_MEMBERSHIP_UPDATE_MESSAGE,
|
|
|
|
Recipients: oldRecipients,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-12-02 15:34:05 +00:00
|
|
|
return nil, err
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
|
2020-04-22 12:58:28 +00:00
|
|
|
chat.updateChatFromGroupMembershipChanges(contactIDFromPublicKey(&m.identity.PublicKey), group)
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
response.Chats = []*Chat{chat}
|
|
|
|
response.Messages = buildSystemMessages(chat.MembershipUpdates, m.systemMessagesTranslations)
|
2020-05-20 12:16:12 +00:00
|
|
|
err = m.persistence.SaveMessages(response.Messages)
|
2020-01-17 12:39:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
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 {
|
2020-04-14 11:48:32 +00:00
|
|
|
return nil, ErrChatNotFound
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
clock, _ := chat.NextClockAndTimestamp(m.getTimesource())
|
2019-12-02 15:34:05 +00:00
|
|
|
// Add members
|
2020-01-15 07:25:09 +00:00
|
|
|
event := v1protocol.NewMembersAddedEvent(members, clock)
|
2019-12-02 15:34:05 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
encodedMessage, err := m.processor.EncodeMembershipUpdate(group, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = m.dispatchMessage(ctx, &RawMessage{
|
|
|
|
LocalChatID: chat.ID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_MEMBERSHIP_UPDATE_MESSAGE,
|
|
|
|
Recipients: recipients,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2019-12-02 15:34:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
|
2020-04-22 12:58:28 +00:00
|
|
|
chat.updateChatFromGroupMembershipChanges(contactIDFromPublicKey(&m.identity.PublicKey), group)
|
2019-12-02 15:34:05 +00:00
|
|
|
|
|
|
|
response.Chats = []*Chat{chat}
|
|
|
|
response.Messages = buildSystemMessages([]v1protocol.MembershipUpdateEvent{event}, m.systemMessagesTranslations)
|
2020-05-20 12:16:12 +00:00
|
|
|
err = m.persistence.SaveMessages(response.Messages)
|
2020-01-17 12:39:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
|
|
|
|
return &response, m.saveChat(chat)
|
|
|
|
}
|
|
|
|
|
2020-04-14 11:48:32 +00:00
|
|
|
func (m *Messenger) ChangeGroupChatName(ctx context.Context, chatID string, name string) (*MessengerResponse, error) {
|
|
|
|
logger := m.logger.With(zap.String("site", "ChangeGroupChatName"))
|
|
|
|
logger.Info("Changing group chat name", zap.String("chatID", chatID), zap.String("name", name))
|
|
|
|
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
chat, ok := m.allChats[chatID]
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrChatNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
group, err := newProtocolGroupFromChat(chat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
clock, _ := chat.NextClockAndTimestamp(m.getTimesource())
|
|
|
|
// Add members
|
|
|
|
event := v1protocol.NewNameChangedEvent(name, clock)
|
|
|
|
event.ChatID = chat.ID
|
|
|
|
err = event.Sign(m.identity)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update in-memory group
|
|
|
|
err = group.ProcessEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
recipients, err := stringSliceToPublicKeys(group.Members(), true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
encodedMessage, err := m.processor.EncodeMembershipUpdate(group, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = m.dispatchMessage(ctx, &RawMessage{
|
|
|
|
LocalChatID: chat.ID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_MEMBERSHIP_UPDATE_MESSAGE,
|
|
|
|
Recipients: recipients,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-22 12:58:28 +00:00
|
|
|
chat.updateChatFromGroupMembershipChanges(contactIDFromPublicKey(&m.identity.PublicKey), group)
|
2020-04-14 11:48:32 +00:00
|
|
|
|
|
|
|
var response MessengerResponse
|
|
|
|
response.Chats = []*Chat{chat}
|
|
|
|
response.Messages = buildSystemMessages([]v1protocol.MembershipUpdateEvent{event}, m.systemMessagesTranslations)
|
2020-05-20 12:16:12 +00:00
|
|
|
err = m.persistence.SaveMessages(response.Messages)
|
2020-04-14 11:48:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &response, m.saveChat(chat)
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
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 {
|
2020-04-14 11:48:32 +00:00
|
|
|
return nil, ErrChatNotFound
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
group, err := newProtocolGroupFromChat(chat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
clock, _ := chat.NextClockAndTimestamp(m.getTimesource())
|
2019-12-02 15:34:05 +00:00
|
|
|
// Add members
|
2020-01-15 07:25:09 +00:00
|
|
|
event := v1protocol.NewAdminsAddedEvent(members, clock)
|
2019-12-02 15:34:05 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
encodedMessage, err := m.processor.EncodeMembershipUpdate(group, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = m.dispatchMessage(ctx, &RawMessage{
|
|
|
|
LocalChatID: chat.ID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_MEMBERSHIP_UPDATE_MESSAGE,
|
|
|
|
Recipients: recipients,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2019-12-02 15:34:05 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
|
2020-04-22 12:58:28 +00:00
|
|
|
chat.updateChatFromGroupMembershipChanges(contactIDFromPublicKey(&m.identity.PublicKey), group)
|
2019-12-02 15:34:05 +00:00
|
|
|
|
|
|
|
response.Chats = []*Chat{chat}
|
|
|
|
response.Messages = buildSystemMessages([]v1protocol.MembershipUpdateEvent{event}, m.systemMessagesTranslations)
|
2020-05-20 12:16:12 +00:00
|
|
|
err = m.persistence.SaveMessages(response.Messages)
|
2020-01-17 12:39:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
|
|
|
|
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 {
|
2020-04-14 11:48:32 +00:00
|
|
|
return nil, ErrChatNotFound
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2020-01-20 16:44:32 +00:00
|
|
|
clock, _ := chat.NextClockAndTimestamp(m.getTimesource())
|
2019-11-21 16:19:22 +00:00
|
|
|
event := v1protocol.NewMemberJoinedEvent(
|
2020-01-15 07:25:09 +00:00
|
|
|
clock,
|
2019-10-14 14:10:48 +00:00
|
|
|
)
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
encodedMessage, err := m.processor.EncodeMembershipUpdate(group, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = m.dispatchMessage(ctx, &RawMessage{
|
|
|
|
LocalChatID: chat.ID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_MEMBERSHIP_UPDATE_MESSAGE,
|
|
|
|
Recipients: recipients,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-12-02 15:34:05 +00:00
|
|
|
return nil, err
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
|
2020-04-22 12:58:28 +00:00
|
|
|
chat.updateChatFromGroupMembershipChanges(contactIDFromPublicKey(&m.identity.PublicKey), group)
|
2019-12-02 15:34:05 +00:00
|
|
|
|
|
|
|
response.Chats = []*Chat{chat}
|
|
|
|
response.Messages = buildSystemMessages([]v1protocol.MembershipUpdateEvent{event}, m.systemMessagesTranslations)
|
2020-05-20 12:16:12 +00:00
|
|
|
err = m.persistence.SaveMessages(response.Messages)
|
2020-01-17 12:39:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
|
|
|
|
return &response, m.saveChat(chat)
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 11:49:03 +00:00
|
|
|
func (m *Messenger) LeaveGroupChat(ctx context.Context, chatID string, remove bool) (*MessengerResponse, error) {
|
2019-12-02 15:34:05 +00:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
var response MessengerResponse
|
|
|
|
|
|
|
|
chat, ok := m.allChats[chatID]
|
|
|
|
if !ok {
|
2020-04-14 11:48:32 +00:00
|
|
|
return nil, ErrChatNotFound
|
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
|
|
|
}
|
2020-01-20 16:44:32 +00:00
|
|
|
clock, _ := chat.NextClockAndTimestamp(m.getTimesource())
|
2019-12-02 15:34:05 +00:00
|
|
|
event := v1protocol.NewMemberRemovedEvent(
|
2020-01-15 07:25:09 +00:00
|
|
|
contactIDFromPublicKey(&m.identity.PublicKey),
|
|
|
|
clock,
|
2019-12-02 15:34:05 +00:00
|
|
|
)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
encodedMessage, err := m.processor.EncodeMembershipUpdate(group, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = m.dispatchMessage(ctx, &RawMessage{
|
|
|
|
LocalChatID: chat.ID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_MEMBERSHIP_UPDATE_MESSAGE,
|
|
|
|
Recipients: recipients,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-12-02 15:34:05 +00:00
|
|
|
return nil, err
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
|
2020-04-22 12:58:28 +00:00
|
|
|
chat.updateChatFromGroupMembershipChanges(contactIDFromPublicKey(&m.identity.PublicKey), group)
|
|
|
|
|
2020-04-14 11:49:03 +00:00
|
|
|
if remove {
|
|
|
|
chat.Active = false
|
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
|
|
|
|
response.Chats = []*Chat{chat}
|
|
|
|
response.Messages = buildSystemMessages([]v1protocol.MembershipUpdateEvent{event}, m.systemMessagesTranslations)
|
2020-05-20 12:16:12 +00:00
|
|
|
err = m.persistence.SaveMessages(response.Messages)
|
2020-01-17 12:39:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
return &response, m.saveChat(chat)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) saveChat(chat *Chat) error {
|
2020-01-15 07:25:09 +00:00
|
|
|
_, ok := m.allChats[chat.ID]
|
2020-05-20 12:16:12 +00:00
|
|
|
if chat.OneToOne() {
|
|
|
|
name, identicon, err := generateAliasAndIdenticon(chat.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
chat.Alias = name
|
|
|
|
chat.Identicon = identicon
|
|
|
|
}
|
2020-01-15 07:25:09 +00:00
|
|
|
// Sync chat if it's a new active public chat
|
|
|
|
if !ok && chat.Active && chat.Public() {
|
|
|
|
if err := m.syncPublicChat(context.Background(), chat); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
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()
|
2020-01-15 07:25:09 +00:00
|
|
|
return m.saveChat(chat)
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
func (m *Messenger) isNewContact(contact *Contact) bool {
|
|
|
|
previousContact, ok := m.allContacts[contact.ID]
|
|
|
|
return contact.IsAdded() && (!ok || !previousContact.IsAdded())
|
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
func (m *Messenger) saveContact(contact *Contact) error {
|
2020-05-20 12:16:12 +00:00
|
|
|
name, identicon, err := generateAliasAndIdenticon(contact.ID)
|
2019-09-26 07:01:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
contact.Identicon = identicon
|
|
|
|
contact.Alias = name
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
if m.isNewContact(contact) {
|
|
|
|
err := m.syncContact(context.Background(), contact)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
err = m.persistence.SaveContact(contact, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.allContacts[contact.ID] = contact
|
2020-01-10 18:59:01 +00:00
|
|
|
|
2020-05-20 12:16:12 +00:00
|
|
|
return nil
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
2020-05-20 12:16:12 +00:00
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
func (m *Messenger) SaveContact(contact *Contact) error {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
return m.saveContact(contact)
|
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 {
|
2020-04-17 11:22:38 +00:00
|
|
|
if contact.HasCustomFields() {
|
|
|
|
contacts = append(contacts, contact)
|
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
return contacts
|
2019-07-30 18:39:16 +00:00
|
|
|
}
|
|
|
|
|
2020-02-13 11:20:45 +00:00
|
|
|
// GetContactByID assumes pubKey includes 0x prefix
|
2020-06-04 13:32:47 +00:00
|
|
|
func (m *Messenger) GetContactByID(pubKey string) *Contact {
|
2020-02-13 11:20:45 +00:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
2020-06-04 13:32:47 +00:00
|
|
|
|
|
|
|
return m.allContacts[pubKey]
|
2020-02-13 11:20:45 +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
|
2020-01-10 18:59:01 +00:00
|
|
|
func (m *Messenger) ReSendChatMessage(ctx context.Context, messageID string) error {
|
2019-12-02 15:34:05 +00:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
message, err := m.persistence.RawMessageByID(messageID)
|
2019-09-26 07:01:17 +00:00
|
|
|
if err != nil {
|
2020-01-10 18:59:01 +00:00
|
|
|
return err
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
chat, ok := m.allChats[message.LocalChatID]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("chat not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = m.dispatchMessage(ctx, &RawMessage{
|
|
|
|
LocalChatID: chat.ID,
|
|
|
|
Payload: message.Payload,
|
|
|
|
MessageType: message.MessageType,
|
|
|
|
Recipients: message.Recipients,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) hasPairedDevices() bool {
|
|
|
|
var count int
|
|
|
|
for _, i := range m.allInstallations {
|
|
|
|
if i.Enabled {
|
2020-02-10 11:22:37 +00:00
|
|
|
count++
|
2020-01-10 18:59:01 +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
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
return count > 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendToPairedDevices will check if we have any paired devices and send to them if necessary
|
2020-05-13 13:24:52 +00:00
|
|
|
func (m *Messenger) sendToPairedDevices(ctx context.Context, spec *RawMessage) error {
|
2020-01-10 18:59:01 +00:00
|
|
|
hasPairedDevices := m.hasPairedDevices()
|
|
|
|
// We send a message to any paired device
|
|
|
|
if hasPairedDevices {
|
2020-05-13 13:24:52 +00:00
|
|
|
_, err := m.processor.SendPrivate(ctx, &m.identity.PublicKey, spec)
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return 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
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-07-17 22:25:42 +00:00
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
func (m *Messenger) dispatchPairInstallationMessage(ctx context.Context, spec *RawMessage) ([]byte, error) {
|
|
|
|
var err error
|
|
|
|
var id []byte
|
|
|
|
|
2020-05-13 13:24:52 +00:00
|
|
|
id, err = m.processor.SendPairInstallation(ctx, &m.identity.PublicKey, spec)
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
spec.ID = types.EncodeHex(id)
|
2020-02-10 11:22:37 +00:00
|
|
|
spec.SendCount++
|
2020-01-10 18:59:01 +00:00
|
|
|
err = m.persistence.SaveRawMessage(spec)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) dispatchMessage(ctx context.Context, spec *RawMessage) ([]byte, error) {
|
|
|
|
var err error
|
|
|
|
var id []byte
|
|
|
|
logger := m.logger.With(zap.String("site", "dispatchMessage"), zap.String("chatID", spec.LocalChatID))
|
|
|
|
chat, ok := m.allChats[spec.LocalChatID]
|
2019-12-02 15:34:05 +00:00
|
|
|
if !ok {
|
2020-01-10 18:59:01 +00:00
|
|
|
return nil, errors.New("no chat 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
|
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
if !isPubKeyEqual(publicKey, &m.identity.PublicKey) {
|
2020-05-13 13:24:52 +00:00
|
|
|
id, err = m.processor.SendPrivate(ctx, publicKey, spec)
|
2019-12-02 15:34:05 +00:00
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-14 14:10:48 +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
|
|
|
|
2020-05-13 13:24:52 +00:00
|
|
|
err = m.sendToPairedDevices(ctx, spec)
|
2019-12-02 15:34:05 +00:00
|
|
|
|
2019-10-14 14:10:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
case ChatTypePublic:
|
|
|
|
logger.Debug("sending public message", zap.String("chatName", chat.Name))
|
2020-05-13 13:24:52 +00:00
|
|
|
id, err = m.processor.SendPublic(ctx, chat.ID, spec)
|
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
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
case ChatTypePrivateGroupChat:
|
|
|
|
logger.Debug("sending group message", zap.String("chatName", chat.Name))
|
|
|
|
if spec.Recipients == nil {
|
|
|
|
spec.Recipients, err = chat.MembersAsPublicKeys()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
hasPairedDevices := m.hasPairedDevices()
|
|
|
|
|
|
|
|
if !hasPairedDevices {
|
|
|
|
// Filter out my key from the recipients
|
|
|
|
n := 0
|
|
|
|
for _, recipient := range spec.Recipients {
|
|
|
|
if !isPubKeyEqual(recipient, &m.identity.PublicKey) {
|
|
|
|
spec.Recipients[n] = recipient
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spec.Recipients = spec.Recipients[:n]
|
2019-10-14 14:10:48 +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
|
|
|
|
2020-05-13 13:24:52 +00:00
|
|
|
spec.MessageType = protobuf.ApplicationMetadataMessage_MEMBERSHIP_UPDATE_MESSAGE
|
2020-01-10 18:59:01 +00:00
|
|
|
// We always wrap in group information
|
2020-05-13 13:24:52 +00:00
|
|
|
id, err = m.processor.SendGroup(ctx, spec.Recipients, spec)
|
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
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
spec.ID = types.EncodeHex(id)
|
2020-02-10 11:22:37 +00:00
|
|
|
spec.SendCount++
|
2020-01-10 18:59:01 +00:00
|
|
|
err = m.persistence.SaveRawMessage(spec)
|
2019-12-02 15:34:05 +00:00
|
|
|
if err != nil {
|
2020-01-10 18:59:01 +00:00
|
|
|
return nil, err
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
return id, 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()
|
|
|
|
|
2020-05-13 13:16:17 +00:00
|
|
|
if len(message.ImagePath) != 0 {
|
|
|
|
file, err := os.Open(message.ImagePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
payload, err := ioutil.ReadAll(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
image := protobuf.ImageMessage{
|
|
|
|
Payload: payload,
|
|
|
|
Type: images.ImageType(payload),
|
|
|
|
}
|
|
|
|
message.Payload = &protobuf.ChatMessage_Image{Image: &image}
|
|
|
|
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
err := extendMessageFromChat(message, chat, &m.identity.PublicKey, m.getTimesource())
|
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
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
var encodedMessage []byte
|
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
|
|
|
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
|
2020-01-10 18:59:01 +00:00
|
|
|
encodedMessage, err = proto.Marshal(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
|
2020-01-10 18:59:01 +00:00
|
|
|
encodedMessage, err = proto.Marshal(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 ChatTypePrivateGroupChat:
|
|
|
|
message.MessageType = protobuf.ChatMessage_PRIVATE_GROUP
|
|
|
|
logger.Debug("sending group message", zap.String("chatName", chat.Name))
|
|
|
|
|
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
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
encodedMessage, err = m.processor.EncodeMembershipUpdate(group, &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
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, errors.New("chat type not supported")
|
2019-09-26 07:01:17 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
id, err := m.dispatchMessage(ctx, &RawMessage{
|
|
|
|
LocalChatID: chat.ID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: 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
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
message.ID = types.EncodeHex(id)
|
|
|
|
err = message.PrepareContent()
|
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
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
err = chat.UpdateFromMessage(message, m.getTimesource())
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-17 22:25:42 +00:00
|
|
|
|
2020-05-20 12:16:12 +00:00
|
|
|
err = m.persistence.SaveMessages([]*Message{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
|
|
|
|
}
|
|
|
|
|
Add replies to messages
Currently replies to messages are handled in status-react.
This causes some issues with the fact that sometimes replies might come
out of order, they might be offloaded to the database etc.
This commit changes the behavior so that status-go always returns the
replies, and in case a reply comes out of order (first the reply, later
the message being replied to), it will include in the messages the
updated message.
It also adds some fields (RTL,Replace,LineCount) to the database which
were not previously saved, resulting in some potential bugs.
The method that we use to pull replies is currently a bit naive, we just
pull all the message again from the database, but has the advantage of
being simple. It will go through performance testing to make sure
performnace are acceptable, if so I think it's reasonable to avoid some
complexity.
2020-04-08 13:42:02 +00:00
|
|
|
response.Messages, err = m.pullMessagesAndResponsesFromDB([]*Message{message})
|
|
|
|
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
|
|
|
response.Chats = []*Chat{chat}
|
2019-12-02 15:34:05 +00:00
|
|
|
return &response, m.saveChat(chat)
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
// Send contact updates to all contacts added by us
|
|
|
|
func (m *Messenger) SendContactUpdates(ctx context.Context, ensName, profileImage string) error {
|
|
|
|
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
|
|
|
|
2020-01-15 07:25:09 +00:00
|
|
|
myID := contactIDFromPublicKey(&m.identity.PublicKey)
|
|
|
|
|
|
|
|
if _, err := m.sendContactUpdate(ctx, myID, ensName, profileImage); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: This should not be sending paired messages, as we do it above
|
2020-01-10 18:59:01 +00:00
|
|
|
for _, contact := range m.allContacts {
|
|
|
|
if contact.IsAdded() {
|
|
|
|
if _, err := m.sendContactUpdate(ctx, contact.ID, ensName, profileImage); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-09-02 09:29:06 +00:00
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
return 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
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
// SendContactUpdate sends a contact update to a user and adds the user to contacts
|
|
|
|
func (m *Messenger) SendContactUpdate(ctx context.Context, chatID, ensName, profileImage string) (*MessengerResponse, error) {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
return m.sendContactUpdate(ctx, chatID, ensName, profileImage)
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
func (m *Messenger) sendContactUpdate(ctx context.Context, chatID, ensName, profileImage string) (*MessengerResponse, error) {
|
|
|
|
var response MessengerResponse
|
|
|
|
|
|
|
|
contact, ok := m.allContacts[chatID]
|
|
|
|
if !ok {
|
|
|
|
pubkeyBytes, err := types.DecodeHex(chatID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
publicKey, err := crypto.UnmarshalPubkey(pubkeyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
contact, err = buildContact(publicKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
chat, ok := m.allChats[chatID]
|
|
|
|
if !ok {
|
|
|
|
publicKey, err := contact.PublicKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-20 16:44:32 +00:00
|
|
|
chat = OneToOneFromPublicKey(publicKey, m.getTimesource())
|
2020-01-10 18:59:01 +00:00
|
|
|
// We don't want to show the chat to the user
|
|
|
|
chat.Active = false
|
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
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
m.allChats[chat.ID] = chat
|
2020-01-20 16:44:32 +00:00
|
|
|
clock, _ := chat.NextClockAndTimestamp(m.getTimesource())
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
contactUpdate := &protobuf.ContactUpdate{
|
|
|
|
Clock: clock,
|
|
|
|
EnsName: ensName,
|
|
|
|
ProfileImage: profileImage}
|
|
|
|
encodedMessage, err := proto.Marshal(contactUpdate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
_, err = m.dispatchMessage(ctx, &RawMessage{
|
|
|
|
LocalChatID: chatID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_CONTACT_UPDATE,
|
|
|
|
ResendAutomatically: 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
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-15 07:25:09 +00:00
|
|
|
if !contact.IsAdded() && contact.ID != contactIDFromPublicKey(&m.identity.PublicKey) {
|
2020-01-10 18:59:01 +00:00
|
|
|
contact.SystemTags = append(contact.SystemTags, contactAdded)
|
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
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
response.Contacts = []*Contact{contact}
|
|
|
|
response.Chats = []*Chat{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
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
chat.LastClockValue = clock
|
|
|
|
err = m.saveChat(chat)
|
|
|
|
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
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
return &response, m.saveContact(contact)
|
|
|
|
}
|
|
|
|
|
2020-01-15 07:25:09 +00:00
|
|
|
// SyncDevices sends all public chats and contacts to paired devices
|
|
|
|
func (m *Messenger) SyncDevices(ctx context.Context, ensName, photoPath string) error {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
myID := contactIDFromPublicKey(&m.identity.PublicKey)
|
|
|
|
|
|
|
|
if _, err := m.sendContactUpdate(ctx, myID, ensName, photoPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, chat := range m.allChats {
|
|
|
|
if chat.Public() && chat.Active {
|
|
|
|
if err := m.syncPublicChat(ctx, chat); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, contact := range m.allContacts {
|
|
|
|
if contact.IsAdded() && contact.ID != myID {
|
|
|
|
if err := m.syncContact(ctx, contact); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
// SendPairInstallation sends a pair installation message
|
|
|
|
func (m *Messenger) SendPairInstallation(ctx context.Context) (*MessengerResponse, error) {
|
|
|
|
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
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
var err error
|
|
|
|
var response MessengerResponse
|
2019-12-02 15:34:05 +00:00
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
installation, ok := m.allInstallations[m.installationID]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("no installation found")
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
if installation.InstallationMetadata == nil {
|
|
|
|
return nil, errors.New("no installation metadata")
|
|
|
|
}
|
|
|
|
|
2020-01-15 07:25:09 +00:00
|
|
|
chatID := contactIDFromPublicKey(&m.identity.PublicKey)
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
chat, ok := m.allChats[chatID]
|
|
|
|
if !ok {
|
2020-01-20 16:44:32 +00:00
|
|
|
chat = OneToOneFromPublicKey(&m.identity.PublicKey, m.getTimesource())
|
2020-01-10 18:59:01 +00:00
|
|
|
// We don't want to show the chat to the user
|
|
|
|
chat.Active = false
|
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
|
|
|
m.allChats[chat.ID] = chat
|
2020-01-20 16:44:32 +00:00
|
|
|
clock, _ := chat.NextClockAndTimestamp(m.getTimesource())
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
pairMessage := &protobuf.PairInstallation{
|
|
|
|
Clock: clock,
|
|
|
|
Name: installation.InstallationMetadata.Name,
|
|
|
|
InstallationId: installation.ID,
|
|
|
|
DeviceType: installation.InstallationMetadata.DeviceType}
|
|
|
|
encodedMessage, err := proto.Marshal(pairMessage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = m.dispatchPairInstallationMessage(ctx, &RawMessage{
|
|
|
|
LocalChatID: chatID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_PAIR_INSTALLATION,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Chats = []*Chat{chat}
|
2019-12-02 15:34:05 +00:00
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
chat.LastClockValue = clock
|
|
|
|
err = m.saveChat(chat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &response, nil
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-15 07:25:09 +00:00
|
|
|
// syncPublicChat sync a public chat with paired devices
|
|
|
|
func (m *Messenger) syncPublicChat(ctx context.Context, publicChat *Chat) error {
|
|
|
|
var err error
|
|
|
|
if !m.hasPairedDevices() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
chatID := contactIDFromPublicKey(&m.identity.PublicKey)
|
|
|
|
|
|
|
|
chat, ok := m.allChats[chatID]
|
|
|
|
if !ok {
|
2020-01-20 16:44:32 +00:00
|
|
|
chat = OneToOneFromPublicKey(&m.identity.PublicKey, m.getTimesource())
|
2020-01-15 07:25:09 +00:00
|
|
|
// We don't want to show the chat to the user
|
|
|
|
chat.Active = false
|
|
|
|
}
|
|
|
|
|
|
|
|
m.allChats[chat.ID] = chat
|
2020-01-20 16:44:32 +00:00
|
|
|
clock, _ := chat.NextClockAndTimestamp(m.getTimesource())
|
2020-01-15 07:25:09 +00:00
|
|
|
|
|
|
|
syncMessage := &protobuf.SyncInstallationPublicChat{
|
|
|
|
Clock: clock,
|
|
|
|
Id: publicChat.ID,
|
|
|
|
}
|
|
|
|
encodedMessage, err := proto.Marshal(syncMessage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = m.dispatchMessage(ctx, &RawMessage{
|
|
|
|
LocalChatID: chatID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_SYNC_INSTALLATION_PUBLIC_CHAT,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
chat.LastClockValue = clock
|
|
|
|
return m.saveChat(chat)
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
// syncContact sync as contact with paired devices
|
|
|
|
func (m *Messenger) syncContact(ctx context.Context, contact *Contact) error {
|
|
|
|
var err error
|
|
|
|
if !m.hasPairedDevices() {
|
|
|
|
return nil
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
2020-01-15 07:25:09 +00:00
|
|
|
chatID := contactIDFromPublicKey(&m.identity.PublicKey)
|
2019-12-02 15:34:05 +00:00
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
chat, ok := m.allChats[chatID]
|
|
|
|
if !ok {
|
2020-01-20 16:44:32 +00:00
|
|
|
chat = OneToOneFromPublicKey(&m.identity.PublicKey, m.getTimesource())
|
2020-01-10 18:59:01 +00:00
|
|
|
// We don't want to show the chat to the user
|
|
|
|
chat.Active = false
|
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
m.allChats[chat.ID] = chat
|
2020-01-20 16:44:32 +00:00
|
|
|
clock, _ := chat.NextClockAndTimestamp(m.getTimesource())
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
syncMessage := &protobuf.SyncInstallationContact{
|
|
|
|
Clock: clock,
|
|
|
|
Id: contact.ID,
|
|
|
|
EnsName: contact.Name,
|
|
|
|
ProfileImage: contact.Photo,
|
|
|
|
}
|
|
|
|
encodedMessage, err := proto.Marshal(syncMessage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = m.dispatchMessage(ctx, &RawMessage{
|
|
|
|
LocalChatID: chatID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_SYNC_INSTALLATION_CONTACT,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
chat.LastClockValue = clock
|
|
|
|
return m.saveChat(chat)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RetrieveAll retrieves messages from all filters, processes them and returns a
|
|
|
|
// MessengerResponse to the client
|
|
|
|
func (m *Messenger) RetrieveAll() (*MessengerResponse, error) {
|
|
|
|
chatWithMessages, err := m.transport.RetrieveRawAll()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
return m.handleRetrievedMessages(chatWithMessages)
|
|
|
|
}
|
|
|
|
|
|
|
|
type CurrentMessageState 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
|
|
|
|
}
|
|
|
|
|
|
|
|
type ReceivedMessageState struct {
|
|
|
|
// State on the message being processed
|
|
|
|
CurrentMessageState *CurrentMessageState
|
|
|
|
// AllChats in memory
|
|
|
|
AllChats map[string]*Chat
|
|
|
|
// List of chats modified
|
|
|
|
ModifiedChats map[string]bool
|
|
|
|
// All contacts in memory
|
|
|
|
AllContacts map[string]*Contact
|
2020-02-10 11:22:37 +00:00
|
|
|
// List of contacts modified
|
2020-01-10 18:59:01 +00:00
|
|
|
ModifiedContacts map[string]bool
|
|
|
|
// All installations in memory
|
|
|
|
AllInstallations map[string]*multidevice.Installation
|
|
|
|
// List of installations modified
|
|
|
|
ModifiedInstallations map[string]bool
|
|
|
|
// Map of existing messages
|
|
|
|
ExistingMessagesMap map[string]bool
|
|
|
|
// Response to the client
|
|
|
|
Response *MessengerResponse
|
2020-02-07 11:30:26 +00:00
|
|
|
// Timesource is a time source for clock values/timestamps.
|
|
|
|
Timesource TimeSource
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) handleRetrievedMessages(chatWithMessages map[transport.Filter][]*types.Message) (*MessengerResponse, error) {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
2020-01-10 18:59:01 +00:00
|
|
|
messageState := &ReceivedMessageState{
|
|
|
|
AllChats: m.allChats,
|
|
|
|
ModifiedChats: make(map[string]bool),
|
|
|
|
AllContacts: m.allContacts,
|
|
|
|
ModifiedContacts: make(map[string]bool),
|
|
|
|
AllInstallations: m.allInstallations,
|
|
|
|
ModifiedInstallations: m.modifiedInstallations,
|
|
|
|
ExistingMessagesMap: make(map[string]bool),
|
|
|
|
Response: &MessengerResponse{},
|
2020-01-20 16:44:32 +00:00
|
|
|
Timesource: m.getTimesource(),
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logger := m.logger.With(zap.String("site", "RetrieveAll"))
|
2020-01-28 11:16:28 +00:00
|
|
|
for _, 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
|
|
|
|
2020-03-09 06:19:23 +00:00
|
|
|
logger.Debug("processing messages further", zap.Int("count", len(statusMessages)))
|
|
|
|
|
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
|
2020-01-15 07:25:09 +00:00
|
|
|
senderID := contactIDFromPublicKey(publicKey)
|
2020-01-10 18:59:01 +00:00
|
|
|
if _, ok := messageState.AllContacts[senderID]; ok && messageState.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)
|
2020-01-10 18:59:01 +00:00
|
|
|
exists, err := m.handler.messageExists(messageID, messageState.ExistingMessagesMap)
|
2019-12-02 15:34:05 +00:00
|
|
|
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
|
2020-01-10 18:59:01 +00:00
|
|
|
if c, ok := messageState.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
|
2020-01-10 18:59:01 +00:00
|
|
|
messageState.AllContacts[senderID] = c
|
|
|
|
messageState.ModifiedContacts[contact.ID] = true
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
messageState.CurrentMessageState = &CurrentMessageState{
|
2019-12-02 15:34:05 +00:00
|
|
|
MessageID: messageID,
|
|
|
|
WhisperTimestamp: uint64(msg.TransportMessage.Timestamp) * 1000,
|
|
|
|
Contact: contact,
|
|
|
|
PublicKey: publicKey,
|
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 {
|
2020-01-10 18:59:01 +00:00
|
|
|
logger.Debug("Handling parsed message")
|
2019-12-02 15:34:05 +00:00
|
|
|
switch msg.ParsedMessage.(type) {
|
|
|
|
case protobuf.MembershipUpdateMessage:
|
2020-01-10 18:59:01 +00:00
|
|
|
logger.Debug("Handling 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)
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
err = m.handler.HandleMembershipUpdate(messageState, messageState.AllChats[rawMembershipUpdate.ChatId], rawMembershipUpdate, m.systemMessagesTranslations)
|
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 {
|
2020-01-10 18:59:01 +00:00
|
|
|
logger.Warn("failed to handle MembershipUpdate", 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
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
case protobuf.ChatMessage:
|
|
|
|
logger.Debug("Handling ChatMessage")
|
|
|
|
messageState.CurrentMessageState.Message = msg.ParsedMessage.(protobuf.ChatMessage)
|
|
|
|
err = m.handler.HandleChatMessage(messageState)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn("failed to handle ChatMessage", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case protobuf.PairInstallation:
|
|
|
|
if !isPubKeyEqual(messageState.CurrentMessageState.PublicKey, &m.identity.PublicKey) {
|
|
|
|
logger.Warn("not coming from us, ignoring")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
p := msg.ParsedMessage.(protobuf.PairInstallation)
|
|
|
|
logger.Debug("Handling PairInstallation", zap.Any("message", p))
|
|
|
|
err = m.handler.HandlePairInstallation(messageState, p)
|
2019-12-02 15:34:05 +00:00
|
|
|
if err != nil {
|
2020-01-10 18:59:01 +00:00
|
|
|
logger.Warn("failed to handle PairInstallation", 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
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
case protobuf.SyncInstallationContact:
|
|
|
|
if !isPubKeyEqual(messageState.CurrentMessageState.PublicKey, &m.identity.PublicKey) {
|
|
|
|
logger.Warn("not coming from us, ignoring")
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
p := msg.ParsedMessage.(protobuf.SyncInstallationContact)
|
|
|
|
logger.Debug("Handling SyncInstallationContact", zap.Any("message", p))
|
|
|
|
err = m.handler.HandleSyncInstallationContact(messageState, p)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn("failed to handle SyncInstallationContact", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
2020-01-15 07:25:09 +00:00
|
|
|
case protobuf.SyncInstallationPublicChat:
|
|
|
|
if !isPubKeyEqual(messageState.CurrentMessageState.PublicKey, &m.identity.PublicKey) {
|
|
|
|
logger.Warn("not coming from us, ignoring")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
p := msg.ParsedMessage.(protobuf.SyncInstallationPublicChat)
|
|
|
|
logger.Debug("Handling SyncInstallationPublicChat", zap.Any("message", p))
|
|
|
|
err = m.handler.HandleSyncInstallationPublicChat(messageState, p)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn("failed to handle SyncInstallationPublicChat", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
case protobuf.RequestAddressForTransaction:
|
|
|
|
command := msg.ParsedMessage.(protobuf.RequestAddressForTransaction)
|
|
|
|
logger.Debug("Handling RequestAddressForTransaction", zap.Any("message", command))
|
|
|
|
err = m.handler.HandleRequestAddressForTransaction(messageState, command)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn("failed to handle RequestAddressForTransaction", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case protobuf.SendTransaction:
|
|
|
|
command := msg.ParsedMessage.(protobuf.SendTransaction)
|
|
|
|
logger.Debug("Handling SendTransaction", zap.Any("message", command))
|
|
|
|
err = m.handler.HandleSendTransaction(messageState, command)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn("failed to handle SendTransaction", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case protobuf.AcceptRequestAddressForTransaction:
|
|
|
|
command := msg.ParsedMessage.(protobuf.AcceptRequestAddressForTransaction)
|
|
|
|
logger.Debug("Handling AcceptRequestAddressForTransaction")
|
|
|
|
err = m.handler.HandleAcceptRequestAddressForTransaction(messageState, command)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn("failed to handle AcceptRequestAddressForTransaction", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
case protobuf.DeclineRequestAddressForTransaction:
|
|
|
|
command := msg.ParsedMessage.(protobuf.DeclineRequestAddressForTransaction)
|
|
|
|
logger.Debug("Handling DeclineRequestAddressForTransaction")
|
|
|
|
err = m.handler.HandleDeclineRequestAddressForTransaction(messageState, command)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn("failed to handle DeclineRequestAddressForTransaction", 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
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
case protobuf.DeclineRequestTransaction:
|
|
|
|
command := msg.ParsedMessage.(protobuf.DeclineRequestTransaction)
|
|
|
|
logger.Debug("Handling DeclineRequestTransaction")
|
|
|
|
err = m.handler.HandleDeclineRequestTransaction(messageState, command)
|
2019-12-02 15:34:05 +00:00
|
|
|
if err != nil {
|
2020-01-10 18:59:01 +00:00
|
|
|
logger.Warn("failed to handle DeclineRequestTransaction", zap.Error(err))
|
2019-12-02 15:34:05 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
case protobuf.RequestTransaction:
|
|
|
|
command := msg.ParsedMessage.(protobuf.RequestTransaction)
|
|
|
|
logger.Debug("Handling RequestTransaction")
|
|
|
|
err = m.handler.HandleRequestTransaction(messageState, command)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn("failed to handle RequestTransaction", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case protobuf.ContactUpdate:
|
|
|
|
logger.Debug("Handling ContactUpdate")
|
|
|
|
|
|
|
|
contactUpdate := msg.ParsedMessage.(protobuf.ContactUpdate)
|
|
|
|
|
|
|
|
err = m.handler.HandleContactUpdate(messageState, contactUpdate)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn("failed to handle ContactUpdate", zap.Error(err))
|
|
|
|
continue
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
default:
|
2020-01-28 11:16:28 +00:00
|
|
|
logger.Debug("message not handled")
|
|
|
|
|
2019-11-15 08:52:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-02 09:29:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 11:22:38 +00:00
|
|
|
var contactsToSave []*Contact
|
2020-01-10 18:59:01 +00:00
|
|
|
for id := range messageState.ModifiedContacts {
|
2020-04-17 11:22:38 +00:00
|
|
|
contact := messageState.AllContacts[id]
|
|
|
|
if contact != nil {
|
|
|
|
// We save all contacts so we can pull back name/image,
|
|
|
|
// but we only send to client those
|
|
|
|
// that have some custom fields
|
|
|
|
contactsToSave = append(contactsToSave, contact)
|
|
|
|
if contact.HasCustomFields() {
|
|
|
|
messageState.Response.Contacts = append(messageState.Response.Contacts, contact)
|
|
|
|
}
|
|
|
|
}
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 12:16:12 +00:00
|
|
|
for id := range messageState.ModifiedChats {
|
|
|
|
chat := messageState.AllChats[id]
|
|
|
|
if chat.OneToOne() {
|
|
|
|
contact, ok := m.allContacts[chat.ID]
|
|
|
|
if ok {
|
|
|
|
chat.Alias = contact.Alias
|
|
|
|
chat.Identicon = contact.Identicon
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
messageState.Response.Chats = append(messageState.Response.Chats, chat)
|
|
|
|
}
|
|
|
|
|
2020-02-10 11:22:37 +00:00
|
|
|
for id := range messageState.ModifiedInstallations {
|
2020-01-10 18:59:01 +00:00
|
|
|
installation := messageState.AllInstallations[id]
|
|
|
|
messageState.Response.Installations = append(messageState.Response.Installations, installation)
|
|
|
|
if installation.InstallationMetadata != nil {
|
|
|
|
err := m.setInstallationMetadata(id, installation.InstallationMetadata)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2019-09-26 07:01:17 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
var err error
|
2020-01-10 18:59:01 +00:00
|
|
|
if len(messageState.Response.Chats) > 0 {
|
|
|
|
err = m.saveChats(messageState.Response.Chats)
|
2019-12-02 15:34:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
if len(messageState.Response.Messages) > 0 {
|
|
|
|
err = m.SaveMessages(messageState.Response.Messages)
|
2019-12-02 15:34:05 +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-09-26 07:01:17 +00:00
|
|
|
|
2020-04-17 11:22:38 +00:00
|
|
|
if len(contactsToSave) > 0 {
|
|
|
|
err = m.persistence.SaveContacts(contactsToSave)
|
2019-12-02 15:34:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2019-09-26 07:01:17 +00:00
|
|
|
|
Add replies to messages
Currently replies to messages are handled in status-react.
This causes some issues with the fact that sometimes replies might come
out of order, they might be offloaded to the database etc.
This commit changes the behavior so that status-go always returns the
replies, and in case a reply comes out of order (first the reply, later
the message being replied to), it will include in the messages the
updated message.
It also adds some fields (RTL,Replace,LineCount) to the database which
were not previously saved, resulting in some potential bugs.
The method that we use to pull replies is currently a bit naive, we just
pull all the message again from the database, but has the advantage of
being simple. It will go through performance testing to make sure
performnace are acceptable, if so I think it's reasonable to avoid some
complexity.
2020-04-08 13:42:02 +00:00
|
|
|
messagesWithResponses, err := m.pullMessagesAndResponsesFromDB(messageState.Response.Messages)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
messageState.Response.Messages = messagesWithResponses
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
// Reset installations
|
|
|
|
m.modifiedInstallations = make(map[string]bool)
|
|
|
|
|
|
|
|
return messageState.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-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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
func (m *Messenger) MessageByID(id string) (*Message, error) {
|
|
|
|
return m.persistence.MessageByID(id)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) MessageByChatID(chatID, cursor string, limit int) ([]*Message, string, error) {
|
|
|
|
return m.persistence.MessageByChatID(chatID, cursor, limit)
|
|
|
|
}
|
|
|
|
|
2019-08-20 11:20:25 +00:00
|
|
|
func (m *Messenger) SaveMessages(messages []*Message) error {
|
2020-05-20 12:16:12 +00:00
|
|
|
return m.persistence.SaveMessages(messages)
|
2019-08-06 21:50:13 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-04-06 12:08:53 +00:00
|
|
|
// MarkMessagesSeen marks messages with `ids` as seen in the chat `chatID`.
|
|
|
|
// It returns the number of affected messages or error. If there is an error,
|
|
|
|
// the number of affected messages is always zero.
|
|
|
|
func (m *Messenger) MarkMessagesSeen(chatID string, ids []string) (uint64, error) {
|
2019-12-02 15:34:05 +00:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
2020-04-06 12:08:53 +00:00
|
|
|
count, err := m.persistence.MarkMessagesSeen(chatID, ids)
|
2019-12-02 15:34:05 +00:00
|
|
|
if err != nil {
|
2020-04-06 12:08:53 +00:00
|
|
|
return 0, err
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
chat, err := m.persistence.Chat(chatID)
|
|
|
|
if err != nil {
|
2020-04-06 12:08:53 +00:00
|
|
|
return 0, err
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
m.allChats[chatID] = chat
|
2020-04-06 12:08:53 +00:00
|
|
|
return count, nil
|
2019-08-06 21:50:13 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 12:31:48 +00:00
|
|
|
func (m *Messenger) MarkAllRead(chatID string) error {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
chat, ok := m.allChats[chatID]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("chat not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := m.persistence.MarkAllRead(chatID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
chat.UnviewedMessagesCount = 0
|
|
|
|
m.allChats[chat.ID] = chat
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-06 21:50:13 +00:00
|
|
|
func (m *Messenger) UpdateMessageOutgoingStatus(id, newOutgoingStatus string) error {
|
|
|
|
return m.persistence.UpdateMessageOutgoingStatus(id, newOutgoingStatus)
|
|
|
|
}
|
2019-09-26 07:01:17 +00:00
|
|
|
|
|
|
|
// 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
|
2020-02-05 10:09:33 +00:00
|
|
|
func (m *Messenger) VerifyENSNames(ctx context.Context, rpcEndpoint, contractAddress string) (*MessengerResponse, error) {
|
2019-12-02 15:34:05 +00:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
2020-02-05 10:09:33 +00:00
|
|
|
|
|
|
|
m.logger.Debug("verifying ENS Names", zap.String("endpoint", rpcEndpoint))
|
2019-11-23 17:57:05 +00:00
|
|
|
verifier := m.node.NewENSVerifier(m.logger)
|
2019-11-04 10:08:22 +00:00
|
|
|
|
2020-02-05 10:09:33 +00:00
|
|
|
var response MessengerResponse
|
|
|
|
|
|
|
|
var ensDetails []enstypes.ENSDetails
|
|
|
|
|
2020-04-09 12:48:55 +00:00
|
|
|
// Now in seconds
|
|
|
|
now := m.getTimesource().GetCurrentTime() / 1000
|
2020-02-05 10:09:33 +00:00
|
|
|
for _, contact := range m.allContacts {
|
|
|
|
if shouldENSBeVerified(contact, now) {
|
|
|
|
ensDetails = append(ensDetails, enstypes.ENSDetails{
|
|
|
|
PublicKeyString: contact.ID[2:],
|
|
|
|
Name: contact.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-04 10:08:22 +00:00
|
|
|
ensResponse, err := verifier.CheckBatch(ensDetails, rpcEndpoint, contractAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, details := range ensResponse {
|
2020-02-05 10:09:33 +00:00
|
|
|
contact, ok := m.allContacts["0x"+details.PublicKeyString]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("contact must be existing")
|
|
|
|
}
|
|
|
|
|
|
|
|
m.logger.Debug("verifying ENS Name", zap.Any("details", details), zap.Any("contact", contact))
|
|
|
|
|
|
|
|
contact.ENSVerifiedAt = uint64(details.VerifiedAt)
|
|
|
|
|
2019-11-04 10:08:22 +00:00
|
|
|
if details.Error == nil {
|
|
|
|
contact.ENSVerified = details.Verified
|
2020-01-17 12:39:09 +00:00
|
|
|
m.allContacts[contact.ID] = 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),
|
|
|
|
)
|
2020-04-09 12:48:55 +00:00
|
|
|
contact.ENSVerificationRetries++
|
2019-11-04 10:08:22 +00:00
|
|
|
}
|
2020-02-05 10:09:33 +00:00
|
|
|
response.Contacts = append(response.Contacts, contact)
|
2019-11-04 10:08:22 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 10:09:33 +00:00
|
|
|
if len(response.Contacts) != 0 {
|
|
|
|
err = m.persistence.SaveContacts(response.Contacts)
|
2019-11-04 10:08:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-05 10:09:33 +00:00
|
|
|
return &response, nil
|
2019-11-04 10:08:22 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
func (m *Messenger) RequestTransaction(ctx context.Context, chatID, value, contract, address string) (*MessengerResponse, error) {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
var response MessengerResponse
|
|
|
|
|
|
|
|
// A valid added chat is required.
|
|
|
|
chat, ok := m.allChats[chatID]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("Chat not found")
|
|
|
|
}
|
|
|
|
if chat.ChatType != ChatTypeOneToOne {
|
|
|
|
return nil, errors.New("Need to be a one-to-one chat")
|
|
|
|
}
|
|
|
|
|
|
|
|
message := &Message{}
|
2020-01-20 16:44:32 +00:00
|
|
|
err := extendMessageFromChat(message, chat, &m.identity.PublicKey, m.transport)
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
message.MessageType = protobuf.ChatMessage_ONE_TO_ONE
|
|
|
|
message.ContentType = protobuf.ChatMessage_TRANSACTION_COMMAND
|
|
|
|
message.Text = "Request transaction"
|
|
|
|
|
|
|
|
request := &protobuf.RequestTransaction{
|
|
|
|
Clock: message.Clock,
|
|
|
|
Address: address,
|
|
|
|
Value: value,
|
|
|
|
Contract: contract,
|
|
|
|
}
|
|
|
|
encodedMessage, err := proto.Marshal(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
id, err := m.dispatchMessage(ctx, &RawMessage{
|
|
|
|
LocalChatID: chat.ID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_REQUEST_TRANSACTION,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
message.CommandParameters = &CommandParameters{
|
|
|
|
ID: types.EncodeHex(id),
|
|
|
|
Value: value,
|
|
|
|
Address: address,
|
|
|
|
Contract: contract,
|
|
|
|
CommandState: CommandStateRequestTransaction,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
messageID := types.EncodeHex(id)
|
|
|
|
|
|
|
|
message.ID = messageID
|
|
|
|
message.CommandParameters.ID = messageID
|
|
|
|
err = message.PrepareContent()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
err = chat.UpdateFromMessage(message, m.transport)
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:16:12 +00:00
|
|
|
err = m.persistence.SaveMessages([]*Message{message})
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Chats = []*Chat{chat}
|
|
|
|
response.Messages = []*Message{message}
|
|
|
|
return &response, m.saveChat(chat)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) RequestAddressForTransaction(ctx context.Context, chatID, from, value, contract string) (*MessengerResponse, error) {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
var response MessengerResponse
|
|
|
|
|
|
|
|
// A valid added chat is required.
|
|
|
|
chat, ok := m.allChats[chatID]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("Chat not found")
|
|
|
|
}
|
|
|
|
if chat.ChatType != ChatTypeOneToOne {
|
|
|
|
return nil, errors.New("Need to be a one-to-one chat")
|
|
|
|
}
|
|
|
|
|
|
|
|
message := &Message{}
|
2020-01-20 16:44:32 +00:00
|
|
|
err := extendMessageFromChat(message, chat, &m.identity.PublicKey, m.transport)
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
message.MessageType = protobuf.ChatMessage_ONE_TO_ONE
|
|
|
|
message.ContentType = protobuf.ChatMessage_TRANSACTION_COMMAND
|
|
|
|
message.Text = "Request address for transaction"
|
|
|
|
|
|
|
|
request := &protobuf.RequestAddressForTransaction{
|
|
|
|
Clock: message.Clock,
|
|
|
|
Value: value,
|
|
|
|
Contract: contract,
|
|
|
|
}
|
|
|
|
encodedMessage, err := proto.Marshal(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
id, err := m.dispatchMessage(ctx, &RawMessage{
|
|
|
|
LocalChatID: chat.ID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_REQUEST_ADDRESS_FOR_TRANSACTION,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
message.CommandParameters = &CommandParameters{
|
|
|
|
ID: types.EncodeHex(id),
|
|
|
|
From: from,
|
|
|
|
Value: value,
|
|
|
|
Contract: contract,
|
|
|
|
CommandState: CommandStateRequestAddressForTransaction,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
messageID := types.EncodeHex(id)
|
|
|
|
|
|
|
|
message.ID = messageID
|
|
|
|
message.CommandParameters.ID = messageID
|
|
|
|
err = message.PrepareContent()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
err = chat.UpdateFromMessage(message, m.transport)
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:16:12 +00:00
|
|
|
err = m.persistence.SaveMessages([]*Message{message})
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Chats = []*Chat{chat}
|
|
|
|
response.Messages = []*Message{message}
|
|
|
|
return &response, m.saveChat(chat)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) AcceptRequestAddressForTransaction(ctx context.Context, messageID, address string) (*MessengerResponse, error) {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
var response MessengerResponse
|
|
|
|
|
|
|
|
message, err := m.MessageByID(messageID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if message == nil {
|
|
|
|
return nil, errors.New("message not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
chatID := message.LocalChatID
|
|
|
|
|
|
|
|
// A valid added chat is required.
|
|
|
|
chat, ok := m.allChats[chatID]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("Chat not found")
|
|
|
|
}
|
|
|
|
if chat.ChatType != ChatTypeOneToOne {
|
|
|
|
return nil, errors.New("Need to be a one-to-one chat")
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
clock, timestamp := chat.NextClockAndTimestamp(m.transport)
|
2020-01-10 18:59:01 +00:00
|
|
|
message.Clock = clock
|
|
|
|
message.WhisperTimestamp = timestamp
|
|
|
|
message.Timestamp = timestamp
|
|
|
|
message.Text = "Request address for transaction accepted"
|
|
|
|
message.OutgoingStatus = OutgoingStatusSending
|
|
|
|
|
|
|
|
// Hide previous message
|
2020-01-17 12:39:09 +00:00
|
|
|
previousMessage, err := m.persistence.MessageByCommandID(chatID, messageID)
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if previousMessage == nil {
|
|
|
|
return nil, errors.New("No previous message found")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.persistence.HideMessage(previousMessage.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
message.Replace = previousMessage.ID
|
|
|
|
|
|
|
|
request := &protobuf.AcceptRequestAddressForTransaction{
|
|
|
|
Clock: message.Clock,
|
|
|
|
Id: messageID,
|
|
|
|
Address: address,
|
|
|
|
}
|
|
|
|
encodedMessage, err := proto.Marshal(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
newMessageID, err := m.dispatchMessage(ctx, &RawMessage{
|
|
|
|
LocalChatID: chat.ID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_ACCEPT_REQUEST_ADDRESS_FOR_TRANSACTION,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
message.ID = types.EncodeHex(newMessageID)
|
|
|
|
message.CommandParameters.Address = address
|
|
|
|
message.CommandParameters.CommandState = CommandStateRequestAddressForTransactionAccepted
|
|
|
|
|
|
|
|
err = message.PrepareContent()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
err = chat.UpdateFromMessage(message, m.transport)
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:16:12 +00:00
|
|
|
err = m.persistence.SaveMessages([]*Message{message})
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Chats = []*Chat{chat}
|
|
|
|
response.Messages = []*Message{message}
|
|
|
|
return &response, m.saveChat(chat)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) DeclineRequestTransaction(ctx context.Context, messageID string) (*MessengerResponse, error) {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
var response MessengerResponse
|
|
|
|
|
|
|
|
message, err := m.MessageByID(messageID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if message == nil {
|
|
|
|
return nil, errors.New("message not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
chatID := message.LocalChatID
|
|
|
|
|
|
|
|
// A valid added chat is required.
|
|
|
|
chat, ok := m.allChats[chatID]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("Chat not found")
|
|
|
|
}
|
|
|
|
if chat.ChatType != ChatTypeOneToOne {
|
|
|
|
return nil, errors.New("Need to be a one-to-one chat")
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
clock, timestamp := chat.NextClockAndTimestamp(m.transport)
|
2020-01-10 18:59:01 +00:00
|
|
|
message.Clock = clock
|
|
|
|
message.WhisperTimestamp = timestamp
|
|
|
|
message.Timestamp = timestamp
|
|
|
|
message.Text = "Transaction request declined"
|
|
|
|
message.OutgoingStatus = OutgoingStatusSending
|
|
|
|
message.Replace = messageID
|
|
|
|
|
|
|
|
err = m.persistence.HideMessage(messageID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
request := &protobuf.DeclineRequestTransaction{
|
|
|
|
Clock: message.Clock,
|
|
|
|
Id: messageID,
|
|
|
|
}
|
|
|
|
encodedMessage, err := proto.Marshal(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
newMessageID, err := m.dispatchMessage(ctx, &RawMessage{
|
|
|
|
LocalChatID: chat.ID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_DECLINE_REQUEST_TRANSACTION,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
message.ID = types.EncodeHex(newMessageID)
|
|
|
|
message.CommandParameters.CommandState = CommandStateRequestTransactionDeclined
|
|
|
|
|
|
|
|
err = message.PrepareContent()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
err = chat.UpdateFromMessage(message, m.transport)
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:16:12 +00:00
|
|
|
err = m.persistence.SaveMessages([]*Message{message})
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Chats = []*Chat{chat}
|
|
|
|
response.Messages = []*Message{message}
|
|
|
|
return &response, m.saveChat(chat)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) DeclineRequestAddressForTransaction(ctx context.Context, messageID string) (*MessengerResponse, error) {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
var response MessengerResponse
|
|
|
|
|
|
|
|
message, err := m.MessageByID(messageID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if message == nil {
|
|
|
|
return nil, errors.New("message not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
chatID := message.LocalChatID
|
|
|
|
|
|
|
|
// A valid added chat is required.
|
|
|
|
chat, ok := m.allChats[chatID]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("Chat not found")
|
|
|
|
}
|
|
|
|
if chat.ChatType != ChatTypeOneToOne {
|
|
|
|
return nil, errors.New("Need to be a one-to-one chat")
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
clock, timestamp := chat.NextClockAndTimestamp(m.transport)
|
2020-01-10 18:59:01 +00:00
|
|
|
message.Clock = clock
|
|
|
|
message.WhisperTimestamp = timestamp
|
|
|
|
message.Timestamp = timestamp
|
|
|
|
message.Text = "Request address for transaction declined"
|
|
|
|
message.OutgoingStatus = OutgoingStatusSending
|
|
|
|
message.Replace = messageID
|
|
|
|
|
|
|
|
err = m.persistence.HideMessage(messageID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
request := &protobuf.DeclineRequestAddressForTransaction{
|
|
|
|
Clock: message.Clock,
|
|
|
|
Id: messageID,
|
|
|
|
}
|
|
|
|
encodedMessage, err := proto.Marshal(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
newMessageID, err := m.dispatchMessage(ctx, &RawMessage{
|
|
|
|
LocalChatID: chat.ID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_DECLINE_REQUEST_ADDRESS_FOR_TRANSACTION,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
message.ID = types.EncodeHex(newMessageID)
|
|
|
|
message.CommandParameters.CommandState = CommandStateRequestAddressForTransactionDeclined
|
|
|
|
|
|
|
|
err = message.PrepareContent()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
err = chat.UpdateFromMessage(message, m.transport)
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:16:12 +00:00
|
|
|
err = m.persistence.SaveMessages([]*Message{message})
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Chats = []*Chat{chat}
|
|
|
|
response.Messages = []*Message{message}
|
|
|
|
return &response, m.saveChat(chat)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) AcceptRequestTransaction(ctx context.Context, transactionHash, messageID string, signature []byte) (*MessengerResponse, error) {
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
var response MessengerResponse
|
|
|
|
|
|
|
|
message, err := m.MessageByID(messageID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if message == nil {
|
|
|
|
return nil, errors.New("message not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
chatID := message.LocalChatID
|
|
|
|
|
|
|
|
// A valid added chat is required.
|
|
|
|
chat, ok := m.allChats[chatID]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("Chat not found")
|
|
|
|
}
|
|
|
|
if chat.ChatType != ChatTypeOneToOne {
|
|
|
|
return nil, errors.New("Need to be a one-to-one chat")
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
clock, timestamp := chat.NextClockAndTimestamp(m.transport)
|
2020-01-10 18:59:01 +00:00
|
|
|
message.Clock = clock
|
|
|
|
message.WhisperTimestamp = timestamp
|
|
|
|
message.Timestamp = timestamp
|
2020-02-10 11:22:37 +00:00
|
|
|
message.Text = transactionSentTxt
|
2020-01-10 18:59:01 +00:00
|
|
|
message.OutgoingStatus = OutgoingStatusSending
|
|
|
|
|
|
|
|
// Hide previous message
|
2020-01-17 12:39:09 +00:00
|
|
|
previousMessage, err := m.persistence.MessageByCommandID(chatID, messageID)
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil && err != errRecordNotFound {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if previousMessage != nil {
|
|
|
|
err = m.persistence.HideMessage(previousMessage.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
message.Replace = previousMessage.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.persistence.HideMessage(messageID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
request := &protobuf.SendTransaction{
|
|
|
|
Clock: message.Clock,
|
|
|
|
Id: messageID,
|
|
|
|
TransactionHash: transactionHash,
|
|
|
|
Signature: signature,
|
|
|
|
}
|
|
|
|
encodedMessage, err := proto.Marshal(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
newMessageID, err := m.dispatchMessage(ctx, &RawMessage{
|
|
|
|
LocalChatID: chat.ID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_SEND_TRANSACTION,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
message.ID = types.EncodeHex(newMessageID)
|
|
|
|
message.CommandParameters.TransactionHash = transactionHash
|
|
|
|
message.CommandParameters.Signature = signature
|
|
|
|
message.CommandParameters.CommandState = CommandStateTransactionSent
|
|
|
|
|
|
|
|
err = message.PrepareContent()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
err = chat.UpdateFromMessage(message, m.transport)
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:16:12 +00:00
|
|
|
err = m.persistence.SaveMessages([]*Message{message})
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Chats = []*Chat{chat}
|
|
|
|
response.Messages = []*Message{message}
|
|
|
|
return &response, m.saveChat(chat)
|
|
|
|
}
|
|
|
|
|
2020-01-15 07:25:09 +00:00
|
|
|
func (m *Messenger) SendTransaction(ctx context.Context, chatID, value, contract, transactionHash string, signature []byte) (*MessengerResponse, error) {
|
2020-01-10 18:59:01 +00:00
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
var response MessengerResponse
|
|
|
|
|
|
|
|
// A valid added chat is required.
|
|
|
|
chat, ok := m.allChats[chatID]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("Chat not found")
|
|
|
|
}
|
|
|
|
if chat.ChatType != ChatTypeOneToOne {
|
|
|
|
return nil, errors.New("Need to be a one-to-one chat")
|
|
|
|
}
|
|
|
|
|
|
|
|
message := &Message{}
|
2020-01-20 16:44:32 +00:00
|
|
|
err := extendMessageFromChat(message, chat, &m.identity.PublicKey, m.transport)
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
message.MessageType = protobuf.ChatMessage_ONE_TO_ONE
|
|
|
|
message.ContentType = protobuf.ChatMessage_TRANSACTION_COMMAND
|
|
|
|
message.LocalChatID = chatID
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
clock, timestamp := chat.NextClockAndTimestamp(m.transport)
|
2020-01-10 18:59:01 +00:00
|
|
|
message.Clock = clock
|
|
|
|
message.WhisperTimestamp = timestamp
|
|
|
|
message.Timestamp = timestamp
|
2020-02-10 11:22:37 +00:00
|
|
|
message.Text = transactionSentTxt
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
request := &protobuf.SendTransaction{
|
|
|
|
Clock: message.Clock,
|
|
|
|
TransactionHash: transactionHash,
|
|
|
|
Signature: signature,
|
|
|
|
}
|
|
|
|
encodedMessage, err := proto.Marshal(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
newMessageID, err := m.dispatchMessage(ctx, &RawMessage{
|
|
|
|
LocalChatID: chat.ID,
|
|
|
|
Payload: encodedMessage,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_SEND_TRANSACTION,
|
|
|
|
ResendAutomatically: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
message.ID = types.EncodeHex(newMessageID)
|
|
|
|
message.CommandParameters = &CommandParameters{
|
|
|
|
TransactionHash: transactionHash,
|
2020-01-15 07:25:09 +00:00
|
|
|
Value: value,
|
|
|
|
Contract: contract,
|
2020-01-10 18:59:01 +00:00
|
|
|
Signature: signature,
|
|
|
|
CommandState: CommandStateTransactionSent,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = message.PrepareContent()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
err = chat.UpdateFromMessage(message, m.transport)
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:16:12 +00:00
|
|
|
err = m.persistence.SaveMessages([]*Message{message})
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Chats = []*Chat{chat}
|
|
|
|
response.Messages = []*Message{message}
|
|
|
|
return &response, m.saveChat(chat)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) ValidateTransactions(ctx context.Context, addresses []types.Address) (*MessengerResponse, error) {
|
|
|
|
if m.verifyTransactionClient == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
m.mutex.Lock()
|
|
|
|
defer m.mutex.Unlock()
|
|
|
|
|
|
|
|
modifiedChats := make(map[string]bool)
|
|
|
|
|
|
|
|
logger := m.logger.With(zap.String("site", "ValidateTransactions"))
|
|
|
|
logger.Debug("Validating transactions")
|
|
|
|
txs, err := m.persistence.TransactionsToValidate()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Error pulling", zap.Error(err))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
logger.Debug("Txs", zap.Int("count", len(txs)), zap.Any("txs", txs))
|
|
|
|
var response MessengerResponse
|
|
|
|
validator := NewTransactionValidator(addresses, m.persistence, m.verifyTransactionClient, m.logger)
|
|
|
|
responses, err := validator.ValidateTransactions(ctx)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("Error validating", zap.Error(err))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, validationResult := range responses {
|
|
|
|
var message *Message
|
2020-01-15 07:25:09 +00:00
|
|
|
chatID := contactIDFromPublicKey(validationResult.Transaction.From)
|
2020-01-10 18:59:01 +00:00
|
|
|
chat, ok := m.allChats[chatID]
|
|
|
|
if !ok {
|
2020-01-20 16:44:32 +00:00
|
|
|
chat = OneToOneFromPublicKey(validationResult.Transaction.From, m.transport)
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
if validationResult.Message != nil {
|
|
|
|
message = validationResult.Message
|
|
|
|
} else {
|
|
|
|
message = &Message{}
|
2020-01-20 16:44:32 +00:00
|
|
|
err := extendMessageFromChat(message, chat, &m.identity.PublicKey, m.transport)
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
message.MessageType = protobuf.ChatMessage_ONE_TO_ONE
|
|
|
|
message.ContentType = protobuf.ChatMessage_TRANSACTION_COMMAND
|
|
|
|
message.LocalChatID = chatID
|
|
|
|
message.OutgoingStatus = ""
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
clock, timestamp := chat.NextClockAndTimestamp(m.transport)
|
2020-01-10 18:59:01 +00:00
|
|
|
message.Clock = clock
|
|
|
|
message.Timestamp = timestamp
|
|
|
|
message.WhisperTimestamp = timestamp
|
|
|
|
message.Text = "Transaction received"
|
2020-04-06 12:08:53 +00:00
|
|
|
message.Seen = false
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
message.ID = validationResult.Transaction.MessageID
|
|
|
|
if message.CommandParameters == nil {
|
|
|
|
message.CommandParameters = &CommandParameters{}
|
|
|
|
} else {
|
|
|
|
message.CommandParameters = validationResult.Message.CommandParameters
|
|
|
|
}
|
|
|
|
|
|
|
|
message.CommandParameters.Value = validationResult.Value
|
|
|
|
message.CommandParameters.Contract = validationResult.Contract
|
|
|
|
message.CommandParameters.Address = validationResult.Address
|
|
|
|
message.CommandParameters.CommandState = CommandStateTransactionSent
|
|
|
|
message.CommandParameters.TransactionHash = validationResult.Transaction.TransactionHash
|
|
|
|
|
|
|
|
err = message.PrepareContent()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:44:32 +00:00
|
|
|
err = chat.UpdateFromMessage(message, m.transport)
|
2020-01-10 18:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-17 12:39:09 +00:00
|
|
|
if len(message.CommandParameters.ID) != 0 {
|
|
|
|
// Hide previous message
|
|
|
|
previousMessage, err := m.persistence.MessageByCommandID(chatID, message.CommandParameters.ID)
|
|
|
|
if err != nil && err != errRecordNotFound {
|
2020-01-10 18:59:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-17 12:39:09 +00:00
|
|
|
|
|
|
|
if previousMessage != nil {
|
|
|
|
err = m.persistence.HideMessage(previousMessage.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
message.Replace = previousMessage.ID
|
|
|
|
}
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
response.Messages = append(response.Messages, message)
|
|
|
|
m.allChats[chat.ID] = chat
|
|
|
|
modifiedChats[chat.ID] = true
|
|
|
|
|
|
|
|
}
|
2020-02-10 11:22:37 +00:00
|
|
|
for id := range modifiedChats {
|
2020-01-10 18:59:01 +00:00
|
|
|
response.Chats = append(response.Chats, m.allChats[id])
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(response.Messages) > 0 {
|
|
|
|
err = m.SaveMessages(response.Messages)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &response, nil
|
|
|
|
}
|
2020-01-20 16:44:32 +00:00
|
|
|
|
Add replies to messages
Currently replies to messages are handled in status-react.
This causes some issues with the fact that sometimes replies might come
out of order, they might be offloaded to the database etc.
This commit changes the behavior so that status-go always returns the
replies, and in case a reply comes out of order (first the reply, later
the message being replied to), it will include in the messages the
updated message.
It also adds some fields (RTL,Replace,LineCount) to the database which
were not previously saved, resulting in some potential bugs.
The method that we use to pull replies is currently a bit naive, we just
pull all the message again from the database, but has the advantage of
being simple. It will go through performance testing to make sure
performnace are acceptable, if so I think it's reasonable to avoid some
complexity.
2020-04-08 13:42:02 +00:00
|
|
|
// pullMessagesAndResponsesFromDB pulls all the messages and the one that have
|
|
|
|
// been replied to from the database
|
|
|
|
func (m *Messenger) pullMessagesAndResponsesFromDB(messages []*Message) ([]*Message, error) {
|
|
|
|
var messageIDs []string
|
|
|
|
for _, message := range messages {
|
|
|
|
messageIDs = append(messageIDs, message.ID)
|
|
|
|
if len(message.ResponseTo) != 0 {
|
|
|
|
messageIDs = append(messageIDs, message.ResponseTo)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
// We pull from the database all the messages & replies involved,
|
|
|
|
// so we let the db build the correct messages
|
|
|
|
return m.persistence.MessagesByIDs(messageIDs)
|
|
|
|
}
|
|
|
|
|
2020-02-07 11:30:26 +00:00
|
|
|
func (m *Messenger) getTimesource() TimeSource {
|
2020-01-20 16:44:32 +00:00
|
|
|
return m.transport
|
|
|
|
}
|
2020-02-07 11:30:26 +00:00
|
|
|
|
|
|
|
func (m *Messenger) Timesource() TimeSource {
|
|
|
|
return m.getTimesource()
|
|
|
|
}
|
2020-05-20 12:16:12 +00:00
|
|
|
|
|
|
|
func generateAliasAndIdenticon(pk string) (string, string, error) {
|
|
|
|
identicon, err := identicon.GenerateBase64(pk)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
name, err := alias.GenerateFromPublicKeyString(pk)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
return name, identicon, nil
|
|
|
|
|
|
|
|
}
|