Enhanced receive params

This commit is contained in:
janherich 2018-09-05 16:28:17 +02:00 committed by jhe
parent e11a3c910d
commit 9ad29bfc56
No known key found for this signature in database
GPG Key ID: 75A22903170BB4AA
5 changed files with 34 additions and 12 deletions

View File

@ -306,8 +306,9 @@
:success-event :wallet/update-gas-price-success
:edit? false}}))
protocol/EnhancedParameters
(enhance-parameters [_ parameters cofx]
(inject-network-price-info parameters cofx)))
(enhance-send-parameters [_ parameters cofx]
(inject-network-price-info parameters cofx))
(enhance-receive-parameters [_ _ _]))
;; `/request` command
@ -452,5 +453,6 @@
(preview [_ command-message]
(request-preview command-message))
protocol/EnhancedParameters
(enhance-parameters [_ parameters cofx]
(inject-network-price-info parameters cofx)))
(enhance-send-parameters [_ parameters cofx]
(inject-network-price-info parameters cofx))
(enhance-receive-parameters [_ _ _]))

View File

@ -54,11 +54,17 @@
(defprotocol EnhancedParameters
"Protocol for command messages which wish to modify/inject additional data into parameters,
other then those collected from the chat input.
Good example would be the `/send` and `/receive` commands - we would like to indicate
Good example would be the `/send` and `/request` commands - we would like to indicate
network selected in sender's device, but we of course don't want to force user to type
it in when it could be effortlessly looked up from context.
Another usage would be for example command where one of the input parameters will be
hashed after validation and we would want to avoid the original unhashed parameter
to be ever saved on the sender device, nor to be sent over the wire."
(enhance-parameters [this parameters cofx]
to be ever saved on the sender device, nor to be sent over the wire.
For maximal flexibility, parameters can be enhanced both on the sending side and receiving
side, as sometimes thing needs to be added/enhanced in parameters map which are depending
on the receiver context - as for example calculated fiat price values for the `/request`
command"
(enhance-send-parameters [this parameters cofx]
"Function which takes original parameters + cofx map and returns new map of parameters")
(enhance-receive-parameters [this parameters cofx]
"Function which takes original parameters + cofx map and returns new map of parameters"))

View File

@ -23,3 +23,16 @@
(let [id->command (:id->command db)]
(when-let [{:keys [type]} (lookup-command-by-ref message id->command)]
(protocol/on-receive type message cofx))))
(defn enhance-receive-parameters
"Enhances parameters for the received command message.
If the message is not of the command type, or command doesn't implement the
`EnhancedParameters` protocol, returns unaltered message, otherwise updates
its parameters."
[message {:keys [db] :as cofx}]
(let [id->command (:id->command db)
{:keys [type content]} (lookup-command-by-ref message id->command)]
(if-let [new-params (and (satisfies? protocol/EnhancedParameters type)
(protocol/enhance-receive-parameters type (:params content) cofx))]
(assoc-in message [:content :params] new-params)
message)))

View File

@ -31,15 +31,15 @@
(defn- create-command-message
"Create message map from chat-id, command & input parameters"
[chat-id type parameter-map cofx]
(let [command-path (commands/command-id type)
(let [command-path (commands/command-id type)
;; TODO(janherich) this is just for backward compatibility, can be removed later
{:keys [content content-type]} (new->old command-path parameter-map)]
{:keys [content content-type]} (new->old command-path parameter-map)
new-parameter-map (and (satisfies? protocol/EnhancedParameters type)
(protocol/enhance-send-parameters type parameter-map cofx))]
{:chat-id chat-id
:content-type content-type
:content (merge {:command-path command-path
:params (if (satisfies? protocol/EnhancedParameters type)
(protocol/enhance-parameters type parameter-map cofx)
parameter-map)}
:params (or new-parameter-map parameter-map)}
content)}))
(defn validate-and-send

View File

@ -155,6 +155,7 @@
{:keys [public?] :as chat} (get-in db [:chats chat-id])
add-message-fn (if batch? add-batch-message add-single-message)
message (-> raw-message
(commands-receiving/enhance-receive-parameters cofx)
(ensure-clock-value chat)
;; TODO (cammellos): Refactor so it's not computed twice
(add-outgoing-status cofx)