Adding use cases to get and set the extra matrix client Info

This commit is contained in:
Maxime NATUREL 2022-09-29 17:42:13 +02:00
parent 1e87621e48
commit 9877e90df6
7 changed files with 190 additions and 1 deletions

View File

@ -64,9 +64,10 @@ internal class DefaultSessionAccountDataService @Inject constructor(
return roomAccountDataDataSource.getLiveAccountDataEvents(null, types)
}
// TODO add unit tests
override suspend fun updateUserAccountData(type: String, content: Content) {
val params = UpdateUserAccountDataTask.AnyParams(type = type, any = content)
awaitCallback<Unit> { callback ->
awaitCallback { callback ->
updateUserAccountDataTask.configureWith(params) {
this.retryCount = 5 // TODO Need to refactor retrying out into a helper method.
this.callback = callback

View File

@ -112,6 +112,7 @@ internal class DefaultUpdateUserAccountDataTask @Inject constructor(
private val globalErrorReceiver: GlobalErrorReceiver
) : UpdateUserAccountDataTask {
// TODO add unit tests
override suspend fun execute(params: UpdateUserAccountDataTask.Params) {
return executeRequest(globalErrorReceiver) {
accountDataApi.setAccountData(userId, params.type, params.getData())

View File

@ -0,0 +1,40 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* 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 im.vector.app.features.settings.devices.v2.details.extended
import MATRIX_CLIENT_INFO_KEY_PREFIX
import im.vector.app.core.di.ActiveSessionHolder
import javax.inject.Inject
/**
* This use case retrieves the current account data event containing extended client info.
*/
class GetMatrixClientInfoUseCase @Inject constructor(
private val activeSessionHolder: ActiveSessionHolder,
) {
// TODO add unit tests
fun execute(): MatrixClientInfoContent? {
return activeSessionHolder
.getSafeActiveSession()
?.let { session ->
val type = MATRIX_CLIENT_INFO_KEY_PREFIX + session.sessionParams.deviceId
val content = session.accountDataService().getUserAccountDataEvent(type)?.content
MatrixClientInfoContent.fromJson(content)
}
}
}

View File

@ -0,0 +1,43 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* 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 im.vector.app.features.settings.devices.v2.details.extended
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import com.squareup.moshi.Moshi
@JsonClass(generateAdapter = true)
data class MatrixClientInfoContent(
// app name
@Json(name = "name")
val name: String? = null,
// app version
@Json(name = "version")
val version: String? = null,
// app url (optional, applicable only for web)
@Json(name = "url")
val url: String? = null,
) {
companion object {
fun fromJson(obj: Any?): MatrixClientInfoContent? {
return Moshi.Builder()
.build()
.adapter(MatrixClientInfoContent::class.java)
.fromJsonValue(obj)
}
}
}

View File

@ -0,0 +1,20 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* 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.
*/
/**
* Prefix for th key account data event which holds client info.
*/
const val MATRIX_CLIENT_INFO_KEY_PREFIX = "io.element.matrix_client_information."

View File

@ -0,0 +1,40 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* 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 im.vector.app.features.settings.devices.v2.details.extended
import MATRIX_CLIENT_INFO_KEY_PREFIX
import im.vector.app.core.di.ActiveSessionHolder
import org.matrix.android.sdk.api.session.events.model.toContent
import javax.inject.Inject
/**
* This use case sets the account data event containing extended client info.
*/
class SetMatrixClientInfoUseCase @Inject constructor(
private val activeSessionHolder: ActiveSessionHolder,
) {
// TODO add unit tests
suspend fun execute(clientInfo: MatrixClientInfoContent): Result<Unit> = runCatching {
activeSessionHolder.getSafeActiveSession()
?.let { session ->
val type = MATRIX_CLIENT_INFO_KEY_PREFIX + session.sessionParams.deviceId
session.accountDataService()
.updateUserAccountData(type, clientInfo.toContent())
}
}
}

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* 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 im.vector.app.features.settings.devices.v2.details.extended
import im.vector.app.core.resources.AppNameProvider
import im.vector.app.core.resources.BuildMeta
import javax.inject.Inject
/**
* This use case updates if needed the account data event containing extended client info.
*/
class UpdateMatrixClientInfoUseCase @Inject constructor(
private val appNameProvider: AppNameProvider,
private val buildMeta: BuildMeta,
private val getMatrixClientInfoUseCase: GetMatrixClientInfoUseCase,
private val setMatrixClientInfoUseCase: SetMatrixClientInfoUseCase,
) {
// TODO add unit tests
suspend fun execute(): Result<Unit> = runCatching {
val clientInfo = MatrixClientInfoContent(
name = appNameProvider.getAppName(),
version = buildMeta.versionName
)
val storedClientInfo = getMatrixClientInfoUseCase.execute()
if (clientInfo != storedClientInfo) {
setMatrixClientInfoUseCase.execute(clientInfo)
}
}
}