From 237d533492686aec1cca354ca304c9d8c462e05b Mon Sep 17 00:00:00 2001 From: manuroe Date: Thu, 7 Nov 2019 18:46:50 +0100 Subject: [PATCH] Integrations: Use the integrations manager provided by the homeserver admin via .well-known #2815 --- CHANGES.rst | 3 +- Riot/Managers/Widgets/WidgetManager.m | 57 ++++++++++++++++++- .../Widgets/WidgetManagerConfig.swift | 9 --- 3 files changed, 57 insertions(+), 12 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 738c6c413f..4fc8e61514 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -10,7 +10,8 @@ Bug fix: * RoomVC: Fix crash occurring when tap on an unsent media with retrieved event equal to nil. * Emoji Picker: Background color is not white (#2630). * Device Verification: Selecting 'start verification' from a keyshare request wedges you in an entirely blank verification screen (#2504). - * Integrations: Fix terms consent display when they are required + * Integrations: Fix terms consent display when they are required. + * Integrations: Use the integrations manager provided by the homeserver admin via .well-known (#2815). Changes in 0.10.0 (2019-10-11) =============================================== diff --git a/Riot/Managers/Widgets/WidgetManager.m b/Riot/Managers/Widgets/WidgetManager.m index 08c76edcc5..f38e6f5b1a 100644 --- a/Riot/Managers/Widgets/WidgetManager.m +++ b/Riot/Managers/Widgets/WidgetManager.m @@ -50,6 +50,9 @@ @interface WidgetManager () // User id -> scalar token NSMutableDictionary *configs; + + // User id -> MXSession + NSMutableDictionary *matrixSessions; } @end @@ -73,6 +76,7 @@ - (instancetype)init self = [super init]; if (self) { + matrixSessions = [NSMutableDictionary dictionary]; widgetEventListener = [NSMutableDictionary dictionary]; successBlockForWidgetCreation = [NSMutableDictionary dictionary]; failureBlockForWidgetCreation = [NSMutableDictionary dictionary]; @@ -367,6 +371,8 @@ - (void)addMatrixSession:(MXSession *)mxSession { __weak __typeof__(self) weakSelf = self; + matrixSessions[mxSession.matrixRestClient.credentials.userId] = mxSession; + NSString *hash = [NSString stringWithFormat:@"%p", mxSession]; id listener = [mxSession listenToEventsOfTypes:@[kWidgetMatrixEventTypeString, kWidgetModularEventTypeString] onEvent:^(MXEvent *event, MXTimelineDirection direction, id customObject) { @@ -421,6 +427,12 @@ - (void)addMatrixSession:(MXSession *)mxSession - (void)removeMatrixSession:(MXSession *)mxSession { + // Remove by value in a dict + for (NSString *key in [matrixSessions allKeysForObject:mxSession]) + { + [matrixSessions removeObjectForKey:key]; + } + // mxSession.myUser.userId and mxSession.matrixRestClient.credentials.userId may be nil here // So, use a kind of hash value instead NSString *hash = [NSString stringWithFormat:@"%p", mxSession]; @@ -433,18 +445,59 @@ - (void)removeMatrixSession:(MXSession *)mxSession [failureBlockForWidgetCreation removeObjectForKey:hash]; } +- (MXSession*)matrixSessionForUser:(NSString*)userId +{ + return matrixSessions[userId]; +} + - (void)deleteDataForUser:(NSString *)userId { [configs removeObjectForKey:userId]; [self saveConfigs]; } +#pragma mark - User integrations configuration + +- (WidgetManagerConfig*)createWidgetManagerConfigForUser:(NSString*)userId +{ + WidgetManagerConfig *config; + + MXSession *session = [self matrixSessionForUser:userId]; + + // Find the integrations settings for the user + + // First, look at matrix account + // TODO in another user story + + // Then, try to the homeserver configuration + MXWellknownIntegrationsManager *integrationsManager = session.homeserverWellknown.integrations.managers.firstObject; + if (integrationsManager) + { + config = [[WidgetManagerConfig alloc] initWithApiUrl:integrationsManager.apiUrl uiUrl:integrationsManager.uiUrl]; + } + else + { + // Fallback on app settings + config = [self createWidgetManagerConfigWithAppSettings]; + } + + return config; +} + +- (WidgetManagerConfig*)createWidgetManagerConfigWithAppSettings +{ + NSString *apiUrl = [[NSUserDefaults standardUserDefaults] objectForKey:@"integrationsRestUrl"]; + NSString *uiUrl = [[NSUserDefaults standardUserDefaults] objectForKey:@"integrationsUiUrl"]; + + return [[WidgetManagerConfig alloc] initWithApiUrl:apiUrl uiUrl:uiUrl]; +} + #pragma mark - Modular interface - (WidgetManagerConfig*)configForUser:(NSString*)userId { // Return a default config by default - return configs[userId] ? configs[userId] : [WidgetManagerConfig new]; + return configs[userId] ? configs[userId] : [self createWidgetManagerConfigForUser:userId]; } - (BOOL)hasIntegrationManagerForUser:(NSString*)userId @@ -691,7 +744,7 @@ - (void)loadConfigs NSLog(@"[WidgetManager] migrate scalarTokens to integrationManagerConfigs for %@", userId); - WidgetManagerConfig *config = [WidgetManagerConfig new]; + WidgetManagerConfig *config = [self createWidgetManagerConfigWithAppSettings]; config.scalarToken = scalarToken; configs[userId] = config; diff --git a/Riot/Managers/Widgets/WidgetManagerConfig.swift b/Riot/Managers/Widgets/WidgetManagerConfig.swift index 3bd8287479..05b48be7df 100644 --- a/Riot/Managers/Widgets/WidgetManagerConfig.swift +++ b/Riot/Managers/Widgets/WidgetManagerConfig.swift @@ -75,15 +75,6 @@ class WidgetManagerConfig: NSObject, NSCoding { super.init() } - override convenience init () { - // Use app settings as default - let apiUrl = UserDefaults.standard.object(forKey: "integrationsRestUrl") as? NSString - let uiUrl = UserDefaults.standard.object(forKey: "integrationsUiUrl") as? NSString - - self.init(apiUrl: apiUrl, uiUrl: uiUrl) - } - - /// MARK: - NSCoding enum CodingKeys: String {