From f0e29e9391b1ba9df58eefc8dcd7f1767b714f3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=BCller?= Date: Thu, 23 Jan 2025 18:14:37 +0100 Subject: [PATCH] perf: Read number of pending invitations from header MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcel Müller --- ...erationInvitationTableViewController.swift | 10 +++++++ NextcloudTalk/NCAPIControllerExtensions.swift | 17 +++++++++--- NextcloudTalk/NCDatabaseManager.h | 1 - NextcloudTalk/NCDatabaseManager.m | 14 +--------- NextcloudTalk/NCRoomsManagerExtensions.swift | 27 ------------------- NextcloudTalk/RoomsTableViewController.m | 11 +++++--- NextcloudTalk/TalkAccount.h | 1 - 7 files changed, 32 insertions(+), 49 deletions(-) diff --git a/NextcloudTalk/FederationInvitationTableViewController.swift b/NextcloudTalk/FederationInvitationTableViewController.swift index 06705eebc..3578a6117 100644 --- a/NextcloudTalk/FederationInvitationTableViewController.swift +++ b/NextcloudTalk/FederationInvitationTableViewController.swift @@ -3,6 +3,14 @@ // SPDX-License-Identifier: GPL-3.0-or-later // +extension Notification.Name { + static let FederationInvitationDidAcceptNotification = Notification.Name(rawValue: "FederationInvitationDidAccept") +} + +@objc extension NSNotification { + public static let FederationInvitationDidAcceptNotification = Notification.Name.FederationInvitationDidAcceptNotification +} + class FederationInvitationTableViewController: UITableViewController, FederationInvitationCellDelegate { private let federationInvitationCellIdentifier = "FederationInvitationCell" @@ -120,6 +128,8 @@ class FederationInvitationTableViewController: UITableViewController, Federation NCAPIController.sharedInstance().acceptFederationInvitation(for: invitation.accountId, with: invitation.invitationId) { [weak self] success in if !success { NotificationPresenter.shared().present(text: NSLocalizedString("Failed to accept invitation", comment: ""), dismissAfterDelay: 5.0, includedStyle: .error) + } else { + NotificationCenter.default.post(name: .FederationInvitationDidAcceptNotification, object: self, userInfo: nil) } self?.getData() diff --git a/NextcloudTalk/NCAPIControllerExtensions.swift b/NextcloudTalk/NCAPIControllerExtensions.swift index e0e9569f2..472e53a05 100644 --- a/NextcloudTalk/NCAPIControllerExtensions.swift +++ b/NextcloudTalk/NCAPIControllerExtensions.swift @@ -28,6 +28,19 @@ import Foundation } apiSessionManager.getOcs(urlString, account: account, parameters: parameters) { ocsResponse, ocsError in + if let response = ocsResponse?.task?.response as? HTTPURLResponse { + var numberOfPendingInvitations = 0 + + // If the header is not present, there are no pending invites + if let federationInvitesString = response.allHeaderFields["x-nextcloud-talk-federation-invites"] as? String { + numberOfPendingInvitations = Int(federationInvitesString) ?? 0 + } + + if account.pendingFederationInvitations != numberOfPendingInvitations { + NCDatabaseManager.sharedInstance().setPendingFederationInvitationForAccountId(account.accountId, with: numberOfPendingInvitations) + } + } + // TODO: Move away from generic dictionary return type // let rooms = ocs?.dataArrayDict.compactMap { NCRoom(dictionary: $0, andAccountId: account.accountId) } completionBlock(ocsResponse?.dataArrayDict, ocsError?.error) @@ -261,8 +274,6 @@ import Foundation apiSessionManager.getOcs(urlString, account: account) { ocs, _ in let invitations = ocs?.dataArrayDict?.map { FederationInvitation(dictionary: $0, for: accountId) } completionBlock(invitations) - - NCDatabaseManager.sharedInstance().updateLastFederationInvitationUpdate(forAccountId: accountId, withTimestamp: Int(Date().timeIntervalSince1970)) } } @@ -664,7 +675,7 @@ import Foundation let urlString = "\(account.server)/ocs/v2.php/apps/notifications/api/v3/test/self" - apiSessionManager.postOcs(urlString, account: account) { ocsResponse, ocsError in + apiSessionManager.postOcs(urlString, account: account) { ocsResponse, _ in let message = ocsResponse?.dataDict?["message"] as? String completionBlock(message) } diff --git a/NextcloudTalk/NCDatabaseManager.h b/NextcloudTalk/NCDatabaseManager.h index b527c49c8..6673a6a60 100644 --- a/NextcloudTalk/NCDatabaseManager.h +++ b/NextcloudTalk/NCDatabaseManager.h @@ -148,7 +148,6 @@ extern NSString * const NCDatabaseManagerRoomCapabilitiesChangedNotification; - (void)increasePendingFederationInvitationForAccountId:(NSString *)accountId; - (void)decreasePendingFederationInvitationForAccountId:(NSString *)accountId; - (void)setPendingFederationInvitationForAccountId:(NSString *)accountId with:(NSInteger)numberOfPendingInvitations; -- (void)updateLastFederationInvitationUpdateForAccountId:(NSString *)accountId withTimestamp:(NSInteger)timestamp; @end diff --git a/NextcloudTalk/NCDatabaseManager.m b/NextcloudTalk/NCDatabaseManager.m index 5047f9d60..937ba4f6e 100644 --- a/NextcloudTalk/NCDatabaseManager.m +++ b/NextcloudTalk/NCDatabaseManager.m @@ -16,7 +16,7 @@ NSString *const kTalkDatabaseFolder = @"Library/Application Support/Talk"; NSString *const kTalkDatabaseFileName = @"talk.realm"; -uint64_t const kTalkDatabaseSchemaVersion = 74; +uint64_t const kTalkDatabaseSchemaVersion = 75; NSString * const kCapabilitySystemMessages = @"system-messages"; NSString * const kCapabilityNotificationLevels = @"notification-levels"; @@ -808,16 +808,4 @@ - (void)setPendingFederationInvitationForAccountId:(NSString *)accountId with:(N userInfo:nil]; } -- (void)updateLastFederationInvitationUpdateForAccountId:(NSString *)accountId withTimestamp:(NSInteger)timestamp -{ - BGTaskHelper *bgTask = [BGTaskHelper startBackgroundTaskWithName:@"updateLastFederationInvitationUpdateForAccountId" expirationHandler:nil]; - RLMRealm *realm = [RLMRealm defaultRealm]; - [realm beginWriteTransaction]; - NSPredicate *query = [NSPredicate predicateWithFormat:@"accountId = %@", accountId]; - TalkAccount *account = [TalkAccount objectsWithPredicate:query].firstObject; - account.lastPendingFederationInvitationFetch = timestamp; - [realm commitWriteTransaction]; - [bgTask stopBackgroundTask]; -} - @end diff --git a/NextcloudTalk/NCRoomsManagerExtensions.swift b/NextcloudTalk/NCRoomsManagerExtensions.swift index ee94c0734..f79a5b54a 100644 --- a/NextcloudTalk/NCRoomsManagerExtensions.swift +++ b/NextcloudTalk/NCRoomsManagerExtensions.swift @@ -440,31 +440,4 @@ import Foundation completionBlock?() } - // MARK: - Federation invitations - - public func checkUpdateNeededForPendingFederationInvitations() { - guard NCDatabaseManager.sharedInstance().serverHasTalkCapability(kCapabilityFederationV1) else { return } - - let activeAccount = NCDatabaseManager.sharedInstance().activeAccount() - let tenMinutesAgo = Int(Date().timeIntervalSince1970 - (10 * 60)) - - if activeAccount.lastPendingFederationInvitationFetch == 0 || activeAccount.lastPendingFederationInvitationFetch < tenMinutesAgo { - self.updatePendingFederationInvitations() - } - } - - public func updatePendingFederationInvitations() { - guard NCDatabaseManager.sharedInstance().serverHasTalkCapability(kCapabilityFederationV1) else { return } - - let activeAccount = NCDatabaseManager.sharedInstance().activeAccount() - - NCAPIController.sharedInstance().getFederationInvitations(for: activeAccount.accountId) { invitations in - guard let invitations else { return } - let pendingInvitations = invitations.filter { $0.invitationState != .accepted } - - if activeAccount.pendingFederationInvitations != pendingInvitations.count { - NCDatabaseManager.sharedInstance().setPendingFederationInvitationForAccountId(activeAccount.accountId, with: pendingInvitations.count) - } - } - } } diff --git a/NextcloudTalk/RoomsTableViewController.m b/NextcloudTalk/RoomsTableViewController.m index 610235764..1d6660d25 100644 --- a/NextcloudTalk/RoomsTableViewController.m +++ b/NextcloudTalk/RoomsTableViewController.m @@ -172,6 +172,7 @@ - (void)viewDidLoad [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(roomCreated:) name:NCRoomCreatedNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(activeAccountDidChange:) name:NCSettingsControllerDidChangeActiveAccountNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pendingInvitationsDidUpdate:) name:NCDatabaseManagerPendingFederationInvitationsDidChange object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(inviationDidAccept:) name:NSNotification.FederationInvitationDidAcceptNotification object:nil]; } - (void)setupNavigationBar @@ -329,7 +330,12 @@ - (void)roomsDidUpdate:(NSNotification *)notification - (void)pendingInvitationsDidUpdate:(NSNotification *)notification { - // Update the rooms: In case we accepted an invitation, we want to show the new room directly + [self refreshRoomList]; +} + +- (void)inviationDidAccept:(NSNotification *)notification +{ + // We accepted an invitation, so we refresh the rooms from the API to show it directly [self refreshRooms]; } @@ -405,7 +411,6 @@ - (void)stopRefreshRoomsTimer - (void)refreshRooms { [[NCRoomsManager sharedInstance] updateRoomsAndChatsUpdatingUserStatus:YES onlyLastModified:NO withCompletionBlock:nil]; - [[NCRoomsManager sharedInstance] checkUpdateNeededForPendingFederationInvitations]; if ([NCConnectionController sharedInstance].connectionState == kConnectionStateConnected) { [[NCRoomsManager sharedInstance] resendOfflineMessagesWithCompletionBlock:nil]; @@ -440,8 +445,6 @@ - (void)refreshControlTarget { [[NCRoomsManager sharedInstance] updateRoomsAndChatsUpdatingUserStatus:YES onlyLastModified:NO withCompletionBlock:nil]; - // When we manually forced a room update, we update the invitation list as well - [[NCRoomsManager sharedInstance] updatePendingFederationInvitations]; [self getUserStatusWithCompletionBlock:nil]; // Actuate `Peek` feedback (weak boom) diff --git a/NextcloudTalk/TalkAccount.h b/NextcloudTalk/TalkAccount.h index f36151e46..a4f3ea9c7 100644 --- a/NextcloudTalk/TalkAccount.h +++ b/NextcloudTalk/TalkAccount.h @@ -41,7 +41,6 @@ NS_ASSUME_NONNULL_BEGIN @property NSString *lastReceivedModifiedSince; @property NSInteger lastNotificationId; @property NSString *lastNotificationETag; -@property NSInteger lastPendingFederationInvitationFetch; @property NSInteger pendingFederationInvitations; @property NSString *frequentlyUsedEmojisJSONString;