Parse messages in status-go

This commit enables parsing of messages in status-go.
Currently only a few messages are supported in status-protocol-go.
For now we only enable Message types.
Status-react will conditionally use the parsed version if present.
Eventually this can be moved to a separate signal/different structure,
but for the time being is best to validate with the minimum amount of
changes.

The next step would be handle validation and processing of the field in
status-go, so we can skip saving the message from status-react.

This commit should improve performance of receiving messages from a
chat, although haven't had time to validate that.

Signed-off-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
This commit is contained in:
Andrea Maria Piana 2019-11-05 16:51:13 +01:00
parent 629464cc35
commit 5fe385c225
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
8 changed files with 37 additions and 19 deletions

View File

@ -133,7 +133,7 @@
:content (cond-> {:chat-id current-chat-id :content (cond-> {:chat-id current-chat-id
:text input-text} :text input-text}
message-id message-id
(assoc :response-to-v2 message-id) (assoc :response-to message-id)
preferred-name preferred-name
(assoc :name preferred-name))}) (assoc :name preferred-name))})
(commands.input/set-command-reference nil) (commands.input/set-command-reference nil)

View File

@ -105,7 +105,7 @@
(defn emoji-only-content? (defn emoji-only-content?
"Determines if text is just an emoji" "Determines if text is just an emoji"
[{:keys [text response-to-v2]}] [{:keys [text response-to]}]
(and (not response-to-v2) (and (not response-to)
(string? text) (string? text)
(re-matches constants/regx-emoji text))) (re-matches constants/regx-emoji text)))

View File

@ -28,7 +28,7 @@
:content-type :contentType :content-type :contentType
:clock-value :clockValue :clock-value :clockValue
:outgoing-status :outgoingStatus}) :outgoing-status :outgoingStatus})
(assoc :replyTo (get-in message [:content :response-to-v2])))) (assoc :replyTo (get-in message [:content :response-to]))))
(defn update-quoted-message [message] (defn update-quoted-message [message]
(let [parsed-content (utils/safe-read-message-content (get-in message [:quotedMessage :content]))] (let [parsed-content (utils/safe-read-message-content (get-in message [:quotedMessage :content]))]

View File

@ -53,7 +53,6 @@
(spec/def :message.content/text (spec/and string? (complement s/blank?))) (spec/def :message.content/text (spec/and string? (complement s/blank?)))
(spec/def :message.content/response-to string?) (spec/def :message.content/response-to string?)
(spec/def :message.content/response-to-v2 string?)
(spec/def :message.content/command-path (spec/tuple string? (spec/coll-of (spec/or :scope keyword? :chat-id string?) :kind set? :min-count 1))) (spec/def :message.content/command-path (spec/tuple string? (spec/coll-of (spec/or :scope keyword? :chat-id string?) :kind set? :min-count 1)))
(spec/def :message.content/uri (spec/and string? (complement s/blank?))) (spec/def :message.content/uri (spec/and string? (complement s/blank?)))
(spec/def :message.content/pack (spec/and string? (complement s/blank?))) (spec/def :message.content/pack (spec/and string? (complement s/blank?)))

View File

@ -16,6 +16,31 @@
[taoensso.timbre :as log] [taoensso.timbre :as log]
[status-im.ethereum.json-rpc :as json-rpc])) [status-im.ethereum.json-rpc :as json-rpc]))
(def message-type-message 1)
(defn build-message [parsed-message-js]
(let [content (.-content parsed-message-js)
built-message
(protocol/Message.
{:text (.-text content)
:response-to (.-response-to content)
:chat-id (.-chat_id content)}
(.-content_type parsed-message-js)
(keyword (.-message_type parsed-message-js))
(.-clock parsed-message-js)
(.-timestamp parsed-message-js))]
built-message))
(defn handle-message
"Check if parsedMessage is present and of a supported type, if so
build a record using the right type. Otherwise defaults to transit
deserializing"
[message-js]
(if (and (.-parsedMessage message-js)
(= message-type-message) (.-messageType message-js))
(build-message (.-parsedMessage message-js))
(transit/deserialize (.-payload message-js))))
(fx/defn receive-message (fx/defn receive-message
"Receive message handles a new status-message. "Receive message handles a new status-message.
dedup-id is passed by status-go and is used to deduplicate messages at that layer. dedup-id is passed by status-go and is used to deduplicate messages at that layer.
@ -23,7 +48,6 @@
in order to stop receiving that message" in order to stop receiving that message"
[{:keys [db]} now-in-s filter-chat-id message-js] [{:keys [db]} now-in-s filter-chat-id message-js]
(let [blocked-contacts (get db :contacts/blocked #{}) (let [blocked-contacts (get db :contacts/blocked #{})
payload (.-payload message-js)
timestamp (.-timestamp (.-message message-js)) timestamp (.-timestamp (.-message message-js))
metadata-js (.-metadata message-js) metadata-js (.-metadata message-js)
metadata {:author {:publicKey (.-publicKey (.-author metadata-js)) metadata {:author {:publicKey (.-publicKey (.-author metadata-js))
@ -32,9 +56,7 @@
:dedupId (.-dedupId metadata-js) :dedupId (.-dedupId metadata-js)
:encryptionId (.-encryptionId metadata-js) :encryptionId (.-encryptionId metadata-js)
:messageId (.-messageId metadata-js)} :messageId (.-messageId metadata-js)}
raw-payload {:raw-payload message-js} status-message (handle-message message-js)
status-message (-> payload
transit/deserialize)
sig (-> metadata :author :publicKey)] sig (-> metadata :author :publicKey)]
(when (and sig (when (and sig
status-message status-message

View File

@ -70,9 +70,7 @@
[{:keys [chat-id message-id content [{:keys [chat-id message-id content
timestamp-str group-chat outgoing current-public-key expanded?] :as message}] timestamp-str group-chat outgoing current-public-key expanded?] :as message}]
[message-view message [message-view message
(let [response-to (or (:response-to content) (let [response-to (:response-to content)
(:response-to-v2 content))
collapsible? (and (:should-collapse? content) group-chat)] collapsible? (and (:should-collapse? content) group-chat)]
[react/view [react/view
(when response-to (when response-to
@ -98,8 +96,7 @@
(defn emoji-message (defn emoji-message
[{:keys [content current-public-key alias] :as message}] [{:keys [content current-public-key alias] :as message}]
(let [response-to (or (:response-to content) (let [response-to (:response-to content)]
(:response-to-v2 content))]
[message-view message [message-view message
[react/view {:style (style/style-message-text false)} [react/view {:style (style/style-message-text false)}
(when response-to (when response-to

View File

@ -2,7 +2,7 @@
"_comment": "DO NOT EDIT THIS FILE BY HAND. USE 'scripts/update-status-go.sh <tag>' instead", "_comment": "DO NOT EDIT THIS FILE BY HAND. USE 'scripts/update-status-go.sh <tag>' instead",
"owner": "status-im", "owner": "status-im",
"repo": "status-go", "repo": "status-go",
"version": "v0.34.0-beta.4", "version": "v0.34.0-beta.6",
"commit-sha1": "e311307061ebf67fece33ff02e811d94972ae5b1", "commit-sha1": "9d6601207f7bb2acf0ef0b952b2735995062fca7",
"src-sha256": "063yfb9r1vdgskhfqg0r400wrpwk2ibryc785pffwash70yyf5y0" "src-sha256": "1w6c8vgawnd48ygarh18gjzzf3x90zidsi7kiwszmygfd7dh0dp8"
} }

View File

@ -10,7 +10,7 @@
(testing "message to rpc" (testing "message to rpc"
(let [message {:message-id message-id (let [message {:message-id message-id
:content {:chat-id chat-id :content {:chat-id chat-id
:response-to-v2 "id-2" :response-to "id-2"
:text "hta"} :text "hta"}
:whisper-timestamp 1 :whisper-timestamp 1
:dedup-id "ATIwMTkwODE0YTdkNWZhZGY1N2E0ZDU3MzUxZmJkNDZkZGM1ZTU4ZjRlYzUyYWYyMDA5NTc2NWYyYmIxOTQ2OTM3NGUwNjdiMvEpTIGEjHOTAyqsrN39wST4npnSAv1AR8jJWeubanjkoGIyJooD5RVRnx6ZMt+/JzBOD2hoZzlHQWA0bC6XbdU=" :dedup-id "ATIwMTkwODE0YTdkNWZhZGY1N2E0ZDU3MzUxZmJkNDZkZGM1ZTU4ZjRlYzUyYWYyMDA5NTc2NWYyYmIxOTQ2OTM3NGUwNjdiMvEpTIGEjHOTAyqsrN39wST4npnSAv1AR8jJWeubanjkoGIyJooD5RVRnx6ZMt+/JzBOD2hoZzlHQWA0bC6XbdU="
@ -26,7 +26,7 @@
:from from :from from
:chatId chat-id :chatId chat-id
:replyTo "id-2" :replyTo "id-2"
:content "{\"chat-id\":\"chat-id\",\"response-to-v2\":\"id-2\",\"text\":\"hta\"}" :content "{\"chat-id\":\"chat-id\",\"response-to\":\"id-2\",\"text\":\"hta\"}"
:contentType "text/plain" :contentType "text/plain"
:messageType "public-group-user-message" :messageType "public-group-user-message"
:clockValue 2 :clockValue 2