diff --git a/src/status_im/chat/models/message.cljs b/src/status_im/chat/models/message.cljs index 679ea78ffa..fcc6b4de0a 100644 --- a/src/status_im/chat/models/message.cljs +++ b/src/status_im/chat/models/message.cljs @@ -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)))) diff --git a/src/status_im/transport/handlers.cljs b/src/status_im/transport/handlers.cljs index bee9e4cbdd..c6021f6e28 100644 --- a/src/status_im/transport/handlers.cljs +++ b/src/status_im/transport/handlers.cljs @@ -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)))) diff --git a/src/status_im/transport/message/v1/contact.cljs b/src/status_im/transport/message/v1/contact.cljs index ab2987dec9..c81dc413ab 100644 --- a/src/status_im/transport/message/v1/contact.cljs +++ b/src/status_im/transport/message/v1/contact.cljs @@ -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?]}]] - (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)))) + ;;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)) + ;;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" diff --git a/src/status_im/utils/handlers_macro.cljs b/src/status_im/utils/handlers_macro.cljs deleted file mode 100644 index cbf5428fdb..0000000000 --- a/src/status_im/utils/handlers_macro.cljs +++ /dev/null @@ -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)))