From 22431b443bf0f4e2751b1d85ed5c19962a7c3c3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=BCller?= Date: Mon, 31 Oct 2022 13:56:07 +0100 Subject: [PATCH] Don't deliver local notifications when user status is DND MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcel Müller --- NextcloudTalk/NCAPIController.h | 2 +- NextcloudTalk/NCAPIController.m | 4 ++-- NextcloudTalk/NCNotificationController.m | 12 ++++++++++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/NextcloudTalk/NCAPIController.h b/NextcloudTalk/NCAPIController.h index ec061969c..b1f26e150 100644 --- a/NextcloudTalk/NCAPIController.h +++ b/NextcloudTalk/NCAPIController.h @@ -96,7 +96,7 @@ typedef void (^SetUserStatusCompletionBlock)(NSError *error); typedef void (^GetServerCapabilitiesCompletionBlock)(NSDictionary *serverCapabilities, NSError *error); typedef void (^GetServerNotificationCompletionBlock)(NSDictionary *notification, NSError *error, NSInteger statusCode); -typedef void (^GetServerNotificationsCompletionBlock)(NSArray *notifications, NSString *ETag, NSError *error); +typedef void (^GetServerNotificationsCompletionBlock)(NSArray *notifications, NSString *ETag, NSString *userStatus, NSError *error); typedef void (^SubscribeToNextcloudServerCompletionBlock)(NSDictionary *responseDict, NSError *error); typedef void (^UnsubscribeToNextcloudServerCompletionBlock)(NSError *error); diff --git a/NextcloudTalk/NCAPIController.m b/NextcloudTalk/NCAPIController.m index 3efe7058d..af23bee32 100644 --- a/NextcloudTalk/NCAPIController.m +++ b/NextcloudTalk/NCAPIController.m @@ -2417,11 +2417,11 @@ - (NSURLSessionDataTask *)getServerNotificationsForAccount:(TalkAccount *)accoun NSDictionary *headers = [self getResponseHeaders:response]; if (block) { - block(notifications, [headers objectForKey:@"ETag"], nil); + block(notifications, [headers objectForKey:@"ETag"], [headers objectForKey:@"x-nextcloud-user-status"], nil); } } else { if (block) { - block(nil, nil, error); + block(nil, nil, nil, error); } } }]; diff --git a/NextcloudTalk/NCNotificationController.m b/NextcloudTalk/NCNotificationController.m index 0e4b903ce..e9d7cfb03 100644 --- a/NextcloudTalk/NCNotificationController.m +++ b/NextcloudTalk/NCNotificationController.m @@ -32,6 +32,7 @@ #import "NCRoomsManager.h" #import "NCSettingsController.h" #import "NCUserInterfaceController.h" +#import "NCUserStatus.h" NSString * const NCNotificationControllerWillPresentNotification = @"NCNotificationControllerWillPresentNotification"; NSString * const NCLocalNotificationJoinChatNotification = @"NCLocalNotificationJoinChatNotification"; @@ -295,12 +296,15 @@ - (void)checkForNewNotificationsWithCompletionBlock:(CheckForNewNotificationsCom dispatch_group_enter(notificationsGroup); - [[NCAPIController sharedInstance] getServerNotificationsForAccount:account withLastETag:account.lastNotificationETag withCompletionBlock:^(NSArray *notifications, NSString* ETag, NSError *error) { + [[NCAPIController sharedInstance] getServerNotificationsForAccount:account withLastETag:account.lastNotificationETag withCompletionBlock:^(NSArray *notifications, NSString* ETag, NSString *userStatus, NSError *error) { if (error) { dispatch_group_leave(notificationsGroup); return; } + // Don't show notifications if the user has status "do not disturb" + BOOL suppressNotifications = (serverCapabilities.userStatus && [userStatus isEqualToString:kUserStatusDND]); + NSInteger lastNotificationId = 0; NSMutableArray *activeServerNotificationsIds = [NSMutableArray new]; @@ -318,7 +322,11 @@ - (void)checkForNewNotificationsWithCompletionBlock:(CheckForNewNotificationsCom lastNotificationId = serverNotification.notificationId; } - if (account.lastNotificationId != 0 && serverNotification.notificationId > account.lastNotificationId && serverNotification.notificationType != kNCNotificationTypeChat) { + if (suppressNotifications || serverNotification.notificationType != kNCNotificationTypeChat) { + continue; + } + + if (account.lastNotificationId != 0 && serverNotification.notificationId > account.lastNotificationId) { // Don't show notifications if this is the first time we retrieve notifications for this account // Otherwise after adding a new account all unread notifications from the server would be shown