From 074a2d79106a29b85ee87e0b69e04d3703cdc6fb Mon Sep 17 00:00:00 2001 From: Mauro Romito Date: Thu, 15 Oct 2026 15:13:38 +0200 Subject: [PATCH] Remember seen invites per account Seen invites were stored for the whole app, so an invite seen by one account showed as seen in another account invited to the same room. They now live in the account settings, are migrated to the first account restored and are wiped when signing out. --- .../Sources/Application/AppCoordinator.swift | 5 +++++ .../Settings/AccountSettings.swift | 5 +++++ .../Application/Settings/AppSettings.swift | 5 ----- .../HomeScreen/HomeScreenViewModel.swift | 8 ++++---- .../JoinRoomScreenViewModel.swift | 6 +++--- .../Sources/HomeScreenViewModelTests.swift | 16 +++++++-------- .../JoinRoomScreenViewModelTests.swift | 20 +++++++++---------- 7 files changed, 35 insertions(+), 30 deletions(-) diff --git a/ElementX/Sources/Application/AppCoordinator.swift b/ElementX/Sources/Application/AppCoordinator.swift index 48aa8f032..f0d8df816 100644 --- a/ElementX/Sources/Application/AppCoordinator.swift +++ b/ElementX/Sources/Application/AppCoordinator.swift @@ -579,6 +579,11 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationFlowCoordinatorDeleg MXLog.info("Migrating to version 26.10.3, moving the notifications switch into the account.") userSession.userSettings.account.migrateAppSettingsValue(\.enableNotificationsKey) } + + if oldVersion < Version(26, 10, 4) { + MXLog.info("Migrating to version 26.10.4, moving the seen invites into the account.") + userSession.userSettings.account.migrateAppSettingsValue(\.seenInvitesKey) + } } /// This could be removed once the adoption of 25.06.x is widespread. diff --git a/ElementX/Sources/Application/Settings/AccountSettings.swift b/ElementX/Sources/Application/Settings/AccountSettings.swift index 9fb9d5dc7..302b6562c 100644 --- a/ElementX/Sources/Application/Settings/AccountSettings.swift +++ b/ElementX/Sources/Application/Settings/AccountSettings.swift @@ -27,6 +27,11 @@ final nonisolated class AccountSettings: @unchecked Sendable { @UserPreference(defaultValue: [SearchBreadcrumb]()) var searchBreadcrumbs: [SearchBreadcrumb] + /// The Set of room identifiers of invites that the user already saw in the invites list. + /// This Set is being used to implement badges for unread invites. + @UserPreference(defaultValue: Set()) + var seenInvites: Set + // MARK: Visible Preferences @UserPreference(defaultValue: true) diff --git a/ElementX/Sources/Application/Settings/AppSettings.swift b/ElementX/Sources/Application/Settings/AppSettings.swift index ecccd99e1..789b81c59 100644 --- a/ElementX/Sources/Application/Settings/AppSettings.swift +++ b/ElementX/Sources/Application/Settings/AppSettings.swift @@ -106,11 +106,6 @@ final nonisolated class AppSettings: @unchecked Sendable { @UserPreference var lastVersionLaunched: String? - /// The Set of room identifiers of invites that the user already saw in the invites list. - /// This Set is being used to implement badges for unread invites. - @UserPreference(defaultValue: Set()) - var seenInvites: Set - /// Defaults to `true` for new users, and we use a migration to set it to `false` for existing users. @UserPreference(defaultValue: true) var hasSeenNewSoundBanner: Bool diff --git a/ElementX/Sources/Screens/HomeScreen/HomeScreenViewModel.swift b/ElementX/Sources/Screens/HomeScreen/HomeScreenViewModel.swift index 944a82bb0..6724d79bd 100644 --- a/ElementX/Sources/Screens/HomeScreen/HomeScreenViewModel.swift +++ b/ElementX/Sources/Screens/HomeScreen/HomeScreenViewModel.swift @@ -119,7 +119,7 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol } .store(in: &cancellables) - userSettings.app.seenInvitesPublisher + userSettings.account.seenInvitesPublisher .removeDuplicates() .sink { [weak self] _ in self?.updateRooms() @@ -388,7 +388,7 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol } var rooms = [HomeScreenRoom]() - let seenInvites = userSettings.app.seenInvites + let seenInvites = userSettings.account.seenInvites for summary in roomSummaryProvider.roomListPublisher.value { let room = HomeScreenRoom(summary: summary, @@ -550,7 +550,7 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol analyticsService.trackJoinedRoom(isDM: roomProxy.info.isDirect, isSpace: roomProxy.info.isSpace, activeMemberCount: UInt(roomProxy.info.activeMembersCount)) - userSettings.app.seenInvites.remove(roomProxy.id) + userSettings.account.seenInvites.remove(roomProxy.id) } private func showDeclineInviteConfirmationAlert(roomID: String) async { @@ -600,7 +600,7 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol switch result { case .success: userSession.notificationService.removeDeliveredMessageNotifications(for: roomID) // Normally handled by the room flow, but that's never presented in this case. - userSettings.app.seenInvites.remove(roomID) + userSettings.account.seenInvites.remove(roomID) case .failure: displayError() } diff --git a/ElementX/Sources/Screens/JoinRoomScreen/JoinRoomScreenViewModel.swift b/ElementX/Sources/Screens/JoinRoomScreen/JoinRoomScreenViewModel.swift index acc8fc0fa..b24c47a66 100644 --- a/ElementX/Sources/Screens/JoinRoomScreen/JoinRoomScreenViewModel.swift +++ b/ElementX/Sources/Screens/JoinRoomScreen/JoinRoomScreenViewModel.swift @@ -51,7 +51,7 @@ class JoinRoomScreenViewModel: JoinRoomScreenViewModelType, JoinRoomScreenViewMo .sink { [userSettings = self.userSettings] mode in switch mode { case .invited: - userSettings.app.seenInvites.insert(roomID) + userSettings.account.seenInvites.insert(roomID) default: break } @@ -349,7 +349,7 @@ class JoinRoomScreenViewModel: JoinRoomScreenViewModelType, JoinRoomScreenViewMo guard !hasSentJoinAction else { return } let roomID = state.roomID - userSettings.app.seenInvites.remove(roomID) + userSettings.account.seenInvites.remove(roomID) guard state.roomDetails?.isSpace == true else { hasSentJoinAction = true @@ -463,7 +463,7 @@ class JoinRoomScreenViewModel: JoinRoomScreenViewModelType, JoinRoomScreenViewMo return false } - userSettings.app.seenInvites.remove(roomID) + userSettings.account.seenInvites.remove(roomID) actionsSubject.send(.dismiss) return true diff --git a/UnitTests/Sources/HomeScreenViewModelTests.swift b/UnitTests/Sources/HomeScreenViewModelTests.swift index 9c17dd172..100c14904 100644 --- a/UnitTests/Sources/HomeScreenViewModelTests.swift +++ b/UnitTests/Sources/HomeScreenViewModelTests.swift @@ -275,7 +275,7 @@ final class HomeScreenViewModelTests { room.roomID == invites[0].roomID && room.badges.isDotShown == false } } - userSettings.app.seenInvites = Set(invites.compactMap(\.roomID)) + userSettings.account.seenInvites = Set(invites.compactMap(\.roomID)) try await deferred.fulfill() invites = context.viewState.rooms.invites @@ -289,14 +289,14 @@ final class HomeScreenViewModelTests { setupViewModel(invites: .rooms) let invitedRoomIDs = context.viewState.rooms.invites.compactMap(\.roomID) - userSettings.app.seenInvites = Set(invitedRoomIDs) + userSettings.account.seenInvites = Set(invitedRoomIDs) #expect(invitedRoomIDs.count == 2) let deferred = deferFulfillment(viewModel.actions) { $0 == .presentRoom(roomIdentifier: invitedRoomIDs[0]) } context.send(viewAction: .acceptInvite(roomIdentifier: invitedRoomIDs[0])) try await deferred.fulfill() - #expect(userSettings.app.seenInvites == [invitedRoomIDs[1]]) + #expect(userSettings.account.seenInvites == [invitedRoomIDs[1]]) #expect(!notificationService.removeDeliveredMessageNotificationsForCalled, "The notification will be dismissed when opening the room.") } @@ -305,7 +305,7 @@ final class HomeScreenViewModelTests { setupViewModel(invites: .spaces) let invitedRoomIDs = context.viewState.rooms.invites.compactMap(\.roomID) - userSettings.app.seenInvites = Set(invitedRoomIDs) + userSettings.account.seenInvites = Set(invitedRoomIDs) #expect(invitedRoomIDs.count == 2) let deferred = deferFulfillment(viewModel.actions) { @@ -314,7 +314,7 @@ final class HomeScreenViewModelTests { context.send(viewAction: .acceptInvite(roomIdentifier: invitedRoomIDs[0])) try await deferred.fulfill() - #expect(userSettings.app.seenInvites == [invitedRoomIDs[1]]) + #expect(userSettings.account.seenInvites == [invitedRoomIDs[1]]) #expect(!notificationService.removeDeliveredMessageNotificationsForCalled, "The notification will be dismissed when opening the room.") } @@ -322,7 +322,7 @@ final class HomeScreenViewModelTests { func declineInvite() async throws { setupViewModel(invites: .rooms) let invitedRoomIDs = context.viewState.rooms.invites.compactMap(\.roomID) - userSettings.app.seenInvites = Set(invitedRoomIDs) + userSettings.account.seenInvites = Set(invitedRoomIDs) #expect(invitedRoomIDs.count == 2) let deferred = deferFulfillment(context.$viewState) { $0.bindings.alertInfo != nil } @@ -345,7 +345,7 @@ final class HomeScreenViewModelTests { try await Task.sleep(for: .milliseconds(100)) #expect(rejectCalled) - #expect(userSettings.app.seenInvites == [invitedRoomIDs[1]]) + #expect(userSettings.account.seenInvites == [invitedRoomIDs[1]]) #expect(notificationService.removeDeliveredMessageNotificationsForCalled) #expect(notificationService.removeDeliveredMessageNotificationsForReceivedInvocations == [invitedRoomIDs[0]]) } @@ -354,7 +354,7 @@ final class HomeScreenViewModelTests { func declineAndBlockInvite() async throws { setupViewModel(invites: .rooms) let invitedRoomIDs = context.viewState.rooms.invites.compactMap(\.roomID) - userSettings.app.seenInvites = Set(invitedRoomIDs) + userSettings.account.seenInvites = Set(invitedRoomIDs) #expect(invitedRoomIDs.count == 2) let deferred = deferFulfillment(context.$viewState) { $0.bindings.alertInfo != nil } diff --git a/UnitTests/Sources/JoinRoomScreenViewModelTests.swift b/UnitTests/Sources/JoinRoomScreenViewModelTests.swift index 1f0f06d86..a48607642 100644 --- a/UnitTests/Sources/JoinRoomScreenViewModelTests.swift +++ b/UnitTests/Sources/JoinRoomScreenViewModelTests.swift @@ -45,12 +45,12 @@ final class JoinRoomScreenViewModelTests { @Test func interaction() async throws { - #expect(userSettings.app.seenInvites.isEmpty, "There shouldn't be any seen invites before running the tests.") + #expect(userSettings.account.seenInvites.isEmpty, "There shouldn't be any seen invites before running the tests.") setupViewModel() try await deferFulfillment(viewModel.context.$viewState) { $0.mode == .joinable }.fulfill() - #expect(userSettings.app.seenInvites.isEmpty, "Only an invited room should register the room ID as a seen invite.") + #expect(userSettings.account.seenInvites.isEmpty, "Only an invited room should register the room ID as a seen invite.") let deferred = deferFulfillment(viewModel.actionsPublisher) { $0 == .joined(.roomID("1")) } context.send(viewAction: .join) @@ -59,28 +59,28 @@ final class JoinRoomScreenViewModelTests { @Test func acceptInviteInteraction() async throws { - #expect(userSettings.app.seenInvites.isEmpty, "There shouldn't be any seen invites before running the tests.") + #expect(userSettings.account.seenInvites.isEmpty, "There shouldn't be any seen invites before running the tests.") setupViewModel(mode: .invited) try await deferFulfillment(viewModel.context.$viewState) { $0.mode == .invited(isDM: false) }.fulfill() - #expect(userSettings.app.seenInvites == ["1"], "The invited room's ID should be registered as a seen invite.") + #expect(userSettings.account.seenInvites == ["1"], "The invited room's ID should be registered as a seen invite.") let deferred = deferFulfillment(viewModel.actionsPublisher) { $0 == .joined(.roomID("1")) } context.send(viewAction: .acceptInvite) try await deferred.fulfill() - #expect(userSettings.app.seenInvites.isEmpty, "The after accepting an invite the invite should be forgotten in case the user leaves.") + #expect(userSettings.account.seenInvites.isEmpty, "The after accepting an invite the invite should be forgotten in case the user leaves.") } @Test func declineInviteInteraction() async throws { - #expect(userSettings.app.seenInvites.isEmpty, "There shouldn't be any seen invites before running the tests.") + #expect(userSettings.account.seenInvites.isEmpty, "There shouldn't be any seen invites before running the tests.") setupViewModel(mode: .invited) try await deferFulfillment(viewModel.context.$viewState) { $0.mode == .invited(isDM: false) }.fulfill() - #expect(userSettings.app.seenInvites == ["1"], "The invited room's ID should be registered as a seen invite.") + #expect(userSettings.account.seenInvites == ["1"], "The invited room's ID should be registered as a seen invite.") context.send(viewAction: .declineInvite) @@ -89,17 +89,17 @@ final class JoinRoomScreenViewModelTests { context.alertInfo?.secondaryButton?.action?() try await deferred.fulfill() - #expect(userSettings.app.seenInvites.isEmpty, "The after declining an invite the invite should be forgotten in case another invite is received.") + #expect(userSettings.account.seenInvites.isEmpty, "The after declining an invite the invite should be forgotten in case another invite is received.") } @Test func knockedState() async throws { - #expect(userSettings.app.seenInvites.isEmpty, "There shouldn't be any seen invites before running the tests.") + #expect(userSettings.account.seenInvites.isEmpty, "There shouldn't be any seen invites before running the tests.") setupViewModel(mode: .knocked) try await deferFulfillment(viewModel.context.$viewState) { $0.mode == .knocked }.fulfill() - #expect(userSettings.app.seenInvites.isEmpty, "Only an invited room should register the room ID as a seen invite.") + #expect(userSettings.account.seenInvites.isEmpty, "Only an invited room should register the room ID as a seen invite.") } @Test