2016-10-04 11:49:59 +00:00
|
|
|
(ns status-im.data-store.messages
|
2019-06-02 02:22:56 +00:00
|
|
|
(:require [clojure.set :as clojure.set]
|
2019-08-05 06:17:55 +00:00
|
|
|
[re-frame.core :as re-frame]
|
|
|
|
[status-im.utils.fx :as fx]
|
2018-12-17 07:44:19 +00:00
|
|
|
[clojure.string :as string]
|
2019-08-05 06:17:55 +00:00
|
|
|
[taoensso.timbre :as log]
|
2019-06-02 02:22:56 +00:00
|
|
|
[re-frame.core :as re-frame]
|
2019-10-23 07:09:11 +00:00
|
|
|
[status-im.utils.types :as utils.types]
|
2020-01-30 11:39:22 +00:00
|
|
|
[status-im.utils.config :as config]
|
2019-08-05 06:17:55 +00:00
|
|
|
[status-im.ethereum.json-rpc :as json-rpc]
|
2017-10-19 13:58:21 +00:00
|
|
|
[status-im.constants :as constants]
|
2019-06-02 02:22:56 +00:00
|
|
|
[status-im.utils.core :as utils]))
|
2018-04-02 16:17:15 +00:00
|
|
|
|
2019-11-26 13:15:19 +00:00
|
|
|
(defn ->rpc [{:keys [content] :as message}]
|
|
|
|
(cond-> message
|
2017-12-05 13:03:25 +00:00
|
|
|
content
|
2019-11-26 13:15:19 +00:00
|
|
|
(assoc :text (:text content)
|
|
|
|
:sticker (:sticker content))
|
|
|
|
:always
|
|
|
|
(clojure.set/rename-keys {:chat-id :chatId
|
|
|
|
:clock-value :clock})))
|
2019-08-05 06:17:55 +00:00
|
|
|
|
|
|
|
(defn <-rpc [message]
|
2019-11-26 13:15:19 +00:00
|
|
|
(-> message
|
|
|
|
(clojure.set/rename-keys {:id :message-id
|
|
|
|
:whisperTimestamp :whisper-timestamp
|
2019-11-25 13:33:43 +00:00
|
|
|
:commandParameters :command-parameters
|
2019-11-26 13:15:19 +00:00
|
|
|
:messageType :message-type
|
|
|
|
:localChatId :chat-id
|
|
|
|
:contentType :content-type
|
|
|
|
:clock :clock-value
|
|
|
|
:quotedMessage :quoted-message
|
|
|
|
:outgoingStatus :outgoing-status})
|
|
|
|
|
|
|
|
(update :outgoing-status keyword)
|
2019-11-25 13:33:43 +00:00
|
|
|
(update :command-parameters clojure.set/rename-keys {:transactionHash :transaction-hash
|
|
|
|
:commandState :command-state})
|
2019-11-26 13:15:19 +00:00
|
|
|
(assoc :content {:chat-id (:chatId message)
|
|
|
|
:text (:text message)
|
|
|
|
:sticker (:sticker message)
|
|
|
|
:ens-name (:ensName message)
|
|
|
|
:line-count (:lineCount message)
|
|
|
|
:parsed-text (:parsedText message)
|
|
|
|
:rtl (:rtl message)
|
|
|
|
:response-to (:responseTo message)}
|
|
|
|
:outgoing (boolean (:outgoingStatus message)))
|
|
|
|
(dissoc :ensName :chatId :text :rtl :responseTo :sticker :lineCount :parsedText)))
|
2019-08-05 06:17:55 +00:00
|
|
|
|
|
|
|
(defn update-outgoing-status-rpc [message-id status]
|
2020-02-18 19:23:02 +00:00
|
|
|
{::json-rpc/call [{:method (json-rpc/call-ext-method "updateMessageOutgoingStatus")
|
2019-08-05 06:17:55 +00:00
|
|
|
:params [message-id status]
|
|
|
|
:on-success #(log/debug "updated message outgoing stauts" message-id status)
|
|
|
|
:on-failure #(log/error "failed to update message outgoing status" message-id status %)}]})
|
|
|
|
|
2019-11-26 13:15:19 +00:00
|
|
|
(defn save-system-messages-rpc [messages]
|
2020-02-18 19:23:02 +00:00
|
|
|
(json-rpc/call {:method (json-rpc/call-ext-method "addSystemMessages")
|
2019-11-26 13:15:19 +00:00
|
|
|
:params [(map ->rpc messages)]
|
|
|
|
:on-success #(re-frame/dispatch [:messages/system-messages-saved (map <-rpc %)])
|
|
|
|
:on-failure #(log/error "failed to save messages" %)}))
|
2019-08-05 06:17:55 +00:00
|
|
|
|
2020-02-06 15:17:30 +00:00
|
|
|
(defn messages-by-chat-id-rpc [chat-id
|
|
|
|
cursor
|
|
|
|
limit
|
|
|
|
on-success
|
|
|
|
on-failure]
|
2020-02-18 19:23:02 +00:00
|
|
|
{::json-rpc/call [{:method (json-rpc/call-ext-method "chatMessages")
|
2019-08-05 06:17:55 +00:00
|
|
|
:params [chat-id cursor limit]
|
|
|
|
:on-success (fn [result]
|
|
|
|
(on-success (update result :messages #(map <-rpc %))))
|
2020-02-06 15:17:30 +00:00
|
|
|
:on-failure on-failure}]})
|
2019-08-05 06:17:55 +00:00
|
|
|
|
2019-11-26 13:15:19 +00:00
|
|
|
(defn mark-seen-rpc [chat-id ids]
|
2020-02-18 19:23:02 +00:00
|
|
|
{::json-rpc/call [{:method (json-rpc/call-ext-method "markMessagesSeen")
|
2019-11-26 13:15:19 +00:00
|
|
|
:params [chat-id ids]
|
2019-08-05 06:17:55 +00:00
|
|
|
:on-success #(log/debug "successfully marked as seen")
|
|
|
|
:on-failure #(log/error "failed to get messages" %)}]})
|
|
|
|
|
|
|
|
(defn delete-message-rpc [id]
|
2020-02-18 19:23:02 +00:00
|
|
|
{::json-rpc/call [{:method (json-rpc/call-ext-method "deleteMessage")
|
2019-08-05 06:17:55 +00:00
|
|
|
:params [id]
|
|
|
|
:on-success #(log/debug "successfully deleted message" id)
|
|
|
|
:on-failure #(log/error "failed to delete message" % id)}]})
|
|
|
|
|
|
|
|
(defn delete-messages-from-rpc [author]
|
2020-02-18 19:23:02 +00:00
|
|
|
{::json-rpc/call [{:method (json-rpc/call-ext-method "deleteMessagesFrom")
|
2019-08-05 06:17:55 +00:00
|
|
|
:params [author]
|
|
|
|
:on-success #(log/debug "successfully deleted messages from" author)
|
|
|
|
:on-failure #(log/error "failed to delete messages from" % author)}]})
|
|
|
|
|
|
|
|
(defn delete-messages-by-chat-id-rpc [chat-id]
|
2020-02-18 19:23:02 +00:00
|
|
|
{::json-rpc/call [{:method (json-rpc/call-ext-method "deleteMessagesByChatID")
|
2019-08-05 06:17:55 +00:00
|
|
|
:params [chat-id]
|
|
|
|
:on-success #(log/debug "successfully deleted messages by chat-id" chat-id)
|
|
|
|
:on-failure #(log/error "failed to delete messages by chat-id" % chat-id)}]})
|
|
|
|
|
|
|
|
(re-frame/reg-fx
|
2019-11-26 13:15:19 +00:00
|
|
|
::save-system-message
|
2019-08-05 06:17:55 +00:00
|
|
|
(fn [messages]
|
2019-11-26 13:15:19 +00:00
|
|
|
(save-system-messages-rpc messages)))
|
|
|
|
|
|
|
|
(fx/defn save-system-messages [cofx messages]
|
|
|
|
{::save-system-message messages})
|
2019-08-05 06:17:55 +00:00
|
|
|
|
|
|
|
(fx/defn delete-message [cofx id]
|
|
|
|
(delete-message-rpc id))
|
|
|
|
|
|
|
|
(fx/defn delete-messages-from [cofx author]
|
|
|
|
(delete-messages-from-rpc author))
|
|
|
|
|
2019-11-26 13:15:19 +00:00
|
|
|
(fx/defn mark-messages-seen [_ chat-id ids]
|
|
|
|
(mark-seen-rpc chat-id ids))
|
2019-08-05 06:17:55 +00:00
|
|
|
|
|
|
|
(fx/defn update-outgoing-status [cofx message-id status]
|
|
|
|
(update-outgoing-status-rpc message-id status))
|
|
|
|
|
|
|
|
(fx/defn delete-messages-by-chat-id [cofx chat-id]
|
|
|
|
(delete-messages-by-chat-id-rpc chat-id))
|