Fetch messages from mailserver upon PN if db is unlocked. Part of #3451

Signed-off-by: Pedro Pombeiro <pombeirp@users.noreply.github.com>
This commit is contained in:
Pedro Pombeiro 2019-01-31 13:32:06 +01:00
parent fd46113ab3
commit 40b3100cfc
No known key found for this signature in database
GPG Key ID: A65DEB11E4BBC647
3 changed files with 27 additions and 15 deletions

View File

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

View File

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

View File

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