Skip to content

Commit e11236c

Browse files
committed
Fix notification features
1 parent 80220e7 commit e11236c

File tree

5 files changed

+59
-36
lines changed

5 files changed

+59
-36
lines changed

android/app/src/main/java/chat/rocket/reactnative/notification/Ejson.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package chat.rocket.reactnative.notification;
22

3+
import android.util.Log;
4+
35
import com.facebook.react.bridge.ReactApplicationContext;
46
import com.facebook.react.bridge.Callback;
57

68
import com.ammarahmed.mmkv.SecureKeystore;
79
import com.tencent.mmkv.MMKV;
10+
import com.wix.reactnativenotifications.core.AppLifecycleFacade;
11+
import com.wix.reactnativenotifications.core.AppLifecycleFacadeHolder;
812

913
import java.math.BigInteger;
1014

@@ -38,20 +42,21 @@ public class Ejson {
3842

3943
Content content;
4044

45+
private ReactApplicationContext reactContext;
46+
4147
private MMKV mmkv;
4248

4349
private String TOKEN_KEY = "reactnativemeteor_usertoken-";
4450

4551
public Ejson() {
46-
ReactApplicationContext reactApplicationContext = CustomPushNotification.reactApplicationContext;
47-
48-
if (reactApplicationContext == null) {
49-
return;
52+
AppLifecycleFacade facade = AppLifecycleFacadeHolder.get();
53+
if (facade != null && facade.getRunningReactContext() instanceof ReactApplicationContext) {
54+
this.reactContext = (ReactApplicationContext) facade.getRunningReactContext();
5055
}
5156

5257
// Start MMKV container
53-
MMKV.initialize(reactApplicationContext);
54-
SecureKeystore secureKeystore = new SecureKeystore(reactApplicationContext);
58+
MMKV.initialize(this.reactContext);
59+
SecureKeystore secureKeystore = new SecureKeystore(this.reactContext);
5560

5661
// https://github.com/ammarahm-ed/react-native-mmkv-storage/blob/master/src/loader.js#L31
5762
String alias = Utils.toHex("com.MMKV.default");

android/app/src/main/java/chat/rocket/reactnative/notification/Encryption.java

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
package chat.rocket.reactnative.notification;
22

3-
import android.database.Cursor;
4-
import android.database.sqlite.SQLiteDatabase;
3+
import android.database.Cursor;
54
import android.util.Base64;
65
import android.util.Log;
76

87
import com.facebook.react.bridge.Arguments;
98
import com.facebook.react.bridge.ReactApplicationContext;
109
import com.facebook.react.bridge.WritableMap;
10+
import com.wix.reactnativenotifications.core.AppLifecycleFacade;
11+
import com.wix.reactnativenotifications.core.AppLifecycleFacadeHolder;
1112
import com.google.gson.Gson;
1213
import com.pedrouid.crypto.RCTAes;
1314
import com.pedrouid.crypto.RCTRsaUtils;
1415
import com.pedrouid.crypto.RSA;
1516
import com.pedrouid.crypto.Util;
17+
import com.nozbe.watermelondb.WMDatabase;
1618

1719
import java.io.File;
1820
import java.lang.reflect.Field;
@@ -73,25 +75,25 @@ class Encryption {
7375

7476
public Room readRoom(final Ejson ejson) {
7577
String dbName = getDatabaseName(ejson.serverURL());
76-
SQLiteDatabase db = null;
78+
WMDatabase db = null;
7779

7880
try {
79-
db = SQLiteDatabase.openDatabase(dbName, null, SQLiteDatabase.OPEN_READONLY);
80-
String[] queryArgs = {ejson.rid};
81+
db = WMDatabase.getInstance(dbName, reactContext);
82+
String[] queryArgs = {ejson.rid};
8183

82-
Cursor cursor = db.rawQuery("SELECT * FROM subscriptions WHERE id == ? LIMIT 1", queryArgs);
84+
Cursor cursor = db.rawQuery("SELECT * FROM subscriptions WHERE id == ? LIMIT 1", queryArgs);
8385

84-
if (cursor.getCount() == 0) {
85-
cursor.close();
86-
return null;
87-
}
86+
if (cursor.getCount() == 0) {
87+
cursor.close();
88+
return null;
89+
}
8890

89-
cursor.moveToFirst();
90-
String e2eKey = cursor.getString(cursor.getColumnIndex("e2e_key"));
91-
Boolean encrypted = cursor.getInt(cursor.getColumnIndex("encrypted")) > 0;
92-
cursor.close();
91+
cursor.moveToFirst();
92+
String e2eKey = cursor.getString(cursor.getColumnIndex("e2e_key"));
93+
Boolean encrypted = cursor.getInt(cursor.getColumnIndex("encrypted")) > 0;
94+
cursor.close();
9395

94-
return new Room(e2eKey, encrypted);
96+
return new Room(e2eKey, encrypted);
9597

9698
} catch (Exception e) {
9799
Log.e("[ENCRYPTION]", "Error reading room", e);
@@ -117,15 +119,16 @@ private String getDatabaseName(String serverUrl) {
117119
e.printStackTrace();
118120
}
119121

120-
String dbName = serverUrl.replace("https://", "");
122+
// Match JS WatermelonDB naming: strip scheme, replace '/' with '.', add '-experimental' when needed, and append one ".db".
123+
String name = serverUrl.replaceFirst("^(\\w+:)?//", "").replace("/", ".");
121124
if (!isOfficial) {
122-
dbName += "-experimental";
125+
name += "-experimental";
123126
}
124-
// Old issue. Safer to accept it then to migrate away from it.
125-
dbName += ".db.db";
126-
// https://github.com/Nozbe/WatermelonDB/blob/a757e646141437ad9a06f7314ad5555a8a4d252e/native/android-jsi/src/main/java/com/nozbe/watermelondb/jsi/JSIInstaller.java#L18
127-
File databasePath = new File(reactContext.getDatabasePath(dbName).getPath().replace("/databases", ""));
128-
return databasePath.getPath();
127+
name += ".db";
128+
129+
// Important: return just the name (not an absolute path). WMDatabase will resolve and append its own ".db" internally,
130+
// so the physical file becomes "*.db.db", matching the JS adapter.
131+
return name;
129132
}
130133

131134
public String readUserKey(final Ejson ejson) throws Exception {
@@ -213,6 +216,17 @@ public String decryptMessage(final Ejson ejson, final ReactApplicationContext re
213216

214217
public String encryptMessage(final String message, final String id, final Ejson ejson) {
215218
try {
219+
AppLifecycleFacade facade = AppLifecycleFacadeHolder.get();
220+
if (facade != null && facade.getRunningReactContext() instanceof ReactApplicationContext) {
221+
this.reactContext = (ReactApplicationContext) facade.getRunningReactContext();
222+
} else {
223+
this.reactContext = null;
224+
}
225+
if (this.reactContext == null) {
226+
Log.i("[ROCKETCHAT][E2E]", "ReactApplicationContext is null, returning unencrypted message");
227+
return message;
228+
}
229+
216230
Room room = readRoom(ejson);
217231
if (room == null || !room.encrypted || room.e2eKey == null) {
218232
return message;

app/lib/database/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import serversMigrations from './model/servers/migrations';
2727
import { TAppDatabase, TServerDatabase } from './interfaces';
2828

2929
if (__DEV__) {
30-
console.log(`📂 ${appGroupPath}`);
30+
console.log(appGroupPath);
3131
}
3232

3333
const getDatabasePath = (name: string) => `${appGroupPath}${name}${isOfficial ? '' : '-experimental'}.db`;
@@ -40,7 +40,9 @@ export const getDatabase = (database = ''): Database => {
4040
dbName,
4141
schema: appSchema,
4242
migrations,
43-
jsi: true
43+
jsi: true,
44+
// @ts-expect-error
45+
experimentalUnsafeNativeReuse: true
4446
});
4547

4648
return new Database({
@@ -75,7 +77,9 @@ class DB {
7577
dbName: getDatabasePath('default'),
7678
schema: serversSchema,
7779
migrations: serversMigrations,
78-
jsi: true
80+
jsi: true,
81+
// @ts-expect-error
82+
experimentalUnsafeNativeReuse: true
7983
}),
8084
modelClasses: [Server, LoggedUser, ServersHistory]
8185
}) as TServerDatabase

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"@discord/bottom-sheet": "bluesky-social/react-native-bottom-sheet",
3737
"@hookform/resolvers": "^2.9.10",
3838
"@notifee/react-native": "7.8.2",
39-
"@nozbe/watermelondb": "^0.28.0-2",
39+
"@nozbe/watermelondb": "^0.28.0",
4040
"@react-native-async-storage/async-storage": "^1.22.3",
4141
"@react-native-camera-roll/camera-roll": "^7.10.0",
4242
"@react-native-clipboard/clipboard": "^1.13.2",

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3660,10 +3660,10 @@
36603660
resolved "https://registry.yarnpkg.com/@nozbe/sqlite/-/sqlite-3.46.0.tgz#aeda9df305e2f49ef951409e44544c19a5a3e32d"
36613661
integrity sha512-ntt8eNp5hh+axX9+kFb5uwyVE0edyfhiYYr+zHDzzFleGC7Qm+a2wHDWDtmRr5nSfbgomhY1uh30kpsHEIR3Mw==
36623662

3663-
"@nozbe/watermelondb@^0.28.0-2":
3664-
version "0.28.0-2"
3665-
resolved "https://registry.yarnpkg.com/@nozbe/watermelondb/-/watermelondb-0.28.0-2.tgz#4a1e6e9416aa3d459a7cf80c54cdc10699182ccd"
3666-
integrity sha512-0BnbTpcy6n44sHnNZrgdrGQCpAgD8h/8BCi6/Sx1QkZiYp0+6bHppuBmUuGFcvJnYj3Xvnx2/5gbWh23oKUeJg==
3663+
"@nozbe/watermelondb@^0.28.0":
3664+
version "0.28.0"
3665+
resolved "https://registry.yarnpkg.com/@nozbe/watermelondb/-/watermelondb-0.28.0.tgz#6130ac9017f9019966f892919ada79234c306c70"
3666+
integrity sha512-40ttcqPOLCTGnbfCQAXSEo8J4KlH/QQhwTfTb9E21CyX/driMEZueiJSI2rSkHICZrI2vnu52aRubNbI3UAzQQ==
36673667
dependencies:
36683668
"@babel/runtime" "7.26.0"
36693669
"@nozbe/simdjson" "3.9.4"

0 commit comments

Comments
 (0)