Code review fixes.

This commit is contained in:
Onuray Sahin 2022-12-12 16:36:40 +03:00
parent 85a6c8c6f2
commit 8c6c2dd5c2
7 changed files with 92 additions and 8 deletions

1
changelog.d/7754.feature Normal file
View file

@ -0,0 +1 @@
Delete unused client information from account data

View file

@ -64,7 +64,7 @@ internal class UserAccountDataDataSource @Inject constructor(
return realmSessionProvider.withRealm { realm ->
realm
.where(UserAccountDataEntity::class.java)
.contains(UserAccountDataEntityFields.TYPE, type)
.beginsWith(UserAccountDataEntityFields.TYPE, type)
.findAll()
.map(accountDataMapper::map)
}

View file

@ -0,0 +1,53 @@
/*
* Copyright 2022 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.android.sdk.internal.session.user.accountdata
import io.mockk.coVerify
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.matrix.android.sdk.test.fakes.FakeAccountDataApi
import org.matrix.android.sdk.test.fakes.FakeGlobalErrorReceiver
private const val A_TYPE = "a-type"
private const val A_USER_ID = "a-user-id"
@ExperimentalCoroutinesApi
class DefaultDeleteUserAccountDataTaskTest {
private val fakeGlobalErrorReceiver = FakeGlobalErrorReceiver()
private val fakeAccountDataApi = FakeAccountDataApi()
private val deleteUserAccountDataTask = DefaultDeleteUserAccountDataTask(
accountDataApi = fakeAccountDataApi.instance,
userId = A_USER_ID,
globalErrorReceiver = fakeGlobalErrorReceiver
)
@Test
fun `given parameters when executing the task then api is called`() = runTest {
// Given
val params = DeleteUserAccountDataTask.Params(type = A_TYPE)
fakeAccountDataApi.givenParamsToDeleteAccountData(A_USER_ID, A_TYPE)
// When
deleteUserAccountDataTask.execute(params)
// Then
coVerify { fakeAccountDataApi.instance.deleteAccountData(A_USER_ID, A_TYPE) }
}
}

View file

@ -0,0 +1,32 @@
/*
* Copyright 2022 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.android.sdk.test.fakes
import io.mockk.coEvery
import io.mockk.just
import io.mockk.mockk
import io.mockk.runs
import org.matrix.android.sdk.internal.session.user.accountdata.AccountDataAPI
internal class FakeAccountDataApi {
val instance: AccountDataAPI = mockk()
fun givenParamsToDeleteAccountData(userId: String, type: String) {
coEvery { instance.deleteAccountData(userId, type) } just runs
}
}

View file

@ -14,10 +14,9 @@
* limitations under the License.
*/
package im.vector.app.features.settings.devices.v2
package im.vector.app.core.session.clientinfo
import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.session.clientinfo.MATRIX_CLIENT_INFO_KEY_PREFIX
import org.matrix.android.sdk.api.session.crypto.model.DeviceInfo
import javax.inject.Inject
@ -25,9 +24,9 @@ class DeleteUnusedClientInformationUseCase @Inject constructor(
private val activeSessionHolder: ActiveSessionHolder,
) {
suspend fun execute(deviceInfoList: List<DeviceInfo>) {
suspend fun execute(deviceInfoList: List<DeviceInfo>): Result<Unit> = runCatching {
// A defensive approach against local storage reports an empty device list (although it is not a seen situation).
if (deviceInfoList.isEmpty()) return
if (deviceInfoList.isEmpty()) return Result.success(Unit)
val expectedClientInfoKeyList = deviceInfoList.map { MATRIX_CLIENT_INFO_KEY_PREFIX + it.deviceId }
activeSessionHolder

View file

@ -31,8 +31,8 @@ import im.vector.app.core.di.hiltMavericksViewModelFactory
import im.vector.app.core.platform.EmptyViewEvents
import im.vector.app.core.platform.VectorViewModel
import im.vector.app.core.platform.VectorViewModelAction
import im.vector.app.core.session.clientinfo.DeleteUnusedClientInformationUseCase
import im.vector.app.core.time.Clock
import im.vector.app.features.settings.devices.v2.DeleteUnusedClientInformationUseCase
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.launchIn

View file

@ -14,9 +14,8 @@
* limitations under the License.
*/
package im.vector.app.features.settings.devices.v2
package im.vector.app.core.session.clientinfo
import im.vector.app.core.session.clientinfo.MATRIX_CLIENT_INFO_KEY_PREFIX
import im.vector.app.test.fakes.FakeActiveSessionHolder
import io.mockk.coVerify
import kotlinx.coroutines.test.runTest