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.utils.handlers :as u :refer [get-hashtags]]
[status-im.accounts.statuses :as statuses] [status-im.accounts.statuses :as statuses]
[status-im.utils.gfycat.core :refer [generate-gfy]] [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]] (defn save-account [_ [_ account]]
@ -44,14 +45,14 @@
:photo-path (identicon public-key)}] :photo-path (identicon public-key)}]
(log/debug "account-created") (log/debug "account-created")
(when-not (str/blank? public-key) (when-not (str/blank? public-key)
(do
(dispatch [:add-account account])
(dispatch [:show-mnemonic mnemonic]) (dispatch [:show-mnemonic mnemonic])
(dispatch [:login-account address password]))))) (dispatch [:add-account account])
(dispatch [:login-account address password true]))))
(register-handler :create-account (register-handler :create-account
(u/side-effect! (u/side-effect!
(fn [_ [_ password]] (fn [_ [_ password]]
(s/execute-later #(dispatch [:account-generation-message]) 400)
(status/create-account (status/create-account
password password
#(account-created % password))))) #(account-created % password)))))

View File

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

View File

@ -9,7 +9,8 @@
content-type-command-request]] content-type-command-request]]
[cljs.reader :refer [read-string]] [cljs.reader :refer [read-string]]
[status-im.data-store.chats :as chats] [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}] (defn check-preview [{:keys [content] :as message}]
(if-let [preview (:preview content)] (if-let [preview (:preview content)]
@ -44,7 +45,7 @@
(not= from current-identity) (not= from current-identity)
(or (not exists?) active?)) (or (not exists?) active?))
(let [group-chat? (not (nil? group-id)) (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 message' (assoc (->> message
(cu/check-author-direction previous-message) (cu/check-author-direction previous-message)
(check-preview)) (check-preview))
@ -85,3 +86,17 @@
(register-handler ::add-message (register-handler ::add-message
(fn [db [_ add-to-chat-id {:keys [chat-id new?] :as 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?))) (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"}]))) :to "me"}])))
;; -- Saving password ---------------------------------------- ;; -- Saving password ----------------------------------------
(defn passpharse-messages [mnemonic] (defn account-generation-message []
(dispatch [:received-message (dispatch [:received-message
{:message-id (random/id) {:message-id (random/id)
:content (label :t/here-is-your-passphrase) :content (label :t/account-generation-message)
:content-type text-content-type :content-type text-content-type
:outgoing false :outgoing false
:chat-id console-chat-id :chat-id console-chat-id
:from console-chat-id :from console-chat-id
:to "me" :to "me"}]))
:new? false}])
(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 (dispatch [:received-message
{:message-id (random/id) {:message-id (random/id)
:content mnemonic :content mnemonic
@ -122,8 +133,7 @@
:outgoing false :outgoing false
:chat-id console-chat-id :chat-id console-chat-id
:from console-chat-id :from console-chat-id
:to "me" :to "me"}])
:new? false}])
;; TODO highlight '!phone' ;; TODO highlight '!phone'
(start-signup)) (start-signup))
@ -138,7 +148,8 @@
(defn intro [] (defn intro []
(dispatch [:received-message intro-status]) (dispatch [:received-message intro-status])
(dispatch [:received-message (dispatch [:received-message-when-commands-loaded
console-chat-id
{:chat-id console-chat-id {:chat-id console-chat-id
:message-id "intro-message1" :message-id "intro-message1"
:content (command-content :content (command-content

View File

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

View File

@ -80,10 +80,12 @@
message)))))) message))))))
(defn get-last-message (defn get-last-message
[chat-id] [{: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)] (let [{:keys [content-type] :as message} (data-store/get-last-message chat-id)]
(when (and message (command-type? content-type)) (when (and message (command-type? content-type))
(clojure.core/update message :content str-to-map)))) (clojure.core/update message :content str-to-map)))))
(defn get-unviewed (defn get-unviewed
[] []

View File

@ -42,15 +42,16 @@
(assoc-in db [:animations k] v))) (assoc-in db [:animations k] v)))
(register-handler :initialize-db (register-handler :initialize-db
(fn [_ _] (fn [{:keys [status-module-initialized?]} _]
(data-store/init) (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 (register-handler :initialize-account-db
(fn [db _] (fn [db _]
(assoc db (assoc db :current-chat-id console-chat-id)))
:chats {}
:current-chat-id console-chat-id)))
(register-handler :initialize-account (register-handler :initialize-account
(u/side-effect! (u/side-effect!

View File

@ -85,11 +85,13 @@
:incorrect-code (str "Sorry the code was incorrect, please enter again") :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 " :generate-passphrase (str "I'll generate a passphrase for you so you can restore your "
"access or log in from another device") "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." :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" :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" :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-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!" :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 "Chats" :chats "Chats"