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] [re-frame/trim-v]
(fn [{:keys [db] :as cofx} [envelope-hash status]] (fn [{:keys [db] :as cofx} [envelope-hash status]]
(let [{:keys [chat-id message-id]} (get-in db [:transport/message-envelopes envelope-hash]) (let [{:keys [chat-id message-id]} (get-in db [:transport/message-envelopes envelope-hash])
message (get-in db [:chats chat-id :messages message-id])] message (get-in db [:chats chat-id :messages message-id])
(models.message/update-message-status message status cofx)))) {: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" ;; Change status of messages which are still in "sending" status to "not-sent"
;; (If signal from status-go has not been received) ;; (If signal from status-go has not been received)

View File

@ -169,19 +169,13 @@
(defn- send (defn- send
[chat-id message-id send-record {{:contacts/keys [contacts] :keys [network-status current-public-key]} :db :as cofx}] [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? (if dapp?
(send-dapp-message! cofx chat-id send-record) (send-dapp-message! cofx chat-id send-record)
(if (= network-status :offline) (if (= network-status :offline)
{:dispatch-later [{:ms 10000 {:dispatch-later [{:ms 10000
:dispatch [:update-message-status chat-id message-id current-public-key :not-sent]}]} :dispatch [:update-message-status chat-id message-id current-public-key :not-sent]}]}
(if fcm-token (transport/send send-record chat-id cofx)))))
(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))))))
(defn add-message-type [message {:keys [chat-id group-chat public?]}] (defn add-message-type [message {:keys [chat-id group-chat public?]}]
(cond-> message (cond-> message
@ -217,6 +211,12 @@
(add-message chat-id message-with-id true) (add-message chat-id message-with-id true)
(send chat-id message-id send-record)))) (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]}] (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)] (let [updated-message (assoc-in message [:user-statuses from] status)]
{:db (assoc-in db [:chats chat-id :messages message-id] updated-message) {: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.instabug :as instabug]
[status-im.utils.snoopy :as snoopy])) [status-im.utils.snoopy :as snoopy]))
(defn app-state-change-handler [state]
(dispatch [:app-state-change state]))
(defn app-root [] (defn app-root []
(let [keyboard-height (subscribe [:get :keyboard-height])] (let [keyboard-height (subscribe [:get :keyboard-height])]
(reagent/create-class (reagent/create-class
@ -29,14 +32,16 @@
"keyboardWillHide" "keyboardWillHide"
#(when-not (= 0 @keyboard-height) #(when-not (= 0 @keyboard-height)
(dispatch [:set :keyboard-height 0]))) (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 :component-did-mount
(fn [] (fn []
(notifications/on-refresh-fcm-token) (notifications/on-refresh-fcm-token)
(notifications/on-notification)) (notifications/on-notification))
:component-will-unmount :component-will-unmount
(fn [] (fn []
(.stop react/http-bridge)) (.stop react/http-bridge)
(.removeEventListener react/app-state "change" app-state-change-handler))
:display-name "root" :display-name "root"
:reagent-render views/main}))) :reagent-render views/main})))

View File

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

View File

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

View File

@ -34,6 +34,7 @@
[status-im.i18n :as i18n] [status-im.i18n :as i18n]
[status-im.js-dependencies :as dependencies] [status-im.js-dependencies :as dependencies]
[status-im.transport.core :as transport] [status-im.transport.core :as transport]
[status-im.transport.inbox :as inbox]
[status-im.ui.screens.db :refer [app-db]] [status-im.ui.screens.db :refer [app-db]]
[status-im.utils.datetime :as time] [status-im.utils.datetime :as time]
[status-im.utils.ethereum.core :as ethereum] [status-im.utils.ethereum.core :as ethereum]
@ -206,6 +207,11 @@
(fn [_] (fn [_]
(status/close-application))) (status/close-application)))
(re-frame/reg-fx
::app-state-change-fx
(fn [state]
(status/app-state-change state)))
;;;; Handlers ;;;; Handlers
(handlers/register-handler-db (handlers/register-handler-db
@ -407,8 +413,14 @@
(handlers/register-handler-fx (handlers/register-handler-fx
:app-state-change :app-state-change
(fn [_ [_ state]] (fn [{{:keys [network-status mailserver-status]} :db :as cofx} [_ state]]
(status/app-state-change 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 (handlers/register-handler-fx
:request-permissions :request-permissions