Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete old account data (PSG-635) #7164

Merged
merged 4 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,16 @@ class UserSessionsDataProvider: UserSessionsDataProviderProtocol {
}

func devices(completion: @escaping (MXResponse<[MXDevice]>) -> Void) {
session.matrixRestClient.devices(completion: completion)
session.matrixRestClient.devices { [weak self] response in
switch response {
case .success(let devices):
self?.deleteAccountDataIfNeeded(deviceList: devices)
case .failure:
break
}

completion(response)
}
}

func device(withDeviceId deviceId: String, ofUser userId: String) -> MXDeviceInfo? {
Expand Down Expand Up @@ -66,3 +75,29 @@ class UserSessionsDataProvider: UserSessionsDataProviderProtocol {
return try await service.isServiceAvailable()
}
}

extension UserSessionsDataProvider {
private func deleteAccountDataIfNeeded(deviceList: [MXDevice]) {
let obsoletedDeviceAccountDataKeys = obsoletedDeviceAccountData(deviceList: deviceList,
accountDataEvents: session.accountData.allAccountDataEvents())

for accountDataKey in obsoletedDeviceAccountDataKeys {
session.deleteAccountData(withType: accountDataKey, success: {}, failure: { _ in })
}
}

// internal just to facilitate tests
func obsoletedDeviceAccountData(deviceList: [MXDevice], accountDataEvents: [String: Any]) -> Set<String> {
let deviceAccountDataKeys = Set(
accountDataEvents
.map(\.key)
.filter { $0.hasPrefix(kMXAccountDataTypeClientInformation) }
)

let expectedDeviceAccountDataKeys = Set(deviceList.map {
"\(kMXAccountDataTypeClientInformation).\($0.deviceId)"
})

return deviceAccountDataKeys.subtracting(expectedDeviceAccountDataKeys)
}
}
59 changes: 59 additions & 0 deletions RiotTests/UserSessionsDataProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,69 @@ class UserSessionCardViewDataTests: XCTestCase {

XCTAssertEqual(verificationState, .permanentlyUnverified)
}

func testObsoletedDeviceInformation_someMatch() {
let mxSession = MockSession(canCrossSign: true)
let dataProvider = UserSessionsDataProvider(session: mxSession)

let accountDataEvents: [String: Any] = [
"io.element.matrix_client_information.D": "",
"foo": ""
]

let expectedObsoletedEvents: Set = [
"io.element.matrix_client_information.D"
]

let obsoletedEvents = dataProvider.obsoletedDeviceAccountData(deviceList: .mockDevices, accountDataEvents: accountDataEvents)

XCTAssertEqual(obsoletedEvents, expectedObsoletedEvents)
}

func testObsoletedDeviceInformation_noMatch() {
let mxSession = MockSession(canCrossSign: true)
let dataProvider = UserSessionsDataProvider(session: mxSession)

let accountDataEvents: [String: Any] = [
"io.element.matrix_client_information.C": "",
"foo": ""
]

let expectedObsoletedEvents: Set<String> = []
let obsoletedEvents = dataProvider.obsoletedDeviceAccountData(deviceList: .mockDevices, accountDataEvents: accountDataEvents)

XCTAssertEqual(obsoletedEvents, expectedObsoletedEvents)
}

func testObsoletedDeviceInformation_allMatch() {
let mxSession = MockSession(canCrossSign: true)
let dataProvider = UserSessionsDataProvider(session: mxSession)

let expectedObsoletedEvents = Set(["D", "E", "F"].map { "io.element.matrix_client_information.\($0)"})

let accountDataEvents: [String: Any] = expectedObsoletedEvents.reduce(into: ["foo": ""]) { partialResult, value in
partialResult[value] = ""
}

let obsoletedEvents = dataProvider.obsoletedDeviceAccountData(deviceList: .mockDevices, accountDataEvents: accountDataEvents)

XCTAssertEqual(obsoletedEvents, expectedObsoletedEvents)
}
}

// MARK: Mocks

private extension Array where Element == MXDevice {
static let mockDevices: [MXDevice] = {
["A", "B", "C"]
.map {
let device = MXDevice()
device.deviceId = $0
return device
}
}()
}

// Device ID constants.
private extension String {
static var otherDeviceA: String { "abcdef" }
Expand Down
1 change: 1 addition & 0 deletions changelog.d/pr-7164.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add old device data from user's account data events.