11package chat .rocket .reactnative .notification ;
22
3- import android .database .Cursor ;
4- import android .database .sqlite .SQLiteDatabase ;
3+ import android .database .Cursor ;
54import android .util .Base64 ;
65import android .util .Log ;
76
87import com .facebook .react .bridge .Arguments ;
98import com .facebook .react .bridge .ReactApplicationContext ;
109import com .facebook .react .bridge .WritableMap ;
10+ import com .wix .reactnativenotifications .core .AppLifecycleFacade ;
11+ import com .wix .reactnativenotifications .core .AppLifecycleFacadeHolder ;
1112import com .google .gson .Gson ;
1213import com .pedrouid .crypto .RCTAes ;
1314import com .pedrouid .crypto .RCTRsaUtils ;
1415import com .pedrouid .crypto .RSA ;
1516import com .pedrouid .crypto .Util ;
17+ import com .nozbe .watermelondb .WMDatabase ;
1618
1719import java .io .File ;
1820import 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 ;
0 commit comments