Merge pull request #419 from status-im/bug/console#415

Fix bugs related to console chat (#415)
This commit is contained in:
Roman Volosovskyi 2016-11-06 11:54:37 +02:00 committed by GitHub
commit 21aa16b842
10 changed files with 101 additions and 55 deletions

View File

@ -16,7 +16,8 @@
[status-im.utils.handlers :as u :refer [get-hashtags]]
[status-im.accounts.statuses :as statuses]
[status-im.utils.gfycat.core :refer [generate-gfy]]
[status-im.constants :refer [console-chat-id]]))
[status-im.constants :refer [console-chat-id]]
[status-im.utils.scheduler :as s]))
(defn save-account [_ [_ account]]
@ -29,29 +30,29 @@
(update db :accounts assoc address account))))
(defn account-created [result password]
(let [data (json->clj result)
(let [data (json->clj result)
public-key (:pubkey data)
address (:address data)
mnemonic (:mnemonic data)
address (:address data)
mnemonic (:mnemonic data)
{:keys [public private]} (protocol/new-keypair!)
account {:public-key public-key
:address address
:name (generate-gfy)
:status (rand-nth statuses/data)
:signed-up? true
:updates-public-key public
:updates-private-key private
:photo-path (identicon public-key)}]
account {:public-key public-key
:address address
:name (generate-gfy)
:status (rand-nth statuses/data)
:signed-up? true
:updates-public-key public
:updates-private-key private
:photo-path (identicon public-key)}]
(log/debug "account-created")
(when-not (str/blank? public-key)
(do
(dispatch [:add-account account])
(dispatch [:show-mnemonic mnemonic])
(dispatch [:login-account address password])))))
(dispatch [:show-mnemonic mnemonic])
(dispatch [:add-account account])
(dispatch [:login-account address password true]))))
(register-handler :create-account
(u/side-effect!
(fn [_ [_ password]]
(s/execute-later #(dispatch [:account-generation-message]) 400)
(status/create-account
password
#(account-created % password)))))
@ -89,7 +90,7 @@
(register-handler
:account-update
(-> (fn [{:keys [current-account-id accounts] :as db} [_ data]]
(let [data (assoc data :last-updated (time/now-ms))
(let [data (assoc data :last-updated (time/now-ms))
account (merge (get accounts current-account-id) data)]
(assoc-in db [:accounts current-account-id] account)))
((after save-account!))
@ -100,7 +101,7 @@
(u/side-effect!
(fn [{:keys [current-account-id accounts]} _]
(let [{:keys [last-updated]} (get accounts current-account-id)
now (time/now-ms)
now (time/now-ms)
needs-update? (> (- now last-updated) time/week)]
(log/info "Need to send account-update: " needs-update?)
(when needs-update?

View File

@ -49,7 +49,7 @@
(register-handler
:login-account
(u/side-effect!
(after
(fn [db [_ address password]]
(status/login address password
(fn [result]
@ -59,4 +59,6 @@
(log/debug "Logged in account: ")
(if success
(logged-in db address)
(dispatch [:set-in [:login :error] error]))))))))
(dispatch [:set-in [:login :error] error])))))))
(fn [db [_ _ _ account-creation?]]
(assoc db :account-creation? account-creation?)))

View File

@ -205,6 +205,7 @@
(if (chats console-chat-id)
db
(do
(dispatch [:add-contacts [sign-up-service/console-contact]])
(chats/save new-chat)
(contacts/save-all [sign-up-service/console-contact])
(sign-up-service/intro)
@ -220,10 +221,17 @@
(fn [db _]
(init-console-chat db false)))
(register-handler :account-generation-message
(u/side-effect!
(fn [{:keys [chats]}]
(when (> 4 (count (get-in chats [console-chat-id :messages])))
(sign-up-service/account-generation-message)))))
(register-handler :show-mnemonic
(u/side-effect!
(fn [_ [_ mnemonic]]
(sign-up-service/passpharse-messages mnemonic))))
(fn [{:keys [chats]} [_ mnemonic]]
(let [messages-count (count (get-in chats [console-chat-id :messages]))]
(sign-up-service/passpharse-messages mnemonic messages-count)))))
(register-handler :sign-up
(after (fn [_ [_ phone-number]]
@ -277,23 +285,27 @@
((after load-commands!))))
(defn initialize-chats
[{:keys [loaded-chats] :as db} _]
(let [chats (->> loaded-chats
(map (fn [{:keys [chat-id] :as chat}]
(let [last-message (messages/get-last-message chat-id)]
[chat-id (assoc chat :last-message last-message)])))
(into {}))
ids (set (keys chats))]
[{:keys [loaded-chats account-creation? chats] :as db} _]
(let [chats' (if account-creation?
chats
(->> loaded-chats
(map (fn [{:keys [chat-id] :as chat}]
(let [last-message (messages/get-last-message db chat-id)]
[chat-id (assoc chat :last-message last-message)])))
(into {})))
ids (set (keys chats'))]
(-> db
(assoc :chats chats)
(assoc :chats chats')
(assoc :chats-ids ids)
(dissoc :loaded-chats)
(init-console-chat true))))
(defn load-chats!
[db _]
(assoc db :loaded-chats (chats/get-all)))
[{:keys [account-creation?] :as db} _]
(if account-creation?
db
(assoc db :loaded-chats (chats/get-all))))
;TODO: check if its new account / signup status / create console chat
(register-handler :initialize-chats

View File

@ -9,7 +9,8 @@
content-type-command-request]]
[cljs.reader :refer [read-string]]
[status-im.data-store.chats :as chats]
[taoensso.timbre :as log]))
[taoensso.timbre :as log]
[status-im.utils.scheduler :as s]))
(defn check-preview [{:keys [content] :as message}]
(if-let [preview (:preview content)]
@ -44,7 +45,7 @@
(not= from current-identity)
(or (not exists?) active?))
(let [group-chat? (not (nil? group-id))
previous-message (messages/get-last-message chat-id')
previous-message (messages/get-last-message db chat-id')
message' (assoc (->> message
(cu/check-author-direction previous-message)
(check-preview))
@ -73,9 +74,9 @@
(u/side-effect!
(fn [_ [_ {:keys [from to payload]}]]
(dispatch [:received-message (merge payload
{:from from
:to to
:chat-id from})]))))
{:from from
:to to
:chat-id from})]))))
(register-handler :received-message
(u/side-effect!
@ -85,3 +86,17 @@
(register-handler ::add-message
(fn [db [_ add-to-chat-id {:keys [chat-id new?] :as message}]]
(cu/add-message-to-db db add-to-chat-id chat-id message new?)))
(defn commands-loaded? [db chat-id]
(get-in db [:chats chat-id :commands-loaded]))
(def timeout 50)
(register-handler :received-message-when-commands-loaded
(u/side-effect!
(fn [db [_ chat-id message]]
(if (commands-loaded? db chat-id)
(dispatch [:received-message message])
(s/execute-later
#(dispatch [:received-message-when-commands-loaded chat-id message])
timeout)))))

View File

@ -105,16 +105,27 @@
:to "me"}])))
;; -- Saving password ----------------------------------------
(defn passpharse-messages [mnemonic]
(defn account-generation-message []
(dispatch [:received-message
{:message-id (random/id)
:content (label :t/here-is-your-passphrase)
:content (label :t/account-generation-message)
:content-type text-content-type
:outgoing false
:chat-id console-chat-id
:from console-chat-id
:to "me"
:new? false}])
:to "me"}]))
(defn passpharse-messages [mnemonic messages-count]
(dispatch [:received-message
{:message-id (random/id)
:content (if (> messages-count 3)
(label :t/phew-here-is-your-passphrase)
(label :t/here-is-your-passphrase))
:content-type text-content-type
:outgoing false
:chat-id console-chat-id
:from console-chat-id
:to "me"}])
(dispatch [:received-message
{:message-id (random/id)
:content mnemonic
@ -122,8 +133,7 @@
:outgoing false
:chat-id console-chat-id
:from console-chat-id
:to "me"
:new? false}])
:to "me"}])
;; TODO highlight '!phone'
(start-signup))
@ -138,7 +148,8 @@
(defn intro []
(dispatch [:received-message intro-status])
(dispatch [:received-message
(dispatch [:received-message-when-commands-loaded
console-chat-id
{:chat-id console-chat-id
:message-id "intro-message1"
:content (command-content

View File

@ -266,4 +266,4 @@
(defn new-message-container [margin on-top?]
{:background-color color-white
:margin-bottom margin
:elevation (if on-top? 6 5)})
:elevation (if on-top? 6 5)})

View File

@ -53,7 +53,7 @@
:loop? loop?}]
(r/create-class
{:component-did-mount
(if @answered? #(request-button-animation-logic context) (fn []))
(if @answered? (fn []) #(request-button-animation-logic context))
:component-will-unmount
#(reset! loop? false)
:reagent-render

View File

@ -80,10 +80,12 @@
message))))))
(defn get-last-message
[chat-id]
(let [{:keys [content-type] :as message} (data-store/get-last-message chat-id)]
(when (and message (command-type? content-type))
(clojure.core/update message :content str-to-map))))
[{:keys [chats] :as db} chat-id]
(if-let [message (first (get-in db [:chats chat-id :messages]))]
message
(let [{:keys [content-type] :as message} (data-store/get-last-message chat-id)]
(when (and message (command-type? content-type))
(clojure.core/update message :content str-to-map)))))
(defn get-unviewed
[]

View File

@ -42,15 +42,16 @@
(assoc-in db [:animations k] v)))
(register-handler :initialize-db
(fn [_ _]
(fn [{:keys [status-module-initialized?]} _]
(data-store/init)
(assoc app-db :current-account-id nil)))
(let [db' (assoc app-db :current-account-id nil)]
(if status-module-initialized?
(assoc db' :status-module-initialized? true)
db'))))
(register-handler :initialize-account-db
(fn [db _]
(assoc db
:chats {}
:current-chat-id console-chat-id)))
(assoc db :current-chat-id console-chat-id)))
(register-handler :initialize-account
(u/side-effect!

View File

@ -85,11 +85,13 @@
:incorrect-code (str "Sorry the code was incorrect, please enter again")
:generate-passphrase (str "I'll generate a passphrase for you so you can restore your "
"access or log in from another device")
:phew-here-is-your-passphrase "*Phew* that was hard, here is your passphrase, *write this down and keep this safe!* You will need it to recover your account."
:here-is-your-passphrase "Here is your passphrase, *write this down and keep this safe!* You will need it to recover your account."
:written-down "Make sure you had securely written it down"
:phone-number-required "Tap here to enter your phone number & I'll find your friends"
:intro-status "Chat with me to setup your account and change your settings!"
:intro-message1 "Welcome to Status\nTap this message to set your password & get started!"
:account-generation-message "Gimmie a sec, I gotta do some crazy math to generate your account!"
;chats
:chats "Chats"