From f053f2ff5bcaca07759f6f9cf173d9eab2e25c9d Mon Sep 17 00:00:00 2001 From: manuroe Date: Tue, 12 Apr 2016 14:19:38 +0200 Subject: [PATCH] Universal link: Handle the case where the link contains a room alias that only the HS knows. Do it by asking hs to resolve the alias into a room id --- Vector/AppDelegate.m | 45 +++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/Vector/AppDelegate.m b/Vector/AppDelegate.m index 173c65763..d1d86203d 100644 --- a/Vector/AppDelegate.m +++ b/Vector/AppDelegate.m @@ -688,15 +688,22 @@ - (BOOL)handleUniversalLink:(NSUserActivity*)userActivity { - BOOL continueUserActivity = NO; - NSURL *webURL = userActivity.webpageURL; NSLog(@"[AppDelegate] handleUniversalLink: %@", webURL.absoluteString); + return [self handleUniversalLinkFragment:webURL.fragment]; +} + +- (BOOL)handleUniversalLinkFragment:(NSString*)fragment +{ + BOOL continueUserActivity = NO; + + NSLog(@"[AppDelegate] handleUniversalLinkFragment: %@", fragment); + // Extract params NSArray *pathParams; NSMutableDictionary *queryParams; - [self parseUniversalLinkFragment:webURL.fragment outPathParams:&pathParams outQueryParams:&queryParams]; + [self parseUniversalLinkFragment:fragment outPathParams:&pathParams outQueryParams:&queryParams]; // Check the action to do if ([pathParams[0] isEqualToString:@"room"] && pathParams.count >= 2) @@ -723,19 +730,31 @@ } } - if (!eventId) - { - // Open the room page - [self showRoom:roomId andEventId:nil withMatrixSession:account.mxSession]; - } - else - { - // Open the room page in read only focusing on the passed event - [self showRoom:roomId andEventId:eventId withMatrixSession:account.mxSession]; - } + // Open the room page + [self showRoom:roomId andEventId:eventId withMatrixSession:account.mxSession]; continueUserActivity = YES; } + else if ([roomIdOrAlias hasPrefix:@"#"] && [MXKAccountManager sharedManager].activeAccounts.count) + { + // The alias may be not part of user's rooms states + // Ask the HS to resolve the room alias into a room id + MXKAccount* account = [MXKAccountManager sharedManager].activeAccounts.firstObject; + [account.mxSession.matrixRestClient roomIDForRoomAlias:roomIdOrAlias success:^(NSString *roomId) { + + // Retry opening the link but with the returned room id + NSString *newUniversalLinkFragment = + [fragment stringByReplacingOccurrencesOfString:[roomIdOrAlias stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] + withString:[roomId stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; + [self handleUniversalLinkFragment:newUniversalLinkFragment]; + + } failure:^(NSError *error) { + NSLog(@"[AppDelegate] Universal link. Error: The home server failed to resolve the room alias (%@)", roomIdOrAlias); + }]; + + // Let's say we are handling the case + continueUserActivity = YES; + } else { // TODO