1
1
package chat .rocket .reactnative .notification ;
2
2
3
- import android .database .Cursor ;
4
- import android .database .sqlite .SQLiteDatabase ;
3
+ import android .database .Cursor ;
5
4
import android .util .Base64 ;
6
5
import android .util .Log ;
7
6
8
7
import com .facebook .react .bridge .Arguments ;
9
8
import com .facebook .react .bridge .ReactApplicationContext ;
10
9
import com .facebook .react .bridge .WritableMap ;
10
+ import com .wix .reactnativenotifications .core .AppLifecycleFacade ;
11
+ import com .wix .reactnativenotifications .core .AppLifecycleFacadeHolder ;
11
12
import com .google .gson .Gson ;
12
13
import com .pedrouid .crypto .RCTAes ;
13
14
import com .pedrouid .crypto .RCTRsaUtils ;
14
15
import com .pedrouid .crypto .RSA ;
15
16
import com .pedrouid .crypto .Util ;
17
+ import com .nozbe .watermelondb .WMDatabase ;
16
18
17
19
import java .io .File ;
18
20
import java .lang .reflect .Field ;
@@ -73,25 +75,25 @@ class Encryption {
73
75
74
76
public Room readRoom (final Ejson ejson ) {
75
77
String dbName = getDatabaseName (ejson .serverURL ());
76
- SQLiteDatabase db = null ;
78
+ WMDatabase db = null ;
77
79
78
80
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 };
81
83
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 );
83
85
84
- if (cursor .getCount () == 0 ) {
85
- cursor .close ();
86
- return null ;
87
- }
86
+ if (cursor .getCount () == 0 ) {
87
+ cursor .close ();
88
+ return null ;
89
+ }
88
90
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 ();
93
95
94
- return new Room (e2eKey , encrypted );
96
+ return new Room (e2eKey , encrypted );
95
97
96
98
} catch (Exception e ) {
97
99
Log .e ("[ENCRYPTION]" , "Error reading room" , e );
@@ -117,15 +119,16 @@ private String getDatabaseName(String serverUrl) {
117
119
e .printStackTrace ();
118
120
}
119
121
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 ("/" , "." );
121
124
if (!isOfficial ) {
122
- dbName += "-experimental" ;
125
+ name += "-experimental" ;
123
126
}
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 ;
129
132
}
130
133
131
134
public String readUserKey (final Ejson ejson ) throws Exception {
@@ -213,6 +216,17 @@ public String decryptMessage(final Ejson ejson, final ReactApplicationContext re
213
216
214
217
public String encryptMessage (final String message , final String id , final Ejson ejson ) {
215
218
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
+
216
230
Room room = readRoom (ejson );
217
231
if (room == null || !room .encrypted || room .e2eKey == null ) {
218
232
return message ;
0 commit comments