Change settings on toggle

Signed-off-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
This commit is contained in:
Andrea Maria Piana 2020-10-15 16:46:19 +02:00
parent cf0cd283a6
commit 8c42e717c1
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
3 changed files with 42 additions and 11 deletions

View File

@ -170,7 +170,7 @@
:group-chat true :group-chat true
:contacts #{} :contacts #{}
:public? true :public? true
:might-have-join-time-messages? true :might-have-join-time-messages? (get-in cofx [:multiaccount :use-mailservers?])
:unviewed-messages-count 0 :unviewed-messages-count 0
:loaded-unviewed-messages-ids #{}} :loaded-unviewed-messages-ids #{}}
nil)) nil))

View File

@ -100,7 +100,7 @@
payload (.stringify js/JSON (clj->js args))] payload (.stringify js/JSON (clj->js args))]
(status/call-private-rpc payload (status/call-private-rpc payload
(handlers/response-handler (handlers/response-handler
#(log/debug "mailserver: remove-peer success" %) #(log/info "mailserver: remove-peer success" %)
#(log/error "mailserver: remove-peer error" %))))) #(log/error "mailserver: remove-peer error" %)))))
(re-frame/reg-fx (re-frame/reg-fx
@ -214,6 +214,21 @@
current-request current-request
[:from :to :force-to? :topics :chat-id]))) [: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 (fx/defn connect-to-mailserver
"Add mailserver as a peer using `::add-peer` cofx and generate sym-key when "Add mailserver as a peer using `::add-peer` cofx and generate sym-key when
it doesn't exists it doesn't exists
@ -226,7 +241,7 @@
[{:keys [db] :as cofx}] [{:keys [db] :as cofx}]
(let [{:keys [address]} (fetch-current db) (let [{:keys [address]} (fetch-current db)
{:keys [peers-summary]} 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) added? (registered-peer? peers-summary address)
gap-request? (executing-gap-request? db)] gap-request? (executing-gap-request? db)]
(fx/merge cofx (fx/merge cofx
@ -449,6 +464,7 @@
(when (and (when (and
(:filters/initialized db) (:filters/initialized db)
(mobile-network-utils/syncing-allowed? cofx) (mobile-network-utils/syncing-allowed? cofx)
(fetch-use-mailservers? cofx)
(not (:mailserver/current-request db))) (not (:mailserver/current-request db)))
(when-let [mailserver (get-mailserver-when-ready cofx)] (when-let [mailserver (get-mailserver-when-ready cofx)]
(let [request-to (or (:mailserver/request-to db) (let [request-to (or (:mailserver/request-to db)
@ -494,13 +510,22 @@
(fx/defn update-use-mailservers (fx/defn update-use-mailservers
[cofx 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 (fx/defn change-mailserver
"mark mailserver status as `:error` if custom mailserver is used "mark mailserver status as `:error` if custom mailserver is used
otherwise try to reconnect to another mailserver" otherwise try to reconnect to another mailserver"
[{:keys [db] :as cofx}] [{: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)] (if-let [preferred-mailserver (preferred-mailserver-id db)]
(let [current-fleet (node/current-fleet-key db)] (let [current-fleet (node/current-fleet-key db)]
{:db {:db
@ -532,7 +557,8 @@
;; check if logged into multiaccount ;; check if logged into multiaccount
(when (contains? db :multiaccount) (when (contains? db :multiaccount)
(let [last-connection-attempt (:mailserver/last-connection-attempt db)] (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 (fx/merge cofx
(when (not= :connected (:mailserver/state db)) (when (not= :connected (:mailserver/state db))
(change-mailserver))))))) (change-mailserver)))))))
@ -1132,7 +1158,7 @@
pinned-mailservers (get-in db [:multiaccount :pinned-mailservers]) pinned-mailservers (get-in db [:multiaccount :pinned-mailservers])
;; Check if previous mailserver was pinned ;; Check if previous mailserver was pinned
pinned? (get pinned-mailservers current-fleet) pinned? (get pinned-mailservers current-fleet)
use-mailservers? (get-in db [:multiaccount :use-mailservers?])] use-mailservers? (fetch-use-mailservers? cofx)]
(fx/merge cofx (fx/merge cofx
{:db (assoc db :mailserver/current-id mailserver-id) {:db (assoc db :mailserver/current-id mailserver-id)
:mailserver/remove-peer address} :mailserver/remove-peer address}

View File

@ -35,12 +35,14 @@
(deftest change-mailserver (deftest change-mailserver
(testing "we are offline" (testing "we are offline"
(testing "it does not change mailserver" (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 "we are online"
(testing "there's a preferred mailserver" (testing "there's a preferred mailserver"
(testing "it shows the popup" (testing "it shows the popup"
(is (:ui/show-confirmation (mailserver/change-mailserver (is (:ui/show-confirmation (mailserver/change-mailserver
{:db {:multiaccount {:fleet :staging {:db {:multiaccount {:fleet :staging
:use-mailservers? true
:pinned-mailservers {:staging "id"}} :pinned-mailservers {:staging "id"}}
:peers-count 1}}))))) :peers-count 1}})))))
(testing "there's not a preferred mailserver" (testing "there's not a preferred mailserver"
@ -49,12 +51,14 @@
(get-in (get-in
(mailserver/change-mailserver (mailserver/change-mailserver
{:db {:mailserver/mailservers {:staging {:a "b"}} {:db {:mailserver/mailservers {:staging {:a "b"}}
:multiaccount {:fleet :staging} :multiaccount {:use-mailservers? true
:fleet :staging}
:peers-count 1}}) :peers-count 1}})
[::json-rpc/call 0 :method])))) [::json-rpc/call 0 :method]))))
(testing "it does not show the popup" (testing "it does not show the popup"
(is (not (:ui/show-confirmation (mailserver/change-mailserver (is (not (:ui/show-confirmation (mailserver/change-mailserver
{:db {:peers-count 1}})))))))) {:db {:multiaccount {:use-mailservers? true}
:peers-count 1}}))))))))
(deftest test-registered-peer? (deftest test-registered-peer?
(testing "Peer is registered" (testing "Peer is registered"
@ -355,7 +359,8 @@
(testing "it changes mailserver" (testing "it changes mailserver"
(is (= "mailservers_ping" (is (= "mailservers_ping"
(-> (mailserver/resend-request (-> (mailserver/resend-request
{:db {:mailserver/current-request {:db {:multiaccount {:use-mailservers? true}
:mailserver/current-request
{:attempts constants/maximum-number-of-attempts}}} {:attempts constants/maximum-number-of-attempts}}}
{}) {})
::json-rpc/call ::json-rpc/call