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.
This commit is contained in:
Mauro Romito
2026-10-15 15:13:38 +02:00
committed by Mauro
parent fac5f345fb
commit 074a2d7910
7 changed files with 35 additions and 30 deletions
@@ -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.
@@ -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<String>())
var seenInvites: Set<String>
// MARK: Visible Preferences
@UserPreference(defaultValue: true)
@@ -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<String>())
var seenInvites: Set<String>
/// 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
@@ -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()
}
@@ -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
@@ -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 }
@@ -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