Skip to content

Commit

Permalink
[ios] Fix native suspension, #2858
Browse files Browse the repository at this point in the history
  • Loading branch information
charlag authored and bedhub committed May 21, 2021
1 parent 3611cc5 commit 48cdcde
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions app-ios/tutanota/Sources/TUTAlarmManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
static const int EVENTS_SCHEDULED_AHEAD = 24;
static const long MISSED_NOTIFICATION_TTL_SEC = 30L * 24 * 60 * 60; // 30 days

static const int OK_HTTP_CODE = 200;
static const int NOT_AUTHENTICATED_HTTP_CODE = 401;
static const int NOT_FOUND_HTTP_CODE = 404;
static const int TOO_MANY_REQUESTS_HTTP_CODE = 429;
static const int SERVICE_UNAVAILABLE_HTTP_CODE = 503;

@interface TUTAlarmManager ()
@property (nonnull, readonly) TUTKeychainManager *keychainManager;
@property (nonnull, readonly) TUTUserPreferenceFacade *userPreference;
Expand Down Expand Up @@ -107,33 +113,25 @@ - (void)fetchMissedNotifications:(void(^)(NSError *_Nullable error))completionHa
TUTLog(@"Fetched missed notifications with status code %zd, error: %@", httpResponse.statusCode, error);
if (error) {
complete(error);
} else if (httpResponse.statusCode == 401) {
} else if (httpResponse.statusCode == NOT_AUTHENTICATED_HTTP_CODE) {
TUTLog(@"Not authenticated to download missed notification w/ user %@", userId);
// Not authenticated, remove user id and try again with the next one
[strongSelf unscheduleAllAlarmsForUserId:userId];
[strongSelf.userPreference removeUser:userId];
queueCompletionHandler();
[strongSelf fetchMissedNotifications:completionHandler];
} else if (httpResponse.statusCode == 503) {
NSString *_Nullable retryAfterHeader = httpResponse.allHeaderFields[@"Retry-After"];
if (retryAfterHeader == nil) {
retryAfterHeader = httpResponse.allHeaderFields[@"Suspension-Time"];
}
int suspensionTime;
if (retryAfterHeader != nil) {
suspensionTime = retryAfterHeader.intValue;
} else {
suspensionTime = 0;
}
} else if (httpResponse.statusCode == SERVICE_UNAVAILABLE_HTTP_CODE ||
httpResponse.statusCode == TOO_MANY_REQUESTS_HTTP_CODE) {
int suspensionTime = [strongSelf extractSuspensionTimeFrom:httpResponse];
TUTLog(@"ServiceUnavailible when downloading missed notifications, waiting for %d s", suspensionTime);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, suspensionTime * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
TUTLog(@"Timer fired");
[weakSelf fetchMissedNotifications:completionHandler];
});
queueCompletionHandler();
} else if (httpResponse.statusCode == 404) {
} else if (httpResponse.statusCode == NOT_FOUND_HTTP_CODE) {
complete(nil);
} else if (httpResponse.statusCode != 200) {
} else if (httpResponse.statusCode != OK_HTTP_CODE) {
let error = [NSError errorWithDomain:TUT_NETWORK_ERROR
code:httpResponse.statusCode
userInfo:@{@"message": @"Failed to fetch missed notification"}
Expand Down Expand Up @@ -162,6 +160,20 @@ - (void)fetchMissedNotifications:(void(^)(NSError *_Nullable error))completionHa

}

- (int)extractSuspensionTimeFrom:(NSHTTPURLResponse *)httpResponse {
NSString *_Nullable retryAfterHeader = httpResponse.allHeaderFields[@"Retry-After"];
if (retryAfterHeader == nil) {
retryAfterHeader = httpResponse.allHeaderFields[@"Suspension-Time"];
}
int suspensionTime;
if (retryAfterHeader != nil) {
suspensionTime = retryAfterHeader.intValue;
} else {
suspensionTime = 0;
}
return suspensionTime;
}

- (BOOL)hasNotificationTTLExpired {
let lastMissedNotificationCheckTime = _userPreference.lastMissedNotificationCheckTime;
if (!lastMissedNotificationCheckTime) {
Expand Down

0 comments on commit 48cdcde

Please sign in to comment.