Console chat: password request (fixes #286)

Former-commit-id: 9624bd6d0f
This commit is contained in:
Roman Volosovskyi 2016-10-06 16:15:57 +03:00
parent c371400f3d
commit 90ace73995
17 changed files with 293 additions and 169 deletions

View File

@ -59,11 +59,11 @@ function startsWith(str1, str2) {
function phoneSuggestions(params) { function phoneSuggestions(params) {
var ph, suggestions; var ph, suggestions;
if (!params.value || params.value == "") { if (!params.phone || params.phone == "") {
ph = phones; ph = phones;
} else { } else {
ph = phones.filter(function (phone) { 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) { validator: function (params) {
return { return {
validationHandler: "phone", validationHandler: "phone",
parameters: [params.value] parameters: [params.phone]
}; };
}, },
params: [{ params: [{
@ -116,7 +116,7 @@ var phoneConfig = {
handler: function (params) { handler: function (params) {
return { return {
event: "sign-up", event: "sign-up",
params: [params.value] params: [params.phone, params]
}; };
} }
}; };
@ -155,11 +155,11 @@ status.response({
handler: function (params) { handler: function (params) {
return { return {
event: "confirm-sign-up", event: "confirm-sign-up",
params: [params.value] params: [params.code]
}; };
}, },
validator: function(params){ validator: function (params) {
if(!/^[\d]{4}$/.test(params.value)){ if (!/^[\d]{4}$/.test(params.code)) {
var error = status.components.validationMessage( var error = status.components.validationMessage(
"Confirmation code", "Confirmation code",
"Wrong format" "Wrong format"
@ -173,32 +173,56 @@ status.response({
status.response({ status.response({
name: "password", name: "password",
color: "#7099e6", color: "#7099e6",
description: "Password Request", description: "Password",
icon: "icon_lock_white", icon: "lock_white",
params: [{ params: [{
name: "password", name: "password",
type: status.types.PASSWORD, type: status.types.PASSWORD,
placeholder: "Type your password" placeholder: "Type your password"
}, {
name: "password-confirmation",
type: status.types.PASSWORD,
placeholder: "Please re-enter password to confirm"
}], }],
handler: function (params) { handler: function (params) {
return { return {
event: "save-password", event: "save-password",
params: [params.value] params: [params.password]
}; };
}, },
validator: function (params) { validator: function (params, context) {
if(!params.value || params.value.length < 6){ var errorMessages = [];
return { var currentParameter = context["current-parameter"];
errors: [
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( status.components.validationMessage(
"Password", "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 = { var style = {
marginTop: 5, marginTop: 5,
marginHorizontal: 0, marginHorizontal: 0,
@ -206,7 +230,7 @@ status.response({
color: "black" color: "black"
}; };
if(params.platform == "ios"){ if (context.platform == "ios") {
style.fontSize = 8; style.fontSize = 8;
style.marginTop = 10; style.marginTop = 10;
style.marginBottom = 2; style.marginBottom = 2;

View File

@ -61,7 +61,7 @@ function call(pathStr, paramsStr) {
return null; return null;
} }
res = fn(params); res = fn(params.parameters, params.context);
return JSON.stringify(res); return JSON.stringify(res);
} }

View File

@ -22,14 +22,14 @@
(defn invoke-suggestions-handler! (defn invoke-suggestions-handler!
[{:keys [current-chat-id canceled-command] :as db} _] [{:keys [current-chat-id canceled-command] :as db} _]
(when-not canceled-command (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 {:keys [name type]} command
path [(if (= :command type) :commands :responses) path [(if (= :command type) :commands :responses)
name name
:params :params
0 0
:suggestions] :suggestions]
params {:value (content-by-command command content)}] params {:parameters (or params {})}]
(status/call-jail current-chat-id (status/call-jail current-chat-id
path path
params params
@ -55,21 +55,25 @@
(after #(dispatch [:clear-validation-errors]))] (after #(dispatch [:clear-validation-errors]))]
(fn [{:keys [current-chat-id] :as db} [_ content]] (fn [{:keys [current-chat-id] :as db} [_ content]]
(let [starts-as-command? (str/starts-with? content command-prefix) (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 (as-> db db
(commands/set-chat-command-content db content) (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-in db [:chats current-chat-id :input-text] nil)
(assoc db :canceled-command (and command? (not starts-as-command?))))))) (assoc db :canceled-command (and command? (not starts-as-command?)))))))
(defn invoke-command-preview! (defn invoke-command-preview!
[{:keys [staged-command]} [_ chat-id]] [{:keys [staged-command] :as db} [_ chat-id]]
(let [{:keys [command content id]} staged-command (let [{:keys [command id]} staged-command
{:keys [name type]} command {:keys [name type]} command
path [(if (= :command type) :commands :responses) parameters (:params (commands/get-command-input db))
name path [(if (= :command type) :commands :responses)
:preview] name
params {:value content :preview]
:platform platform/platform}] params {:parameters parameters
:context {:platform platform/platform}}]
(status/call-jail chat-id (status/call-jail chat-id
path path
params params
@ -84,7 +88,7 @@
(register-handler ::validate! (register-handler ::validate!
(u/side-effect! (u/side-effect!
(fn [_ [_ {:keys [chat-id]} {:keys [error result]}]] (fn [_ [_ {:keys [chat-id handler]} {:keys [error result]}]]
;; todo handle error ;; todo handle error
(when-not error (when-not error
(let [{:keys [errors validationHandler parameters]} result] (let [{:keys [errors validationHandler parameters]} result]
@ -97,42 +101,28 @@
validationHandler validationHandler
parameters]) parameters])
:else (dispatch [::finish-command-staging chat-id]))))))) :else (if handler
(handler)
(defn start-validate! [db] (dispatch [::finish-command-staging chat-id]))))))))
(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 %]))))
(register-handler :stage-command (register-handler :stage-command
(after start-validate!)
(fn [{:keys [current-chat-id current-account-id] :as db}] (fn [{:keys [current-chat-id current-account-id] :as db}]
(let [{:keys [command content]} (command-input db) (let [command-input (commands/get-command-input db)
content' (content-by-command command content)] command (commands/get-chat-command db)]
(-> db (dispatch [::start-command-validation! {:command-input command-input
(assoc ::command {:content content' :command command
:command command :chat-id current-chat-id
:chat-id current-chat-id :address current-account-id}])
:address current-account-id}) (assoc-in db [:disable-staging current-chat-id] true))))
(assoc-in [:disable-staging current-chat-id] true)))))
(register-handler ::finish-command-staging (register-handler ::finish-command-staging
[(after #(dispatch [:start-cancel-command])) [(after #(dispatch [:start-cancel-command]))
(after invoke-command-preview!)] (after invoke-command-preview!)]
(fn [db [_ chat-id]] (fn [db [_ chat-id]]
(let [db (assoc-in db [:chats chat-id :input-text] nil) (let [db (assoc-in db [:chats chat-id :input-text] nil)
{:keys [command content to-message-id]} (command-input db) {:keys [command content to-message-id params]} (command-input db)
content' (content-by-command command content)
command-info {:command command command-info {:command command
:content content' :params params
:to-message to-message-id :to-message to-message-id
:created-at (time/now-ms) :created-at (time/now-ms)
:id (random/id)}] :id (random/id)}]
@ -210,3 +200,65 @@
(register-handler :invoke-commands-suggestions! (register-handler :invoke-commands-suggestions!
(u/side-effect! (u/side-effect!
invoke-suggestions-handler!)) 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]))))))

View File

@ -16,9 +16,9 @@
[taoensso.timbre :refer-macros [debug]])) [taoensso.timbre :refer-macros [debug]]))
(defn prepare-command (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) (let [content {:command (command :name)
:content content}] :params params}]
{:message-id (random/id) {:message-id (random/id)
:from identity :from identity
:to chat-id :to chat-id
@ -36,12 +36,12 @@
(u/side-effect! (u/side-effect!
(fn [{:keys [current-chat-id current-public-key current-account-id] :as db}] (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])) (let [staged-commands (vals (get-in db [:chats current-chat-id :staged-commands]))
text (get-in db [:chats current-chat-id :input-text]) text (get-in db [:chats current-chat-id :input-text])
data {:commands staged-commands data {:commands staged-commands
:message text :message text
:chat-id current-chat-id :chat-id current-chat-id
:identity current-public-key :identity current-public-key
:address current-account-id}] :address current-account-id}]
(dispatch [:clear-input current-chat-id]) (dispatch [:clear-input current-chat-id])
(cond (cond
(seq staged-commands) (seq staged-commands)
@ -54,7 +54,7 @@
(u/side-effect! (u/side-effect!
(fn [_ [_ {:keys [commands message] :as params}]] (fn [_ [_ {:keys [commands message] :as params}]]
(doseq [{:keys [command] :as message} commands] (doseq [{:keys [command] :as message} commands]
(let [params' (assoc params :command message)] (let [params' (assoc params :staged-command message)]
(if (:sending message) (if (:sending message)
(dispatch [:navigate-to :confirm]) (dispatch [:navigate-to :confirm])
(if (:has-handler command) (if (:has-handler command)
@ -70,11 +70,12 @@
(register-handler :prepare-command! (register-handler :prepare-command!
(u/side-effect! (u/side-effect!
(fn [{:keys [current-public-key] :as db} [_ {:keys [chat-id command] :as params}]] (fn [{:keys [current-public-key] :as db}
(let [command' (->> command [_ {:keys [chat-id staged-command] :as params}]]
(let [command' (->> staged-command
(prepare-command current-public-key chat-id) (prepare-command current-public-key chat-id)
(cu/check-author-direction db 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')]))))) (dispatch [::send-command! (assoc params :command command')])))))
(register-handler ::clear-command (register-handler ::clear-command
@ -97,7 +98,7 @@
(register-handler ::save-command! (register-handler ::save-command!
(u/side-effect! (u/side-effect!
(fn [{:keys [current-public-key]} [_ {:keys [command chat-id]}]] (fn [_ [_ {:keys [command chat-id]}]]
(messages/save (messages/save
chat-id chat-id
(dissoc command :rendered-preview :to-message :has-handler))))) (dissoc command :rendered-preview :to-message :has-handler)))))
@ -111,16 +112,16 @@
(register-handler ::invoke-command-handlers! (register-handler ::invoke-command-handlers!
(u/side-effect! (u/side-effect!
(fn [db [_ {:keys [chat-id address] :as parameters}]] (fn [db [_ {:keys [chat-id address staged-command] :as parameters}]]
(let [{:keys [command content]} (:command parameters) (let [{:keys [command params]} staged-command
{:keys [type name]} command {:keys [type name]} command
path [(if (= :command type) :commands :responses) path [(if (= :command type) :commands :responses)
name name
:handler] :handler]
to (get-in db [:contacts chat-id :address]) to (get-in db [:contacts chat-id :address])
params {:value content params {:parameters params
:command {:from address :context {:from address
:to to}}] :to to}}]
(status/call-jail chat-id (status/call-jail chat-id
path path
params params
@ -130,19 +131,19 @@
(u/side-effect! (u/side-effect!
(fn [db [_ {:keys [chat-id identity message] :as params}]] (fn [db [_ {:keys [chat-id identity message] :as params}]]
(let [{:keys [group-chat]} (get-in db [:chats chat-id]) (let [{:keys [group-chat]} (get-in db [:chats chat-id])
message' (cu/check-author-direction message' (cu/check-author-direction
db chat-id db chat-id
{:message-id (random/id) {:message-id (random/id)
:chat-id chat-id :chat-id chat-id
:content message :content message
:from identity :from identity
:content-type text-content-type :content-type text-content-type
:outgoing true :outgoing true
:timestamp (time/now-ms)}) :timestamp (time/now-ms)})
message'' (if group-chat message'' (if group-chat
(assoc message' :group-id chat-id :message-type :group-user-message) (assoc message' :group-id chat-id :message-type :group-user-message)
(assoc message' :to chat-id :message-type :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 [::add-message params'])
(dispatch [::save-message! params']))))) (dispatch [::save-message! params'])))))
@ -164,9 +165,9 @@
chat-id :chat-id}]] chat-id :chat-id}]]
(when (and message (cu/not-console? chat-id)) (when (and message (cu/not-console? chat-id))
(let [message' (select-keys message [:from :message-id]) (let [message' (select-keys message [:from :message-id])
payload (select-keys message [:timestamp :content :content-type]) payload (select-keys message [:timestamp :content :content-type])
options {:web3 web3 options {:web3 web3
:message (assoc message' :payload payload)}] :message (assoc message' :payload payload)}]
(if (= message-type :group-user-message) (if (= message-type :group-user-message)
(let [{:keys [public-key private-key]} (chats chat-id)] (let [{:keys [public-key private-key]} (chats chat-id)]
(protocol/send-group-message! (assoc options (protocol/send-group-message! (assoc options

View File

@ -15,8 +15,8 @@
:justifyContent :center}) :justifyContent :center})
(def drag-icon (def drag-icon
{:width 14 {:width 14
:height 3}) :height 3})
(def command-icon-container (def command-icon-container
{:marginTop 1 {:marginTop 1
@ -28,24 +28,25 @@
:borderRadius 16 :borderRadius 16
:backgroundColor color-white}) :backgroundColor color-white})
(def command-icon (defn command-icon [color]
{:width 9 {:width 13
:height 15}) :height 13
:tint-color color})
(def info-container (def info-container
{:flex 1 {:flex 1
:marginLeft 12}) :marginLeft 12})
(def command-name (def command-name
{:marginTop 0 {:marginTop 0
:fontSize 12 :fontSize 12
:color color-white}) :color color-white})
(def message-info (def message-info
{:marginTop 1 {:marginTop 1
:fontSize 12 :fontSize 12
:opacity 0.69 :opacity 0.69
:color color-white}) :color color-white})
(defn response-view [keyboard-height height] (defn response-view [keyboard-height height]
{:flexDirection :column {:flexDirection :column

View File

@ -80,7 +80,7 @@
(register-sub :valid-plain-message? (register-sub :valid-plain-message?
(fn [_ _] (fn [_ _]
(let [input-message (subscribe [:get-chat-input-text]) (let [input-message (subscribe [:get-chat-input-text])
staged-commands (subscribe [:get-chat-staged-commands])] staged-commands (subscribe [:get-chat-staged-commands])]
(reaction (reaction
(plain-message/message-valid? @staged-commands @input-message))))) (plain-message/message-valid? @staged-commands @input-message)))))
@ -100,7 +100,7 @@
chat-id (subscribe [:get-current-chat-id])] chat-id (subscribe [:get-current-chat-id])]
(reaction (reaction
(let [path [:chats @chat-id :command-input :parameter-idx] (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))))))) (when n (nth (:params @command) n)))))))
(register-sub :get-chat-command-content (register-sub :get-chat-command-content
@ -141,10 +141,10 @@
(register-sub :messages-offset (register-sub :messages-offset
(fn [] (fn []
(let [command? (subscribe [:command?]) (let [command? (subscribe [:command?])
type (subscribe [:command-type]) type (subscribe [:command-type])
command-suggestions (subscribe [:get-content-suggestions]) command-suggestions (subscribe [:get-content-suggestions])
suggestions (subscribe [:get-suggestions])] suggestions (subscribe [:get-suggestions])]
(reaction (reaction
(cond (and @command? (= @type :response)) (cond (and @command? (= @type :response))
c/request-info-height c/request-info-height
@ -160,7 +160,7 @@
(register-sub :command-icon-width (register-sub :command-icon-width
(fn [] (fn []
(let [width (subscribe [:get :command-icon-width]) (let [width (subscribe [:get :command-icon-width])
type (subscribe [:command-type])] type (subscribe [:command-type])]
(reaction (if (= :command @type) (reaction (if (= :command @type)
@width @width
0))))) 0)))))
@ -170,6 +170,19 @@
(let [chat-id (subscribe [:get-current-chat-id])] (let [chat-id (subscribe [:get-current-chat-id])]
(reaction (get-in @db [:chats @chat-id :requests]))))) (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 (register-sub :get-response
(fn [db [_ n]] (fn [db [_ n]]
(let [chat-id (subscribe [:get-current-chat-id])] (let [chat-id (subscribe [:get-current-chat-id])]
@ -224,9 +237,9 @@
(register-sub :input-margin (register-sub :input-margin
(fn [] (fn []
(let [kb-height (subscribe [:get :keyboard-height]) (let [kb-height (subscribe [:get :keyboard-height])
command (subscribe [:get-chat-command]) command (subscribe [:get-chat-command])
focused (subscribe [:get :focused]) focused (subscribe [:get :focused])
mode (subscribe [:kb-mode])] mode (subscribe [:kb-mode])]
(reaction (reaction
(cond (or ios? (cond (or ios?
(and (not @focused) (and (not @focused)

View File

@ -22,10 +22,6 @@
(validator message) (validator message)
(pos? (count message)))) (pos? (count message))))
(defn try-send [message validator]
(when (valid? message validator)
(send-command)))
(defview command-icon [command] (defview command-icon [command]
[icon-width [:get :command-icon-width]] [icon-width [:get :command-icon-width]]
[view st/command-container [view st/command-container

View File

@ -7,6 +7,7 @@
[status-im.components.react :refer [view [status-im.components.react :refer [view
text text
image image
icon
animated-view animated-view
touchable-highlight]] touchable-highlight]]
[status-im.components.animation :as anim] [status-im.components.animation :as anim]
@ -17,7 +18,6 @@
[status-im.models.commands :refer [parse-command-message-content [status-im.models.commands :refer [parse-command-message-content
parse-command-request]] parse-command-request]]
[status-im.resources :as res] [status-im.resources :as res]
[status-im.utils.datetime :as time]
[status-im.constants :refer [text-content-type [status-im.constants :refer [text-content-type
content-type-status content-type-status
content-type-command content-type-command
@ -69,23 +69,25 @@
(defview message-content-command [content preview] (defview message-content-command [content preview]
[commands [:get-commands-and-responses]] [commands [:get-commands-and-responses]]
(let [{:keys [command content]} (parse-command-message-content commands content) (let [{:keys [command params]} (parse-command-message-content commands content)
{:keys [name icon type]} command] {:keys [name type]
icon-path :icon} command]
[view st/content-command-view [view st/content-command-view
[view st/command-container [view st/command-container
[view (pill-st/pill command) [view (pill-st/pill command)
[text {:style pill-st/pill-text [text {:style pill-st/pill-text
:font :default} :font :default}
(str (if (= :command type) "!" "?") name)]]] (str (if (= :command type) "!" "?") name)]]]
(when icon (when icon-path
[view st/command-image-view [view st/command-image-view
[image {:source {:uri icon} [icon icon-path st/command-image]])
:style st/command-image}]])
(if preview (if preview
preview preview
[text {:style st/command-text [text {:style st/command-text
:font :default} :font :default}
(str content)])])) (if (= 1 (count params))
(first (vals params))
(str params))])]))
(defn set-chat-command [message-id command] (defn set-chat-command [message-id command]
(dispatch [:set-response-chat-command message-id (keyword (:name command))])) (dispatch [:set-response-chat-command message-id (keyword (:name command))]))

View File

@ -30,9 +30,7 @@
(defn on-press-commands-handler (defn on-press-commands-handler
[{:keys [suggestions-trigger]}] [{:keys [suggestions-trigger]}]
(if (= :on-send (keyword suggestions-trigger)) #(dispatch [:send-command!]))
#(dispatch [:invoke-commands-suggestions!])
command/send-command))
(defn command-input-options [command icon-width disable?] (defn command-input-options [command icon-width disable?]
{:style (st-response/command-input icon-width disable?) {:style (st-response/command-input icon-width disable?)
@ -102,7 +100,8 @@
[message-input input-options set-layout-size])] [message-input input-options set-layout-size])]
;; TODO emoticons: not implemented ;; TODO emoticons: not implemented
[plain-message/smile-button height] [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? (let [on-press (if @command?
(on-press-commands-handler @command) (on-press-commands-handler @command)
plain-message/send)] plain-message/send)]

View File

@ -7,6 +7,7 @@
[status-im.components.react :refer [view [status-im.components.react :refer [view
animated-view animated-view
icon icon
image
text text
text-input text-input
touchable-highlight touchable-highlight
@ -20,38 +21,49 @@
[status-im.chat.constants :as c] [status-im.chat.constants :as c]
[status-im.chat.views.command-validation :as cv] [status-im.chat.views.command-validation :as cv]
[status-im.utils.platform :refer [ios?]] [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 [] (defn drag-icon []
[view st/drag-container [view st/drag-container
[icon :drag_white st/drag-icon]]) [icon :drag_white st/drag-icon]])
(defn command-icon [] (defn command-icon [{icon-path :icon
color :color}]
[view st/command-icon-container [view st/command-icon-container
;; TODO stub data: command icon (when icon-path
[icon :dollar_green st/command-icon]]) [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 [view st/info-container
[text {:style st/command-name} [text {:style st/command-name}
(:description command)] (str (:description command) " " (label :t/request))]
[text {:style st/message-info} (when added
;; TODO stub data: request message info [text {:style st/message-info}
"By ???, MMM 1st at HH:mm"]]) (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] (defn request-info [response-height]
(let [layout-height (subscribe [:get :layout-height]) (let [layout-height (subscribe [:get :layout-height])
pan-responder (resp/pan-responder response-height pan-responder (resp/pan-responder response-height
layout-height layout-height
:fix-response-height) :fix-response-height)
command (subscribe [:get-chat-command])] command (subscribe [:get-chat-command])]
(fn [response-height] (fn [response-height]
(if (= :response (:type @command)) (if (= :response (:type @command))
[view (merge (drag/pan-handlers pan-responder) [view (merge (drag/pan-handlers pan-responder)
{:style (st/request-info (:color @command))}) {:style (st/request-info (:color @command))})
[drag-icon] [drag-icon]
[view st/inner-container [view st/inner-container
[command-icon nil] [command-icon @command]
[info-container @command] [info-container @command]
[touchable-highlight {:on-press #(dispatch [:start-cancel-command])} [touchable-highlight {:on-press #(dispatch [:start-cancel-command])}
[view st/cancel-container [view st/cancel-container

View File

@ -12,8 +12,9 @@
(defn cancel-command-input [staged-command] (defn cancel-command-input [staged-command]
(dispatch [:unstage-command staged-command])) (dispatch [:unstage-command staged-command]))
(defn simple-command-staged-view [staged-command] (defn simple-command-staged-view
(let [{:keys [type name] :as command} (:command staged-command)] [{:keys [command params] :as staged-command}]
(let [{:keys [type name]} command]
[view st/staged-command-container [view st/staged-command-container
[view st/staged-command-background [view st/staged-command-background
[view {:flex-direction :row} [view {:flex-direction :row}
@ -30,4 +31,6 @@
(if-let [preview (:preview staged-command)] (if-let [preview (:preview staged-command)]
preview preview
[text {:style st/staged-command-content} [text {:style st/staged-command-content}
(:content staged-command)])]])) (if (= 1 (count params))
(first (vals params))
(str params))])]]))

View File

@ -7,7 +7,8 @@
[status-im.commands.utils :refer [generate-hiccup reg-handler]] [status-im.commands.utils :refer [generate-hiccup reg-handler]]
[clojure.string :as s] [clojure.string :as s]
[status-im.components.react :as r] [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! (defn init-render-command!
[_ [chat-id command message-id data]] [_ [chat-id command message-id data]]
@ -78,7 +79,7 @@
(fn [_ params] (fn [_ params]
(when (:error (last params)) (when (:error (last params))
(show-popup "Error" (s/join "\n" [message 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 :init-render-command! init-render-command!)
(reg-handler ::render-command render-command) (reg-handler ::render-command render-command)

View File

@ -8,7 +8,8 @@
[status-im.components.status :as status] [status-im.components.status :as status]
[status-im.utils.types :refer [json->clj]] [status-im.utils.types :refer [json->clj]]
[status-im.commands.utils :refer [reg-handler]] [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") (def commands-js "commands.js")
@ -95,7 +96,7 @@
(name reason) (name reason)
details])] details])]
(show-popup "Error" m) (show-popup "Error" m)
(println m)))) (log/debug m))))
(reg-handler :load-commands! (u/side-effect! load-commands!)) (reg-handler :load-commands! (u/side-effect! load-commands!))
(reg-handler ::fetch-commands! (u/side-effect! fetch-commands!)) (reg-handler ::fetch-commands! (u/side-effect! fetch-commands!))

View File

@ -47,10 +47,12 @@
(defn call-jail [chat-id path params callback] (defn call-jail [chat-id path params callback]
(when status (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 [cb (fn [r]
(let [r' (t/json->clj r)] (let [r' (t/json->clj r)]
(println r') (log/debug r')
(callback r')))] (callback r')))]
(.callJail status chat-id (cljs->json path) (cljs->json params) cb)))) (.callJail status chat-id (cljs->json path) (cljs->json params) cb))))

View File

@ -9,9 +9,6 @@
[type {:keys [current-chat-id] :as db} command-key] [type {:keys [current-chat-id] :as db} command-key]
((or (get-in db [:chats current-chat-id type]) {}) 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 (defn get-chat-command-content
[{:keys [current-chat-id] :as db}] [{:keys [current-chat-id] :as db}]
(get-in db (db/chat-command-content-path current-chat-id))) (get-in db (db/chat-command-content-path current-chat-id)))
@ -20,18 +17,26 @@
[{:keys [current-chat-id] :as db} content] [{:keys [current-chat-id] :as db} content]
(assoc-in db [:chats current-chat-id :command-input :content] 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 (defn get-chat-command
[{:keys [current-chat-id] :as db}] [{:keys [current-chat-id] :as db}]
(get-in db (db/chat-command-path current-chat-id))) (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 (defn set-command-input
([db type command-key] ([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] ([{:keys [current-chat-id] :as db} type message-id command-key]
(update-in db [:chats current-chat-id :command-input] merge (update-in db [:chats current-chat-id :command-input] merge
{:content nil {:content nil
:command (get-response-or-command type db command-key) :command (get-response-or-command type db command-key)
:parameter-idx 0 :parameter-idx 0
:params nil
:to-message-id message-id}))) :to-message-id message-id})))
(defn get-chat-command-to-message-id (defn get-chat-command-to-message-id
@ -44,11 +49,11 @@
(defn stage-command (defn stage-command
[{:keys [current-chat-id] :as db} {:keys [id] :as command-info}] [{:keys [current-chat-id] :as db} {:keys [id] :as command-info}]
(let [path (db/chat-staged-commands-path current-chat-id) (let [path (db/chat-staged-commands-path current-chat-id)
staged-commands (get-in db path) staged-commands (get-in db path)
staged-coomands' (if (seq staged-commands) staged-coomands' (if (seq staged-commands)
staged-commands staged-commands
(priority-map-by compare-commands))] (priority-map-by compare-commands))]
(assoc-in db path (assoc staged-coomands' id command-info)))) (assoc-in db path (assoc staged-coomands' id command-info))))
(defn unstage-command [db {:keys [id]}] (defn unstage-command [db {:keys [id]}]
@ -60,11 +65,6 @@
(get-in db (db/chat-command-request-path current-chat-id (get-in db (db/chat-command-request-path current-chat-id
(get-chat-command-to-message-id db)))) (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] (defn parse-command-message-content [commands content]
(update content :command #((keyword %) commands))) (update content :command #((keyword %) commands)))

View File

@ -133,6 +133,7 @@
:request-command-description "Send request" :request-command-description "Send request"
:keypair-password-command-description "" :keypair-password-command-description ""
:help-command-description "Help" :help-command-description "Help"
:request "Request"
;new-group ;new-group
:group-chat-name "Chat name" :group-chat-name "Chat name"

View File

@ -1,6 +1,6 @@
(ns status-im.utils.datetime (ns status-im.utils.datetime
(:require [cljs-time.core :as t :refer [date-time now plus days hours before?]] (: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 [cljs-time.format :refer [formatters
formatter formatter
unparse]] unparse]]
@ -20,7 +20,7 @@
(defn to-short-str (defn to-short-str
([ms] ([ms]
(to-short-str ms #(unparse (formatters :hour-minute) %))) (to-short-str ms #(unparse (formatters :hour-minute) %)))
([ms today-format-fn] ([ms today-format-fn]
(let [date (from-long ms) (let [date (from-long ms)
local (plus date time-zone-offset) local (plus date time-zone-offset)
@ -59,3 +59,19 @@
(defn now-ms [] (defn now-ms []
(to-long (now))) (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)))))