11import {
22 saveRoomMessageAndUpdate ,
33 setRoomMessages ,
4+ syncRoomMessages ,
45 setLatestRoomMessages ,
56} from '@/services/bare' ;
6- import { getCurrentRoom , useGlobalStore } from '@/services/zustand' ;
7+ import { getCurrentRoom , useGlobalStore , usePreferencesStore } from '@/services/zustand' ;
8+ import { getDeviceId } from '../services/pushnotifications' ;
79import { sleep } from '@/utils' ;
810import Toast from 'react-native-toast-message' ;
911import { Peers } from 'lib/connections' ;
@@ -55,6 +57,7 @@ export class Bridge {
5557 this . pushRegistrationTimer = null ;
5658 this . pushRegistrationDebouncePromise = null ;
5759 this . pushRegistrationDebounceMs = 750 ;
60+ this . pushRegistrationDone = false ;
5861 this . rpc = new RPC ( IPC , ( req , error ) => {
5962 const data = this . parse ( b4a . toString ( req . data ) ) ;
6063 if ( ! data ) {
@@ -114,11 +117,13 @@ export class Bridge {
114117
115118 get_pending_push_registrations ( room_keys = [ ] ) {
116119 const all_room_keys = [ ...new Set ( [ ...this . get_room_keys_from_store ( ) , ...room_keys ] ) ] ;
117- const pending_room_keys = all_room_keys . filter ( ( room_key ) => ! this . registeredRoomKeys . has ( room_key ) ) ;
120+ const prefs = usePreferencesStore . getState ( ) . preferences ;
121+ const prefs_registered = prefs ?. registeredRoomKeys || [ ] ;
122+ const pending_room_keys = all_room_keys . filter ( ( room_key ) => ! this . registeredRoomKeys . has ( room_key ) && ! prefs_registered . includes ( room_key ) ) ;
118123
119124 return {
120- include_device_push : ! this . basePushRegistered ,
121- include_call_push : ! this . callPushRegistered ,
125+ include_device_push : ! this . basePushRegistered && ! prefs ?. basePushVerified ,
126+ include_call_push : ! this . callPushRegistered && ! prefs ?. basePushVerified ,
122127 pending_room_keys,
123128 } ;
124129 }
@@ -161,8 +166,13 @@ export class Bridge {
161166 }
162167
163168 const sent = await this . send_push_registration ( push_registration_batch ) ;
169+ console . log ( '[rpc.js] Push registration sent:' , sent ) ;
164170 if ( sent && sent . success === true ) {
165171 this . mark_push_registrations_sent ( state ) ;
172+ const currentFirebaseToken = getDeviceId ( ) ;
173+ if ( currentFirebaseToken ) {
174+ usePreferencesStore . getState ( ) . setLastRegisteredDeviceToken ( currentFirebaseToken ) ;
175+ }
166176 }
167177 return sent ;
168178 }
@@ -176,6 +186,24 @@ export class Bridge {
176186 async sync_push_registrations ( room_keys = [ ] ) {
177187 this . collect_pending_push_room_keys ( room_keys ) ;
178188
189+ const prefs = usePreferencesStore . getState ( ) . preferences ;
190+ const currentFirebaseToken = getDeviceId ( ) ;
191+ const tokenIsSame = currentFirebaseToken && currentFirebaseToken === prefs ?. lastRegisteredDeviceToken ;
192+
193+ if ( ! tokenIsSame && currentFirebaseToken ) {
194+ usePreferencesStore . getState ( ) . clearRegisteredRoomKeys ( ) ;
195+ }
196+
197+ const state = this . get_pending_push_registrations ( [ ...this . pendingPushRoomKeys ] ) ;
198+
199+ console . log ( '[rpc.js] Push registration rooms number:' + state . pending_room_keys ?. length ) ;
200+ console . log ( '[rpc.js] Push registration token is same:' + tokenIsSame ) ;
201+
202+ if ( state . pending_room_keys . length === 0 && tokenIsSame && ! state . include_device_push && ! state . include_call_push ) {
203+ this . pendingPushRoomKeys . clear ( ) ;
204+ return { success : true , skipped : true } ;
205+ }
206+
179207 if ( ! useGlobalStore . getState ( ) . huginNode . connected ) {
180208 return null ;
181209 }
@@ -231,12 +259,33 @@ export class Bridge {
231259 case 'hugin-node-connected' :
232260 useGlobalStore . getState ( ) . setHuginNode ( { connected : true } ) ;
233261
262+ if ( useGlobalStore . getState ( ) . appState !== 'active' ) {
263+ console . log ( '[rpc.js] App not active, skipping push registration sync' ) ;
264+ break ;
265+ }
266+
267+ if ( this . pushRegistrationDone ) {
268+ console . log ( '[rpc.js] Push registration already done this run, skipping' ) ;
269+ break ;
270+ }
271+
234272 if ( useGlobalStore . getState ( ) . contacts . length === 0
235273 && useGlobalStore . getState ( ) . rooms . length === 0 ) {
236274 console . log ( '[rpc.js] No contacts or rooms, skipping push registration sync' )
237275 break ;
238276 }
277+
278+ const prefs = usePreferencesStore . getState ( ) . preferences ;
279+ const currentFirebaseToken = getDeviceId ( ) ;
280+ if ( currentFirebaseToken && currentFirebaseToken === prefs ?. lastRegisteredDeviceToken ) {
281+ console . log ( '[rpc.js] Push token has not changed since last registration, skipping' ) ;
282+ this . pushRegistrationDone = true ;
283+ break ;
284+ }
285+
239286 await this . sync_push_registrations ( ) ;
287+ console . log ( '[rpc.js] Push registration sync completed' ) ;
288+ this . pushRegistrationDone = true ;
240289 break ;
241290 case 'hugin-node-disconnected' :
242291 console . log ( 'Hugin node disconnected!' )
@@ -248,6 +297,7 @@ export class Bridge {
248297 break ;
249298 case 'new-swarm' :
250299 if ( ! json . beam && typeof json . key === 'string' && json . key . length > 0 ) {
300+ console . log ( '[rpc.js] New swarm started, syncing push registrations' )
251301 await this . sync_push_registrations ( [ json . key ] ) ;
252302 }
253303 break ;
@@ -315,11 +365,7 @@ export class Bridge {
315365 case 'history-update' :
316366 await sleep ( 500 ) ;
317367 if ( getCurrentRoom ( ) === json . key ) {
318- const currentState = useGlobalStore . getState ( ) ;
319- // This might need to be checked, may cause history sync not to show messages immediately
320- if ( ! currentState . roomMessages || currentState . roomMessages . length === 0 ) {
321- setRoomMessages ( json . key , 0 ) ;
322- }
368+ syncRoomMessages ( json . key ) ;
323369 if ( json . history && ! json . background ) {
324370 Toast . show ( {
325371 type : 'success' ,
@@ -376,7 +422,7 @@ export class Bridge {
376422 WebRTC . signal ( key , topic , address , data ) ;
377423 break ;
378424 case 'error-message' :
379- console . log ( 'Error:' , json . data ) ;
425+ console . log ( 'Error:' , json ) ;
380426 Toast . show ( { text1 : json . data ?. message || 'Error' , type : 'error' } ) ;
381427 break ;
382428 case 'file-saved-to-downloads' :
0 commit comments