From 90ace7399536c27742b582987302f82ed7198f78 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Thu, 6 Oct 2016 16:15:57 +0300 Subject: [PATCH] Console chat: password request (fixes #286) Former-commit-id: 9624bd6d0f8c0625346e6ba423274fe764449edf --- resources/console.js | 62 +++++--- resources/status.js | 2 +- src/status_im/chat/handlers/commands.cljs | 138 ++++++++++++------ src/status_im/chat/handlers/send_message.cljs | 71 ++++----- src/status_im/chat/styles/response.cljs | 25 ++-- src/status_im/chat/subs.cljs | 31 ++-- src/status_im/chat/views/command.cljs | 4 - src/status_im/chat/views/message.cljs | 16 +- src/status_im/chat/views/message_input.cljs | 7 +- src/status_im/chat/views/response.cljs | 34 +++-- src/status_im/chat/views/staged_command.cljs | 9 +- src/status_im/commands/handlers/jail.cljs | 5 +- src/status_im/commands/handlers/loading.cljs | 5 +- src/status_im/components/status.cljs | 6 +- src/status_im/models/commands.cljs | 26 ++-- src/status_im/translations/en.cljs | 1 + src/status_im/utils/datetime.cljs | 20 ++- 17 files changed, 293 insertions(+), 169 deletions(-) diff --git a/resources/console.js b/resources/console.js index 7662184748..0d914f50f7 100644 --- a/resources/console.js +++ b/resources/console.js @@ -59,11 +59,11 @@ function startsWith(str1, str2) { function phoneSuggestions(params) { var ph, suggestions; - if (!params.value || params.value == "") { + if (!params.phone || params.phone == "") { ph = phones; } else { ph = phones.filter(function (phone) { - return startsWith(phone.number, params.value); + return startsWith(phone.number, params.phone); }); } @@ -104,7 +104,7 @@ var phoneConfig = { validator: function (params) { return { validationHandler: "phone", - parameters: [params.value] + parameters: [params.phone] }; }, params: [{ @@ -116,7 +116,7 @@ var phoneConfig = { handler: function (params) { return { event: "sign-up", - params: [params.value] + params: [params.phone, params] }; } }; @@ -155,11 +155,11 @@ status.response({ handler: function (params) { return { event: "confirm-sign-up", - params: [params.value] + params: [params.code] }; }, - validator: function(params){ - if(!/^[\d]{4}$/.test(params.value)){ + validator: function (params) { + if (!/^[\d]{4}$/.test(params.code)) { var error = status.components.validationMessage( "Confirmation code", "Wrong format" @@ -173,32 +173,56 @@ status.response({ status.response({ name: "password", color: "#7099e6", - description: "Password Request", - icon: "icon_lock_white", + description: "Password", + icon: "lock_white", params: [{ name: "password", type: status.types.PASSWORD, placeholder: "Type your password" + }, { + name: "password-confirmation", + type: status.types.PASSWORD, + placeholder: "Please re-enter password to confirm" }], handler: function (params) { return { event: "save-password", - params: [params.value] + params: [params.password] }; }, - validator: function (params) { - if(!params.value || params.value.length < 6){ - return { - errors: [ + validator: function (params, context) { + var errorMessages = []; + var currentParameter = context["current-parameter"]; + + if ( + currentParameter == "password" && + params.password.length < 6 + ) { + errorMessages.push("Password should be not less then 6 symbols."); + } + + if (currentParameter == "password-confirmation" && + params.password != params["password-confirmation"]) { + errorMessages.push("Password confirmation doesn't match password."); + } + + if (errorMessages.length) { + var errors = []; + for (var idx in errorMessages) { + errors.push( status.components.validationMessage( "Password", - "Password should be not less then 6 symbols." + errorMessages[idx] ) - ] - }; + ); + } + + return {errors: errors}; } + + return {params: params, context: context}; }, - preview: function (params) { + preview: function (params, context) { var style = { marginTop: 5, marginHorizontal: 0, @@ -206,7 +230,7 @@ status.response({ color: "black" }; - if(params.platform == "ios"){ + if (context.platform == "ios") { style.fontSize = 8; style.marginTop = 10; style.marginBottom = 2; diff --git a/resources/status.js b/resources/status.js index 4303bf1de1..335c417580 100644 --- a/resources/status.js +++ b/resources/status.js @@ -61,7 +61,7 @@ function call(pathStr, paramsStr) { return null; } - res = fn(params); + res = fn(params.parameters, params.context); return JSON.stringify(res); } diff --git a/src/status_im/chat/handlers/commands.cljs b/src/status_im/chat/handlers/commands.cljs index b7c4d14480..a37d7c778a 100644 --- a/src/status_im/chat/handlers/commands.cljs +++ b/src/status_im/chat/handlers/commands.cljs @@ -22,14 +22,14 @@ (defn invoke-suggestions-handler! [{:keys [current-chat-id canceled-command] :as db} _] (when-not canceled-command - (let [{:keys [command content]} (get-in db [:chats current-chat-id :command-input]) + (let [{:keys [command content params]} (get-in db [:chats current-chat-id :command-input]) {:keys [name type]} command - path [(if (= :command type) :commands :responses) - name - :params - 0 - :suggestions] - params {:value (content-by-command command content)}] + path [(if (= :command type) :commands :responses) + name + :params + 0 + :suggestions] + params {:parameters (or params {})}] (status/call-jail current-chat-id path params @@ -55,21 +55,25 @@ (after #(dispatch [:clear-validation-errors]))] (fn [{:keys [current-chat-id] :as db} [_ content]] (let [starts-as-command? (str/starts-with? content command-prefix) - command? (= :command (current-command db :type))] + command? (= :command (current-command db :type)) + {:keys [parameter-idx command]} (commands/get-command-input db) + parameter-name (-> command :params (get parameter-idx) :name)] (as-> db db (commands/set-chat-command-content db content) + (commands/set-command-parameter db parameter-name content) (assoc-in db [:chats current-chat-id :input-text] nil) (assoc db :canceled-command (and command? (not starts-as-command?))))))) (defn invoke-command-preview! - [{:keys [staged-command]} [_ chat-id]] - (let [{:keys [command content id]} staged-command + [{:keys [staged-command] :as db} [_ chat-id]] + (let [{:keys [command id]} staged-command {:keys [name type]} command - path [(if (= :command type) :commands :responses) - name - :preview] - params {:value content - :platform platform/platform}] + parameters (:params (commands/get-command-input db)) + path [(if (= :command type) :commands :responses) + name + :preview] + params {:parameters parameters + :context {:platform platform/platform}}] (status/call-jail chat-id path params @@ -84,7 +88,7 @@ (register-handler ::validate! (u/side-effect! - (fn [_ [_ {:keys [chat-id]} {:keys [error result]}]] + (fn [_ [_ {:keys [chat-id handler]} {:keys [error result]}]] ;; todo handle error (when-not error (let [{:keys [errors validationHandler parameters]} result] @@ -97,42 +101,28 @@ validationHandler parameters]) - :else (dispatch [::finish-command-staging chat-id]))))))) - -(defn start-validate! [db] - (let [{:keys [content command chat-id] :as data} (::command db) - {:keys [name type]} command - path [(if (= :command type) :commands :responses) - name - :validator] - params {:value content - :command data}] - (status/call-jail chat-id - path - params - #(dispatch [::validate! data %])))) + :else (if handler + (handler) + (dispatch [::finish-command-staging chat-id])))))))) (register-handler :stage-command - (after start-validate!) (fn [{:keys [current-chat-id current-account-id] :as db}] - (let [{:keys [command content]} (command-input db) - content' (content-by-command command content)] - (-> db - (assoc ::command {:content content' - :command command - :chat-id current-chat-id - :address current-account-id}) - (assoc-in [:disable-staging current-chat-id] true))))) + (let [command-input (commands/get-command-input db) + command (commands/get-chat-command db)] + (dispatch [::start-command-validation! {:command-input command-input + :command command + :chat-id current-chat-id + :address current-account-id}]) + (assoc-in db [:disable-staging current-chat-id] true)))) (register-handler ::finish-command-staging [(after #(dispatch [:start-cancel-command])) (after invoke-command-preview!)] (fn [db [_ chat-id]] - (let [db (assoc-in db [:chats chat-id :input-text] nil) - {:keys [command content to-message-id]} (command-input db) - content' (content-by-command command content) + (let [db (assoc-in db [:chats chat-id :input-text] nil) + {:keys [command content to-message-id params]} (command-input db) command-info {:command command - :content content' + :params params :to-message to-message-id :created-at (time/now-ms) :id (random/id)}] @@ -210,3 +200,65 @@ (register-handler :invoke-commands-suggestions! (u/side-effect! invoke-suggestions-handler!)) + +(register-handler :send-command! + (u/side-effect! + (fn [{:keys [current-chat-id current-account-id] :as db}] + (let [{:keys [params] :as command} (commands/get-chat-command db) + {:keys [parameter-idx]} (commands/get-command-input db) + + last-parameter? (= (inc parameter-idx) (count params)) + + parameters {:command command :input command-input} + + {:keys [command content]} (command-input db) + content' (content-by-command command content)] + (dispatch [:set-command-parameter + {:value content' + :parameter (params parameter-idx)}]) + (if last-parameter? + (dispatch [:check-suggestions-trigger! parameters]) + (dispatch [::start-command-validation! + {:chat-id current-chat-id + :address current-account-id + :handler #(dispatch [:next-command-parameter])}])))))) + +(register-handler ::start-command-validation! + (u/side-effect! + (fn [db [_ {:keys [command-input chat-id] :as data}]] + (let [command-input' (or command-input (commands/get-command-input db)) + {:keys [parameter-idx params command]} command-input' + {:keys [name type]} command + current-parameter (-> command + :params + (get parameter-idx) + :name) + context {:current-parameter current-parameter} + path [(if (= :command type) :commands :responses) + name + :validator] + parameters {:context context + :parameters params}] + (status/call-jail chat-id + path + parameters + #(dispatch [::validate! data %])))))) + +(register-handler :set-command-parameter + (fn [db [_ {:keys [value parameter]}]] + (let [name (:name parameter)] + (commands/set-command-parameter db name value)))) + +(register-handler :next-command-parameter + (fn [{:keys [current-chat-id] :as db}] + (-> db + (update-in [:chats current-chat-id :command-input :parameter-idx] inc) + (commands/set-chat-command-content nil)))) + +(register-handler :check-suggestions-trigger! + (u/side-effect! + (fn [_ [_ {:keys [command]}]] + (let [suggestions-trigger (keyword (:suggestions-trigger command))] + (if (= :on-send suggestions-trigger) + (dispatch [:invoke-commands-suggestions!]) + (dispatch [:stage-command])))))) diff --git a/src/status_im/chat/handlers/send_message.cljs b/src/status_im/chat/handlers/send_message.cljs index 18afe87c30..a1bf1b4d22 100644 --- a/src/status_im/chat/handlers/send_message.cljs +++ b/src/status_im/chat/handlers/send_message.cljs @@ -16,9 +16,9 @@ [taoensso.timbre :refer-macros [debug]])) (defn prepare-command - [identity chat-id {:keys [preview preview-string content command to-message]}] + [identity chat-id {:keys [preview preview-string params command to-message]}] (let [content {:command (command :name) - :content content}] + :params params}] {:message-id (random/id) :from identity :to chat-id @@ -36,12 +36,12 @@ (u/side-effect! (fn [{:keys [current-chat-id current-public-key current-account-id] :as db}] (let [staged-commands (vals (get-in db [:chats current-chat-id :staged-commands])) - 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}] + 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}] (dispatch [:clear-input current-chat-id]) (cond (seq staged-commands) @@ -54,7 +54,7 @@ (u/side-effect! (fn [_ [_ {:keys [commands message] :as params}]] (doseq [{:keys [command] :as message} commands] - (let [params' (assoc params :command message)] + (let [params' (assoc params :staged-command message)] (if (:sending message) (dispatch [:navigate-to :confirm]) (if (:has-handler command) @@ -70,11 +70,12 @@ (register-handler :prepare-command! (u/side-effect! - (fn [{:keys [current-public-key] :as db} [_ {:keys [chat-id command] :as params}]] - (let [command' (->> command + (fn [{:keys [current-public-key] :as db} + [_ {:keys [chat-id staged-command] :as params}]] + (let [command' (->> staged-command (prepare-command current-public-key chat-id) (cu/check-author-direction db chat-id))] - (dispatch [::clear-command chat-id (:id command)]) + (dispatch [::clear-command chat-id (:id staged-command)]) (dispatch [::send-command! (assoc params :command command')]))))) (register-handler ::clear-command @@ -97,7 +98,7 @@ (register-handler ::save-command! (u/side-effect! - (fn [{:keys [current-public-key]} [_ {:keys [command chat-id]}]] + (fn [_ [_ {:keys [command chat-id]}]] (messages/save chat-id (dissoc command :rendered-preview :to-message :has-handler))))) @@ -111,16 +112,16 @@ (register-handler ::invoke-command-handlers! (u/side-effect! - (fn [db [_ {:keys [chat-id address] :as parameters}]] - (let [{:keys [command content]} (:command parameters) + (fn [db [_ {:keys [chat-id address staged-command] :as parameters}]] + (let [{:keys [command params]} staged-command {:keys [type name]} command - path [(if (= :command type) :commands :responses) - name - :handler] - to (get-in db [:contacts chat-id :address]) - params {:value content - :command {:from address - :to to}}] + path [(if (= :command type) :commands :responses) + name + :handler] + to (get-in db [:contacts chat-id :address]) + params {:parameters params + :context {:from address + :to to}}] (status/call-jail chat-id path params @@ -130,19 +131,19 @@ (u/side-effect! (fn [db [_ {:keys [chat-id identity message] :as params}]] (let [{:keys [group-chat]} (get-in db [:chats chat-id]) - 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)}) + 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)}) message'' (if group-chat (assoc message' :group-id chat-id :message-type :group-user-message) (assoc message' :to chat-id :message-type :user-message)) - params' (assoc params :message message'')] + params' (assoc params :message message'')] (dispatch [::add-message params']) (dispatch [::save-message! params']))))) @@ -164,9 +165,9 @@ chat-id :chat-id}]] (when (and message (cu/not-console? chat-id)) (let [message' (select-keys message [:from :message-id]) - payload (select-keys message [:timestamp :content :content-type]) - options {:web3 web3 - :message (assoc message' :payload payload)}] + payload (select-keys message [:timestamp :content :content-type]) + options {:web3 web3 + :message (assoc message' :payload payload)}] (if (= message-type :group-user-message) (let [{:keys [public-key private-key]} (chats chat-id)] (protocol/send-group-message! (assoc options diff --git a/src/status_im/chat/styles/response.cljs b/src/status_im/chat/styles/response.cljs index fbc29b247b..e85d845f8e 100644 --- a/src/status_im/chat/styles/response.cljs +++ b/src/status_im/chat/styles/response.cljs @@ -15,8 +15,8 @@ :justifyContent :center}) (def drag-icon - {:width 14 - :height 3}) + {:width 14 + :height 3}) (def command-icon-container {:marginTop 1 @@ -28,24 +28,25 @@ :borderRadius 16 :backgroundColor color-white}) -(def command-icon - {:width 9 - :height 15}) +(defn command-icon [color] + {:width 13 + :height 13 + :tint-color color}) (def info-container {:flex 1 :marginLeft 12}) (def command-name - {:marginTop 0 - :fontSize 12 - :color color-white}) + {:marginTop 0 + :fontSize 12 + :color color-white}) (def message-info - {:marginTop 1 - :fontSize 12 - :opacity 0.69 - :color color-white}) + {:marginTop 1 + :fontSize 12 + :opacity 0.69 + :color color-white}) (defn response-view [keyboard-height height] {:flexDirection :column diff --git a/src/status_im/chat/subs.cljs b/src/status_im/chat/subs.cljs index 418e8e243a..575dead253 100644 --- a/src/status_im/chat/subs.cljs +++ b/src/status_im/chat/subs.cljs @@ -80,7 +80,7 @@ (register-sub :valid-plain-message? (fn [_ _] - (let [input-message (subscribe [:get-chat-input-text]) + (let [input-message (subscribe [:get-chat-input-text]) staged-commands (subscribe [:get-chat-staged-commands])] (reaction (plain-message/message-valid? @staged-commands @input-message))))) @@ -100,7 +100,7 @@ chat-id (subscribe [:get-current-chat-id])] (reaction (let [path [:chats @chat-id :command-input :parameter-idx] - n (get-in @db path)] + n (get-in @db path)] (when n (nth (:params @command) n))))))) (register-sub :get-chat-command-content @@ -141,10 +141,10 @@ (register-sub :messages-offset (fn [] - (let [command? (subscribe [:command?]) - type (subscribe [:command-type]) + (let [command? (subscribe [:command?]) + type (subscribe [:command-type]) command-suggestions (subscribe [:get-content-suggestions]) - suggestions (subscribe [:get-suggestions])] + suggestions (subscribe [:get-suggestions])] (reaction (cond (and @command? (= @type :response)) c/request-info-height @@ -160,7 +160,7 @@ (register-sub :command-icon-width (fn [] (let [width (subscribe [:get :command-icon-width]) - type (subscribe [:command-type])] + type (subscribe [:command-type])] (reaction (if (= :command @type) @width 0))))) @@ -170,6 +170,19 @@ (let [chat-id (subscribe [:get-current-chat-id])] (reaction (get-in @db [:chats @chat-id :requests]))))) +(register-sub :get-requests-map + (fn [db] + (let [chat-id (subscribe [:get-current-chat-id])] + (reaction (->> (get-in @db [:chats @chat-id :requests]) + (map #(vector (:message-id %) %)) + (into {})))))) + +(register-sub :get-current-request + (fn [] + (let [requests (subscribe [:get-requests-map]) + message-id (subscribe [:get-chat-command-to-message-id])] + (reaction (@requests @message-id))))) + (register-sub :get-response (fn [db [_ n]] (let [chat-id (subscribe [:get-current-chat-id])] @@ -224,9 +237,9 @@ (register-sub :input-margin (fn [] (let [kb-height (subscribe [:get :keyboard-height]) - command (subscribe [:get-chat-command]) - focused (subscribe [:get :focused]) - mode (subscribe [:kb-mode])] + command (subscribe [:get-chat-command]) + focused (subscribe [:get :focused]) + mode (subscribe [:kb-mode])] (reaction (cond (or ios? (and (not @focused) diff --git a/src/status_im/chat/views/command.cljs b/src/status_im/chat/views/command.cljs index ec59d296c8..af813a6d9d 100644 --- a/src/status_im/chat/views/command.cljs +++ b/src/status_im/chat/views/command.cljs @@ -22,10 +22,6 @@ (validator message) (pos? (count message)))) -(defn try-send [message validator] - (when (valid? message validator) - (send-command))) - (defview command-icon [command] [icon-width [:get :command-icon-width]] [view st/command-container diff --git a/src/status_im/chat/views/message.cljs b/src/status_im/chat/views/message.cljs index d35322ceb2..02e6260fb6 100644 --- a/src/status_im/chat/views/message.cljs +++ b/src/status_im/chat/views/message.cljs @@ -7,6 +7,7 @@ [status-im.components.react :refer [view text image + icon animated-view touchable-highlight]] [status-im.components.animation :as anim] @@ -17,7 +18,6 @@ [status-im.models.commands :refer [parse-command-message-content parse-command-request]] [status-im.resources :as res] - [status-im.utils.datetime :as time] [status-im.constants :refer [text-content-type content-type-status content-type-command @@ -69,23 +69,25 @@ (defview message-content-command [content preview] [commands [:get-commands-and-responses]] - (let [{:keys [command content]} (parse-command-message-content commands content) - {:keys [name icon type]} command] + (let [{:keys [command params]} (parse-command-message-content commands content) + {:keys [name type] + icon-path :icon} command] [view st/content-command-view [view st/command-container [view (pill-st/pill command) [text {:style pill-st/pill-text :font :default} (str (if (= :command type) "!" "?") name)]]] - (when icon + (when icon-path [view st/command-image-view - [image {:source {:uri icon} - :style st/command-image}]]) + [icon icon-path st/command-image]]) (if preview preview [text {:style st/command-text :font :default} - (str content)])])) + (if (= 1 (count params)) + (first (vals params)) + (str params))])])) (defn set-chat-command [message-id command] (dispatch [:set-response-chat-command message-id (keyword (:name command))])) diff --git a/src/status_im/chat/views/message_input.cljs b/src/status_im/chat/views/message_input.cljs index 299fd6e516..601573827f 100644 --- a/src/status_im/chat/views/message_input.cljs +++ b/src/status_im/chat/views/message_input.cljs @@ -30,9 +30,7 @@ (defn on-press-commands-handler [{:keys [suggestions-trigger]}] - (if (= :on-send (keyword suggestions-trigger)) - #(dispatch [:invoke-commands-suggestions!]) - command/send-command)) + #(dispatch [:send-command!])) (defn command-input-options [command icon-width disable?] {:style (st-response/command-input icon-width disable?) @@ -102,7 +100,8 @@ [message-input input-options set-layout-size])] ;; TODO emoticons: not implemented [plain-message/smile-button height] - (when (or @command? @valid-plain-message?) + (when (or (and @command? (not (str/blank? @input-command))) + @valid-plain-message?) (let [on-press (if @command? (on-press-commands-handler @command) plain-message/send)] diff --git a/src/status_im/chat/views/response.cljs b/src/status_im/chat/views/response.cljs index 537d006cd5..a33c16e8fc 100644 --- a/src/status_im/chat/views/response.cljs +++ b/src/status_im/chat/views/response.cljs @@ -7,6 +7,7 @@ [status-im.components.react :refer [view animated-view icon + image text text-input touchable-highlight @@ -20,38 +21,49 @@ [status-im.chat.constants :as c] [status-im.chat.views.command-validation :as cv] [status-im.utils.platform :refer [ios?]] - [status-im.components.webview-bridge :refer [webview-bridge]])) + [status-im.components.webview-bridge :refer [webview-bridge]] + [status-im.i18n :refer [label]] + [status-im.utils.datetime :as dt])) (defn drag-icon [] [view st/drag-container [icon :drag_white st/drag-icon]]) -(defn command-icon [] +(defn command-icon [{icon-path :icon + color :color}] [view st/command-icon-container - ;; TODO stub data: command icon - [icon :dollar_green st/command-icon]]) + (when icon-path + [icon icon-path (st/command-icon color)])]) -(defn info-container [command] +(defview info-container + [command] + [{:keys [name chat-id]} [:get-current-chat] + {:keys [added]} [:get-current-request]] [view st/info-container [text {:style st/command-name} - (:description command)] - [text {:style st/message-info} - ;; TODO stub data: request message info - "By ???, MMM 1st at HH:mm"]]) + (str (:description command) " " (label :t/request))] + (when added + [text {:style st/message-info} + (str "By " (or name chat-id) ", " + (dt/format-date "MMM" added) + " " + (dt/get-ordinal-date added) + " at " + (dt/format-date "HH:mm" added))])]) (defn request-info [response-height] (let [layout-height (subscribe [:get :layout-height]) pan-responder (resp/pan-responder response-height layout-height :fix-response-height) - command (subscribe [:get-chat-command])] + command (subscribe [:get-chat-command])] (fn [response-height] (if (= :response (:type @command)) [view (merge (drag/pan-handlers pan-responder) {:style (st/request-info (:color @command))}) [drag-icon] [view st/inner-container - [command-icon nil] + [command-icon @command] [info-container @command] [touchable-highlight {:on-press #(dispatch [:start-cancel-command])} [view st/cancel-container diff --git a/src/status_im/chat/views/staged_command.cljs b/src/status_im/chat/views/staged_command.cljs index a32476926d..c32c699116 100644 --- a/src/status_im/chat/views/staged_command.cljs +++ b/src/status_im/chat/views/staged_command.cljs @@ -12,8 +12,9 @@ (defn cancel-command-input [staged-command] (dispatch [:unstage-command staged-command])) -(defn simple-command-staged-view [staged-command] - (let [{:keys [type name] :as command} (:command staged-command)] +(defn simple-command-staged-view + [{:keys [command params] :as staged-command}] + (let [{:keys [type name]} command] [view st/staged-command-container [view st/staged-command-background [view {:flex-direction :row} @@ -30,4 +31,6 @@ (if-let [preview (:preview staged-command)] preview [text {:style st/staged-command-content} - (:content staged-command)])]])) + (if (= 1 (count params)) + (first (vals params)) + (str params))])]])) diff --git a/src/status_im/commands/handlers/jail.cljs b/src/status_im/commands/handlers/jail.cljs index 2f82f3dda6..ea0fc85e43 100644 --- a/src/status_im/commands/handlers/jail.cljs +++ b/src/status_im/commands/handlers/jail.cljs @@ -7,7 +7,8 @@ [status-im.commands.utils :refer [generate-hiccup reg-handler]] [clojure.string :as s] [status-im.components.react :as r] - [status-im.constants :refer [console-chat-id]])) + [status-im.constants :refer [console-chat-id]] + [taoensso.timbre :as log])) (defn init-render-command! [_ [chat-id command message-id data]] @@ -78,7 +79,7 @@ (fn [_ params] (when (:error (last params)) (show-popup "Error" (s/join "\n" [message params])) - (println message params)))) + (log/debug message params)))) (reg-handler :init-render-command! init-render-command!) (reg-handler ::render-command render-command) diff --git a/src/status_im/commands/handlers/loading.cljs b/src/status_im/commands/handlers/loading.cljs index 84889ec1bf..d35a066ea9 100644 --- a/src/status_im/commands/handlers/loading.cljs +++ b/src/status_im/commands/handlers/loading.cljs @@ -8,7 +8,8 @@ [status-im.components.status :as status] [status-im.utils.types :refer [json->clj]] [status-im.commands.utils :refer [reg-handler]] - [status-im.constants :refer [console-chat-id wallet-chat-id]])) + [status-im.constants :refer [console-chat-id wallet-chat-id]] + [taoensso.timbre :as log])) (def commands-js "commands.js") @@ -95,7 +96,7 @@ (name reason) details])] (show-popup "Error" m) - (println m)))) + (log/debug m)))) (reg-handler :load-commands! (u/side-effect! load-commands!)) (reg-handler ::fetch-commands! (u/side-effect! fetch-commands!)) diff --git a/src/status_im/components/status.cljs b/src/status_im/components/status.cljs index d0a5e5418f..672af4d944 100644 --- a/src/status_im/components/status.cljs +++ b/src/status_im/components/status.cljs @@ -47,10 +47,12 @@ (defn call-jail [chat-id path params callback] (when status - (println :call chat-id (cljs->json path) (cljs->json params)) + (log/debug :chat-id chat-id) + (log/debug :path path) + (log/debug :params params) (let [cb (fn [r] (let [r' (t/json->clj r)] - (println r') + (log/debug r') (callback r')))] (.callJail status chat-id (cljs->json path) (cljs->json params) cb)))) diff --git a/src/status_im/models/commands.cljs b/src/status_im/models/commands.cljs index 7ce9d94370..926365ea7c 100644 --- a/src/status_im/models/commands.cljs +++ b/src/status_im/models/commands.cljs @@ -9,9 +9,6 @@ [type {:keys [current-chat-id] :as db} command-key] ((or (get-in db [:chats current-chat-id type]) {}) command-key)) -(defn find-command [commands command-key] - (first (filter #(= command-key (:command %)) commands))) - (defn get-chat-command-content [{:keys [current-chat-id] :as db}] (get-in db (db/chat-command-content-path current-chat-id))) @@ -20,18 +17,26 @@ [{:keys [current-chat-id] :as db} content] (assoc-in db [:chats current-chat-id :command-input :content] content)) +(defn set-command-parameter + [{:keys [current-chat-id] :as db} name value] + (assoc-in db [:chats current-chat-id :command-input :params name] value)) + (defn get-chat-command [{:keys [current-chat-id] :as db}] (get-in db (db/chat-command-path current-chat-id))) +(defn get-command-input [{:keys [current-chat-id] :as db}] + (get-in db [:chats current-chat-id :command-input])) + (defn set-command-input ([db type command-key] - (set-command-input db type nil command-key)) + (set-command-input db type nil command-key)) ([{:keys [current-chat-id] :as db} type message-id command-key] (update-in db [:chats current-chat-id :command-input] merge {:content nil :command (get-response-or-command type db command-key) :parameter-idx 0 + :params nil :to-message-id message-id}))) (defn get-chat-command-to-message-id @@ -44,11 +49,11 @@ (defn stage-command [{:keys [current-chat-id] :as db} {:keys [id] :as command-info}] - (let [path (db/chat-staged-commands-path current-chat-id) - staged-commands (get-in db path) + (let [path (db/chat-staged-commands-path current-chat-id) + staged-commands (get-in db path) staged-coomands' (if (seq staged-commands) - staged-commands - (priority-map-by compare-commands))] + staged-commands + (priority-map-by compare-commands))] (assoc-in db path (assoc staged-coomands' id command-info)))) (defn unstage-command [db {:keys [id]}] @@ -60,11 +65,6 @@ (get-in db (db/chat-command-request-path current-chat-id (get-chat-command-to-message-id db)))) -(defn set-chat-command-request - [{:keys [current-chat-id] :as db} message-id handler] - (update-in db (db/chat-command-requests-path current-chat-id) - #(assoc % message-id handler))) - (defn parse-command-message-content [commands content] (update content :command #((keyword %) commands))) diff --git a/src/status_im/translations/en.cljs b/src/status_im/translations/en.cljs index a66446758f..9db362b5ed 100644 --- a/src/status_im/translations/en.cljs +++ b/src/status_im/translations/en.cljs @@ -133,6 +133,7 @@ :request-command-description "Send request" :keypair-password-command-description "" :help-command-description "Help" + :request "Request" ;new-group :group-chat-name "Chat name" diff --git a/src/status_im/utils/datetime.cljs b/src/status_im/utils/datetime.cljs index aafe186284..3f15a4a0f4 100644 --- a/src/status_im/utils/datetime.cljs +++ b/src/status_im/utils/datetime.cljs @@ -1,6 +1,6 @@ (ns status-im.utils.datetime (:require [cljs-time.core :as t :refer [date-time now plus days hours before?]] - [cljs-time.coerce :refer [from-long to-long]] + [cljs-time.coerce :refer [from-long to-long from-date]] [cljs-time.format :refer [formatters formatter unparse]] @@ -20,7 +20,7 @@ (defn to-short-str ([ms] - (to-short-str ms #(unparse (formatters :hour-minute) %))) + (to-short-str ms #(unparse (formatters :hour-minute) %))) ([ms today-format-fn] (let [date (from-long ms) local (plus date time-zone-offset) @@ -59,3 +59,19 @@ (defn now-ms [] (to-long (now))) + +(defn format-date [format date] + (let [local (plus (from-date date) time-zone-offset)] + (unparse (formatter format) local))) + +(defn get-ordinal-date [date] + (let [local (plus (from-date date) time-zone-offset) + day (js/parseInt (unparse (formatter "d") local)) + s {0 "th" + 1 "st" + 2 "nd" + 3 "rd"} + m (mod day 100)] + (str day (or (s (mod (- m 20) 10)) + (s m) + (s 0)))))