diff --git a/RiotShareExtension/Shared/ShareDataSource.h b/RiotShareExtension/Shared/ShareDataSource.h index 9dc36d5f5..92aef0c1d 100644 --- a/RiotShareExtension/Shared/ShareDataSource.h +++ b/RiotShareExtension/Shared/ShareDataSource.h @@ -31,7 +31,7 @@ @property (nonatomic, strong, readonly) NSSet *selectedRoomIdentifiers; - (instancetype)initWithFileStore:(MXFileStore *)fileStore - credentials:(MXCredentials *)credentials; + session:(MXSession *)session; - (void)selectRoomWithIdentifier:(NSString *)roomIdentifier animated:(BOOL)animated; diff --git a/RiotShareExtension/Shared/ShareDataSource.m b/RiotShareExtension/Shared/ShareDataSource.m index 83889c333..e097b5da7 100644 --- a/RiotShareExtension/Shared/ShareDataSource.m +++ b/RiotShareExtension/Shared/ShareDataSource.m @@ -20,7 +20,7 @@ @interface ShareDataSource () @property (nonatomic, strong, readonly) MXFileStore *fileStore; -@property (nonatomic, strong, readonly) MXCredentials *credentials; +@property (nonatomic, strong, readonly) MXSession *session; @property NSArray *recentCellDatas; @property NSMutableArray *visibleRoomCellDatas; @@ -32,12 +32,12 @@ @implementation ShareDataSource - (instancetype)initWithFileStore:(MXFileStore *)fileStore - credentials:(MXCredentials *)credentials + session:(MXSession *)session { if (self = [super init]) { _fileStore = fileStore; - _credentials = credentials; + _session = session; _internalSelectedRoomIdentifiers = [NSMutableSet set]; @@ -81,19 +81,13 @@ NSMutableArray *cellData = [NSMutableArray array]; - MXRestClient *mxRestClient = [[MXRestClient alloc] initWithCredentials:self.credentials andOnUnrecognizedCertificateBlock:nil andPersistentTokenDataHandler:^(void (^handler)(NSArray *credentials, void (^completion)(BOOL didUpdateCredentials))) { - [[MXKAccountManager sharedManager] readAndWriteCredentials:handler]; - } andUnauthenticatedHandler:nil]; - // Add a fake matrix session to each room summary to provide it a REST client (used to handle correctly the room avatar). - MXSession *session = [[MXSession alloc] initWithMatrixRestClient:mxRestClient]; - for (id summary in summaries) { if (!summary.hiddenFromUser && summary.roomType == MXRoomTypeRoom) { if ([summary respondsToSelector:@selector(setMatrixSession:)]) { - [summary setMatrixSession:session]; + [summary setMatrixSession:self.session]; } MXKRecentCellData *recentCellData = [[MXKRecentCellData alloc] initWithRoomSummary:summary dataSource:nil]; diff --git a/RiotShareExtension/Shared/ShareManager.m b/RiotShareExtension/Shared/ShareManager.m index ffb94fc8c..2233b352d 100644 --- a/RiotShareExtension/Shared/ShareManager.m +++ b/RiotShareExtension/Shared/ShareManager.m @@ -45,6 +45,10 @@ @implementation ShareManager +/// A fake matrix session used to provide summaries with a REST client to handle room avatars. +/// The session is stored statically to prevent new ones from being created for each share. +static MXSession *fakeSession; + - (instancetype)initWithShareItemSender:(id)itemSender type:(ShareManagerType)type { @@ -182,6 +186,7 @@ // We consider the first enabled account. // TODO: Handle multiple accounts self.userAccount = [MXKAccountManager sharedManager].activeAccounts.firstObject; + [self checkFakeSession]; } // Reset the file store to reload the room data. @@ -191,12 +196,12 @@ _fileStore = nil; } - if (self.userAccount) + if (self.userAccount && fakeSession) { _fileStore = [[MXFileStore alloc] initWithCredentials:self.userAccount.mxCredentials]; ShareDataSource *roomDataSource = [[ShareDataSource alloc] initWithFileStore:_fileStore - credentials:self.userAccount.mxCredentials]; + session:fakeSession]; [self.shareViewController configureWithState:ShareViewControllerAccountStateConfigured roomDataSource:roomDataSource]; @@ -206,6 +211,27 @@ } } +- (void)checkFakeSession +{ + if (!self.userAccount) + { + return; + } + + if (fakeSession && [fakeSession.credentials.userId isEqualToString:self.userAccount.mxCredentials.userId]) + { + return; + } + + MXRestClient *mxRestClient = [[MXRestClient alloc] initWithCredentials:self.userAccount.mxCredentials + andOnUnrecognizedCertificateBlock:nil + andPersistentTokenDataHandler:^(void (^handler)(NSArray *credentials, void (^completion)(BOOL didUpdateCredentials))) { + [[MXKAccountManager sharedManager] readAndWriteCredentials:handler]; + } andUnauthenticatedHandler:nil]; + + fakeSession = [[MXSession alloc] initWithMatrixRestClient:mxRestClient]; +} + - (void)didStartSending { [self.shareViewController showProgressIndicator];