2017-08-15 20:56:44 +00:00
|
|
|
(ns status-im.bots.events
|
|
|
|
(:require [re-frame.core :as re-frame]
|
2018-01-18 10:15:37 +00:00
|
|
|
[status-im.utils.core :as utils]
|
2017-10-20 12:04:24 +00:00
|
|
|
[status-im.utils.handlers :as handlers]
|
|
|
|
[status-im.chat.models.input :as input-model]
|
|
|
|
[taoensso.timbre :as log]))
|
2017-08-15 20:56:44 +00:00
|
|
|
|
|
|
|
;;;; Helper fns
|
|
|
|
|
2017-10-20 12:04:24 +00:00
|
|
|
(defn- subscription-values [sub-params current-bot-db]
|
|
|
|
(reduce (fn [sub-values [sub-key sub-path]]
|
|
|
|
(assoc sub-values sub-key (get-in current-bot-db sub-path)))
|
2017-08-15 20:56:44 +00:00
|
|
|
{}
|
2017-10-20 12:04:24 +00:00
|
|
|
sub-params))
|
2017-08-15 20:56:44 +00:00
|
|
|
|
|
|
|
(defn- check-subscriptions-fx
|
2017-11-04 12:59:19 +00:00
|
|
|
[{:keys [bot-db] :contacts/keys [contacts] :as app-db} {:keys [bot path]}]
|
|
|
|
(when-let [subscriptions (and bot (get-in contacts (concat [bot :subscriptions] [path])))]
|
2017-10-25 15:33:46 +00:00
|
|
|
{:call-jail-function-n
|
|
|
|
(for [[sub-name sub-params] subscriptions]
|
|
|
|
{:chat-id bot
|
|
|
|
:function :subscription
|
|
|
|
:parameters {:name sub-name
|
|
|
|
:subscriptions (subscription-values sub-params (get bot-db bot))}
|
2018-04-02 16:17:15 +00:00
|
|
|
:callback-event-creator (fn [jail-response]
|
|
|
|
[::calculated-subscription
|
|
|
|
{:bot bot
|
|
|
|
:path [sub-name]
|
|
|
|
:result jail-response}])})}))
|
2017-08-15 20:56:44 +00:00
|
|
|
|
|
|
|
(defn set-in-bot-db
|
2017-10-25 15:33:46 +00:00
|
|
|
"Associates value at specified path in bot-db and checks if there are any subscriptions
|
|
|
|
dependent on that path, if yes, adds effects for calling jail-functions to recalculate
|
|
|
|
relevant subscriptions."
|
|
|
|
[app-db {:keys [bot path value] :as params}]
|
|
|
|
(let [new-db (assoc-in app-db (concat [:bot-db bot] path) value)]
|
2017-08-15 20:56:44 +00:00
|
|
|
(merge {:db new-db}
|
2017-10-25 15:33:46 +00:00
|
|
|
(check-subscriptions-fx new-db params))))
|
2017-08-15 20:56:44 +00:00
|
|
|
|
|
|
|
(defn update-bot-db
|
2017-10-25 15:33:46 +00:00
|
|
|
[app-db {:keys [bot db]}]
|
|
|
|
(update-in app-db [:bot-db bot] merge db))
|
2017-08-15 20:56:44 +00:00
|
|
|
|
|
|
|
(defn clear-bot-db
|
2017-10-25 15:33:46 +00:00
|
|
|
[app-db bot-id]
|
|
|
|
(assoc-in app-db [:bot-db bot-id] nil))
|
2017-08-15 20:56:44 +00:00
|
|
|
|
2017-10-20 12:04:24 +00:00
|
|
|
(def ^:private keywordize-vector (partial mapv keyword))
|
|
|
|
|
2017-11-04 12:59:19 +00:00
|
|
|
(defn transform-bot-subscriptions
|
2017-10-25 15:33:46 +00:00
|
|
|
"Transforms bot subscriptions as returned from jail in the following format:
|
|
|
|
|
|
|
|
`{:calculatedFee {:subscriptions {:value [\"sliderValue\"]
|
|
|
|
:tx [\"transaction\"]}}
|
|
|
|
:feeExplanation {:subscriptions {:value [\"sliderValue\"]}}}`
|
|
|
|
|
|
|
|
into data-structure better suited for subscription lookups based on changes
|
|
|
|
in `:bot-db`:
|
|
|
|
|
|
|
|
`{[:sliderValue] {:calculatedFee {:value [:sliderValue]
|
|
|
|
:tx [:transaction]}
|
|
|
|
:feeExplanation {:value [:sliderValue]}}
|
|
|
|
[:transaction] {:calculatedFee {:value [:sliderValue]
|
|
|
|
:tx [:transaction]}}}`
|
|
|
|
|
|
|
|
In the resulting data-structure, the top level keys are the (keywordized) paths
|
|
|
|
in the `:bot-db` data structure, so it's quick and easy to look-up all the
|
|
|
|
subscriptions which must be recalculated when something in their path changes."
|
|
|
|
[bot-subscriptions]
|
|
|
|
(reduce-kv (fn [acc sub-key {:keys [subscriptions]}]
|
|
|
|
(reduce-kv (fn [acc sub-param-key sub-param-path]
|
|
|
|
(update acc
|
|
|
|
(keywordize-vector sub-param-path)
|
|
|
|
assoc sub-key
|
|
|
|
(utils/map-values subscriptions keywordize-vector)))
|
|
|
|
acc
|
|
|
|
subscriptions))
|
|
|
|
{}
|
|
|
|
bot-subscriptions))
|
|
|
|
|
|
|
|
(defn calculated-subscription
|
|
|
|
[db {:keys [bot path]
|
|
|
|
{:keys [error result]} :result}]
|
|
|
|
(if error
|
|
|
|
db
|
|
|
|
(assoc-in db (concat [:bot-db bot] path) (:returned result))))
|
2017-10-20 12:04:24 +00:00
|
|
|
|
2017-08-15 20:56:44 +00:00
|
|
|
;;;; Handlers
|
|
|
|
|
|
|
|
(handlers/register-handler-fx
|
2018-05-08 08:32:28 +00:00
|
|
|
:set-in-bot-db
|
|
|
|
[re-frame/trim-v]
|
|
|
|
(fn [{:keys [db]} [params]]
|
|
|
|
(set-in-bot-db db params)))
|
2017-08-15 20:56:44 +00:00
|
|
|
|
|
|
|
(handlers/register-handler-db
|
2018-05-08 08:32:28 +00:00
|
|
|
:update-bot-db
|
|
|
|
[re-frame/trim-v]
|
|
|
|
(fn [db [params]]
|
|
|
|
(update-bot-db db params)))
|
2017-08-15 20:56:44 +00:00
|
|
|
|
|
|
|
(handlers/register-handler-db
|
2018-05-08 08:32:28 +00:00
|
|
|
::calculated-subscription
|
|
|
|
[re-frame/trim-v]
|
|
|
|
(fn [db [params]]
|
|
|
|
(calculated-subscription db params)))
|