From e7fa0100885583495bb64d7e03f73780b729f421 Mon Sep 17 00:00:00 2001 From: Andrea Maria Piana Date: Wed, 4 Sep 2019 11:41:05 +0200 Subject: [PATCH] Move gaps to status-go This commit moves gaps to status-go. Signed-off-by: Andrea Maria Piana --- .../status/ethereum/module/StatusModule.java | 1 - src/status_im/chat/models/loading.cljs | 6 +- src/status_im/data_store/mailservers.cljs | 70 ++++++++-------- src/status_im/ethereum/json_rpc.cljs | 4 + src/status_im/events.cljs | 5 +- src/status_im/mailserver/core.cljs | 82 ++++++++++--------- src/status_im/node/core.cljs | 1 + src/status_im/subs.cljs | 10 +-- status-go-version.json | 4 +- test/cljs/status_im/test/chat/models.cljs | 2 +- 10 files changed, 96 insertions(+), 89 deletions(-) diff --git a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusModule.java b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusModule.java index 6a63d71209..a40015e38e 100644 --- a/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusModule.java +++ b/modules/react-native-status/android/src/main/java/im/status/ethereum/module/StatusModule.java @@ -443,7 +443,6 @@ class StatusModule extends ReactContextBaseJavaModule implements LifecycleEventL @Override public void run() { String result = Statusgo.loginWithKeycard(whisperPrivateKey, encryptionPublicKey, configJSON); - callback.invoke(result); } }; diff --git a/src/status_im/chat/models/loading.cljs b/src/status_im/chat/models/loading.cljs index 0628deefe8..522a286910 100644 --- a/src/status_im/chat/models/loading.cljs +++ b/src/status_im/chat/models/loading.cljs @@ -84,8 +84,7 @@ (fx/defn messages-loaded "Loads more messages for current chat" - {:events [::messages-loaded] - :interceptors [(re-frame/inject-cofx :data-store/all-gaps)]} + {:events [::messages-loaded]} [{{:keys [current-chat-id] :as db} :db :as cofx} chat-id {:keys [cursor messages]}] @@ -113,11 +112,10 @@ (assoc-in [:chats current-chat-id :cursor] cursor) (assoc-in [:chats current-chat-id :all-loaded?] (empty? cursor)))} - (mailserver/load-gaps current-chat-id) (group-chat-messages current-chat-id new-messages) (chat-model/mark-messages-seen current-chat-id))))) -(defn load-more-messages +(fx/defn load-more-messages [{:keys [db]}] (when-let [current-chat-id (:current-chat-id db)] (when-not (get-in db [:chats current-chat-id :all-loaded?]) diff --git a/src/status_im/data_store/mailservers.cljs b/src/status_im/data_store/mailservers.cljs index d38c4ca927..533206682b 100644 --- a/src/status_im/data_store/mailservers.cljs +++ b/src/status_im/data_store/mailservers.cljs @@ -2,6 +2,8 @@ (:require [cljs.tools.reader.edn :as edn] [re-frame.core :as re-frame] [taoensso.timbre :as log] + [status-im.utils.fx :as fx] + [status-im.ethereum.json-rpc :as json-rpc] [status-im.data-store.realm.core :as core])) (re-frame/reg-cofx @@ -14,6 +16,39 @@ (core/get-all :mailserver) (core/all-clj :mailserver)))))) +(defn mailserver-request-gaps->rpc [{:keys [chat-id] :as gap}] + (-> gap + (assoc :chatId chat-id) + (dissoc :chat-id))) + +(fx/defn load-gaps [cofx chat-id success-fn] + {::json-rpc/call [{:method "mailservers_getMailserverRequestGaps" + :params [chat-id] + :on-success #(let [indexed-gaps (reduce (fn [acc {:keys [id] :as g}] + (assoc acc id g)) + {} + %)] + (success-fn chat-id indexed-gaps)) + :on-failure #(log/error "failed to fetch gaps" %)}]}) + +(fx/defn save-gaps [cofx gaps] + {::json-rpc/call [{:method "mailservers_addMailserverRequestGaps" + :params [(map mailserver-request-gaps->rpc gaps)] + :on-success #(log/info "saved gaps successfully") + :on-failure #(log/error "failed to save gap" %)}]}) + +(fx/defn delete-gaps [cofx ids] + {::json-rpc/call [{:method "mailservers_deleteMailserverRequestGaps" + :params [ids] + :on-success #(log/info "deleted gaps successfully") + :on-failure #(log/error "failed to delete gap" %)}]}) + +(fx/defn delete-gaps-by-chat-id [cofx chat-id] + {::json-rpc/call [{:method "mailservers_deleteMailserverRequestGapsByChatID" + :params [chat-id] + :on-success #(log/info "deleted gaps successfully") + :on-failure #(log/error "failed to delete gap" %)}]}) + (defn save-tx "Returns tx function for saving a mailserver" [{:keys [id] :as mailserver}] @@ -84,41 +119,6 @@ (core/get-all :chat-requests-range) (core/all-clj :chat-requests-range)))))) -(re-frame/reg-cofx - :data-store/all-gaps - (fn [cofx _] - (assoc cofx - :data-store/all-gaps - (fn [chat-id] - (reduce (fn [acc {:keys [id] :as gap}] - (assoc acc id gap)) - {} - (-> @core/account-realm - (core/get-by-field :mailserver-requests-gap :chat-id chat-id) - (core/all-clj :mailserver-requests-gap))))))) - -(defn save-mailserver-requests-gap - [gap] - (fn [realm] - (log/debug "saving gap" gap) - (core/create realm :mailserver-requests-gap gap true))) - -(defn delete-mailserver-requests-gaps - [ids] - (fn [realm] - (log/debug "deleting gaps" ids) - (doseq [id ids] - (core/delete - realm - (core/get-by-field realm :mailserver-requests-gap :id id))))) - -(defn delete-all-gaps-by-chat - [chat-id] - (fn [realm] - (log/debug "deleting all gaps for chat" chat-id) - (core/delete realm - (core/get-by-field realm :mailserver-requests-gap :chat-id chat-id)))) - (defn delete-range [chat-id] (fn [realm] diff --git a/src/status_im/ethereum/json_rpc.cljs b/src/status_im/ethereum/json_rpc.cljs index 7b382fc159..abfb8845b7 100644 --- a/src/status_im/ethereum/json_rpc.cljs +++ b/src/status_im/ethereum/json_rpc.cljs @@ -63,6 +63,10 @@ "browsers_getBrowsers" {} "browsers_addBrowser" {} "browsers_deleteBrowser" {} + "mailservers_getMailserverRequestGaps" {} + "mailservers_addMailserverRequestGaps" {} + "mailservers_deleteMailserverRequestGaps" {} + "mailservers_deleteMailserverRequestGapsByChatID" {} "permissions_addDappPermissions" {} "permissions_getDappPermissions" {} "permissions_deleteDappPermissions" {} diff --git a/src/status_im/events.cljs b/src/status_im/events.cljs index c49db3424b..06c05f854c 100644 --- a/src/status_im/events.cljs +++ b/src/status_im/events.cljs @@ -547,7 +547,10 @@ (handlers/register-handler-fx :chat.ui/load-more-messages (fn [cofx _] - (chat.loading/load-more-messages cofx))) + (let [chat-id (get-in cofx [:db :current-chat-id])] + (fx/merge cofx + (chat.loading/load-more-messages) + (mailserver/load-gaps-fx chat-id))))) (handlers/register-handler-fx :chat.ui/start-chat diff --git a/src/status_im/mailserver/core.cljs b/src/status_im/mailserver/core.cljs index 8fdbd6de6d..30ab4146b9 100644 --- a/src/status_im/mailserver/core.cljs +++ b/src/status_im/mailserver/core.cljs @@ -492,9 +492,10 @@ (connect-to-mailserver)))) (fx/defn remove-gaps - [{:keys [db]} chat-id] - {:db (update db :mailserver/gaps dissoc chat-id) - :data-store/tx [(data-store.mailservers/delete-all-gaps-by-chat chat-id)]}) + [{:keys [db] :as cofx} chat-id] + (fx/merge cofx + {:db (update db :mailserver/gaps dissoc chat-id)} + (data-store.mailservers/delete-gaps-by-chat-id chat-id))) (fx/defn remove-range [{:keys [db]} chat-id] @@ -654,7 +655,7 @@ chat-ids)) (fx/defn update-gaps - [{:keys [db]}] + [{:keys [db] :as cofx}] (let [{:keys [topics] :as request} (get db :mailserver/current-request) chat-ids (into #{} (comp @@ -668,26 +669,22 @@ ranges (:mailserver/ranges db) prepared-new-gaps (prepare-new-gaps new-gaps ranges request chat-ids)] - {:db - (reduce (fn [db chat-id] - (let [chats-deleted-gaps (get deleted-gaps chat-id) - chats-updated-gaps (merge (get updated-gaps chat-id) - (get prepared-new-gaps chat-id))] - (update-in db [:mailserver/gaps chat-id] - (fn [chat-gaps] - (-> (apply dissoc chat-gaps chats-deleted-gaps) - (merge chats-updated-gaps)))))) - db - chat-ids) - - :data-store/tx - (conj - (map - data-store.mailservers/save-mailserver-requests-gap - (concat (mapcat vals (vals updated-gaps)) - (mapcat vals (vals prepared-new-gaps)))) - (data-store.mailservers/delete-mailserver-requests-gaps - (mapcat val deleted-gaps)))})) + (fx/merge cofx + {:db + (reduce (fn [db chat-id] + (let [chats-deleted-gaps (get deleted-gaps chat-id) + chats-updated-gaps (merge (get updated-gaps chat-id) + (get prepared-new-gaps chat-id))] + (update-in db [:mailserver/gaps chat-id] + (fn [chat-gaps] + (-> (apply dissoc chat-gaps chats-deleted-gaps) + (merge chats-updated-gaps)))))) + db + chat-ids)} + (data-store.mailservers/delete-gaps (mapcat val deleted-gaps)) + (data-store.mailservers/save-gaps + (concat (mapcat vals (vals updated-gaps)) + (mapcat vals (vals prepared-new-gaps))))))) (fx/defn update-chats-and-gaps [cofx cursor] @@ -1068,20 +1065,25 @@ {::data-store.mailservers/all-chat-requests-ranges #(re-frame/dispatch [::ranges-loaded %])}) -(fx/defn load-gaps - [{:keys [db now :data-store/all-gaps]} chat-id] +(fx/defn load-gaps-fx [{:keys [db] :as cofx} chat-id] (when-not (get-in db [:chats chat-id :gaps-loaded?]) - (let [now-s (quot now 1000) - gaps (all-gaps chat-id) - outdated-gaps (into [] (comp (filter #(< (:to %) - (- now-s constants/max-gaps-range))) - (map :id)) - (vals gaps)) - gaps (apply dissoc gaps outdated-gaps)] - {:db - (-> db - (assoc-in [:chats chat-id :gaps-loaded?] true) - (assoc-in [:mailserver/gaps chat-id] gaps)) - :data-store/tx - [(data-store.mailservers/delete-mailserver-requests-gaps - outdated-gaps)]}))) + (let [success-fn #(re-frame/dispatch [::gaps-loaded %1 %2])] + (data-store.mailservers/load-gaps cofx chat-id success-fn)))) + +(fx/defn load-gaps + {:events [::gaps-loaded]} + [{:keys [db now] :as cofx} chat-id gaps] + (let [now-s (quot now 1000) + outdated-gaps (into [] (comp (filter #(< (:to %) + (- now-s constants/max-gaps-range))) + (map :id)) + (vals gaps)) + gaps (apply dissoc gaps outdated-gaps)] + (fx/merge + cofx + {:db + (-> db + (assoc-in [:chats chat-id :gaps-loaded?] true) + (assoc-in [:mailserver/gaps chat-id] gaps))} + + (data-store.mailservers/delete-gaps outdated-gaps)))) diff --git a/src/status_im/node/core.cljs b/src/status_im/node/core.cljs index d91c563a26..ed16598446 100644 --- a/src/status_im/node/core.cljs +++ b/src/status_im/node/core.cljs @@ -119,6 +119,7 @@ (assoc :WalletConfig {:Enabled true} :BrowsersConfig {:Enabled true} :PermissionsConfig {:Enabled true} + :MailserversConfig {:Enabled true} :WhisperConfig {:Enabled true :LightClient true :MinimumPoW 0.001 diff --git a/src/status_im/subs.cljs b/src/status_im/subs.cljs index e1c173f57d..d8bed0cafe 100644 --- a/src/status_im/subs.cljs +++ b/src/status_im/subs.cljs @@ -1596,11 +1596,11 @@ (re-frame/reg-sub :mailserver/connected? :<- [:mailserver/state] - :<- [:network-status] - (fn [[mail-state network-status]] - (let [connected? (= :connected mail-state) - online? (= :online network-status)] - (and connected? online?)))) + :<- [:disconnected?] + (fn [[mail-state disconnected?]] + (let [mailserver-connected? (= :connected mail-state)] + (and mailserver-connected? + (not disconnected?))))) (re-frame/reg-sub :mailserver/preferred-id diff --git a/status-go-version.json b/status-go-version.json index ca17958c43..91587fa836 100644 --- a/status-go-version.json +++ b/status-go-version.json @@ -3,6 +3,6 @@ "owner": "status-im", "repo": "status-go", "version": "develop", - "commit-sha1": "42199e682fdf90a82e59451a58b9ae07749a8e12", - "src-sha256": "11ykhcx057l6s0zg5cansv5x8728k0j86ygsj12m8ax86xjkdzyc" + "commit-sha1": "b27779aa4e202a13a387897332dea377bf9b6552", + "src-sha256": "0yab9pjsg9583b7x89ywbbyxxw4fy03dyw260ybjb9g26z166064" } diff --git a/test/cljs/status_im/test/chat/models.cljs b/test/cljs/status_im/test/chat/models.cljs index 58dd13d3f7..51303fb715 100644 --- a/test/cljs/status_im/test/chat/models.cljs +++ b/test/cljs/status_im/test/chat/models.cljs @@ -116,7 +116,7 @@ (testing "it adds the relevant transactions for realm" (let [actual (chat/remove-chat cofx chat-id)] (is (:data-store/tx actual)) - (is (= 2 (count (:data-store/tx actual)))))))) + (is (= 1 (count (:data-store/tx actual)))))))) (deftest multi-user-chat? (let [chat-id "1"]