From 8c42e717c1093a71f77178a844a43487d1d68b6c Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Thu, 15 Oct 2020 16:46:19 +0200 Subject: [PATCH] Change settings on toggle Signed-off-by: Andrea Maria Piana --- src/status_im/chat/models.cljs | 2 +- src/status_im/mailserver/core.cljs | 38 +++++++++++++++++++++---- src/status_im/mailserver/core_test.cljs | 13 ++++++--- 3 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/status_im/chat/models.cljs b/src/status_im/chat/models.cljs index cbbc751a8e..89bacf9408 100644 --- a/src/status_im/chat/models.cljs +++ b/src/status_im/chat/models.cljs @@ -170,7 +170,7 @@ :group-chat true :contacts #{} :public? true - :might-have-join-time-messages? true + :might-have-join-time-messages? (get-in cofx [:multiaccount :use-mailservers?]) :unviewed-messages-count 0 :loaded-unviewed-messages-ids #{}} nil)) diff --git a/src/status_im/mailserver/core.cljs b/src/status_im/mailserver/core.cljs index 54d946c306..892b16e397 100644 --- a/src/status_im/mailserver/core.cljs +++ b/src/status_im/mailserver/core.cljs @@ -100,7 +100,7 @@ payload (.stringify js/JSON (clj->js args))] (status/call-private-rpc payload (handlers/response-handler - #(log/debug "mailserver: remove-peer success" %) + #(log/info "mailserver: remove-peer success" %) #(log/error "mailserver: remove-peer error" %))))) (re-frame/reg-fx @@ -214,6 +214,21 @@ current-request [:from :to :force-to? :topics :chat-id]))) +(fx/defn disconnect-from-mailserver + [{:keys [db] :as cofx}] + (let [{:keys [address]} (fetch-current db) + {:keys [peers-summary]} db + gap-request? (executing-gap-request? db)] + {:db (cond-> (dissoc db :mailserver/current-request) + gap-request? + (-> (assoc :mailserver/fetching-gaps-in-progress {}) + (dissoc :mailserver/planned-gap-requests))) + + :mailserver/remove-peer address})) + +(defn fetch-use-mailservers? [{:keys [db]}] + (get-in db [:multiaccount :use-mailservers?])) + (fx/defn connect-to-mailserver "Add mailserver as a peer using `::add-peer` cofx and generate sym-key when it doesn't exists @@ -226,7 +241,7 @@ [{:keys [db] :as cofx}] (let [{:keys [address]} (fetch-current db) {:keys [peers-summary]} db - use-mailservers? (get-in db [:multiaccount :use-mailservers?]) + use-mailservers? (fetch-use-mailservers? cofx) added? (registered-peer? peers-summary address) gap-request? (executing-gap-request? db)] (fx/merge cofx @@ -449,6 +464,7 @@ (when (and (:filters/initialized db) (mobile-network-utils/syncing-allowed? cofx) + (fetch-use-mailservers? cofx) (not (:mailserver/current-request db))) (when-let [mailserver (get-mailserver-when-ready cofx)] (let [request-to (or (:mailserver/request-to db) @@ -494,13 +510,22 @@ (fx/defn update-use-mailservers [cofx use-mailservers?] - (multiaccounts.update/multiaccount-update cofx :use-mailservers? use-mailservers? {})) + (fx/merge cofx + (multiaccounts.update/multiaccount-update :use-mailservers? use-mailservers? {}) + (if use-mailservers? + (connect-to-mailserver) + (disconnect-from-mailserver)))) (fx/defn change-mailserver "mark mailserver status as `:error` if custom mailserver is used otherwise try to reconnect to another mailserver" [{:keys [db] :as cofx}] - (when-not (zero? (:peers-count db)) + (when (and (fetch-use-mailservers? cofx) + ;; For some reason the tests are checking + ;; for non-zero, so nil value is ok, not + ;; sure is intentional, but will leave it as it is + ;; instead of using pos? + (not (zero? (:peers-count db)))) (if-let [preferred-mailserver (preferred-mailserver-id db)] (let [current-fleet (node/current-fleet-key db)] {:db @@ -532,7 +557,8 @@ ;; check if logged into multiaccount (when (contains? db :multiaccount) (let [last-connection-attempt (:mailserver/last-connection-attempt db)] - (when (<= (- now last-connection-attempt)) + (when (and (fetch-use-mailservers? cofx) + (<= (- now last-connection-attempt))) (fx/merge cofx (when (not= :connected (:mailserver/state db)) (change-mailserver))))))) @@ -1132,7 +1158,7 @@ pinned-mailservers (get-in db [:multiaccount :pinned-mailservers]) ;; Check if previous mailserver was pinned pinned? (get pinned-mailservers current-fleet) - use-mailservers? (get-in db [:multiaccount :use-mailservers?])] + use-mailservers? (fetch-use-mailservers? cofx)] (fx/merge cofx {:db (assoc db :mailserver/current-id mailserver-id) :mailserver/remove-peer address} diff --git a/src/status_im/mailserver/core_test.cljs b/src/status_im/mailserver/core_test.cljs index 03484fe7d2..3327def661 100644 --- a/src/status_im/mailserver/core_test.cljs +++ b/src/status_im/mailserver/core_test.cljs @@ -35,12 +35,14 @@ (deftest change-mailserver (testing "we are offline" (testing "it does not change mailserver" - (is (not (mailserver/change-mailserver {:db {:peers-count 0}}))))) + (is (not (mailserver/change-mailserver {:db {:multiaccount {:use-mailservers? true} + :peers-count 0}}))))) (testing "we are online" (testing "there's a preferred mailserver" (testing "it shows the popup" (is (:ui/show-confirmation (mailserver/change-mailserver {:db {:multiaccount {:fleet :staging + :use-mailservers? true :pinned-mailservers {:staging "id"}} :peers-count 1}}))))) (testing "there's not a preferred mailserver" @@ -49,12 +51,14 @@ (get-in (mailserver/change-mailserver {:db {:mailserver/mailservers {:staging {:a "b"}} - :multiaccount {:fleet :staging} + :multiaccount {:use-mailservers? true + :fleet :staging} :peers-count 1}}) [::json-rpc/call 0 :method])))) (testing "it does not show the popup" (is (not (:ui/show-confirmation (mailserver/change-mailserver - {:db {:peers-count 1}})))))))) + {:db {:multiaccount {:use-mailservers? true} + :peers-count 1}})))))))) (deftest test-registered-peer? (testing "Peer is registered" @@ -355,7 +359,8 @@ (testing "it changes mailserver" (is (= "mailservers_ping" (-> (mailserver/resend-request - {:db {:mailserver/current-request + {:db {:multiaccount {:use-mailservers? true} + :mailserver/current-request {:attempts constants/maximum-number-of-attempts}}} {}) ::json-rpc/call