remove usage of merge-effects macro and handlers-macro ns

Signed-off-by: yenda <eric@status.im>
This commit is contained in:
yenda 2018-09-25 18:30:29 +02:00
parent 7fe78b708d
commit e9149786c5
No known key found for this signature in database
GPG Key ID: 0095623C0069DCE6
4 changed files with 62 additions and 94 deletions

View File

@ -170,7 +170,8 @@
chat-ids (keys chat->message)
chats-fx-fns (map #(chat-model/upsert-chat {:chat-id %
:is-active true
:timestamp now}) chat-ids)
:timestamp now})
chat-ids)
messages-fx-fns (map #(add-received-message true %) messages)
groups-fx-fns (map #(update-group-messages chat->message %) chat-ids)]
(apply fx/merge cofx (concat chats-fx-fns messages-fx-fns groups-fx-fns))))

View File

@ -7,13 +7,12 @@
[status-im.transport.message.transit :as transit]
[status-im.transport.message.v1.contact :as v1.contact]
[status-im.transport.message.v1.group-chat :as v1.group-chat]
[status-im.transport.message.v1.protocol :as protocol]
[status-im.transport.shh :as shh]
[status-im.transport.utils :as transport.utils]
[status-im.utils.fx :as fx]
[status-im.utils.handlers :as handlers]
[status-im.utils.handlers-macro :as handlers-macro]
[taoensso.timbre :as log]
[status-im.transport.message.v1.protocol :as protocol]
[status-im.utils.fx :as fx]))
[taoensso.timbre :as log]))
(fx/defn update-last-received-from-inbox
"Distinguishes messages that are expired from those that are not
@ -22,7 +21,7 @@
(when (> (- now-in-s timestamp) ttl)
{:db (assoc db :inbox/last-received now)}))
(defn receive-message
(fx/defn receive-message
[cofx now-in-s chat-id js-message]
(let [{:keys [payload sig timestamp ttl]} (js->clj js-message :keywordize-keys true)
status-message (-> payload
@ -43,12 +42,11 @@
[{:keys [now] :as cofx} [_ js-error js-messages chat-id]]
(if (and (not js-error)
js-messages)
(let [now-in-s (quot now 1000)]
(handlers-macro/merge-effects
cofx
(fn [message temp-cofx]
(receive-message temp-cofx now-in-s chat-id message))
(js-array->seq js-messages)))
(let [now-in-s (quot now 1000)
receive-message-fxs (map (fn [message]
(receive-message now-in-s chat-id message))
(js-array->seq js-messages))]
(apply fx/merge cofx receive-message-fxs))
(log/error "Something went wrong" js-error js-messages)))
(handlers/register-handler-fx
@ -229,24 +227,29 @@
(v1.contact/map->ContactRequest own-info))
chat-id cofx))
(fx/defn resend-contact-message
[cofx own-info chat-id]
(let [{:keys [resend?] :as chat} (get-in cofx [:db :transport/chats chat-id])]
(case resend?
"contact-request"
(resend-contact-request cofx own-info chat-id chat)
"contact-request-confirmation"
(message/send (v1.contact/map->ContactRequestConfirmed own-info)
chat-id
cofx)
"contact-update"
(protocol/send cofx
{:chat-id chat-id
:payload (v1.contact/map->ContactUpdate own-info)})
nil)))
(fx/defn resend-contact-messages
[{:keys [db] :as cofx} previous-summary]
(when (and (zero? (count previous-summary))
(= :online (:network-status db))
(pos? (count (:peers-summary db))))
(let [own-info (own-info db)]
(handlers-macro/merge-effects
cofx
(fn [[chat-id {:keys [resend?] :as chat}] temp-cofx]
(case resend?
"contact-request"
(resend-contact-request own-info chat-id chat temp-cofx)
"contact-request-confirmation"
(message/send (v1.contact/map->ContactRequestConfirmed own-info)
chat-id temp-cofx)
"contact-update"
(protocol/send {:chat-id chat-id
:payload (v1.contact/map->ContactUpdate own-info)}
temp-cofx)
nil))
(:transport/chats db)))))
(let [own-info (own-info db)
resend-contact-message-fxs (map (fn [chat-id]
(resend-contact-message own-info chat-id))
(keys (:transport/chats db)))]
(apply fx/merge cofx resend-contact-message-fxs))))

View File

@ -5,7 +5,6 @@
[status-im.transport.message.core :as message]
[status-im.transport.message.v1.protocol :as protocol]
[status-im.transport.utils :as transport.utils]
[status-im.utils.handlers-macro :as handlers-macro]
[status-im.utils.fx :as fx]))
(defrecord ContactRequest [name profile-image address fcm-token]
@ -42,33 +41,39 @@
:payload this
:success-event success-event})))))
(fx/defn send-contact-update
[{:keys [db] :as cofx} chat-id payload]
(when-let [chat (get-in cofx [:db :transport/chats chat-id])]
(let [updated-chat (assoc chat :resend? "contact-update")
tx [(transport-store/save-transport-tx {:chat-id chat-id
:chat updated-chat})]
success-event [:transport/set-contact-message-envelope-hash chat-id]]
(fx/merge cofx
{:db (assoc-in db
[:transport/chats chat-id :resend?]
"contact-update")
:data-store/tx tx}
(protocol/send-with-pubkey {:chat-id chat-id
:payload payload
:success-event success-event})))))
(defrecord ContactUpdate [name profile-image address fcm-token]
message/StatusMessage
(send [this _ {:keys [db] :as cofx}]
(let [public-keys (reduce (fn [acc [_ {:keys [public-key pending?]}]]
;;TODO: here we look for contact which have a :public-key to differentiate
;;actual contacts from dapps
;;This is not an ideal solution and we should think about a more reliable way
;;to do this when we refactor app-db
(let [contact-public-keys (reduce (fn [acc [_ {:keys [public-key pending?]}]]
(if (and public-key
(not pending?))
(conj acc public-key)
acc))
#{}
(:contacts/contacts db))
recipients (filter #(public-keys (first %)) (:transport/chats db))]
(handlers-macro/merge-effects
cofx
(fn [[chat-id chat] temp-cofx]
(let [updated-chat (assoc chat :resend? "contact-update")
tx [(transport-store/save-transport-tx {:chat-id chat-id
:chat updated-chat})]
success-event [:transport/set-contact-message-envelope-hash chat-id]]
(fx/merge temp-cofx
{:db (assoc-in db
[:transport/chats chat-id :resend?]
"contact-update")
:data-store/tx tx}
(protocol/send-with-pubkey {:chat-id chat-id
:payload this
:success-event success-event}))))
recipients))))
;;NOTE: chats with contacts use public-key as chat-id
send-contact-update-fxs (map #(send-contact-update % this) contact-public-keys)]
(apply fx/merge cofx send-contact-update-fxs))))
(fx/defn remove-chat-filter
"Stops the filter for the given chat-id"

View File

@ -1,41 +0,0 @@
(ns status-im.utils.handlers-macro
(:require [clojure.set :as set]
[taoensso.timbre :as log]))
(defn update-db [cofx fx]
(if-let [db (:db fx)]
(assoc cofx :db db)
cofx))
(def ^:private mergable-keys
#{:data-store/tx :data-store/base-tx :chat-received-message/add-fx
:shh/add-new-sym-keys :shh/get-new-sym-keys :shh/post
:shh/generate-sym-key-from-password :confirm-messages-processed
:utils/dispatch-later})
(defn safe-merge [fx new-fx]
(if (:merging-fx-with-common-keys fx)
fx
(let [common-keys (set/intersection (into #{} (keys fx))
(into #{} (keys new-fx)))]
(if (empty? (set/difference common-keys (conj mergable-keys :db)))
(merge (apply dissoc fx mergable-keys)
(apply dissoc new-fx mergable-keys)
(merge-with into
(select-keys fx mergable-keys)
(select-keys new-fx mergable-keys)))
(do (log/error "Merging fx with common-keys: " common-keys)
{:merging-fx-with-common-keys common-keys})))))
(defn merge-effects
([{:keys [db] :as cofx} handler args]
(merge-effects {:db db} cofx handler args))
([initial-fx {:keys [db] :as cofx} handler args]
(reduce (fn [fx arg]
(let [temp-cofx (update-db cofx fx)]
(safe-merge
fx
(handler arg temp-cofx))))
(or initial-fx
{:db db})
args)))