Request offline message when app returns from background

* Add "connecting" mailserver status
* Send push notification when "envelope.sent" signal received

Signed-off-by: Dmitry Novotochinov <trybeee@gmail.com>
This commit is contained in:
Dmitry Novotochinov 2018-05-07 11:12:26 +03:00
parent 7a5467c3b1
commit 428f1d47ee
No known key found for this signature in database
GPG Key ID: 267674DCC86628D9
6 changed files with 46 additions and 24 deletions

View File

@ -105,8 +105,11 @@
[re-frame/trim-v]
(fn [{:keys [db] :as cofx} [envelope-hash status]]
(let [{:keys [chat-id message-id]} (get-in db [:transport/message-envelopes envelope-hash])
message (get-in db [:chats chat-id :messages message-id])]
(models.message/update-message-status message status cofx))))
message (get-in db [:chats chat-id :messages message-id])
{:keys [fcm-token]} (get-in db [:contacts/contacts chat-id])]
(handlers-macro/merge-fx cofx
(models.message/update-message-status message status)
(models.message/send-push-notification fcm-token status)))))
;; Change status of messages which are still in "sending" status to "not-sent"
;; (If signal from status-go has not been received)

View File

@ -169,19 +169,13 @@
(defn- send
[chat-id message-id send-record {{:contacts/keys [contacts] :keys [network-status current-public-key]} :db :as cofx}]
(let [{:keys [dapp? fcm-token]} (get contacts chat-id)]
(let [{:keys [dapp?]} (get contacts chat-id)]
(if dapp?
(send-dapp-message! cofx chat-id send-record)
(if (= network-status :offline)
{:dispatch-later [{:ms 10000
:dispatch [:update-message-status chat-id message-id current-public-key :not-sent]}]}
(if fcm-token
(handlers-macro/merge-fx cofx
{:send-notification {:message "message"
:payload {:title "Status" :body "You have a new message"}
:tokens [fcm-token]}}
(transport/send send-record chat-id))
(transport/send send-record chat-id cofx))))))
(transport/send send-record chat-id cofx)))))
(defn add-message-type [message {:keys [chat-id group-chat public?]}]
(cond-> message
@ -217,6 +211,12 @@
(add-message chat-id message-with-id true)
(send chat-id message-id send-record))))
(defn send-push-notification [fcm-token status cofx]
(when (and fcm-token (= status :sent))
{:send-notification {:message "message"
:payload {:title "Status" :body "You have a new message"}
:tokens [fcm-token]}}))
(defn update-message-status [{:keys [chat-id message-id from] :as message} status {:keys [db]}]
(let [updated-message (assoc-in message [:user-statuses from] status)]
{:db (assoc-in db [:chats chat-id :messages message-id] updated-message)

View File

@ -13,6 +13,9 @@
[status-im.utils.instabug :as instabug]
[status-im.utils.snoopy :as snoopy]))
(defn app-state-change-handler [state]
(dispatch [:app-state-change state]))
(defn app-root []
(let [keyboard-height (subscribe [:get :keyboard-height])]
(reagent/create-class
@ -29,14 +32,16 @@
"keyboardWillHide"
#(when-not (= 0 @keyboard-height)
(dispatch [:set :keyboard-height 0])))
(.hide react/splash-screen))
(.hide react/splash-screen)
(.addEventListener react/app-state "change" app-state-change-handler))
:component-did-mount
(fn []
(notifications/on-refresh-fcm-token)
(notifications/on-notification))
:component-will-unmount
(fn []
(.stop react/http-bridge))
(.stop react/http-bridge)
(.removeEventListener react/app-state "change" app-state-change-handler))
:display-name "root"
:reagent-render views/main})))

View File

@ -28,13 +28,13 @@
(handlers/register-handler-fx
::update-connection-status
[re-frame/trim-v]
(fn [{:keys [db] :as cofx} [is-connected?]]
(let [previous-status (:network-status db)
back-online? (and (= previous-status :offline)
is-connected?)]
(fn [{{:keys [network-status mailserver-status] :as db} :db :as cofx} [is-connected?]]
(let [should-recover? (and (= network-status :offline)
is-connected?
(not= mailserver-status :connecting))]
(cond-> (handlers-macro/merge-fx cofx
{:db (assoc db :network-status (if is-connected? :online :offline))}
(inbox/recover-offline-inbox back-online?))
(inbox/recover-offline-inbox should-recover?))
is-connected?
(assoc :drain-mixpanel-events nil)))))

View File

@ -64,18 +64,20 @@
(log/info "offline inbox: initialize " wnode)
(when wnode
{:async-flow (initialize-offline-inbox-flow)
:db (assoc db :mailserver-status :connecting)
::add-peer {:wnode wnode}}))))
(defn recover-offline-inbox
"Recover offline inbox connection after being offline because of connectivity loss"
[back-online? {:keys [db]}]
(when config/offline-inbox-enabled?
[should-recover? {:keys [db]}]
(when (and config/offline-inbox-enabled?
should-recover?)
(let [wnode (get-current-wnode-address db)]
(when (and back-online?
wnode
(when (and wnode
(:account/account db))
(log/info "offline inbox: recover" wnode)
{:async-flow (recover-offline-inbox-flow)}))))
{:db (assoc db :mailserver-status :connecting)
:async-flow (recover-offline-inbox-flow)}))))
(defn add-peer [enode success-fn error-fn]
(status/add-peer enode (response-handler error-fn success-fn)))

View File

@ -34,6 +34,7 @@
[status-im.i18n :as i18n]
[status-im.js-dependencies :as dependencies]
[status-im.transport.core :as transport]
[status-im.transport.inbox :as inbox]
[status-im.ui.screens.db :refer [app-db]]
[status-im.utils.datetime :as time]
[status-im.utils.ethereum.core :as ethereum]
@ -206,6 +207,11 @@
(fn [_]
(status/close-application)))
(re-frame/reg-fx
::app-state-change-fx
(fn [state]
(status/app-state-change state)))
;;;; Handlers
(handlers/register-handler-db
@ -407,8 +413,14 @@
(handlers/register-handler-fx
:app-state-change
(fn [_ [_ state]]
(status/app-state-change state)))
(fn [{{:keys [network-status mailserver-status]} :db :as cofx} [_ state]]
(let [app-coming-from-background? (= state "active")
should-recover? (and app-coming-from-background?
(= network-status :online)
(not= mailserver-status :connecting))]
(handlers-macro/merge-fx cofx
{::app-state-change-fx state}
(inbox/recover-offline-inbox should-recover?)))))
(handlers/register-handler-fx
:request-permissions