Skip to content

Commit

Permalink
Integrations: Use the integrations manager provided by the homeserver…
Browse files Browse the repository at this point in the history
… admin via .well-known

#2815
  • Loading branch information
manuroe committed Nov 7, 2019
1 parent 394aedb commit 237d533
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 12 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
===============================================
Expand Down
57 changes: 55 additions & 2 deletions Riot/Managers/Widgets/WidgetManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ @interface WidgetManager ()

// User id -> scalar token
NSMutableDictionary<NSString*, WidgetManagerConfig*> *configs;

// User id -> MXSession
NSMutableDictionary<NSString*, MXSession*> *matrixSessions;
}

@end
Expand All @@ -73,6 +76,7 @@ - (instancetype)init
self = [super init];
if (self)
{
matrixSessions = [NSMutableDictionary dictionary];
widgetEventListener = [NSMutableDictionary dictionary];
successBlockForWidgetCreation = [NSMutableDictionary dictionary];
failureBlockForWidgetCreation = [NSMutableDictionary dictionary];
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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];
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 0 additions & 9 deletions Riot/Managers/Widgets/WidgetManagerConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 237d533

Please sign in to comment.