Skip to content

Commit

Permalink
Update PNs to use data-only messaging, and only encode/decode data va…
Browse files Browse the repository at this point in the history
…lues. Fixes #6772

Fix navigation to chat when PN is tapped while signed off. Fixes #3488

Anonymize PN pubkeys. Part of #6772
  • Loading branch information
Pedro Pombeiro authored and rasom committed Jan 17, 2019
1 parent 4af2073 commit 5a69b41
Show file tree
Hide file tree
Showing 26 changed files with 464 additions and 217 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,4 @@ conan*.txt
conanbuildinfo.*
conan.cmake
/yarn-error.log
/default.realm/
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ android-ports: ##@other Add proxies to Android Device/Simulator
adb reverse tcp:4567 tcp:4567
adb forward tcp:5561 tcp:5561

android-logcat:
adb logcat | grep -e StatusModule -e ReactNativeJS -e StatusNativeLogs

startdev-%:
$(eval SYSTEM := $(word 2, $(subst -, , $@)))
Expand Down
2 changes: 1 addition & 1 deletion STATUS_GO_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
181221-204011-e80de6
0.19.0-beta.1
4 changes: 4 additions & 0 deletions android/app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@
-dontwarn java.nio.file.*
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
-dontwarn okio.**

# Firebase
-keep class io.invertase.firebase.** { *; }
-dontwarn io.invertase.firebase.**
2 changes: 1 addition & 1 deletion ci/Jenkinsfile.ios
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pipeline {
timestamps()
disableConcurrentBuilds()
/* Prevent Jenkins jobs from running forever */
timeout(time: 35, unit: 'MINUTES')
timeout(time: 45, unit: 'MINUTES')
/* Limit builds retained */
buildDiscarder(logRotator(
numToKeepStr: '10',
Expand Down
33 changes: 17 additions & 16 deletions doc/codebase-structure-and-guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Each business logic module is managed in a module directory.

*There is no rigid structure on how to organize code inside modules outside of core and db namespaces*

```
```txt
- events.cljs
- subs.cljs
- notifications
Expand Down Expand Up @@ -37,13 +37,13 @@ Core namespace must only contain functions that can be called outside of the mod

- fx producing functions called by events and other modules

```clojure
(def get-current-account
module.db/get-current-account)
```clojure
(def get-current-account
module.db/get-current-account)

(defn set-current-account [{db :db :as cofx}]
{:db (module.db/set-current-account db)})
```
(defn set-current-account [{db :db :as cofx}]
{:db (module.db/set-current-account db)})
```

## db.cljs

Expand All @@ -61,13 +61,14 @@ These guidelines make db.cljs namespaces the place to go when making changes to
- events must always be declared with `register-handler-fx`, no `register-handler-db`
- events must never use the `trim-v` interceptor
- events must only contain a function call defined in a module
```clojure
(handlers/register-handler-fx
:notifications/handle-push-notification
(fn [cofx [_ event]]
(notifications/handle-push-notification event cofx)))
```
```clojure
(handlers/register-handler-fx
:notifications/handle-push-notification-open
(fn [cofx [_ event]]
(notifications/handle-push-notification-open event cofx)))
```
- events must use synthetic namespaces:
- `:module.ui/` for user triggered events
- `:module.callback/` for callback events, which are events bringing back the result of an fx to the event loop, the name of the event should end with `-success` or `-error` most of the time. Other possibilities can be `-granted`, `-denied` for instance.
- `:module/` for internal events, examples are time based events marked `-timed-out`, external changes marked `-changed` or reception of external events marked `-received`.

- `:module.ui/` for user triggered events
- `:module.callback/` for callback events, which are events bringing back the result of an fx to the event loop, the name of the event should end with `-success` or `-error` most of the time. Other possibilities can be `-granted`, `-denied` for instance.
- `:module/` for internal events, examples are time based events marked `-timed-out`, external changes marked `-changed` or reception of external events marked `-received`.
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ QList<ModuleMethod *> DesktopNotification::methodsToExport() {

QVariantMap DesktopNotification::constantsToExport() { return QVariantMap(); }

void DesktopNotification::sendNotification(QString title, QString body, bool prioritary) {
void DesktopNotification::displayNotification(QString title, QString body, bool prioritary) {
Q_D(DesktopNotification);
qCDebug(NOTIFICATION) << "::sendNotification";
qCDebug(NOTIFICATION) << "::displayNotification";

if (m_appHasFocus) {
qCDebug(NOTIFICATION) << "Not sending notification since an application window is active";
qCDebug(NOTIFICATION) << "Not displaying notification since an application window is active";
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class DesktopNotification : public QObject, public ModuleInterface {
QList<ModuleMethod*> methodsToExport() override;
QVariantMap constantsToExport() override;

Q_INVOKABLE void sendNotification(QString title, QString body, bool prioritary);
Q_INVOKABLE void displayNotification(QString title, QString body, bool prioritary);
Q_INVOKABLE void setDockBadgeLabel(const QString label);
private:
QScopedPointer<DesktopNotificationPrivate> d_ptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public String getName() {
}

@Override
public void onHostResume() { // Actvity `onResume`
public void onHostResume() { // Activity `onResume`
module = this;
Activity currentActivity = getCurrentActivity();
if (currentActivity == null) {
Expand Down Expand Up @@ -459,7 +459,7 @@ public void run() {
}

@ReactMethod
public void notifyUsers(final String message, final String payloadJSON, final String tokensJSON, final Callback callback) {
public void notifyUsers(final String dataPayloadJSON, final String tokensJSON, final Callback callback) {
Log.d(TAG, "notifyUsers");
if (!checkAvailability()) {
callback.invoke(false);
Expand All @@ -469,7 +469,7 @@ public void notifyUsers(final String message, final String payloadJSON, final St
Runnable r = new Runnable() {
@Override
public void run() {
String res = Statusgo.NotifyUsers(message, payloadJSON, tokensJSON);
String res = Statusgo.NotifyUsers(dataPayloadJSON, tokensJSON);

callback.invoke(res);
}
Expand Down
8 changes: 4 additions & 4 deletions modules/react-native-status/desktop/rctstatus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,14 @@ void RCTStatus::createAccount(QString password, double callbackId) {
}


void RCTStatus::notifyUsers(QString token, QString payloadJSON, QString tokensJSON, double callbackId) {
void RCTStatus::notifyUsers(QString dataPayloadJSON, QString tokensJSON, double callbackId) {
Q_D(RCTStatus);
qCDebug(RCTSTATUS) << "::notifyUsers call - callbackId:" << callbackId;
QtConcurrent::run([&](QString token, QString payloadJSON, QString tokensJSON, double callbackId) {
const char* result = NotifyUsers(token.toUtf8().data(), payloadJSON.toUtf8().data(), tokensJSON.toUtf8().data());
QtConcurrent::run([&](QString dataPayloadJSON, QString tokensJSON, double callbackId) {
const char* result = NotifyUsers(dataPayloadJSON.toUtf8().data(), tokensJSON.toUtf8().data());
logStatusGoResult("::notifyUsers Notify", result);
d->bridge->invokePromiseCallback(callbackId, QVariantList{result});
}, token, payloadJSON, tokensJSON, callbackId);
}, dataPayloadJSON, tokensJSON, callbackId);
}


Expand Down
2 changes: 1 addition & 1 deletion modules/react-native-status/desktop/rctstatus.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class RCTStatus : public QObject, public ModuleInterface {
Q_INVOKABLE void startNode(QString configString);
Q_INVOKABLE void stopNode();
Q_INVOKABLE void createAccount(QString password, double callbackId);
Q_INVOKABLE void notifyUsers(QString token, QString payloadJSON, QString tokensJSON, double callbackId);
Q_INVOKABLE void notifyUsers(QString dataPayloadJSON, QString tokensJSON, double callbackId);
Q_INVOKABLE void sendLogs(QString dbJSON);
Q_INVOKABLE void addPeer(QString enode, double callbackId);
Q_INVOKABLE void recoverAccount(QString passphrase, QString password, double callbackId);
Expand Down
5 changes: 2 additions & 3 deletions modules/react-native-status/ios/RCTStatus/RCTStatus.m
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,10 @@ -(void)setBridge:(RCTBridge *)newBridge
////////////////////////////////////////////////////////////////////
#pragma mark - NotifyUsers method
//////////////////////////////////////////////////////////////////// notifyUsers
RCT_EXPORT_METHOD(notifyUsers:(NSString *)message
payloadJSON:(NSString *)payloadJSON
RCT_EXPORT_METHOD(notifyUsers:(NSString *)dataPayloadJSON
tokensJSON:(NSString *)tokensJSON
callback:(RCTResponseSenderBlock)callback) {
char * result = NotifyUsers((char *) [message UTF8String], (char *) [payloadJSON UTF8String], (char *) [tokensJSON UTF8String]);
char * result = NotifyUsers((char *) [dataPayloadJSON UTF8String], (char *) [tokensJSON UTF8String]);
callback(@[[NSString stringWithUTF8String: result]]);
#if DEBUG
NSLog(@"NotifyUsers() method called");
Expand Down
4 changes: 1 addition & 3 deletions src/status_im/android/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@
(.addEventListener react/app-state "change" app-state-change-handler))
:component-did-mount
(fn [this]
(dispatch [:set-initial-props (reagent/props this)])
;; TODO(oskarth): Background click_action handler
(notifications/init))
(dispatch [:set-initial-props (reagent/props this)]))
:component-will-unmount
(fn []
(.stop react/http-bridge)
Expand Down
45 changes: 18 additions & 27 deletions src/status_im/chat/models/message.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
(get-in db [:account/account :desktop-notifications?])
(< (time/seconds-ago (time/to-date timestamp)) constants/one-earth-day))
(let [{:keys [title body prioritary?]} (build-desktop-notification cofx message)]
(.sendNotification react/desktop-notification title body prioritary?)))
(.displayNotification react/desktop-notification title body prioritary?)))
(fx/merge cofx
{:db (cond->
(-> db
Expand Down Expand Up @@ -159,19 +159,6 @@
message
(assoc message :clock-value (utils.clocks/send last-clock-value))))

(fx/defn display-notification
[cofx chat-id]
(when config/in-app-notifications-enabled?
(let [view-id (get-in cofx [:db :view-id])
from (accounts.db/current-public-key cofx)
current-chat-id (get-in cofx [:db :current-chat-id])]
(when-not (and (= :chat view-id)
(= current-chat-id chat-id))
{:notifications/display-notification {:title (i18n/label :notifications-new-message-title)
:body (i18n/label :notifications-new-message-body)
:to chat-id
:from from}}))))

(defn check-response-to
[{{:keys [response-to response-to-v2]} :content :as message}
old-id->message]
Expand Down Expand Up @@ -210,7 +197,6 @@
current-chat? :seen
:else :received))
(commands-receiving/receive message)
(display-notification chat-id)
(send-message-seen chat-id message-id (and (not group-chat)
current-chat?
(not (= constants/system from))
Expand Down Expand Up @@ -391,14 +377,14 @@
(add-own-status chat-id message-id :sending)
(send chat-id message-id wrapped-record))))

(fx/defn send-push-notification [cofx fcm-token status]
(fx/defn send-push-notification [cofx message-id fcm-token status]
(log/debug "#6772 - send-push-notification" message-id fcm-token)
(when (and fcm-token (= status :sent))
{:send-notification {:message (js/JSON.stringify #js {:from (accounts.db/current-public-key cofx)
:to (get-in cofx [:db :current-chat-id])})
:payload {:title (i18n/label :notifications-new-message-title)
:body (i18n/label :notifications-new-message-body)
:sound notifications/sound-name}
:tokens [fcm-token]}}))
(let [payload {:from (accounts.db/current-public-key cofx)
:to (get-in cofx [:db :current-chat-id])
:id message-id}]
{:send-notification {:data-payload (notifications/encode-notification-payload payload)
:tokens [fcm-token]}})))

(fx/defn update-message-status [{:keys [db]} chat-id message-id status]
(let [from (get-in db [:chats chat-id :messages message-id :from])
Expand Down Expand Up @@ -484,8 +470,13 @@

(re-frame/reg-fx
:send-notification
(fn [{:keys [message payload tokens]}]
(let [payload-json (types/clj->json payload)
tokens-json (types/clj->json tokens)]
(log/debug "send-notification message: " message " payload-json: " payload-json " tokens-json: " tokens-json)
(status/notify-users {:message message :payload payload-json :tokens tokens-json} #(log/debug "send-notification cb result: " %)))))
(fn [{:keys [data-payload tokens]}]
"Sends a notification to another device. data-payload is a Clojure map of strings to strings"
(let [data-payload-json (types/clj->json data-payload)
tokens-json (types/clj->json tokens)]
(log/debug "send-notification data-payload-json:" data-payload-json "tokens-json:" tokens-json)
;; NOTE: react-native-firebase doesn't have a good implementation of sendMessage
;; (supporting e.g. priority or content_available properties),
;; therefore we must use an implementation in status-go.
(status/notify-users {:data-payload data-payload-json :tokens tokens-json}
#(log/debug "send-notification cb result: " %)))))
6 changes: 5 additions & 1 deletion src/status_im/core.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
(ns status-im.core
(:require [re-frame.core :as re-frame]
[status-im.utils.error-handler :as error-handler]
[status-im.utils.platform :as platform]
[status-im.ui.components.react :as react]
[status-im.notifications.background :as background-messaging]
[reagent.core :as reagent]
status-im.transport.impl.receive
status-im.transport.impl.send
Expand All @@ -18,4 +20,6 @@
(log/set-level! config/log-level)
(error-handler/register-exception-handler!)
(re-frame/dispatch [:init/app-started])
(.registerComponent react/app-registry "StatusIm" #(reagent/reactify-component app-root)))
(.registerComponent react/app-registry "StatusIm" #(reagent/reactify-component app-root))
(when platform/android?
(.registerHeadlessTask react/app-registry "RNFirebaseBackgroundMessage" background-messaging/message-handler-fn)))
11 changes: 8 additions & 3 deletions src/status_im/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -830,9 +830,9 @@
;; notifications module

(handlers/register-handler-fx
:notifications/notification-event-received
(fn [cofx [_ event]]
(notifications/handle-push-notification cofx event)))
:notifications/notification-open-event-received
(fn [cofx [_ decoded-payload ctx]]
(notifications/handle-push-notification-open cofx decoded-payload ctx)))

(handlers/register-handler-fx
:notifications.callback/get-fcm-token-success
Expand All @@ -849,6 +849,11 @@
(fn [cofx _]
(accounts/show-mainnet-is-default-alert cofx)))

(handlers/register-handler-fx
:notifications.callback/on-message
(fn [cofx [_ decoded-payload opts]]
(notifications/handle-on-message cofx decoded-payload opts)))

;; hardwallet module

(handlers/register-handler-fx
Expand Down
3 changes: 2 additions & 1 deletion src/status_im/hardwallet/card.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
(then #(re-frame/dispatch [:hardwallet.callback/check-nfc-support-success %])))))

(defn check-nfc-enabled []
(when platform/android?
(when (and platform/android?
config/hardwallet-enabled?)
(.. keycard
nfcIsEnabled
(then #(re-frame/dispatch [:hardwallet.callback/check-nfc-enabled-success %])))))
Expand Down
Loading

0 comments on commit 5a69b41

Please sign in to comment.