Signed-off-by: Julien Eluard <julien.eluard@gmail.com>
This commit is contained in:
tbenr 2018-12-29 20:28:43 +01:00 committed by Julien Eluard
parent 771cb5c6a2
commit 469556a04b
No known key found for this signature in database
GPG Key ID: 6FD7DB5437FCBEF6
6 changed files with 57 additions and 8 deletions

View File

@ -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]

View File

@ -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.

View File

@ -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))))

View File

@ -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]

View File

@ -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

View File

@ -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)))))))