2020-09-01 13:27:01 +00:00
|
|
|
package common
|
2019-08-06 21:50:13 +00:00
|
|
|
|
|
|
|
import (
|
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
|
|
|
"crypto/ecdsa"
|
2020-05-13 13:16:17 +00:00
|
|
|
"encoding/base64"
|
2021-02-23 07:37:08 +00:00
|
|
|
"encoding/hex"
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
"encoding/json"
|
2020-05-13 13:16:17 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
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
|
|
|
"strings"
|
|
|
|
"unicode"
|
|
|
|
"unicode/utf8"
|
2019-08-06 21:50:13 +00:00
|
|
|
|
2020-07-25 16:13:08 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
2020-09-01 13:27:01 +00:00
|
|
|
|
2020-07-25 16:13:08 +00:00
|
|
|
"github.com/status-im/markdown"
|
2020-09-01 10:34:28 +00:00
|
|
|
"github.com/status-im/markdown/ast"
|
2020-11-25 14:11:12 +00:00
|
|
|
|
2021-02-23 07:37:08 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
2020-11-25 00:34:32 +00:00
|
|
|
"github.com/status-im/status-go/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-08-06 21:50:13 +00:00
|
|
|
)
|
|
|
|
|
2019-08-20 11:20:25 +00:00
|
|
|
// QuotedMessage contains the original text of the message replied to
|
|
|
|
type QuotedMessage struct {
|
|
|
|
// From is a public key of the author of the message.
|
2020-09-09 11:03:29 +00:00
|
|
|
From string `json:"from"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
ParsedText json.RawMessage `json:"parsedText,omitempty"`
|
2020-05-14 05:40:40 +00:00
|
|
|
// Base64Image is the converted base64 image
|
|
|
|
Base64Image string `json:"image,omitempty"`
|
2020-06-17 18:55:49 +00:00
|
|
|
// Base64Audio is the converted base64 audio
|
|
|
|
Base64Audio string `json:"audio,omitempty"`
|
2020-06-23 14:30:39 +00:00
|
|
|
// AudioDurationMs is the audio duration in milliseconds
|
2020-06-24 06:40:03 +00:00
|
|
|
AudioDurationMs uint64 `json:"audioDurationMs,omitempty"`
|
2020-11-18 09:16:51 +00:00
|
|
|
// CommunityID is the id of the community advertised
|
|
|
|
CommunityID string `json:"communityId,omitempty"`
|
2019-08-20 11:20:25 +00:00
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
type CommandState int
|
|
|
|
|
|
|
|
const (
|
|
|
|
CommandStateRequestAddressForTransaction CommandState = iota + 1
|
|
|
|
CommandStateRequestAddressForTransactionDeclined
|
|
|
|
CommandStateRequestAddressForTransactionAccepted
|
|
|
|
CommandStateRequestTransaction
|
|
|
|
CommandStateRequestTransactionDeclined
|
|
|
|
CommandStateTransactionPending
|
|
|
|
CommandStateTransactionSent
|
|
|
|
)
|
|
|
|
|
|
|
|
type CommandParameters struct {
|
|
|
|
// ID is the ID of the initial message
|
|
|
|
ID string `json:"id"`
|
|
|
|
// From is the address we are sending the command from
|
|
|
|
From string `json:"from"`
|
|
|
|
// Address is the address sent with the command
|
|
|
|
Address string `json:"address"`
|
|
|
|
// Contract is the contract address for ERC20 tokens
|
|
|
|
Contract string `json:"contract"`
|
|
|
|
// Value is the value as a string sent
|
|
|
|
Value string `json:"value"`
|
|
|
|
// TransactionHash is the hash of the transaction
|
|
|
|
TransactionHash string `json:"transactionHash"`
|
|
|
|
// CommandState is the state of the command
|
|
|
|
CommandState CommandState `json:"commandState"`
|
|
|
|
// The Signature of the pk-bytes+transaction-hash from the wallet
|
|
|
|
// address originating
|
|
|
|
Signature []byte `json:"signature"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CommandParameters) IsTokenTransfer() bool {
|
|
|
|
return len(c.Contract) != 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
|
|
|
const (
|
2021-02-23 15:47:45 +00:00
|
|
|
OutgoingStatusSending = "sending"
|
|
|
|
OutgoingStatusSent = "sent"
|
|
|
|
OutgoingStatusDelivered = "delivered"
|
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-08-06 21:50:13 +00:00
|
|
|
// Message represents a message record in the database,
|
2020-01-10 18:59:01 +00:00
|
|
|
// more specifically in user_messages table.
|
2019-08-06 21:50:13 +00:00
|
|
|
type Message struct {
|
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
|
|
|
protobuf.ChatMessage
|
|
|
|
|
2019-08-06 21:50:13 +00:00
|
|
|
// ID calculated as keccak256(compressedAuthorPubKey, data) where data is unencrypted payload.
|
|
|
|
ID string `json:"id"`
|
|
|
|
// WhisperTimestamp is a timestamp of a Whisper envelope.
|
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
|
|
|
WhisperTimestamp uint64 `json:"whisperTimestamp"`
|
2019-08-06 21:50:13 +00:00
|
|
|
// From is a public key of the author of the message.
|
2019-08-20 11:20:25 +00:00
|
|
|
From string `json:"from"`
|
2019-09-26 07:01:17 +00:00
|
|
|
// Random 3 words name
|
|
|
|
Alias string `json:"alias"`
|
|
|
|
// Identicon of the author
|
|
|
|
Identicon string `json:"identicon"`
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
// The chat id to be stored locally
|
|
|
|
LocalChatID string `json:"localChatId"`
|
2021-02-23 15:47:45 +00:00
|
|
|
// Seen set to true when user have read this message already
|
2019-08-06 21:50:13 +00:00
|
|
|
Seen bool `json:"seen"`
|
|
|
|
OutgoingStatus string `json:"outgoingStatus,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
|
|
|
|
2019-08-20 11:20:25 +00:00
|
|
|
QuotedMessage *QuotedMessage `json:"quotedMessage"`
|
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
|
|
|
// CommandParameters is the parameters sent with the message
|
|
|
|
CommandParameters *CommandParameters `json:"commandParameters"`
|
|
|
|
|
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
|
|
|
// Computed fields
|
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
|
|
|
// RTL is whether this is a right-to-left message (arabic/hebrew script etc)
|
|
|
|
RTL bool `json:"rtl"`
|
|
|
|
// ParsedText is the parsed markdown for displaying
|
2020-07-30 20:54:33 +00:00
|
|
|
ParsedText []byte `json:"parsedText,omitempty"`
|
2021-03-31 16:23:45 +00:00
|
|
|
// ParsedTextAst is the ast of the parsed text
|
|
|
|
ParsedTextAst *ast.Node `json:"-"`
|
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
|
|
|
// LineCount is the count of newlines in the message
|
|
|
|
LineCount int `json:"lineCount"`
|
2020-05-13 13:16:17 +00:00
|
|
|
// Base64Image is the converted base64 image
|
|
|
|
Base64Image string `json:"image,omitempty"`
|
|
|
|
// ImagePath is the path of the image to be sent
|
|
|
|
ImagePath string `json:"imagePath,omitempty"`
|
2020-06-17 18:55:49 +00:00
|
|
|
// Base64Audio is the converted base64 audio
|
|
|
|
Base64Audio string `json:"audio,omitempty"`
|
|
|
|
// AudioPath is the path of the audio to be sent
|
|
|
|
AudioPath string `json:"audioPath,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
|
|
|
|
2020-11-18 09:16:51 +00:00
|
|
|
// CommunityID is the id of the community to advertise
|
|
|
|
CommunityID string `json:"communityId,omitempty"`
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
// Replace indicates that this is a replacement of a message
|
|
|
|
// that has been updated
|
2020-11-10 15:08:32 +00:00
|
|
|
Replace string `json:"replace,omitempty"`
|
|
|
|
New bool `json:"new,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
|
|
|
SigPubKey *ecdsa.PublicKey `json:"-"`
|
2020-09-01 10:34:28 +00:00
|
|
|
|
|
|
|
// Mentions is an array of mentions for a given message
|
|
|
|
Mentions []string
|
2020-10-27 17:35:28 +00:00
|
|
|
|
|
|
|
// Links is an array of links within given message
|
|
|
|
Links []string
|
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
|
|
|
func (m *Message) MarshalJSON() ([]byte, error) {
|
2020-01-15 07:25:09 +00:00
|
|
|
type StickerAlias struct {
|
|
|
|
Hash string `json:"hash"`
|
|
|
|
Pack int32 `json:"pack"`
|
|
|
|
}
|
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
|
|
|
item := struct {
|
2020-01-10 18:59:01 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
WhisperTimestamp uint64 `json:"whisperTimestamp"`
|
|
|
|
From string `json:"from"`
|
|
|
|
Alias string `json:"alias"`
|
|
|
|
Identicon string `json:"identicon"`
|
|
|
|
Seen bool `json:"seen"`
|
|
|
|
OutgoingStatus string `json:"outgoingStatus,omitempty"`
|
|
|
|
QuotedMessage *QuotedMessage `json:"quotedMessage"`
|
|
|
|
RTL bool `json:"rtl"`
|
2020-07-30 20:54:33 +00:00
|
|
|
ParsedText json.RawMessage `json:"parsedText,omitempty"`
|
2020-01-10 18:59:01 +00:00
|
|
|
LineCount int `json:"lineCount"`
|
|
|
|
Text string `json:"text"`
|
2020-02-10 11:22:37 +00:00
|
|
|
ChatID string `json:"chatId"`
|
2020-01-10 18:59:01 +00:00
|
|
|
LocalChatID string `json:"localChatId"`
|
|
|
|
Clock uint64 `json:"clock"`
|
2020-02-10 11:22:37 +00:00
|
|
|
Replace string `json:"replace"`
|
2020-01-10 18:59:01 +00:00
|
|
|
ResponseTo string `json:"responseTo"`
|
2020-11-10 15:08:32 +00:00
|
|
|
New bool `json:"new,omitempty"`
|
2020-01-10 18:59:01 +00:00
|
|
|
EnsName string `json:"ensName"`
|
2020-05-13 13:16:17 +00:00
|
|
|
Image string `json:"image,omitempty"`
|
2020-06-17 18:55:49 +00:00
|
|
|
Audio string `json:"audio,omitempty"`
|
2020-06-23 14:30:39 +00:00
|
|
|
AudioDurationMs uint64 `json:"audioDurationMs,omitempty"`
|
2020-11-18 09:16:51 +00:00
|
|
|
CommunityID string `json:"communityId,omitempty"`
|
2020-01-15 07:25:09 +00:00
|
|
|
Sticker *StickerAlias `json:"sticker"`
|
2020-01-10 18:59:01 +00:00
|
|
|
CommandParameters *CommandParameters `json:"commandParameters"`
|
|
|
|
Timestamp uint64 `json:"timestamp"`
|
|
|
|
ContentType protobuf.ChatMessage_ContentType `json:"contentType"`
|
2020-07-25 11:46:43 +00:00
|
|
|
MessageType protobuf.MessageType `json:"messageType"`
|
2020-09-01 10:34:28 +00:00
|
|
|
Mentions []string `json:"mentions,omitempty"`
|
2020-10-27 17:35:28 +00:00
|
|
|
Links []string `json:"links,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
|
|
|
}{
|
2020-01-10 18:59:01 +00:00
|
|
|
ID: m.ID,
|
|
|
|
WhisperTimestamp: m.WhisperTimestamp,
|
|
|
|
From: m.From,
|
|
|
|
Alias: m.Alias,
|
|
|
|
Identicon: m.Identicon,
|
|
|
|
Seen: m.Seen,
|
|
|
|
OutgoingStatus: m.OutgoingStatus,
|
|
|
|
QuotedMessage: m.QuotedMessage,
|
|
|
|
RTL: m.RTL,
|
|
|
|
ParsedText: m.ParsedText,
|
|
|
|
LineCount: m.LineCount,
|
|
|
|
Text: m.Text,
|
|
|
|
Replace: m.Replace,
|
2020-02-10 11:22:37 +00:00
|
|
|
ChatID: m.ChatId,
|
2020-01-10 18:59:01 +00:00
|
|
|
LocalChatID: m.LocalChatID,
|
|
|
|
Clock: m.Clock,
|
|
|
|
ResponseTo: m.ResponseTo,
|
2020-11-10 15:08:32 +00:00
|
|
|
New: m.New,
|
2020-01-10 18:59:01 +00:00
|
|
|
EnsName: m.EnsName,
|
2020-05-13 13:16:17 +00:00
|
|
|
Image: m.Base64Image,
|
2020-06-17 18:55:49 +00:00
|
|
|
Audio: m.Base64Audio,
|
2020-11-18 09:16:51 +00:00
|
|
|
CommunityID: m.CommunityID,
|
2020-01-10 18:59:01 +00:00
|
|
|
Timestamp: m.Timestamp,
|
|
|
|
ContentType: m.ContentType,
|
2020-09-01 10:34:28 +00:00
|
|
|
Mentions: m.Mentions,
|
2020-10-27 17:35:28 +00:00
|
|
|
Links: m.Links,
|
2020-01-10 18:59:01 +00:00
|
|
|
MessageType: m.MessageType,
|
|
|
|
CommandParameters: m.CommandParameters,
|
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
|
|
|
if sticker := m.GetSticker(); sticker != nil {
|
|
|
|
item.Sticker = &StickerAlias{
|
|
|
|
Pack: sticker.Pack,
|
|
|
|
Hash: sticker.Hash,
|
|
|
|
}
|
|
|
|
}
|
2020-06-23 14:30:39 +00:00
|
|
|
|
|
|
|
if audio := m.GetAudio(); audio != nil {
|
|
|
|
item.AudioDurationMs = audio.DurationMs
|
|
|
|
}
|
|
|
|
|
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 json.Marshal(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Message) UnmarshalJSON(data []byte) error {
|
|
|
|
type Alias Message
|
|
|
|
aux := struct {
|
|
|
|
*Alias
|
2020-06-23 14:30:39 +00:00
|
|
|
ResponseTo string `json:"responseTo"`
|
|
|
|
EnsName string `json:"ensName"`
|
|
|
|
ChatID string `json:"chatId"`
|
|
|
|
Sticker *protobuf.StickerMessage `json:"sticker"`
|
|
|
|
AudioDurationMs uint64 `json:"audioDurationMs"`
|
2020-07-30 20:54:33 +00:00
|
|
|
ParsedText json.RawMessage `json:"parsedText"`
|
2020-06-23 14:30:39 +00:00
|
|
|
ContentType protobuf.ChatMessage_ContentType `json:"contentType"`
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
}{
|
|
|
|
Alias: (*Alias)(m),
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &aux); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-20 21:36:03 +00:00
|
|
|
if aux.ContentType == protobuf.ChatMessage_STICKER {
|
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
|
|
|
m.Payload = &protobuf.ChatMessage_Sticker{Sticker: aux.Sticker}
|
|
|
|
}
|
2020-06-23 14:30:39 +00:00
|
|
|
if aux.ContentType == protobuf.ChatMessage_AUDIO {
|
|
|
|
m.Payload = &protobuf.ChatMessage_Audio{
|
|
|
|
Audio: &protobuf.AudioMessage{DurationMs: aux.AudioDurationMs},
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
m.ResponseTo = aux.ResponseTo
|
|
|
|
m.EnsName = aux.EnsName
|
|
|
|
m.ChatId = aux.ChatID
|
|
|
|
m.ContentType = aux.ContentType
|
2020-07-30 20:54:33 +00:00
|
|
|
m.ParsedText = aux.ParsedText
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the first character is Hebrew or Arabic or the RTL character
|
|
|
|
func isRTL(s string) bool {
|
|
|
|
first, _ := utf8.DecodeRuneInString(s)
|
|
|
|
return unicode.Is(unicode.Hebrew, first) ||
|
|
|
|
unicode.Is(unicode.Arabic, first) ||
|
|
|
|
// RTL character
|
|
|
|
first == '\u200f'
|
|
|
|
}
|
|
|
|
|
2020-05-13 13:16:17 +00:00
|
|
|
// parseImage check the message contains an image, and if so
|
|
|
|
// it creates the a base64 encoded version of it.
|
|
|
|
func (m *Message) parseImage() error {
|
|
|
|
if m.ContentType != protobuf.ChatMessage_IMAGE {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
image := m.GetImage()
|
|
|
|
if image == nil {
|
|
|
|
return errors.New("image empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
payload := image.Payload
|
|
|
|
|
|
|
|
e64 := base64.StdEncoding
|
|
|
|
|
|
|
|
maxEncLen := e64.EncodedLen(len(payload))
|
|
|
|
encBuf := make([]byte, maxEncLen)
|
|
|
|
|
|
|
|
e64.Encode(encBuf, payload)
|
|
|
|
|
2020-10-26 15:16:41 +00:00
|
|
|
mime, err := images.GetMimeType(image.Payload)
|
2020-05-14 05:40:40 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-05-13 13:16:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m.Base64Image = fmt.Sprintf("data:image/%s;base64,%s", mime, encBuf)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-17 18:55:49 +00:00
|
|
|
// parseAudio check the message contains an audio, and if so
|
2020-07-16 06:21:02 +00:00
|
|
|
// it creates a base64 encoded version of it.
|
2020-06-17 18:55:49 +00:00
|
|
|
func (m *Message) parseAudio() error {
|
|
|
|
if m.ContentType != protobuf.ChatMessage_AUDIO {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
audio := m.GetAudio()
|
|
|
|
if audio == nil {
|
|
|
|
return errors.New("audio empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
payload := audio.Payload
|
|
|
|
|
|
|
|
e64 := base64.StdEncoding
|
|
|
|
|
|
|
|
maxEncLen := e64.EncodedLen(len(payload))
|
|
|
|
encBuf := make([]byte, maxEncLen)
|
|
|
|
|
|
|
|
e64.Encode(encBuf, payload)
|
|
|
|
|
|
|
|
mime, err := getAudioMessageMIME(audio)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Base64Audio = fmt.Sprintf("data:audio/%s;base64,%s", mime, encBuf)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-07 08:25:57 +00:00
|
|
|
// implement interface of https://github.com/status-im/markdown/blob/b9fe921681227b1dace4b56364e15edb3b698308/ast/node.go#L701
|
2021-03-31 16:23:45 +00:00
|
|
|
type SimplifiedTextVisitor struct {
|
|
|
|
text string
|
|
|
|
canonicalNames map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *SimplifiedTextVisitor) Visit(node ast.Node, entering bool) ast.WalkStatus {
|
|
|
|
// only on entering we fetch, otherwise we go on
|
|
|
|
if !entering {
|
|
|
|
return ast.GoToNext
|
|
|
|
}
|
|
|
|
|
|
|
|
switch n := node.(type) {
|
|
|
|
case *ast.Mention:
|
|
|
|
literal := string(n.Literal)
|
|
|
|
canonicalName, ok := v.canonicalNames[literal]
|
|
|
|
if ok {
|
|
|
|
v.text += canonicalName
|
|
|
|
} else {
|
|
|
|
v.text += literal
|
|
|
|
}
|
|
|
|
case *ast.Link:
|
|
|
|
destination := string(n.Destination)
|
|
|
|
v.text += destination
|
|
|
|
default:
|
|
|
|
var literal string
|
|
|
|
|
|
|
|
leaf := node.AsLeaf()
|
|
|
|
container := node.AsContainer()
|
|
|
|
if leaf != nil {
|
|
|
|
literal = string(leaf.Literal)
|
|
|
|
} else if container != nil {
|
|
|
|
literal = string(container.Literal)
|
|
|
|
}
|
|
|
|
v.text += literal
|
|
|
|
}
|
|
|
|
|
|
|
|
return ast.GoToNext
|
|
|
|
}
|
|
|
|
|
|
|
|
// implement interface of https://github.com/status-im/markdown/blob/b9fe921681227b1dace4b56364e15edb3b698308/ast/node.go#L701
|
|
|
|
type MentionsAndLinksVisitor struct {
|
2020-09-01 10:34:28 +00:00
|
|
|
mentions []string
|
2020-10-27 17:35:28 +00:00
|
|
|
links []string
|
2020-09-01 10:34:28 +00:00
|
|
|
}
|
|
|
|
|
2021-03-31 16:23:45 +00:00
|
|
|
func (v *MentionsAndLinksVisitor) Visit(node ast.Node, entering bool) ast.WalkStatus {
|
2020-09-01 10:34:28 +00:00
|
|
|
// only on entering we fetch, otherwise we go on
|
|
|
|
if !entering {
|
|
|
|
return ast.GoToNext
|
|
|
|
}
|
|
|
|
switch n := node.(type) {
|
|
|
|
case *ast.Mention:
|
|
|
|
v.mentions = append(v.mentions, string(n.Literal))
|
2020-10-27 17:35:28 +00:00
|
|
|
case *ast.Link:
|
|
|
|
v.links = append(v.links, string(n.Destination))
|
2020-09-01 10:34:28 +00:00
|
|
|
}
|
2020-10-27 17:35:28 +00:00
|
|
|
|
2020-09-01 10:34:28 +00:00
|
|
|
return ast.GoToNext
|
|
|
|
}
|
|
|
|
|
2020-10-27 17:35:28 +00:00
|
|
|
func extractMentionsAndLinks(parsedText ast.Node) ([]string, []string) {
|
2021-03-31 16:23:45 +00:00
|
|
|
visitor := &MentionsAndLinksVisitor{}
|
2020-09-01 10:34:28 +00:00
|
|
|
ast.Walk(parsedText, visitor)
|
2020-10-27 17:35:28 +00:00
|
|
|
return visitor.mentions, visitor.links
|
2020-09-01 10:34:28 +00:00
|
|
|
}
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
// PrepareContent return the parsed content of the message, the line-count and whether
|
|
|
|
// is a right-to-left message
|
|
|
|
func (m *Message) PrepareContent() error {
|
|
|
|
parsedText := markdown.Parse([]byte(m.Text), nil)
|
2020-10-27 17:35:28 +00:00
|
|
|
m.Mentions, m.Links = extractMentionsAndLinks(parsedText)
|
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
|
|
|
jsonParsedText, err := json.Marshal(parsedText)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-31 16:23:45 +00:00
|
|
|
m.ParsedTextAst = &parsedText
|
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
|
|
|
m.ParsedText = jsonParsedText
|
|
|
|
m.LineCount = strings.Count(m.Text, "\n")
|
|
|
|
m.RTL = isRTL(m.Text)
|
2020-06-17 18:55:49 +00:00
|
|
|
if err := m.parseImage(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return m.parseAudio()
|
2019-08-06 21:50:13 +00:00
|
|
|
}
|
2020-05-14 05:40:40 +00:00
|
|
|
|
2021-03-31 16:23:45 +00:00
|
|
|
// GetSimplifiedText returns a the text stripped of all the markdown and with mentions
|
|
|
|
// replaced by canonical names
|
|
|
|
func (m *Message) GetSimplifiedText(canonicalNames map[string]string) (string, error) {
|
|
|
|
|
|
|
|
if m.ContentType == protobuf.ChatMessage_AUDIO {
|
|
|
|
return "Audio", nil
|
|
|
|
}
|
|
|
|
if m.ContentType == protobuf.ChatMessage_STICKER {
|
|
|
|
return "Sticker", nil
|
|
|
|
}
|
|
|
|
if m.ContentType == protobuf.ChatMessage_IMAGE {
|
|
|
|
return "Image", nil
|
|
|
|
}
|
|
|
|
if m.ContentType == protobuf.ChatMessage_COMMUNITY {
|
|
|
|
return "Community", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.ParsedTextAst == nil {
|
|
|
|
err := m.PrepareContent()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
visitor := &SimplifiedTextVisitor{canonicalNames: canonicalNames}
|
|
|
|
ast.Walk(*m.ParsedTextAst, visitor)
|
|
|
|
return visitor.text, nil
|
|
|
|
}
|
|
|
|
|
2020-06-17 18:55:49 +00:00
|
|
|
func getAudioMessageMIME(i *protobuf.AudioMessage) (string, error) {
|
|
|
|
switch i.Type {
|
|
|
|
case protobuf.AudioMessage_AAC:
|
|
|
|
return "aac", nil
|
|
|
|
case protobuf.AudioMessage_AMR:
|
|
|
|
return "amr", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", errors.New("audio format not supported")
|
|
|
|
}
|
2020-07-25 14:16:00 +00:00
|
|
|
|
|
|
|
// GetSigPubKey returns an ecdsa encoded public key
|
2020-07-25 16:13:08 +00:00
|
|
|
// this function is required to implement the ChatEntity interface
|
2020-07-25 14:16:00 +00:00
|
|
|
func (m Message) GetSigPubKey() *ecdsa.PublicKey {
|
|
|
|
return m.SigPubKey
|
|
|
|
}
|
2020-07-25 16:13:08 +00:00
|
|
|
|
|
|
|
// GetProtoBuf returns the struct's embedded protobuf struct
|
|
|
|
// this function is required to implement the ChatEntity interface
|
|
|
|
func (m *Message) GetProtobuf() proto.Message {
|
|
|
|
return &m.ChatMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMessageType a setter for the MessageType field
|
|
|
|
// this function is required to implement the ChatEntity interface
|
|
|
|
func (m *Message) SetMessageType(messageType protobuf.MessageType) {
|
|
|
|
m.MessageType = messageType
|
|
|
|
}
|
2021-01-15 17:47:30 +00:00
|
|
|
|
|
|
|
// WrapGroupMessage indicates whether we should wrap this in membership information
|
|
|
|
func (m *Message) WrapGroupMessage() bool {
|
|
|
|
return true
|
|
|
|
}
|
2021-02-23 07:37:08 +00:00
|
|
|
|
|
|
|
// GetPublicKey attempts to return or recreate the *ecdsa.PublicKey of the Message sender.
|
|
|
|
// If the m.SigPubKey is set this will be returned
|
|
|
|
// If the m.From is present the string is decoded and unmarshalled into a *ecdsa.PublicKey, the m.SigPubKey is set and returned
|
|
|
|
// Else an error is thrown
|
|
|
|
// This function differs from GetSigPubKey() as this function may return an error
|
|
|
|
func (m *Message) GetSenderPubKey() (*ecdsa.PublicKey, error) {
|
|
|
|
// TODO requires tests
|
|
|
|
|
|
|
|
if m.SigPubKey != nil {
|
|
|
|
return m.SigPubKey, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(m.From) > 0 {
|
|
|
|
fromB, err := hex.DecodeString(m.From[2:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
senderPubKey, err := crypto.UnmarshalPubkey(fromB)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.SigPubKey = senderPubKey
|
|
|
|
return senderPubKey, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("no Message.SigPubKey or Message.From set unable to get public key")
|
|
|
|
}
|