diff --git a/src/status_im/transport/inbox.cljs b/src/status_im/transport/inbox.cljs index b819aacd1f..65d4b2be8b 100644 --- a/src/status_im/transport/inbox.cljs +++ b/src/status_im/transport/inbox.cljs @@ -100,28 +100,37 @@ (def one-day (* 24 3600)) (def seven-days (* 7 one-day)) +(defn request-inbox-messages-params [mailserver from to topics] + (let [days (conj + (into [] (range from to one-day)) + to) + day-ranges (map vector days (drop 1 days))] + (for [topic topics + [current-from current-to] day-ranges] + {:topic topic + :mailServerPeer (:address mailserver) + :symKeyID (:sym-key-id mailserver) + :from current-from + :to current-to}))) + (defn request-inbox-messages - [web3 wnode topics from to success-fn error-fn] - (loop [from from - current-to to] - (let [current-to (if (> (- to from) one-day) - (+ from one-day) - to) - opts (merge {:mailServerPeer (:address wnode) - :symKeyID (:sym-key-id wnode) - :from from - :to current-to})] - (log/info "offline inbox: request-messages request for topics " topics " from " from " to " current-to) - (doseq [topic topics] - (let [opts (assoc opts :topic topic)] - (.requestMessages (transport.utils/shh web3) - (clj->js opts) - (fn [err resp] - (if-not err - (success-fn resp topic) - (error-fn err topic)))))) - (when (< current-to to) - (recur current-to to))))) + [web3 mailserver topics start-from end-to success-fn error-fn] + (log/info "offline inbox: request-messages request for topics " topics " from " start-from " to " end-to) + (doseq [{:keys [topic] :as params} (request-inbox-messages-params + mailserver + start-from + end-to + topics)] + (log/info "offline inbox: request-messages for: " + " topic " topic + " from " (:from params) + " to " (:to params)) + (.requestMessages (transport.utils/shh web3) + (clj->js params) + (fn [err resp] + (if-not err + (success-fn resp topic) + (error-fn err topic)))))) (re-frame/reg-fx ::add-peer diff --git a/test/cljs/status_im/test/transport/inbox.cljs b/test/cljs/status_im/test/transport/inbox.cljs index 959f9dab6b..ec4f8bfeef 100644 --- a/test/cljs/status_im/test/transport/inbox.cljs +++ b/test/cljs/status_im/test/transport/inbox.cljs @@ -103,3 +103,59 @@ (testing "inbox is not ready" (testing "it does not do anything" (is (nil? (inbox/request-messages {}))))))) + +(deftest request-messages-params + (let [mailserver {:address "peer" + :sym-key-id "id"}] + (testing "from is greater that to" + (testing "it returns an empty sequence" + (is (empty? (inbox/request-inbox-messages-params mailserver 2 0 ["a" "b" "c"]))))) + (testing "from is equal to to" + (testing "it returns an empty sequence" + (is (empty? (inbox/request-inbox-messages-params mailserver 2 2 ["a" "b" "c"]))))) + (testing "to is less than the step" + (is (= #{{:topic "a" + :mailServerPeer "peer" + :symKeyID "id" + :from 0 + :to 3} + {:topic "b" + :mailServerPeer "peer" + :symKeyID "id" + :from 0 + :to 3}} + (into #{} (inbox/request-inbox-messages-params mailserver 0 3 ["a" "b"]))))) + (testing "to is equal the step" + (is (= #{{:topic "a" + :mailServerPeer "peer" + :symKeyID "id" + :from 0 + :to 86400} + {:topic "b" + :mailServerPeer "peer" + :symKeyID "id" + :from 0 + :to 86400}} + (into #{} (inbox/request-inbox-messages-params mailserver 0 86400 ["a" "b"]))))) + (testing "to is greather than the step" + (is (= #{{:topic "a" + :mailServerPeer "peer" + :symKeyID "id" + :from 0 + :to 86400} + {:topic "b" + :mailServerPeer "peer" + :symKeyID "id" + :from 0 + :to 86400} + {:topic "a" + :mailServerPeer "peer" + :symKeyID "id" + :from 86400 + :to 90000} + {:topic "b" + :mailServerPeer "peer" + :symKeyID "id" + :from 86400 + :to 90000}} + (into #{} (inbox/request-inbox-messages-params mailserver 0 90000 ["a" "b"])))))))