Dont query mailserver when from is greater than to

Signed-off-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
This commit is contained in:
Andrea Maria Piana 2018-06-26 10:08:15 +02:00
parent b9148cb782
commit 42509e8fb6
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
2 changed files with 86 additions and 21 deletions

View File

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

View File

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