From 469556a04b4a88941e40cd907d74d4ff449d7e3e Mon Sep 17 00:00:00 2001 From: tbenr Date: Sat, 29 Dec 2018 20:28:43 +0100 Subject: [PATCH] Fixes #6594 Signed-off-by: Julien Eluard --- src/status_im/chat/commands/core.cljs | 14 ++++++++ src/status_im/chat/commands/receiving.cljs | 3 +- src/status_im/chat/commands/sending.cljs | 6 ++-- .../ui/screens/chat/message/message.cljs | 5 +-- .../ui/screens/home/views/inner_item.cljs | 5 +-- .../status_im/test/chat/commands/core.cljs | 32 +++++++++++++++++++ 6 files changed, 57 insertions(+), 8 deletions(-) diff --git a/src/status_im/chat/commands/core.cljs b/src/status_im/chat/commands/core.cljs index d510086493..31c3800dff 100644 --- a/src/status_im/chat/commands/core.cljs +++ b/src/status_im/chat/commands/core.cljs @@ -34,6 +34,20 @@ [type] (keyword (str (protocol/id type) "-button"))) +(defn add-chat-contacts + "Enrich command-message by adding contact list of the current private or group chat" + [contacts {:keys [public? group-chat] :as command-message}] + (cond + public? command-message + group-chat (assoc command-message :contacts (map status-im.contact.db/public-key->address contacts)) + :else (assoc command-message :contact (status-im.contact.db/public-key->address (first contacts))))) + +(defn enrich-command-message-for-events + "adds new pairs to command-message to be consumed by extension events" + [db {:keys [chat-id] :as command-message}] + (let [{:keys [contacts public? group-chat]} (get-in db [:chats chat-id])] + (add-chat-contacts contacts (assoc command-message :public? public? :group-chat group-chat)))) + (defn generate-short-preview "Returns short preview for command" [{:keys [type]} command-message] diff --git a/src/status_im/chat/commands/receiving.cljs b/src/status_im/chat/commands/receiving.cljs index 2a71cd5602..2b33fc71f9 100644 --- a/src/status_im/chat/commands/receiving.cljs +++ b/src/status_im/chat/commands/receiving.cljs @@ -1,5 +1,6 @@ (ns status-im.chat.commands.receiving (:require [status-im.chat.commands.protocol :as protocol] + [status-im.chat.commands.core :as commands] [status-im.utils.fx :as fx])) (defn lookup-command-by-ref @@ -14,7 +15,7 @@ [{:keys [db] :as cofx} message] (let [id->command (:id->command db)] (when-let [{:keys [type]} (lookup-command-by-ref message id->command)] - (protocol/on-receive type message cofx)))) + (protocol/on-receive type (commands/enrich-command-message-for-events db message) cofx)))) (defn enhance-receive-parameters "Enhances parameters for the received command message. diff --git a/src/status_im/chat/commands/sending.cljs b/src/status_im/chat/commands/sending.cljs index 8190e4ffa9..2f451f74f1 100644 --- a/src/status_im/chat/commands/sending.cljs +++ b/src/status_im/chat/commands/sending.cljs @@ -37,15 +37,15 @@ ;; no yield control, proceed with sending the command message (let [command-message (create-command-message chat-id type parameter-map cofx)] (fx/merge cofx - #(protocol/on-send type command-message %) + #(protocol/on-send type (commands/enrich-command-message-for-events db command-message) %) (commands.input/set-command-reference nil) (chat.message/send-message command-message))))))) (fx/defn send "Sends command with given parameters in particular chat" - [cofx chat-id {:keys [type]} parameter-map] + [{:keys [db] :as cofx} chat-id {:keys [type]} parameter-map] (let [command-message (create-command-message chat-id type parameter-map cofx)] (fx/merge cofx - #(protocol/on-send type command-message %) + #(protocol/on-send type (commands/enrich-command-message-for-events db command-message) %) (commands.input/set-command-reference nil) (chat.message/send-message command-message)))) diff --git a/src/status_im/ui/screens/chat/message/message.cljs b/src/status_im/ui/screens/chat/message/message.cljs index cc2a43387b..63f8858058 100644 --- a/src/status_im/ui/screens/chat/message/message.cljs +++ b/src/status_im/ui/screens/chat/message/message.cljs @@ -32,7 +32,8 @@ (defview message-content-command [command-message] - (letsubs [id->command [:chats/id->command]] + (letsubs [id->command [:chats/id->command] + {:keys [contacts]} [:chats/current-chat]] (let [{:keys [type] :as command} (commands-receiving/lookup-command-by-ref command-message id->command) extension-id (get-in command-message [:content :params :extension-id])] (if (and platform/mobile? extension-id @@ -43,7 +44,7 @@ ;; or installed extension has differen extension id [install-extension-message extension-id (:outgoing command-message)] (if command - (commands/generate-preview command command-message) + (commands/generate-preview command (commands/add-chat-contacts contacts command-message)) [react/text (str "Unhandled command: " (-> command-message :content :command-path first))]))))) (defview message-timestamp [t justify-timestamp? outgoing command? content] diff --git a/src/status_im/ui/screens/home/views/inner_item.cljs b/src/status_im/ui/screens/home/views/inner_item.cljs index b52c58f145..c1e0d03c9d 100644 --- a/src/status_im/ui/screens/home/views/inner_item.cljs +++ b/src/status_im/ui/screens/home/views/inner_item.cljs @@ -22,9 +22,10 @@ [status-im.browser.core :as browser])) (defview command-short-preview [message] - (letsubs [id->command [:chats/id->command]] + (letsubs [id->command [:chats/id->command] + {:keys [contacts]} [:chats/current-chat]] (when-let [command (commands-receiving/lookup-command-by-ref message id->command)] - (commands/generate-short-preview command message)))) + (commands/generate-short-preview command (commands/add-chat-contacts contacts message))))) (defn message-content-text [{:keys [content content-type] :as message}] [react/view styles/last-message-container diff --git a/test/cljs/status_im/test/chat/commands/core.cljs b/test/cljs/status_im/test/chat/commands/core.cljs index 78a6e2b481..3ce5a92dec 100644 --- a/test/cljs/status_im/test/chat/commands/core.cljs +++ b/test/cljs/status_im/test/chat/commands/core.cljs @@ -106,3 +106,35 @@ (core/chat-commands (get-in fx [:db :id->command]) (get-in fx [:db :access-scope->command-id]) {:chat-id "contact"}))))))) + +(def contacts #{"0x0471b2be1e8b971f75b571ba047baa58e2f40f67dad38f6381b2382df43f7176b1813bf372af4cd8451ed9063213029378b9fbc7db792d496e1a6161c42d999edf" + "0x04b790f2c3f4079f35a1fa396465ceb243cc446c9af211d0a1774f869eb9632a67a6e664e24075ec5c5a8a95a509a2a8173dbfeb88af372e784a37fecc1b5c0ba5" + "0x04cc3cec3f88dc1a39e224388f0304023fc78c2a7d05e4ebd61638192cc592d2c13d8f081b5d9995dbfcbe45a4ca7eb80d5c505eee660e8fee0df2da222f047287"}) + +(def contacts_addresses '("5adf1b9e1fa4bd4889fecd598b45079045d98f0e" + "21631d18d9681d4ffdd460fc45fa52159fcd95c8" + "5541e3be81b76d76cdbf968516caa5a5b773763b")) + +(deftest enrich-command-message-for-events-test-public + (let [db {:chats {"1" {:contacts nil :public? true :group-chat false}}} + msg {:chat-id "1"} + enriched-msg (core/enrich-command-message-for-events db msg)] + (testing "command-message correctly (not) enriched - public chat" + (is (= enriched-msg + (assoc msg :public? true :group-chat false)))))) + +(deftest enrich-command-message-for-events-test-groupchat + (let [db {:chats {"1" {:contacts contacts :public? false :group-chat true}}} + msg {:chat-id "1"} + enriched-msg (core/enrich-command-message-for-events db msg)] + (testing "command-message correctly enriched - group chat" + (is (= enriched-msg + (assoc msg :public? false :group-chat true :contacts contacts_addresses)))))) + +(deftest enrich-command-message-for-events-test-1on1-chat + (let [db {:chats {"1" {:contacts contacts :public? false :group-chat false}}} + msg {:chat-id "1"} + enriched-msg (core/enrich-command-message-for-events db msg)] + (testing "command-message correctly enriched - 1on1 chat" + (is (= enriched-msg + (assoc msg :public? false :group-chat false :contact (first contacts_addresses))))))) \ No newline at end of file