Skip to content

Commit

Permalink
Handle 426 error responses.
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Sein <[email protected]>
  • Loading branch information
Ivansss committed Jul 3, 2023
1 parent 8658ad4 commit c6631c1
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 5 deletions.
5 changes: 5 additions & 0 deletions NextcloudTalk/NCAPIController.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ typedef void (^SetUserProfileFieldCompletionBlock)(NSError *error, NSInteger sta
typedef void (^GetUserStatusCompletionBlock)(NSDictionary *userStatus, NSError *error);
typedef void (^SetUserStatusCompletionBlock)(NSError *error);

typedef void (^GetAppIdCompletionBlock)(NSString *appId, NSError *error);

typedef void (^GetServerCapabilitiesCompletionBlock)(NSDictionary *serverCapabilities, NSError *error);

typedef void (^GetServerNotificationCompletionBlock)(NSDictionary *notification, NSError *error, NSInteger statusCode);
Expand Down Expand Up @@ -155,6 +157,9 @@ extern NSInteger const kReceivedChatMessagesLimit;
- (NSString *)filesPathForAccount:(TalkAccount *)account;
- (SDWebImageDownloaderRequestModifier *)getRequestModifierForAccount:(TalkAccount *)account;

// App Store
- (NSURLSessionDataTask *)getAppStoreAppIdWithCompletionBlock:(GetAppIdCompletionBlock)block;

// Contacts Controller
- (NSURLSessionDataTask *)searchContactsForAccount:(TalkAccount *)account withPhoneNumbers:(NSDictionary *)phoneNumbers andCompletionBlock:(GetContactsWithPhoneNumbersCompletionBlock)block;
- (NSURLSessionDataTask *)getContactsForAccount:(TalkAccount *)account forRoom:(NSString *)room groupRoom:(BOOL)groupRoom withSearchParam:(NSString *)search andCompletionBlock:(GetContactsCompletionBlock)block;
Expand Down
31 changes: 31 additions & 0 deletions NextcloudTalk/NCAPIController.m
Original file line number Diff line number Diff line change
Expand Up @@ -2695,6 +2695,31 @@ - (NSURLSessionDataTask *)setUserStatus:(NSString *)status forAccount:(TalkAccou
return task;
}

#pragma mark - App Store info

- (NSURLSessionDataTask *)getAppStoreAppIdWithCompletionBlock:(GetAppIdCompletionBlock)block
{
NSString *URLString = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?bundleId=%@", bundleIdentifier];

NSURLSessionDataTask *task = [_defaultAPISessionManager GET:URLString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSString *appId = nil;
NSArray *results = [responseObject objectForKey:@"results"];
if (results.count > 0) {
NSDictionary *appInfo = [results objectAtIndex:0];
appId = [[appInfo objectForKey:@"trackId"] stringValue];
}
if (block) {
block(appId, nil);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (block) {
block(nil, error);
}
}];

return task;
}

#pragma mark - Server capabilities

- (NSURLSessionDataTask *)getServerCapabilitiesForServer:(NSString *)server withCompletionBlock:(GetServerCapabilitiesCompletionBlock)block
Expand Down Expand Up @@ -3136,6 +3161,12 @@ - (void)checkResponseStatusCode:(NSInteger)statusCode forAccount:(TalkAccount *)
[[NSNotificationCenter defaultCenter] postNotificationName:NCTokenRevokedResponseReceivedNotification
object:self
userInfo:userInfo];
} else if (statusCode == 426) {
// Upgrade required
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:account.accountId forKey:@"accountId"];
[[NSNotificationCenter defaultCenter] postNotificationName:NCUpgradeRequiredResponseReceivedNotification
object:self
userInfo:userInfo];
} else if (statusCode == 503) {
// Server is in maintenance mode
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:account.accountId forKey:@"accountId"];
Expand Down
3 changes: 3 additions & 0 deletions NextcloudTalk/NCSettingsController.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

#import "ARDSettingsModel.h"

Expand Down Expand Up @@ -71,6 +72,8 @@ typedef enum NCPreferredFileSorting {

@property (nonatomic, strong) NSMutableArray *supportedBrowsers;
@property (nonatomic, copy) ARDSettingsModel *videoSettingsModel;
@property (nonatomic, strong) UIAlertController *updateAlertController;
@property (nonatomic, strong) NSString *updateAlertControllerAccountId;
@property (nonatomic, strong) NSMutableDictionary *signalingConfigurations; // accountId -> signalingConfigutation
@property (nonatomic, strong) NSMutableDictionary *externalSignalingControllers; // accountId -> externalSignalingController

Expand Down
73 changes: 71 additions & 2 deletions NextcloudTalk/NCSettingsController.m
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ - (id)init
[self configureAppSettings];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRevokedResponseReceived:) name:NCTokenRevokedResponseReceivedNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(upgradeRequiredResponseReceived:) name:NCUpgradeRequiredResponseReceivedNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(talkConfigurationHasChanged:) name:NCTalkConfigurationHashChangedNotification object:nil];
}
return self;
Expand Down Expand Up @@ -206,6 +207,69 @@ - (void)tokenRevokedResponseReceived:(NSNotification *)notification
}];
}

- (void)upgradeRequiredResponseReceived:(NSNotification *)notification
{
NSString *accountId = [notification.userInfo objectForKey:@"accountId"];
if (!_updateAlertController || ![_updateAlertControllerAccountId isEqualToString:accountId]) {
[self createUpdateAlertContollerForAccountId:accountId];
}

[[NCUserInterfaceController sharedInstance] presentAlertIfNotPresentedAlready:_updateAlertController];

}

- (void)createUpdateAlertContollerForAccountId:(NSString *)accountId
{
_updateAlertController = [UIAlertController
alertControllerWithTitle:NSLocalizedString(@"App is outdated", nil)
message:NSLocalizedString(@"The app is too old and no longer supported by this server. Please update.", nil)
preferredStyle:UIAlertControllerStyleAlert];

_updateAlertControllerAccountId = accountId;

UIAlertAction* updateButton = [UIAlertAction
actionWithTitle:NSLocalizedString(@"Update", nil)
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {

[[NCAPIController sharedInstance] getAppStoreAppIdWithCompletionBlock:^(NSString *appId, NSError *error) {
if (appId.length > 0) {
NSURL *appStoreURL = [NSURL URLWithString:[NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id%@",appId]];
[[UIApplication sharedApplication] openURL:appStoreURL options:@{} completionHandler:nil];
}

self->_updateAlertControllerAccountId = nil;
}];
}];

[_updateAlertController addAction:updateButton];

NSArray *inactiveAccounts = [[NCDatabaseManager sharedInstance] inactiveAccounts];
if (inactiveAccounts.count > 0) {
UIAlertAction* switchAccountButton = [UIAlertAction
actionWithTitle:NSLocalizedString(@"Switch account", nil)
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {

[self switchToAnyInactiveAccount];
self->_updateAlertControllerAccountId = nil;
}];

[_updateAlertController addAction:switchAccountButton];
}

UIAlertAction* logoutButton = [UIAlertAction
actionWithTitle:NSLocalizedString(@"Log out", nil)
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * _Nonnull action) {

[[NCUserInterfaceController sharedInstance] logOutAccountWithAccountId:accountId];
self->_updateAlertControllerAccountId = nil;
}];

[_updateAlertController addAction:logoutButton];
}

- (void)talkConfigurationHasChanged:(NSNotification *)notification
{
TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount];
Expand Down Expand Up @@ -385,13 +449,18 @@ - (void)logoutAccountWithAccountId:(NSString *)accountId withCompletionBlock:(Lo
[self createAccountsFile];

// Activate any of the inactive accounts
[self switchToAnyInactiveAccount];

if (block) block(nil);
}

- (void)switchToAnyInactiveAccount
{
NSArray *inactiveAccounts = [[NCDatabaseManager sharedInstance] inactiveAccounts];
if (inactiveAccounts.count > 0) {
TalkAccount *inactiveAccount = [inactiveAccounts objectAtIndex:0];
[self setActiveAccountWithAccountId:inactiveAccount.accountId];
}

if (block) block(nil);
}

#pragma mark - App settings
Expand Down
2 changes: 2 additions & 0 deletions NextcloudTalk/NCUserInterfaceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ typedef void (^PresentCallControllerCompletionBlock)(void);
- (void)presentChatForPushNotification:(NCPushNotification *)pushNotification;
- (void)presentAlertForPushNotification:(NCPushNotification *)pushNotification;
- (void)presentAlertViewController:(UIAlertController *)alertViewController;
- (void)presentAlertIfNotPresentedAlready:(UIAlertController *)alertViewController;
- (void)presentAlertWithTitle:(NSString *)title withMessage:(NSString *)message;
- (void)presentChatViewController:(NCChatViewController *)chatViewController;
- (void)presentCallViewController:(CallViewController *)callViewController withCompletionBlock:(PresentCallControllerCompletionBlock)block;
- (void)presentCallKitCallInRoom:(NSString *)token withVideoEnabled:(BOOL)video;
- (void)presentChatForURL:(NSURLComponents *)urlComponents;
- (void)presentLoginViewControllerForServerURL:(NSString *)serverURL withUser:(NSString *)user;
- (void)presentSettingsViewController;
- (void)logOutAccountWithAccountId:(NSString *)accountId;

@end
18 changes: 15 additions & 3 deletions NextcloudTalk/NCUserInterfaceController.m
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,20 @@ - (void)presentServerMaintenanceModeWarning:(NSNotification *)notification
}
}

- (void)logOutCurrentUser
- (void)logOutAccountWithAccountId:(NSString *)accountId
{
TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount];
[[NCSettingsController sharedInstance] logoutAccountWithAccountId:activeAccount.accountId withCompletionBlock:^(NSError *error) {
[[NCSettingsController sharedInstance] logoutAccountWithAccountId:accountId withCompletionBlock:^(NSError *error) {
[[NCUserInterfaceController sharedInstance] presentConversationsList];
[[NCConnectionController sharedInstance] checkAppState];
}];
}

- (void)logOutCurrentUser
{
TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount];
[self logOutAccountWithAccountId:activeAccount.accountId];
}

- (void)presentChatForLocalNotification:(NSDictionary *)userInfo
{
if ([NCConnectionController sharedInstance].appState != kAppStateReady) {
Expand Down Expand Up @@ -326,6 +331,13 @@ - (void)presentAlertViewController:(UIAlertController *)alertViewController
}
}

- (void)presentAlertIfNotPresentedAlready:(UIAlertController *)alertViewController
{
if (alertViewController != _mainViewController.presentedViewController) {
[self presentAlertViewController:alertViewController];
}
}

- (void)presentAlertWithTitle:(NSString *)title withMessage:(NSString *)message
{
UIAlertController *alertDialog = [UIAlertController alertControllerWithTitle:title
Expand Down
1 change: 1 addition & 0 deletions NextcloudTalk/NotificationCenterNotifications.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extern NSString * const NCOutdatedTalkVersionNotification;
extern NSString * const NCServerCapabilitiesUpdatedNotification;
extern NSString * const NCUserProfileImageUpdatedNotification;
extern NSString * const NCTokenRevokedResponseReceivedNotification;
extern NSString * const NCUpgradeRequiredResponseReceivedNotification;
extern NSString * const NCURLWantsToOpenConversationNotification;
extern NSString * const NCServerMaintenanceModeNotification;
extern NSString * const NCTalkConfigurationHashChangedNotification;
Expand Down
1 change: 1 addition & 0 deletions NextcloudTalk/NotificationCenterNotifications.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
NSString * const NCServerCapabilitiesUpdatedNotification = @"NCServerCapabilitiesUpdatedNotification";
NSString * const NCUserProfileImageUpdatedNotification = @"NCUserProfileImageUpdatedNotification";
NSString * const NCTokenRevokedResponseReceivedNotification = @"NCTokenRevokedResponseReceivedNotification";
NSString * const NCUpgradeRequiredResponseReceivedNotification = @"NCUpgradeRequiredResponseReceivedNotification";
NSString * const NCURLWantsToOpenConversationNotification = @"NCURLWantsToOpenConversationNotification";
NSString * const NCServerMaintenanceModeNotification = @"NCServerMaintenanceModeNotification";
NSString * const NCTalkConfigurationHashChangedNotification = @"NCTalkConfigurationHashChangedNotification";
Expand Down

0 comments on commit c6631c1

Please sign in to comment.