2016-06-30 19:00:44 +03:00
|
|
|
(ns status-im.chat.handlers.send-message
|
|
|
|
(:require [status-im.utils.handlers :refer [register-handler] :as u]
|
|
|
|
[clojure.string :as s]
|
2016-10-04 14:49:59 +03:00
|
|
|
[status-im.data-store.messages :as messages]
|
2016-09-07 20:35:04 +03:00
|
|
|
[status-im.components.status :as status]
|
2016-06-30 19:00:44 +03:00
|
|
|
[status-im.utils.random :as random]
|
|
|
|
[status-im.utils.datetime :as time]
|
2016-09-04 18:39:05 +03:00
|
|
|
[re-frame.core :refer [enrich after dispatch path]]
|
2016-06-30 19:00:44 +03:00
|
|
|
[status-im.chat.utils :as cu]
|
|
|
|
[status-im.constants :refer [text-content-type
|
|
|
|
content-type-command
|
|
|
|
content-type-command-request
|
|
|
|
default-number-of-messages]]
|
2016-09-04 18:39:05 +03:00
|
|
|
[status-im.utils.datetime :as datetime]
|
|
|
|
[status-im.protocol.core :as protocol]
|
|
|
|
[taoensso.timbre :refer-macros [debug]]))
|
2016-06-30 19:00:44 +03:00
|
|
|
|
|
|
|
(defn prepare-command
|
2016-10-11 17:24:52 +03:00
|
|
|
[identity chat-id
|
|
|
|
{:keys [id preview preview-string params command to-message handler-data]}]
|
2016-06-30 19:00:44 +03:00
|
|
|
(let [content {:command (command :name)
|
2016-10-06 16:15:57 +03:00
|
|
|
:params params}]
|
2016-10-11 17:24:52 +03:00
|
|
|
{:message-id id
|
2016-06-30 19:00:44 +03:00
|
|
|
:from identity
|
|
|
|
:to chat-id
|
2016-10-04 23:58:33 +03:00
|
|
|
:timestamp (time/now-ms)
|
2016-10-11 17:24:52 +03:00
|
|
|
:content (assoc content :preview preview-string
|
|
|
|
:handler-data handler-data)
|
2016-06-30 19:00:44 +03:00
|
|
|
:content-type content-type-command
|
|
|
|
:outgoing true
|
|
|
|
:preview preview-string
|
|
|
|
:rendered-preview preview
|
|
|
|
:to-message to-message
|
|
|
|
:type (:type command)
|
|
|
|
:has-handler (:has-handler command)}))
|
|
|
|
|
2016-08-24 10:29:40 +03:00
|
|
|
(register-handler :send-chat-message
|
2016-06-30 19:00:44 +03:00
|
|
|
(u/side-effect!
|
2016-09-04 18:39:05 +03:00
|
|
|
(fn [{:keys [current-chat-id current-public-key current-account-id] :as db}]
|
2016-06-30 19:00:44 +03:00
|
|
|
(let [staged-commands (vals (get-in db [:chats current-chat-id :staged-commands]))
|
2016-10-06 16:15:57 +03:00
|
|
|
text (get-in db [:chats current-chat-id :input-text])
|
|
|
|
data {:commands staged-commands
|
|
|
|
:message text
|
|
|
|
:chat-id current-chat-id
|
|
|
|
:identity current-public-key
|
|
|
|
:address current-account-id}]
|
2016-06-30 19:00:44 +03:00
|
|
|
(dispatch [:clear-input current-chat-id])
|
|
|
|
(cond
|
|
|
|
(seq staged-commands)
|
|
|
|
(dispatch [::check-commands-handlers! data])
|
|
|
|
(not (s/blank? text))
|
|
|
|
(dispatch [::prepare-message data]))))))
|
|
|
|
|
|
|
|
|
|
|
|
(register-handler ::check-commands-handlers!
|
|
|
|
(u/side-effect!
|
|
|
|
(fn [_ [_ {:keys [commands message] :as params}]]
|
|
|
|
(doseq [{:keys [command] :as message} commands]
|
2016-10-06 16:15:57 +03:00
|
|
|
(let [params' (assoc params :staged-command message)]
|
2016-10-11 17:24:52 +03:00
|
|
|
(if (:sent-to-jail? message)
|
|
|
|
;; todo there could be other reasons for "long-running"
|
|
|
|
;; hanling of the command besides sendTransaction
|
2016-06-30 19:00:44 +03:00
|
|
|
(dispatch [:navigate-to :confirm])
|
|
|
|
(if (:has-handler command)
|
|
|
|
(dispatch [::invoke-command-handlers! params'])
|
|
|
|
(dispatch [:prepare-command! params'])))))
|
|
|
|
(when-not (s/blank? message)
|
|
|
|
(dispatch [::prepare-message params])))))
|
|
|
|
|
|
|
|
(register-handler :clear-input
|
|
|
|
(path :chats)
|
|
|
|
(fn [db [_ chat-id]]
|
|
|
|
(assoc-in db [chat-id :input-text] nil)))
|
|
|
|
|
|
|
|
(register-handler :prepare-command!
|
|
|
|
(u/side-effect!
|
2016-10-06 16:15:57 +03:00
|
|
|
(fn [{:keys [current-public-key] :as db}
|
2016-10-11 17:24:52 +03:00
|
|
|
[_ {:keys [chat-id staged-command handler-data] :as params}]]
|
|
|
|
(let [command' (->> (assoc staged-command :handler-data handler-data)
|
2016-09-04 18:39:05 +03:00
|
|
|
(prepare-command current-public-key chat-id)
|
2016-06-30 19:00:44 +03:00
|
|
|
(cu/check-author-direction db chat-id))]
|
2016-10-11 17:24:52 +03:00
|
|
|
(dispatch [:clear-command chat-id (:id staged-command)])
|
2016-06-30 19:00:44 +03:00
|
|
|
(dispatch [::send-command! (assoc params :command command')])))))
|
|
|
|
|
2016-10-11 17:24:52 +03:00
|
|
|
(register-handler :clear-command
|
2016-06-30 19:00:44 +03:00
|
|
|
(fn [db [_ chat-id id]]
|
2016-10-11 17:24:52 +03:00
|
|
|
(if chat-id
|
|
|
|
(update-in db [:chats chat-id :staged-commands] dissoc id)
|
|
|
|
db)))
|
2016-06-30 19:00:44 +03:00
|
|
|
|
|
|
|
(register-handler ::send-command!
|
|
|
|
(u/side-effect!
|
|
|
|
(fn [_ [_ params]]
|
|
|
|
(dispatch [::add-command params])
|
|
|
|
(dispatch [::save-command! params])
|
|
|
|
(dispatch [::dispatch-responded-requests! params])
|
|
|
|
(dispatch [::send-command-protocol! params]))))
|
|
|
|
|
|
|
|
(register-handler ::add-command
|
|
|
|
(after (fn [_ [_ {:keys [handler]}]]
|
|
|
|
(when handler (handler))))
|
|
|
|
(fn [db [_ {:keys [chat-id command]}]]
|
|
|
|
(cu/add-message-to-db db chat-id command)))
|
|
|
|
|
|
|
|
(register-handler ::save-command!
|
|
|
|
(u/side-effect!
|
2016-10-06 16:15:57 +03:00
|
|
|
(fn [_ [_ {:keys [command chat-id]}]]
|
2016-10-04 14:49:59 +03:00
|
|
|
(messages/save
|
2016-06-30 19:00:44 +03:00
|
|
|
chat-id
|
|
|
|
(dissoc command :rendered-preview :to-message :has-handler)))))
|
|
|
|
|
|
|
|
(register-handler ::dispatch-responded-requests!
|
|
|
|
(u/side-effect!
|
|
|
|
(fn [_ [_ {:keys [command chat-id]}]]
|
|
|
|
(let [{:keys [to-message]} command]
|
|
|
|
(when to-message
|
|
|
|
(dispatch [:request-answered! chat-id to-message]))))))
|
|
|
|
|
|
|
|
(register-handler ::invoke-command-handlers!
|
|
|
|
(u/side-effect!
|
2016-10-11 17:24:52 +03:00
|
|
|
(fn [db [_ {:keys [chat-id address staged-command]
|
|
|
|
:as parameters}]]
|
|
|
|
(let [{:keys [id command params]} staged-command
|
2016-06-30 19:00:44 +03:00
|
|
|
{:keys [type name]} command
|
2016-10-06 16:15:57 +03:00
|
|
|
path [(if (= :command type) :commands :responses)
|
|
|
|
name
|
|
|
|
:handler]
|
|
|
|
to (get-in db [:contacts chat-id :address])
|
|
|
|
params {:parameters params
|
2016-10-11 17:24:52 +03:00
|
|
|
:context {:from address
|
|
|
|
:to to
|
|
|
|
:message-id id}}]
|
|
|
|
(dispatch [::command-in-processing chat-id id])
|
|
|
|
(status/call-jail
|
|
|
|
chat-id
|
|
|
|
path
|
|
|
|
params
|
|
|
|
#(dispatch [:command-handler! chat-id parameters %]))))))
|
|
|
|
|
|
|
|
(register-handler ::command-in-processing
|
|
|
|
(fn [db [_ chat-id id]]
|
|
|
|
(assoc-in db [:chats chat-id :staged-commands id :sent-to-jail?] true)))
|
2016-06-30 19:00:44 +03:00
|
|
|
|
|
|
|
(register-handler ::prepare-message
|
|
|
|
(u/side-effect!
|
|
|
|
(fn [db [_ {:keys [chat-id identity message] :as params}]]
|
2016-08-24 10:29:40 +03:00
|
|
|
(let [{:keys [group-chat]} (get-in db [:chats chat-id])
|
2016-10-06 16:15:57 +03:00
|
|
|
message' (cu/check-author-direction
|
|
|
|
db chat-id
|
|
|
|
{:message-id (random/id)
|
|
|
|
:chat-id chat-id
|
|
|
|
:content message
|
|
|
|
:from identity
|
|
|
|
:content-type text-content-type
|
|
|
|
:outgoing true
|
|
|
|
:timestamp (time/now-ms)})
|
2016-08-24 10:29:40 +03:00
|
|
|
message'' (if group-chat
|
|
|
|
(assoc message' :group-id chat-id :message-type :group-user-message)
|
|
|
|
(assoc message' :to chat-id :message-type :user-message))
|
2016-10-06 16:15:57 +03:00
|
|
|
params' (assoc params :message message'')]
|
2016-06-30 19:00:44 +03:00
|
|
|
(dispatch [::add-message params'])
|
2016-08-24 16:40:36 +03:00
|
|
|
(dispatch [::save-message! params'])))))
|
2016-06-30 19:00:44 +03:00
|
|
|
|
|
|
|
(register-handler ::add-message
|
|
|
|
(fn [db [_ {:keys [chat-id message]}]]
|
|
|
|
(cu/add-message-to-db db chat-id message)))
|
|
|
|
|
|
|
|
(register-handler ::save-message!
|
2016-08-24 16:40:36 +03:00
|
|
|
(after (fn [_ [_ params]]
|
|
|
|
(dispatch [::send-message! params])))
|
2016-06-30 19:00:44 +03:00
|
|
|
(u/side-effect!
|
|
|
|
(fn [_ [_ {:keys [chat-id message]}]]
|
2016-10-04 14:49:59 +03:00
|
|
|
(messages/save chat-id message))))
|
2016-06-30 19:00:44 +03:00
|
|
|
|
|
|
|
(register-handler ::send-message!
|
|
|
|
(u/side-effect!
|
2016-09-04 18:39:05 +03:00
|
|
|
(fn [{:keys [web3 chats]} [_ {{:keys [message-type]
|
|
|
|
:as message} :message
|
|
|
|
chat-id :chat-id}]]
|
2016-06-30 19:00:44 +03:00
|
|
|
(when (and message (cu/not-console? chat-id))
|
2016-09-04 18:39:05 +03:00
|
|
|
(let [message' (select-keys message [:from :message-id])
|
2016-10-06 16:15:57 +03:00
|
|
|
payload (select-keys message [:timestamp :content :content-type])
|
|
|
|
options {:web3 web3
|
|
|
|
:message (assoc message' :payload payload)}]
|
2016-09-04 18:39:05 +03:00
|
|
|
(if (= message-type :group-user-message)
|
|
|
|
(let [{:keys [public-key private-key]} (chats chat-id)]
|
|
|
|
(protocol/send-group-message! (assoc options
|
|
|
|
:group-id chat-id
|
|
|
|
:keypair {:public public-key
|
|
|
|
:private private-key})))
|
|
|
|
(protocol/send-message! (assoc-in options
|
|
|
|
[:message :to] (:to message)))))))))
|
2016-06-30 19:00:44 +03:00
|
|
|
|
|
|
|
(register-handler ::send-command-protocol!
|
|
|
|
(u/side-effect!
|
2016-09-04 18:39:05 +03:00
|
|
|
(fn [{:keys [web3 current-public-key chats] :as db} [_ {:keys [chat-id command]}]]
|
|
|
|
(let [{:keys [content message-id]} command]
|
2016-06-30 19:00:44 +03:00
|
|
|
(when (cu/not-console? chat-id)
|
2016-09-04 18:39:05 +03:00
|
|
|
(let [{:keys [public-key private-key]} (chats chat-id)
|
|
|
|
{:keys [group-chat]} (get-in db [:chats chat-id])
|
|
|
|
payload {:content content
|
|
|
|
:content-type content-type-command
|
|
|
|
:timestamp (datetime/now-ms)}
|
|
|
|
options {:web3 web3
|
|
|
|
:message {:from current-public-key
|
|
|
|
:message-id message-id
|
|
|
|
:payload payload}}]
|
2016-06-30 19:00:44 +03:00
|
|
|
(if group-chat
|
2016-09-04 18:39:05 +03:00
|
|
|
(protocol/send-group-message! (assoc options
|
|
|
|
:group-id chat-id
|
|
|
|
:keypair {:public public-key
|
|
|
|
:private private-key}))
|
|
|
|
(protocol/send-message! (assoc-in options
|
|
|
|
[:message :to] chat-id)))))))))
|