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

Handle 426 error responses #1291

Merged
merged 5 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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
82 changes: 80 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,78 @@ - (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
{
NSString *appStoreURLString = @"itms-apps://itunes.apple.com/app/id";
BOOL canOpenAppStore = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:appStoreURLString]];

NSString *messageNotification = NSLocalizedString(@"The app is too old and no longer supported by this server.", nil);
NSString *messageAction = canOpenAppStore ? NSLocalizedString(@"Please update.", nil) : NSLocalizedString(@"Please contact your system administrator.", nil);
NSString *message = [NSString stringWithFormat:@"%@ %@", messageNotification, messageAction];

_updateAlertController = [UIAlertController
alertControllerWithTitle:NSLocalizedString(@"App is outdated", nil)
message:message
preferredStyle:UIAlertControllerStyleAlert];

_updateAlertControllerAccountId = accountId;

if (canOpenAppStore) {
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:@"%@%@", appStoreURLString, 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 +458,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
4 changes: 3 additions & 1 deletion NextcloudTalk/NCSplitViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@

override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
self.internalExecuteAfterTransition {
super.present(viewControllerToPresent, animated: flag, completion: completion)
if !viewControllerToPresent.isBeingPresented {
super.present(viewControllerToPresent, animated: flag, completion: completion)
}
}
}

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
18 changes: 18 additions & 0 deletions NextcloudTalk/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@
/* No comment provided by engineer. */
"App" = "App";

/* No comment provided by engineer. */
"App is outdated" = "App is outdated";

/* No comment provided by engineer. */
"Appear offline" = "Appear offline";

Expand Down Expand Up @@ -1174,9 +1177,15 @@
/* No comment provided by engineer. */
"Please check that you entered the correct Nextcloud server address." = "Please check that you entered the correct Nextcloud server address.";

/* No comment provided by engineer. */
"Please contact your system administrator." = "Please contact your system administrator.";

/* Please update your server with the latest {app name} version available. */
"Please update your server with the latest %@ version available." = "Please update your server with the latest %@ version available.";

/* No comment provided by engineer. */
"Please update." = "Please update.";

/* No comment provided by engineer. */
"Please, set a name for this conversation." = "Please, set a name for this conversation.";

Expand Down Expand Up @@ -1456,6 +1465,9 @@
/* No comment provided by engineer. */
"Submit vote" = "Submit vote";

/* No comment provided by engineer. */
"Switch account" = "Switch account";

/* No comment provided by engineer. */
"Switching to another conversation …" = "Switching to another conversation …";

Expand All @@ -1474,6 +1486,9 @@
/* No comment provided by engineer. */
"Tap and hold to record a voice message, release the button to send it." = "Tap and hold to record a voice message, release the button to send it.";

/* No comment provided by engineer. */
"The app is too old and no longer supported by this server." = "The app is too old and no longer supported by this server.";

/* No comment provided by engineer. */
"The password is wrong" = "The password is wrong";

Expand Down Expand Up @@ -1573,6 +1588,9 @@
/* No comment provided by engineer. */
"Unread messages" = "Unread messages";

/* No comment provided by engineer. */
"Update" = "Update";

/* No comment provided by engineer. */
"Upload failed" = "Upload failed";

Expand Down