From 40b3100cfc30289be70b98e4c2797e29ffcc07a3 Mon Sep 17 00:00:00 2001 From: Pedro Pombeiro Date: Thu, 31 Jan 2019 13:32:06 +0100 Subject: [PATCH] Fetch messages from mailserver upon PN if db is unlocked. Part of #3451 Signed-off-by: Pedro Pombeiro --- src/status_im/events.cljs | 7 ++++++- src/status_im/mailserver/core.cljs | 6 ++++-- src/status_im/notifications/core.cljs | 29 ++++++++++++++++----------- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/status_im/events.cljs b/src/status_im/events.cljs index 1364d90c17..1c98b30d40 100644 --- a/src/status_im/events.cljs +++ b/src/status_im/events.cljs @@ -403,6 +403,11 @@ (fn [cofx _] (mailserver/check-connection cofx))) +(handlers/register-handler-fx + :mailserver/fetch-history + (fn [cofx [_ chat-id from-timestamp]] + (mailserver/fetch-history cofx chat-id from-timestamp))) + (handlers/register-handler-fx :mailserver.callback/generate-mailserver-symkey-success (fn [cofx [_ mailserver sym-key-id]] @@ -658,7 +663,7 @@ (handlers/register-handler-fx :chat.ui/fetch-history-pressed (fn [cofx [_ chat-id]] - (mailserver/fetch-history cofx chat-id))) + (mailserver/fetch-history cofx chat-id 1))) (handlers/register-handler-fx :chat.ui/remove-chat-pressed diff --git a/src/status_im/mailserver/core.cljs b/src/status_im/mailserver/core.cljs index b10364306d..90a9fffb86 100644 --- a/src/status_im/mailserver/core.cljs +++ b/src/status_im/mailserver/core.cljs @@ -550,15 +550,17 @@ :data-store/tx [(data-store.mailservers/save-mailserver-topic-tx {:topic topic :mailserver-topic mailserver-topic})]})))) + (fx/defn fetch-history - [{:keys [db] :as cofx} chat-id] + [{:keys [db] :as cofx} chat-id from-timestamp] + (log/debug "fetch-history" "chat-id:" chat-id "from-timestamp:" from-timestamp) (let [public-key (accounts.db/current-public-key cofx) topic (or (get-in db [:transport/chats chat-id :topic]) (transport.topic/public-key->discovery-topic-hash public-key)) {:keys [chat-ids last-request] :as current-mailserver-topic} (get-in db [:mailserver/topics topic] {:chat-ids #{}})] (let [mailserver-topic (-> current-mailserver-topic - (assoc :last-request 1))] + (assoc :last-request (min from-timestamp last-request)))] (fx/merge cofx {:db (assoc-in db [:mailserver/topics topic] mailserver-topic) :data-store/tx [(data-store.mailservers/save-mailserver-topic-tx diff --git a/src/status_im/notifications/core.cljs b/src/status_im/notifications/core.cljs index b403f4dc21..b73d9e83c8 100644 --- a/src/status_im/notifications/core.cljs +++ b/src/status_im/notifications/core.cljs @@ -18,6 +18,7 @@ (def ^:private pn-message-id-hash-length 10) (def ^:private pn-pubkey-hash-length 10) (def ^:private pn-pubkey-length 132) +(def ^:private pull-recent-messages-window (* 15 60)) (when-not platform/desktop? @@ -217,7 +218,7 @@ (vals (:chats db))))) (fx/defn handle-on-message - [{:keys [db] :as cofx} decoded-payload {:keys [force]}] + [{:keys [db now] :as cofx} decoded-payload {:keys [force]}] (let [view-id (:view-id db) current-chat-id (:current-chat-id db) app-state (:app-state db) @@ -226,17 +227,21 @@ (log/debug "handle-on-message" "app-state:" app-state "view-id:" view-id "current-chat-id:" current-chat-id "from:" from "force:" force) - (when (or force - (and - (not= app-state "active") - (show-notification? cofx rehydrated-payload))) - {:db - (assoc-in db [:push-notifications/stored (:to rehydrated-payload)] - (js/JSON.stringify (clj->js rehydrated-payload))) - :notifications/display-notification - {:title (get-contact-name cofx from) - :body (i18n/label :notifications-new-message-body) - :decoded-payload rehydrated-payload}}))) + (merge + (when (and (= (.-length from) pn-pubkey-length) + (show-notification? cofx rehydrated-payload)) + {:dispatch [:mailserver/fetch-history from (- (quot now 1000) pull-recent-messages-window)]}) + (when (or force + (and + (not= app-state "active") + (show-notification? cofx rehydrated-payload))) + {:db + (assoc-in db [:push-notifications/stored (:to rehydrated-payload)] + (js/JSON.stringify (clj->js rehydrated-payload))) + :notifications/display-notification + {:title (get-contact-name cofx from) + :body (i18n/label :notifications-new-message-body) + :decoded-payload rehydrated-payload}})))) (fx/defn handle-push-notification-open [{:keys [db] :as cofx} decoded-payload {:keys [stored?] :as ctx}]