Move gaps to status-go

This commit moves gaps to status-go.

Signed-off-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
This commit is contained in:
Andrea Maria Piana 2019-09-04 11:41:05 +02:00
parent ef36ece15b
commit e7fa010088
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
10 changed files with 96 additions and 89 deletions

View File

@ -443,7 +443,6 @@ class StatusModule extends ReactContextBaseJavaModule implements LifecycleEventL
@Override
public void run() {
String result = Statusgo.loginWithKeycard(whisperPrivateKey, encryptionPublicKey, configJSON);
callback.invoke(result);
}
};

View File

@ -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?])

View File

@ -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]

View File

@ -63,6 +63,10 @@
"browsers_getBrowsers" {}
"browsers_addBrowser" {}
"browsers_deleteBrowser" {}
"mailservers_getMailserverRequestGaps" {}
"mailservers_addMailserverRequestGaps" {}
"mailservers_deleteMailserverRequestGaps" {}
"mailservers_deleteMailserverRequestGapsByChatID" {}
"permissions_addDappPermissions" {}
"permissions_getDappPermissions" {}
"permissions_deleteDappPermissions" {}

View File

@ -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

View File

@ -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))))

View File

@ -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

View File

@ -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

View File

@ -3,6 +3,6 @@
"owner": "status-im",
"repo": "status-go",
"version": "develop",
"commit-sha1": "42199e682fdf90a82e59451a58b9ae07749a8e12",
"src-sha256": "11ykhcx057l6s0zg5cansv5x8728k0j86ygsj12m8ax86xjkdzyc"
"commit-sha1": "b27779aa4e202a13a387897332dea377bf9b6552",
"src-sha256": "0yab9pjsg9583b7x89ywbbyxxw4fy03dyw260ybjb9g26z166064"
}

View File

@ -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"]