@ -1,4 +1,5 @@
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				import  PromiseKit 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				import  SessionMessagingKit 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				extension  AppDelegate  { 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				
 
			
		 
		
	
	
		
			
				
					
						
						
						
							
								 
							 
						
					 
				
			
			 
			 
			
				@ -7,36 +8,17 @@ extension AppDelegate {
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        guard  Storage . shared . getUser ( ) ? . name  !=  nil  else  {  return  } 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        let  userDefaults  =  UserDefaults . standard 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        let  lastSync  =  userDefaults [ . lastConfigurationSync ]  ? ?  . distantPast 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        guard  Date ( ) . timeIntervalSince ( lastSync )  >  7  *  24  *  60  *  60 , 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            let  configurationMessage  =  ConfigurationMessage . getCurrent ( )  else  {  return  }  //   S y n c   e v e r y   2   d a y s 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        let  destination  =  Message . Destination . contact ( publicKey :  getUserHexEncodedPublicKey ( ) ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        Storage . shared . write  {  transaction  in 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            let  job  =  MessageSendJob ( message :  configurationMessage ,  destination :  destination ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            JobQueue . shared . add ( job ,  using :  transaction ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        } 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        guard  Date ( ) . timeIntervalSince ( lastSync )  >  7  *  24  *  60  *  60  else  {  return  }  //   S y n c   e v e r y   2   d a y s 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        //   O n l y   u p d a t e   t h e   ' l a s t C o n f i g u r a t i o n S y n c '   t i m e s t a m p   i f   w e   h a v e   d o n e   t h e   f i r s t   s y n c   ( D o n ' t   w a n t 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        //   a   n e w   d e v i c e   c o n f i g   s y n c   t o   o v e r r i d e   c o n f i g   s y n c s   f r o m   o t h e r   d e v i c e s ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        if  userDefaults [ . hasSyncedInitialConfiguration ]  { 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            userDefaults [ . lastConfigurationSync ]  =  Date ( ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        } 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    } 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    func  forceSyncConfigurationNowIfNeeded ( with  transaction :  YapDatabaseReadWriteTransaction ?  =  nil )  ->  Promise < Void >  { 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        guard  Storage . shared . getUser ( ) ? . name  !=  nil ,  let  configurationMessage  =  ConfigurationMessage . getCurrent ( with :  transaction )  else  { 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            return  Promise . value ( ( ) ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        } 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        let  destination  =  Message . Destination . contact ( publicKey :  getUserHexEncodedPublicKey ( ) ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        let  ( promise ,  seal )  =  Promise < Void > . pending ( ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        Storage . writeSync  {  transaction  in 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            MessageSender . send ( configurationMessage ,  to :  destination ,  using :  transaction ) . done  { 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				                seal . fulfill ( ( ) ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            } . catch  {  _  in 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				                seal . fulfill ( ( ) )  //   F u l f i l l   e v e n   i f   t h i s   f a i l e d ;   t h e   c o n f i g u r a t i o n   i n   t h e   s w a r m   s h o u l d   b e   a t   m o s t   2   d a y s   o l d 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            } . retainUntilComplete ( ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        } 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        return  promise 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        MessageSender . syncConfiguration ( forceSyncNow :  false ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            . done  { 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				                //   O n l y   u p d a t e   t h e   ' l a s t C o n f i g u r a t i o n S y n c '   t i m e s t a m p   i f   w e   h a v e   d o n e   t h e   f i r s t   s y n c   ( D o n ' t   w a n t 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				                //   a   n e w   d e v i c e   c o n f i g   s y n c   t o   o v e r r i d e   c o n f i g   s y n c s   f r o m   o t h e r   d e v i c e s ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				                if  userDefaults [ . hasSyncedInitialConfiguration ]  { 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				                    userDefaults [ . lastConfigurationSync ]  =  Date ( ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				                } 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            } 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				            . retainUntilComplete ( ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    } 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    @objc  func  startClosedGroupPoller ( )  { 
 
			
		 
		
	
	
		
			
				
					
						
						
						
							
								 
							 
						
					 
				
			
			 
			 
			
				@ -48,10 +30,3 @@ extension AppDelegate {
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        ClosedGroupPoller . shared . stop ( ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    } 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				} 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				extension  AppDelegate  { 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    @objc ( forceSyncConfigurationNowIfNeeded ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    func  objc_forceSyncConfigurationNowIfNeeded ( )  { 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				        return  forceSyncConfigurationNowIfNeeded ( with :  nil ) . retainUntilComplete ( ) 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				    } 
 
			
		 
		
	
		
			
				 
				 
			
			 
			 
			
				}