From 9877e90df60732be239179295b2eee002b6a4151 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Thu, 29 Sep 2022 17:42:13 +0200 Subject: [PATCH] Adding use cases to get and set the extra matrix client Info --- .../DefaultSessionAccountDataService.kt | 3 +- .../accountdata/UpdateUserAccountDataTask.kt | 1 + .../extended/GetMatrixClientInfoUseCase.kt | 40 +++++++++++++++++ .../extended/MatrixClientInfoContent.kt | 43 ++++++++++++++++++ .../extended/SessionExtendedInfoConstants.kt | 20 +++++++++ .../extended/SetMatrixClientInfoUseCase.kt | 40 +++++++++++++++++ .../extended/UpdateMatrixClientInfoUseCase.kt | 44 +++++++++++++++++++ 7 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/GetMatrixClientInfoUseCase.kt create mode 100644 vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/MatrixClientInfoContent.kt create mode 100644 vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/SessionExtendedInfoConstants.kt create mode 100644 vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/SetMatrixClientInfoUseCase.kt create mode 100644 vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/UpdateMatrixClientInfoUseCase.kt diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/user/accountdata/DefaultSessionAccountDataService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/user/accountdata/DefaultSessionAccountDataService.kt index df9dcfb903..c81dac0882 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/user/accountdata/DefaultSessionAccountDataService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/user/accountdata/DefaultSessionAccountDataService.kt @@ -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 { callback -> + awaitCallback { callback -> updateUserAccountDataTask.configureWith(params) { this.retryCount = 5 // TODO Need to refactor retrying out into a helper method. this.callback = callback diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/user/accountdata/UpdateUserAccountDataTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/user/accountdata/UpdateUserAccountDataTask.kt index ff0e960f0f..9e42191a50 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/user/accountdata/UpdateUserAccountDataTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/user/accountdata/UpdateUserAccountDataTask.kt @@ -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()) diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/GetMatrixClientInfoUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/GetMatrixClientInfoUseCase.kt new file mode 100644 index 0000000000..a41931ed4a --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/GetMatrixClientInfoUseCase.kt @@ -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) + } + } +} diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/MatrixClientInfoContent.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/MatrixClientInfoContent.kt new file mode 100644 index 0000000000..814f7ef5bb --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/MatrixClientInfoContent.kt @@ -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) + } + } +} diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/SessionExtendedInfoConstants.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/SessionExtendedInfoConstants.kt new file mode 100644 index 0000000000..b87ebc1ffc --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/SessionExtendedInfoConstants.kt @@ -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." diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/SetMatrixClientInfoUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/SetMatrixClientInfoUseCase.kt new file mode 100644 index 0000000000..1e3837bc17 --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/SetMatrixClientInfoUseCase.kt @@ -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 = runCatching { + activeSessionHolder.getSafeActiveSession() + ?.let { session -> + val type = MATRIX_CLIENT_INFO_KEY_PREFIX + session.sessionParams.deviceId + session.accountDataService() + .updateUserAccountData(type, clientInfo.toContent()) + } + } +} diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/UpdateMatrixClientInfoUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/UpdateMatrixClientInfoUseCase.kt new file mode 100644 index 0000000000..4058332127 --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/details/extended/UpdateMatrixClientInfoUseCase.kt @@ -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 = runCatching { + val clientInfo = MatrixClientInfoContent( + name = appNameProvider.getAppName(), + version = buildMeta.versionName + ) + val storedClientInfo = getMatrixClientInfoUseCase.execute() + if (clientInfo != storedClientInfo) { + setMatrixClientInfoUseCase.execute(clientInfo) + } + } +}