From 0e31b555c16ebd10ad124b77c6bac309d11222ed Mon Sep 17 00:00:00 2001 From: QuentinArguillere Date: Tue, 17 May 2022 15:41:55 +0200 Subject: [PATCH] Implement notification mute for group & encrypted chatrooms --- Classes/ChatConversationView.m | 16 ++++-------- Classes/ChatsListTableView.m | 4 ++- Classes/LinphoneManager.h | 2 ++ Classes/LinphoneManager.m | 25 +++++++++++++++++++ .../NotificationService.swift | 15 +++++++++++ .../msgNotificationService.entitlements | 2 ++ 6 files changed, 52 insertions(+), 12 deletions(-) diff --git a/Classes/ChatConversationView.m b/Classes/ChatConversationView.m index f37ad3258..9ad062496 100644 --- a/Classes/ChatConversationView.m +++ b/Classes/ChatConversationView.m @@ -1721,13 +1721,6 @@ void on_chat_room_conference_alert(LinphoneChatRoom *cr, const LinphoneEventLog } } --(BOOL) isConversationMuted { - return FALSE; // TODO -} --(void) toggleMuteConversation { - // TODO -} - -(void) showAddressAndIdentityPopup { char *localAddress = linphone_address_as_string(linphone_chat_room_get_local_address(_chatRoom)); @@ -1799,7 +1792,8 @@ void on_chat_room_conference_alert(LinphoneChatRoom *cr, const LinphoneEventLog [PhoneMainView.instance popToView:view.compositeViewDescription]; } if ((!isEncrypted && indexPath.row == 1) || (isEncrypted && indexPath.row == 3)) { - [self toggleMuteConversation]; + [LinphoneManager setChatroomPushEnabled:_chatRoom withPushEnabled:![LinphoneManager getChatroomPushEnabled:_chatRoom]]; + [_popupMenu reloadData]; } if ((!isEncrypted && indexPath.row == 2) || (isEncrypted && indexPath.row == 4)) { @@ -1865,12 +1859,12 @@ void on_chat_room_conference_alert(LinphoneChatRoom *cr, const LinphoneEventLog } if ((isEncrypted && indexPath.row == 3) || (!isEncrypted && indexPath.row == 1)) { - if ([self isConversationMuted]) { + if ([LinphoneManager getChatroomPushEnabled:_chatRoom]) { cell.imageView.image = [LinphoneUtils resizeImage:[UIImage imageNamed:@"menu_notifications_off.png"] newSize:CGSizeMake(20, 25)]; - cell.textLabel.text = NSLocalizedString(@"NOT IMPLEMENTED",nil); + cell.textLabel.text = NSLocalizedString(@"Mute notifications",nil); } else { cell.imageView.image = [LinphoneUtils resizeImage:[UIImage imageNamed:@"menu_notifications_on.png"] newSize:CGSizeMake(20, 25)]; - cell.textLabel.text = NSLocalizedString(@"NOT IMPLEMENTED",nil); + cell.textLabel.text = NSLocalizedString(@"Un-mute notifications",nil); } } diff --git a/Classes/ChatsListTableView.m b/Classes/ChatsListTableView.m index 6f8c3fd36..dfc9152fe 100644 --- a/Classes/ChatsListTableView.m +++ b/Classes/ChatsListTableView.m @@ -231,7 +231,9 @@ void deletion_chat_room_state_changed(LinphoneChatRoom *cr, LinphoneChatRoomStat } } [ftdToDelete cancel]; - + + // Re-enable push notification after deleting the chatroom, in order to get the notification if we are re-invited, or for secure 1-to-1 chatrooms. + [LinphoneManager setChatroomPushEnabled:chatRoom withPushEnabled:TRUE]; linphone_core_delete_chat_room(LC, chatRoom); chatRooms = chatRooms->next; } diff --git a/Classes/LinphoneManager.h b/Classes/LinphoneManager.h index 2ee225e9b..ce2a60b64 100644 --- a/Classes/LinphoneManager.h +++ b/Classes/LinphoneManager.h @@ -192,6 +192,8 @@ typedef struct _LinphoneManagerSounds { - (BOOL)isCTCallCenterExist; - (void) checkLocalNetworkPermission; ++ (BOOL) getChatroomPushEnabled:(LinphoneChatRoom *)chatroom; ++ (void) setChatroomPushEnabled:(LinphoneChatRoom *)chatroom withPushEnabled:(BOOL)enabled; @property (readonly) BOOL isTesting; @property(readonly, strong) FastAddressBook *fastAddressBook; diff --git a/Classes/LinphoneManager.m b/Classes/LinphoneManager.m index 06a9b8fef..67667dac8 100644 --- a/Classes/LinphoneManager.m +++ b/Classes/LinphoneManager.m @@ -2303,6 +2303,31 @@ void linphone_iphone_conference_state_changed(LinphoneCore *lc, LinphoneConferen [NSNotificationCenter.defaultCenter postNotificationName:kLinphoneConfStateChanged object:nil userInfo:dict]; } ++ (BOOL) getChatroomPushEnabled:(LinphoneChatRoom *)chatroom { + bool currently_enabled = true; + NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:kLinphoneMsgNotificationAppGroupId]; + NSDictionary *chatroomsPushStatus = [defaults dictionaryForKey:@"chatroomsPushStatus"]; + if (chatroomsPushStatus != nil && chatroom) { + char *uri = linphone_address_as_string_uri_only(linphone_chat_room_get_peer_address(chatroom)); + NSString* pushStatus = [chatroomsPushStatus objectForKey:[NSString stringWithUTF8String:uri]]; + currently_enabled = (pushStatus == nil) || [pushStatus isEqualToString:@"enabled"]; + ms_free(uri); + } + return currently_enabled; +} ++ (void) setChatroomPushEnabled:(LinphoneChatRoom *)chatroom withPushEnabled:(BOOL)enabled { + if (!chatroom) return; + + NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:kLinphoneMsgNotificationAppGroupId]; + NSMutableDictionary *chatroomsPushStatus = [[NSMutableDictionary alloc] initWithDictionary:[defaults dictionaryForKey:@"chatroomsPushStatus"]]; + if (chatroomsPushStatus == nil) chatroomsPushStatus = [[NSMutableDictionary dictionary] init]; + + char *uri = linphone_address_as_string_uri_only(linphone_chat_room_get_peer_address(chatroom)); + [chatroomsPushStatus setValue:(enabled ? @"enabled" : @"disabled") forKey:[NSString stringWithUTF8String:uri]]; + ms_free(uri); + + [defaults setObject:chatroomsPushStatus forKey:@"chatroomsPushStatus"]; +} @end diff --git a/msgNotificationService/NotificationService.swift b/msgNotificationService/NotificationService.swift index a47699e24..8fe8690c2 100644 --- a/msgNotificationService/NotificationService.swift +++ b/msgNotificationService/NotificationService.swift @@ -64,6 +64,21 @@ class NotificationService: UNNotificationServiceExtension { createCore() NotificationService.log.message(message: "received push payload : \(bestAttemptContent.userInfo.debugDescription)") + + let defaults = UserDefaults.init(suiteName: APP_GROUP_ID) + if let chatroomsPushStatus = defaults?.dictionary(forKey: "chatroomsPushStatus") { + let aps = bestAttemptContent.userInfo["aps"] as? NSDictionary + let alert = aps?["alert"] as? NSDictionary + let fromAddresses = alert?["loc-args"] as? [String] + + if let from = fromAddresses?.first { + if ((chatroomsPushStatus[from] as? String) == "disabled") { + NotificationService.log.message(message: "message comes from a muted chatroom, ignore it") + contentHandler(UNNotificationContent()) + } + } + } + if let chatRoomInviteAddr = bestAttemptContent.userInfo["chat-room-addr"] as? String, !chatRoomInviteAddr.isEmpty { NotificationService.log.message(message: "fetch chat room for invite, addr: \(chatRoomInviteAddr)") let chatRoom = lc!.getNewChatRoomFromConfAddr(chatRoomAddr: chatRoomInviteAddr) diff --git a/msgNotificationService/msgNotificationService.entitlements b/msgNotificationService/msgNotificationService.entitlements index b63a67474..31f88ecaf 100644 --- a/msgNotificationService/msgNotificationService.entitlements +++ b/msgNotificationService/msgNotificationService.entitlements @@ -2,6 +2,8 @@ + com.apple.developer.usernotifications.filtering + com.apple.security.application-groups group.org.linphone.phone.msgNotification