Fix the bug and remove requests

This commit is contained in:
janherich 2018-09-04 11:58:51 +02:00 committed by Roman Volosovskyi
parent 6de53a5e9c
commit 252d09ee05
No known key found for this signature in database
GPG Key ID: 0238A4B5ECEE70DE
11 changed files with 107 additions and 146 deletions

View File

@ -5,7 +5,7 @@
[re-frame.core :as re-frame]
[status-im.chat.commands.protocol :as protocol]
[status-im.chat.commands.impl.transactions.styles :as transactions-styles]
[status-im.data-store.requests :as requests-store]
[status-im.data-store.messages :as messages-store]
[status-im.ui.components.react :as react]
[status-im.ui.components.icons.vector-icons :as vector-icons]
[status-im.ui.components.colors :as colors]
@ -258,6 +258,12 @@
prices)
:currency currency)))
(defn- params-unchanged? [send-message request-message]
(and (= (get-in send-message [:content :params :asset])
(get-in request-message [:content :params :asset]))
(= (get-in send-message [:content :params :amount])
(get-in request-message [:content :params :amount]))))
(deftype PersonalSendCommand []
protocol/Command
(id [_] "send")
@ -268,10 +274,13 @@
;; Only superficial/formatting validation, "real validation" will be performed
;; by the wallet, where we yield control in the next step
(personal-send-request-validation parameters cofx))
(on-send [_ {:keys [chat-id]} {:keys [db]}]
(on-send [_ {:keys [chat-id] :as send-message} {:keys [db]}]
(when-let [{:keys [responding-to]} (get-in db [:chats chat-id :input-metadata])]
{:db (update-in db [:chats chat-id :requests] dissoc responding-to)
:data-store/tx [(requests-store/mark-request-as-answered-tx chat-id responding-to)]}))
(when-let [request-message (get-in db [:chats chat-id :messages responding-to])]
(when (params-unchanged? send-message request-message)
(let [updated-request-message (assoc-in request-message [:content :params :answered?] true)]
{:db (assoc-in db [:chats chat-id :messages responding-to] updated-request-message)
:data-store/tx [(messages-store/save-message-tx updated-request-message)]})))))
(on-receive [_ command-message cofx]
(when-let [tx-hash (get-in command-message [:content :params :tx-hash])]
(wallet.transactions/store-chat-transaction-hash tx-hash cofx)))
@ -370,67 +379,55 @@
(defview request-preview
[{:keys [message-id content outgoing timestamp timestamp-str group-chat]}]
(letsubs [id->command [:get-id->command]
answered? [:is-request-answered? message-id]
status-initialized? [:get :status-module-initialized?]
network [:network-name]
prices [:prices]]
(let [{:keys [amount asset fiat-amount currency] request-network :network} (:params content)
recipient-name (get-in content [:params :bot-db :public :recipient])
(let [{:keys [amount asset fiat-amount currency answered?] request-network :network} (:params content)
network-mismatch? (and request-network (not= request-network network))
command (get id->command ["send" #{:personal-chats}])
on-press-handler (cond
network-mismatch?
nil
(and (not answered?)
status-initialized?)
#(re-frame/dispatch [:select-chat-input-command
command
[(or asset "ETH") amount]
{:responding-to message-id}]))]
[react/view
[react/touchable-highlight
{:on-press on-press-handler}
[react/view (transactions-styles/command-request-message-view outgoing)
[react/view
[react/view
[react/text {:style (transactions-styles/command-request-header-text outgoing)}
(i18n/label :transaction-request)]]
[react/view transactions-styles/command-request-row
[react/text {:style transactions-styles/command-request-amount-text
:font :medium}
amount
[react/text {:style (transactions-styles/command-amount-currency-separator outgoing)}
"."]
[react/text {:style (transactions-styles/command-request-currency-text outgoing)
:font :default}
asset]]]
[react/view transactions-styles/command-request-fiat-amount-row
[react/text {:style transactions-styles/command-request-fiat-amount-text}
(str "~ " fiat-amount " " (or currency (i18n/label :usd-currency)))]]
(when (and group-chat recipient-name)
[react/text {:style transactions-styles/command-request-recipient-text}
(str
(i18n/label :request-requesting-from)
" "
recipient-name)])
(when network-mismatch?
[react/text {:style transactions-styles/command-request-network-text}
(str (i18n/label :on) " " request-network)])
[react/view transactions-styles/command-request-timestamp-row
[react/text {:style (transactions-styles/command-request-timestamp-text outgoing)}
(str
(datetime/timestamp->mini-date timestamp)
" "
(i18n/label :at)
" "
timestamp-str)]]
(when-not outgoing
[react/view
[react/view transactions-styles/command-request-separator-line]
[react/view transactions-styles/command-request-button
[react/text {:style (transactions-styles/command-request-button-text answered?)
:on-press on-press-handler}
(i18n/label (if answered? :command-button-sent :command-button-send))]]])]]]])))
markup [react/view (transactions-styles/command-request-message-view outgoing)
[react/view
[react/text {:style (transactions-styles/command-request-header-text outgoing)}
(i18n/label :transaction-request)]]
[react/view transactions-styles/command-request-row
[react/text {:style transactions-styles/command-request-amount-text
:font :medium}
amount
[react/text {:style (transactions-styles/command-amount-currency-separator outgoing)}
"."]
[react/text {:style (transactions-styles/command-request-currency-text outgoing)
:font :default}
asset]]]
[react/view transactions-styles/command-request-fiat-amount-row
[react/text {:style transactions-styles/command-request-fiat-amount-text}
(str "~ " fiat-amount " " (or currency (i18n/label :usd-currency)))]]
(when network-mismatch?
[react/text {:style transactions-styles/command-request-network-text}
(str (i18n/label :on) " " request-network)])
[react/view transactions-styles/command-request-timestamp-row
[react/text {:style (transactions-styles/command-request-timestamp-text outgoing)}
(str
(datetime/timestamp->mini-date timestamp)
" "
(i18n/label :at)
" "
timestamp-str)]]
(when-not outgoing
[react/view
[react/view transactions-styles/command-request-separator-line]
[react/view transactions-styles/command-request-button
[react/text {:style (transactions-styles/command-request-button-text answered?)}
(i18n/label (if answered? :command-button-sent :command-button-send))]]])]]
(if (and (not network-mismatch?)
status-initialized?
(not outgoing)
(not answered?))
[react/touchable-highlight {:on-press #(re-frame/dispatch [:select-chat-input-command
command
[(or asset "ETH") amount]
{:responding-to message-id}])}
markup]
markup))))
(deftype PersonalRequestCommand []
protocol/Command
@ -441,13 +438,7 @@
(validate [_ parameters cofx]
(personal-send-request-validation parameters cofx))
(on-send [_ _ _])
(on-receive [_ {:keys [message-id chat-id]} {:keys [db]}]
(let [request {:chat-id chat-id
:message-id message-id
:response "send"
:status "open"}]
{:db (assoc-in db [:chats chat-id :requests message-id] request)
:data-store/tx [(requests-store/save-request-tx request)]}))
(on-receive [_ _ _])
(short-preview [_ command-message]
(personal-send-request-short-preview :command-requesting command-message))
(preview [_ command-message]

View File

@ -1,6 +1,5 @@
(ns status-im.chat.events
(:require status-im.chat.events.input
status-im.chat.events.requests
status-im.chat.events.send-message
status-im.chat.events.receive-message
[clojure.string :as string]

View File

@ -1,24 +0,0 @@
(ns status-im.chat.events.requests
(:require [status-im.constants :as constants]
[status-im.data-store.requests :as requests-store]))
;; Functions
(defn request-answered
"Takes chat-id, message-id and cofx, returns fx necessary data for marking request as answered"
[chat-id message-id {:keys [db]}]
(when message-id
{:db (update-in db [:chats chat-id :requests] dissoc message-id)
:data-store/tx [(requests-store/mark-request-as-answered-tx chat-id message-id)]}))
(defn add-request
"Takes chat-id, message-id + cofx and returns fx with necessary data for adding new request"
[chat-id message-id {:keys [db]}]
(let [{:keys [content-type content]} (get-in db [:chats chat-id :messages message-id])]
(when (= content-type constants/content-type-command-request)
(let [request {:chat-id chat-id
:message-id message-id
:response (:request-command content)
:status "open"}]
{:db (assoc-in db [:chats chat-id :requests message-id] request)
:data-store/tx [(requests-store/save-request-tx request)]}))))

View File

@ -304,12 +304,6 @@
(fn [[show-suggestions-box? selected-command]]
(and show-suggestions-box? (not selected-command))))
(reg-sub
:is-request-answered?
:<- [:get-current-chat]
(fn [{:keys [requests]} [_ message-id]]
(not= "open" (get-in requests [message-id :status]))))
(reg-sub
:unviewed-messages-count
(fn [[_ chat-id]]

View File

@ -10,8 +10,7 @@
status-im.data-store.browser
status-im.data-store.accounts
status-im.data-store.local-storage
status-im.data-store.mailservers
status-im.data-store.requests))
status-im.data-store.mailservers))
(defn init [encryption-key]
(if @data-source/base-realm

View File

@ -8,7 +8,8 @@
[status-im.data-store.realm.schemas.account.v6.core :as v6]
[status-im.data-store.realm.schemas.account.v7.core :as v7]
[status-im.data-store.realm.schemas.account.v8.core :as v8]
[status-im.data-store.realm.schemas.account.v9.core :as v9]))
[status-im.data-store.realm.schemas.account.v9.core :as v9]
[status-im.data-store.realm.schemas.account.v10.core :as v10]))
;; TODO(oskarth): Add failing test if directory vXX exists but isn't in schemas.
@ -39,4 +40,7 @@
:migration v8/migration}
{:schema v9/schema
:schemaVersion 9
:migration v9/migration}])
:migration v9/migration}
{:schema v10/schema
:schemaVersion 10
:migration v10/migration}])

View File

@ -0,0 +1,40 @@
(ns status-im.data-store.realm.schemas.account.v10.core
(:require [status-im.data-store.realm.schemas.account.v5.chat :as chat]
[status-im.data-store.realm.schemas.account.v6.transport :as transport]
[status-im.data-store.realm.schemas.account.v1.contact :as contact]
[status-im.data-store.realm.schemas.account.v7.message :as message]
[status-im.data-store.realm.schemas.account.v1.user-status :as user-status]
[status-im.data-store.realm.schemas.account.v1.local-storage :as local-storage]
[status-im.data-store.realm.schemas.account.v2.mailserver :as mailserver]
[status-im.data-store.realm.schemas.account.v8.browser :as browser]
[status-im.data-store.realm.schemas.account.v9.dapp-permissions :as dapp-permissions]
[cljs.reader :as reader]
[taoensso.timbre :as log]))
(def schema [chat/schema
transport/schema
contact/schema
message/schema
mailserver/schema
user-status/schema
local-storage/schema
browser/schema
dapp-permissions/schema])
(defn message-by-id [realm message-id]
(some-> realm
(.objects "message")
(.filtered (str "message-id = \"" message-id "\""))
(aget 0)))
(defn migration [old-realm new-realm]
(log/debug "migrating v10 account database")
(some-> old-realm
(.objects "request")
(.filtered (str "status = \"answered\""))
(.map (fn [request _ _]
(let [message-id (aget request "message-id")
message (message-by-id new-realm message-id)
content (reader/read-string (aget message "content"))
new-content (assoc-in content [:params :answered?] true)]
(aset message "content" (pr-str new-content)))))))

View File

@ -1,25 +0,0 @@
(ns status-im.data-store.requests
(:require [re-frame.core :as re-frame]
[status-im.data-store.realm.core :as core]))
(re-frame/reg-cofx
:data-store/get-unanswered-requests
(fn [cofx _]
(assoc cofx :stored-unanswered-requests (-> @core/account-realm
(core/get-by-field :request :status "open")
(core/all-clj :request)))))
(defn save-request-tx
"Returns tx function for saving request"
[request]
(fn [realm]
(core/create realm :request request true)))
(defn mark-request-as-answered-tx
"Given chat-id and message-id, returns tx function for marking request as answered"
[chat-id message-id]
(fn [realm]
(some-> (core/get-by-fields realm :request :and [[:chat-id chat-id]
[:message-id message-id]])
core/single
(aset "status" "answered"))))

View File

@ -70,7 +70,6 @@
(re-frame/inject-cofx :data-store/get-user-statuses)
(re-frame/inject-cofx :data-store/get-unviewed-messages)
(re-frame/inject-cofx :data-store/message-ids)
(re-frame/inject-cofx :data-store/get-unanswered-requests)
(re-frame/inject-cofx :data-store/get-local-storage-data)
(re-frame/inject-cofx :data-store/get-all-contacts)
(re-frame/inject-cofx :data-store/get-all-mailservers)

View File

@ -43,16 +43,11 @@
(defn initialize-chats [{:keys [db
default-dapps
all-stored-chats
stored-unanswered-requests
get-stored-messages
get-stored-user-statuses
get-stored-unviewed-messages
stored-message-ids] :as cofx}]
(let [chat->message-id->request (reduce (fn [acc {:keys [chat-id message-id] :as request}]
(assoc-in acc [chat-id message-id] request))
{}
stored-unanswered-requests)
stored-unviewed-messages (get-stored-unviewed-messages (:current-public-key db))
(let [stored-unviewed-messages (get-stored-unviewed-messages (:current-public-key db))
chats (reduce (fn [acc {:keys [chat-id] :as chat}]
(let [chat-messages (index-messages (get-stored-messages chat-id))
message-ids (keys chat-messages)
@ -60,7 +55,6 @@
(assoc acc chat-id
(assoc chat
:unviewed-messages unviewed-ids
:requests (get chat->message-id->request chat-id)
:messages chat-messages
:message-statuses (get-stored-user-statuses chat-id message-ids)
:not-loaded-message-ids (set/difference (get stored-message-ids chat-id)

View File

@ -76,14 +76,4 @@
{:title "Amount"
:description "Max number of decimals is 18"}))
(is (= (protocol/validate personal-request-command {:asset "ETH" :amount "0.01"} cofx)
nil)))
(testing "On receive adds pending request when `/request` command is received"
(let [fx (protocol/on-receive personal-request-command
{:chat-id "recipient"
:message-id "0xAA"}
cofx)]
(is (= (get-in fx [:db :chats "recipient" :requests "0xAA"])
{:chat-id "recipient"
:message-id "0xAA"
:response "send"
:status "open"})))))
nil))))