Skip to content

Commit

Permalink
Nouvelle implémentation de l'activation automatique du cross-signing …
Browse files Browse the repository at this point in the history
…ne nécessitant pas de modification dans Matrix-ios-SDK.
  • Loading branch information
Nicolas Buquet committed Nov 29, 2023
1 parent a148277 commit 359e1b4
Showing 1 changed file with 51 additions and 17 deletions.
68 changes: 51 additions & 17 deletions Riot/Modules/Home/HomeViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ @interface HomeViewController () <SecureBackupSetupCoordinatorBridgePresenterDel
CGFloat selectedCollectionViewContentOffset;

// Tchap: initial sync done listener
id tchapInitialSyncDoneListener;
id tchapSyncDoneListener;
}

@property (nonatomic, strong) SecureBackupSetupCoordinatorBridgePresenter *secureBackupSetupCoordinatorBridgePresenter;
Expand Down Expand Up @@ -104,20 +104,55 @@ - (void)viewDidLoad

// Change the table data source. It must be the home view controller itself.
self.recentsTableView.dataSource = self;

// Tchap: listen to Tchap InitialSyncDone notification added by Tchap in Matrix SDK.
if (tchapInitialSyncDoneListener == nil)
}

// Tchap: set-up cross-signing auto-activation
- (void)tchapSetupCrossSigningAutoActivation
{
// Tchap: listen to Tchap syncDone notification to check the status of the cross-signing after account info are updated.
if (tchapSyncDoneListener == nil)
{
tchapInitialSyncDoneListener = [NSNotificationCenter.defaultCenter addObserverForName:kTchapMXSessionInitialSyncDone
object:nil
queue:NSOperationQueue.mainQueue
usingBlock:^(NSNotification * _Nonnull notification) {
// Try to force activate cross-signing after initial sync.
[self showCrossSigningSetup];
__weak typeof(self) weakSelf = self;

tchapSyncDoneListener = [NSNotificationCenter.defaultCenter addObserverForName:kMXSessionDidSyncNotification
object:nil
queue:NSOperationQueue.mainQueue
usingBlock:^(NSNotification * _Nonnull notification) {

__strong __typeof(weakSelf)strongSelf = weakSelf;
MXSession *session = strongSelf.mainSession;

// Remove listener after first invocation.
[self tchapUnsetupCrossSigningAutoActivation];

// Try to force activate cross-signing after sync if:
// - device is the only one listed on the account (no risk of conflict with other devices)
// - Cross-signing is not enabled for this account.
// - or Cross-signing exists on the account but is not trusted by this device. The account password can be asked to activate cross-signing in this case.
NSString *currentUserId = session.myUserId;
MXCrossSigningState state = session.crypto.crossSigning.state;
if (currentUserId != nil
&& [session.crypto devicesForUser:currentUserId].count < 2
&& (session.crypto.crossSigning.state == MXCrossSigningStateNotBootstrapped || session.crypto.crossSigning.state == MXCrossSigningStateCrossSigningExists) )
{
[strongSelf showCrossSigningSetup];
}
}];
}
}

// Tchap: remove intial sync done listener
- (void)tchapUnsetupCrossSigningAutoActivation
{
if (tchapSyncDoneListener)
{
[NSNotificationCenter.defaultCenter removeObserver:tchapSyncDoneListener
name:kMXSessionDidSyncNotification
object:nil];
tchapSyncDoneListener = nil;
}
}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
Expand All @@ -138,6 +173,10 @@ - (void)viewWillAppear:(BOOL)animated
[recentsDataSource searchWithPatterns:nil];
[self.recentsSearchBar setText:nil];
}

// Tchap: set-up cross-signing auto-activation in viewWillAppear rather than in viewDidLoad
// because this controller is only created once and never released!
[self tchapSetupCrossSigningAutoActivation];
}

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
Expand All @@ -154,13 +193,8 @@ - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIV
- (void)destroy
{
// Tchap: remove intial sync done listener
if (tchapInitialSyncDoneListener)
{
[NSNotificationCenter.defaultCenter removeObserver:tchapInitialSyncDoneListener
name:kTchapMXSessionInitialSyncDone
object:nil];
tchapInitialSyncDoneListener = nil;
}
[self tchapUnsetupCrossSigningAutoActivation];

[super destroy];
}

Expand Down

0 comments on commit 359e1b4

Please sign in to comment.