Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrations: Use the integrations manager provided by the homeserver admin #2823

Merged
merged 3 commits into from
Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +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: Use the integrations manager provided by the homeserver admin via .well-known (#2815).

Changes in 0.10.0 (2019-10-11)
===============================================
Expand Down
88 changes: 69 additions & 19 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 @@ -619,28 +672,25 @@ - (MXHTTPOperation *)validateScalarToken:(NSString*)scalarToken forMXSession:(MX

NSLog(@"[WidgetManager] validateScalarToken. Error in modular/account request. statusCode: %@", @(urlResponse.statusCode));

if (urlResponse && urlResponse.statusCode / 100 != 2)
MXError *mxError = [[MXError alloc] initWithNSError:error];
if ([mxError.errcode isEqualToString:kMXErrCodeStringTermsNotSigned])
{
NSLog(@"[WidgetManager] validateScalarToke. Error: Need to accept terms");
NSError *termsNotSignedError = [NSError errorWithDomain:WidgetManagerErrorDomain
code:WidgetManagerErrorCodeTermsNotSigned
userInfo:@{
NSLocalizedDescriptionKey:error.userInfo[NSLocalizedDescriptionKey]
}];

failure(termsNotSignedError);
}
else if (urlResponse && urlResponse.statusCode / 100 != 2)
{
complete(NO);
}
else if (failure)
{
MXError *mxError = [[MXError alloc] initWithNSError:error];
if ([mxError.errcode isEqualToString:kMXErrCodeStringTermsNotSigned])
{
NSLog(@"[WidgetManager] validateScalarToke. Error: Need to accept terms");
NSError *termsNotSignedError = [NSError errorWithDomain:WidgetManagerErrorDomain
code:WidgetManagerErrorCodeTermsNotSigned
userInfo:@{
NSLocalizedDescriptionKey:error.userInfo[NSLocalizedDescriptionKey]
}];

failure(termsNotSignedError);
}
else
{
failure(error);
}
failure(error);
}
}];
}
Expand Down Expand Up @@ -694,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