remove usage of merge-effects macro and handlers-macro ns
Signed-off-by: yenda <eric@status.im>
This commit is contained in:
parent
7fe78b708d
commit
e9149786c5
|
@ -170,7 +170,8 @@
|
||||||
chat-ids (keys chat->message)
|
chat-ids (keys chat->message)
|
||||||
chats-fx-fns (map #(chat-model/upsert-chat {:chat-id %
|
chats-fx-fns (map #(chat-model/upsert-chat {:chat-id %
|
||||||
:is-active true
|
:is-active true
|
||||||
:timestamp now}) chat-ids)
|
:timestamp now})
|
||||||
|
chat-ids)
|
||||||
messages-fx-fns (map #(add-received-message true %) messages)
|
messages-fx-fns (map #(add-received-message true %) messages)
|
||||||
groups-fx-fns (map #(update-group-messages chat->message %) chat-ids)]
|
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))))
|
(apply fx/merge cofx (concat chats-fx-fns messages-fx-fns groups-fx-fns))))
|
||||||
|
|
|
@ -7,13 +7,12 @@
|
||||||
[status-im.transport.message.transit :as transit]
|
[status-im.transport.message.transit :as transit]
|
||||||
[status-im.transport.message.v1.contact :as v1.contact]
|
[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.group-chat :as v1.group-chat]
|
||||||
|
[status-im.transport.message.v1.protocol :as protocol]
|
||||||
[status-im.transport.shh :as shh]
|
[status-im.transport.shh :as shh]
|
||||||
[status-im.transport.utils :as transport.utils]
|
[status-im.transport.utils :as transport.utils]
|
||||||
|
[status-im.utils.fx :as fx]
|
||||||
[status-im.utils.handlers :as handlers]
|
[status-im.utils.handlers :as handlers]
|
||||||
[status-im.utils.handlers-macro :as handlers-macro]
|
[taoensso.timbre :as log]))
|
||||||
[taoensso.timbre :as log]
|
|
||||||
[status-im.transport.message.v1.protocol :as protocol]
|
|
||||||
[status-im.utils.fx :as fx]))
|
|
||||||
|
|
||||||
(fx/defn update-last-received-from-inbox
|
(fx/defn update-last-received-from-inbox
|
||||||
"Distinguishes messages that are expired from those that are not
|
"Distinguishes messages that are expired from those that are not
|
||||||
|
@ -22,7 +21,7 @@
|
||||||
(when (> (- now-in-s timestamp) ttl)
|
(when (> (- now-in-s timestamp) ttl)
|
||||||
{:db (assoc db :inbox/last-received now)}))
|
{:db (assoc db :inbox/last-received now)}))
|
||||||
|
|
||||||
(defn receive-message
|
(fx/defn receive-message
|
||||||
[cofx now-in-s chat-id js-message]
|
[cofx now-in-s chat-id js-message]
|
||||||
(let [{:keys [payload sig timestamp ttl]} (js->clj js-message :keywordize-keys true)
|
(let [{:keys [payload sig timestamp ttl]} (js->clj js-message :keywordize-keys true)
|
||||||
status-message (-> payload
|
status-message (-> payload
|
||||||
|
@ -43,12 +42,11 @@
|
||||||
[{:keys [now] :as cofx} [_ js-error js-messages chat-id]]
|
[{:keys [now] :as cofx} [_ js-error js-messages chat-id]]
|
||||||
(if (and (not js-error)
|
(if (and (not js-error)
|
||||||
js-messages)
|
js-messages)
|
||||||
(let [now-in-s (quot now 1000)]
|
(let [now-in-s (quot now 1000)
|
||||||
(handlers-macro/merge-effects
|
receive-message-fxs (map (fn [message]
|
||||||
cofx
|
(receive-message now-in-s chat-id message))
|
||||||
(fn [message temp-cofx]
|
(js-array->seq js-messages))]
|
||||||
(receive-message temp-cofx now-in-s chat-id message))
|
(apply fx/merge cofx receive-message-fxs))
|
||||||
(js-array->seq js-messages)))
|
|
||||||
(log/error "Something went wrong" js-error js-messages)))
|
(log/error "Something went wrong" js-error js-messages)))
|
||||||
|
|
||||||
(handlers/register-handler-fx
|
(handlers/register-handler-fx
|
||||||
|
@ -229,24 +227,29 @@
|
||||||
(v1.contact/map->ContactRequest own-info))
|
(v1.contact/map->ContactRequest own-info))
|
||||||
chat-id cofx))
|
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
|
(fx/defn resend-contact-messages
|
||||||
[{:keys [db] :as cofx} previous-summary]
|
[{:keys [db] :as cofx} previous-summary]
|
||||||
(when (and (zero? (count previous-summary))
|
(when (and (zero? (count previous-summary))
|
||||||
(= :online (:network-status db))
|
(= :online (:network-status db))
|
||||||
(pos? (count (:peers-summary db))))
|
(pos? (count (:peers-summary db))))
|
||||||
(let [own-info (own-info db)]
|
(let [own-info (own-info db)
|
||||||
(handlers-macro/merge-effects
|
resend-contact-message-fxs (map (fn [chat-id]
|
||||||
cofx
|
(resend-contact-message own-info chat-id))
|
||||||
(fn [[chat-id {:keys [resend?] :as chat}] temp-cofx]
|
(keys (:transport/chats db)))]
|
||||||
(case resend?
|
(apply fx/merge cofx resend-contact-message-fxs))))
|
||||||
"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)))))
|
|
||||||
|
|
|
@ -5,7 +5,6 @@
|
||||||
[status-im.transport.message.core :as message]
|
[status-im.transport.message.core :as message]
|
||||||
[status-im.transport.message.v1.protocol :as protocol]
|
[status-im.transport.message.v1.protocol :as protocol]
|
||||||
[status-im.transport.utils :as transport.utils]
|
[status-im.transport.utils :as transport.utils]
|
||||||
[status-im.utils.handlers-macro :as handlers-macro]
|
|
||||||
[status-im.utils.fx :as fx]))
|
[status-im.utils.fx :as fx]))
|
||||||
|
|
||||||
(defrecord ContactRequest [name profile-image address fcm-token]
|
(defrecord ContactRequest [name profile-image address fcm-token]
|
||||||
|
@ -42,33 +41,39 @@
|
||||||
:payload this
|
:payload this
|
||||||
:success-event success-event})))))
|
: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]
|
(defrecord ContactUpdate [name profile-image address fcm-token]
|
||||||
message/StatusMessage
|
message/StatusMessage
|
||||||
(send [this _ {:keys [db] :as cofx}]
|
(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
|
(if (and public-key
|
||||||
(not pending?))
|
(not pending?))
|
||||||
(conj acc public-key)
|
(conj acc public-key)
|
||||||
acc))
|
acc))
|
||||||
#{}
|
#{}
|
||||||
(:contacts/contacts db))
|
(:contacts/contacts db))
|
||||||
recipients (filter #(public-keys (first %)) (:transport/chats db))]
|
;;NOTE: chats with contacts use public-key as chat-id
|
||||||
(handlers-macro/merge-effects
|
send-contact-update-fxs (map #(send-contact-update % this) contact-public-keys)]
|
||||||
cofx
|
(apply fx/merge cofx send-contact-update-fxs))))
|
||||||
(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))))
|
|
||||||
|
|
||||||
(fx/defn remove-chat-filter
|
(fx/defn remove-chat-filter
|
||||||
"Stops the filter for the given chat-id"
|
"Stops the filter for the given chat-id"
|
||||||
|
|
|
@ -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)))
|
|
Loading…
Reference in New Issue