Skip to content

Commit

Permalink
perf: Read number of pending invitations from header
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Müller <[email protected]>
  • Loading branch information
SystemKeeper committed Jan 23, 2025
1 parent fc0001c commit 7e4e82f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 48 deletions.
10 changes: 10 additions & 0 deletions NextcloudTalk/FederationInvitationTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
17 changes: 14 additions & 3 deletions NextcloudTalk/NCAPIControllerExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
}
}

Expand Down Expand Up @@ -609,7 +620,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)
}
Expand Down
1 change: 0 additions & 1 deletion NextcloudTalk/NCDatabaseManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,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

Expand Down
14 changes: 1 addition & 13 deletions NextcloudTalk/NCDatabaseManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

NSString *const kTalkDatabaseFolder = @"Library/Application Support/Talk";
NSString *const kTalkDatabaseFileName = @"talk.realm";
uint64_t const kTalkDatabaseSchemaVersion = 73;
uint64_t const kTalkDatabaseSchemaVersion = 74;

NSString * const kCapabilitySystemMessages = @"system-messages";
NSString * const kCapabilityNotificationLevels = @"notification-levels";
Expand Down Expand Up @@ -804,16 +804,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
27 changes: 0 additions & 27 deletions NextcloudTalk/NCRoomsManagerExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
11 changes: 7 additions & 4 deletions NextcloudTalk/RoomsTableViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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];
}

Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7e4e82f

Please sign in to comment.