diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/homeserver/HomeServerCapabilities.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/homeserver/HomeServerCapabilities.kt index 9b01cdef3b..c8526985e1 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/homeserver/HomeServerCapabilities.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/homeserver/HomeServerCapabilities.kt @@ -17,6 +17,10 @@ package im.vector.matrix.android.api.session.homeserver data class HomeServerCapabilities( + /** + * True if it is possible to change the password of the account. + */ + val canChangePassword: Boolean = true, /** * Max size of file which can be uploaded to the homeserver in bytes. [MAX_UPLOAD_FILE_SIZE_UNKNOWN] if unknown or not retrieved yet */ diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/HomeServerCapabilitiesMapper.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/HomeServerCapabilitiesMapper.kt index 66b0f1b379..a0d3662e03 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/HomeServerCapabilitiesMapper.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/HomeServerCapabilitiesMapper.kt @@ -26,12 +26,14 @@ internal object HomeServerCapabilitiesMapper { fun map(entity: HomeServerCapabilitiesEntity): HomeServerCapabilities { return HomeServerCapabilities( + canChangePassword = entity.canChangePassword, maxUploadFileSize = entity.maxUploadFileSize ) } fun map(domain: HomeServerCapabilities): HomeServerCapabilitiesEntity { return HomeServerCapabilitiesEntity( + canChangePassword = domain.canChangePassword, maxUploadFileSize = domain.maxUploadFileSize ) } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/HomeServerCapabilitiesEntity.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/HomeServerCapabilitiesEntity.kt index 0d067286e1..5743597a61 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/HomeServerCapabilitiesEntity.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/HomeServerCapabilitiesEntity.kt @@ -20,6 +20,7 @@ import im.vector.matrix.android.api.session.homeserver.HomeServerCapabilities import io.realm.RealmObject internal open class HomeServerCapabilitiesEntity( + var canChangePassword: Boolean = true, var maxUploadFileSize: Long = HomeServerCapabilities.MAX_UPLOAD_FILE_SIZE_UNKNOWN, var lastUpdatedTimestamp: Long = 0L ) : RealmObject() { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/CapabilitiesAPI.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/CapabilitiesAPI.kt index c83a61cfb0..f37bbfe798 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/CapabilitiesAPI.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/CapabilitiesAPI.kt @@ -22,6 +22,12 @@ import retrofit2.http.GET internal interface CapabilitiesAPI { + /** + * Request the homeserver capabilities + */ + @GET(NetworkConstants.URI_API_PREFIX_PATH_R0 + "capabilities") + fun getCapabilities(): Call + /** * Request the upload capabilities */ diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/DefaultGetHomeServerCapabilitiesTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/DefaultGetHomeServerCapabilitiesTask.kt index 2dbd627ce5..9f068381f0 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/DefaultGetHomeServerCapabilitiesTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/DefaultGetHomeServerCapabilitiesTask.kt @@ -51,15 +51,23 @@ internal class DefaultGetHomeServerCapabilitiesTask @Inject constructor( apiCall = capabilitiesAPI.getUploadCapabilities() } + val capabilities = runCatching { + executeRequest(eventBus) { + apiCall = capabilitiesAPI.getCapabilities() + } + }.getOrNull() + // TODO Add other call here (get version, etc.) - insertInDb(uploadCapabilities) + insertInDb(capabilities, uploadCapabilities) } - private suspend fun insertInDb(getUploadCapabilitiesResult: GetUploadCapabilitiesResult) { + private suspend fun insertInDb(getCapabilitiesResult: GetCapabilitiesResult?, getUploadCapabilitiesResult: GetUploadCapabilitiesResult) { monarchy.awaitTransaction { realm -> val homeServerCapabilitiesEntity = HomeServerCapabilitiesEntity.getOrCreate(realm) + homeServerCapabilitiesEntity.canChangePassword = getCapabilitiesResult.canChangePassword() + homeServerCapabilitiesEntity.maxUploadFileSize = getUploadCapabilitiesResult.maxUploadSize ?: HomeServerCapabilities.MAX_UPLOAD_FILE_SIZE_UNKNOWN diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/GetCapabilitiesResult.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/GetCapabilitiesResult.kt new file mode 100644 index 0000000000..ffaa998789 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/GetCapabilitiesResult.kt @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2020 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.matrix.android.internal.session.homeserver + +import com.squareup.moshi.Json +import com.squareup.moshi.JsonClass +import im.vector.matrix.android.api.extensions.orTrue + +/** + * Ref: https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-capabilities + */ +@JsonClass(generateAdapter = true) +internal data class GetCapabilitiesResult( + /** + * Required. The custom capabilities the server supports, using the Java package naming convention. + */ + @Json(name = "capabilities") + val capabilities: Capabilities? = null +) + +@JsonClass(generateAdapter = true) +internal data class Capabilities( + /** + * Capability to indicate if the user can change their password. + */ + @Json(name = "m.change_password") + val changePassword: ChangePassword? = null + + // No need for m.room_versions for the moment +) + +@JsonClass(generateAdapter = true) +internal data class ChangePassword( + /** + * Required. True if the user can change their password, false otherwise. + */ + @Json(name = "enabled") + val enabled: Boolean? +) + +// The spec says: If not present, the client should assume that password changes are possible via the API +internal fun GetCapabilitiesResult?.canChangePassword(): Boolean { + return this?.capabilities?.changePassword?.enabled.orTrue() +} diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/GetUploadCapabilitiesResult.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/GetUploadCapabilitiesResult.kt index 8e410cc834..c98119db6c 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/GetUploadCapabilitiesResult.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/homeserver/GetUploadCapabilitiesResult.kt @@ -20,7 +20,7 @@ import com.squareup.moshi.Json import com.squareup.moshi.JsonClass @JsonClass(generateAdapter = true) -data class GetUploadCapabilitiesResult( +internal data class GetUploadCapabilitiesResult( /** * The maximum size an upload can be in bytes. Clients SHOULD use this as a guide when uploading content. * If not listed or null, the size limit should be treated as unknown. diff --git a/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsGeneralFragment.kt b/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsGeneralFragment.kt index b778fa02ef..742bbc8201 100644 --- a/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsGeneralFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/settings/VectorSettingsGeneralFragment.kt @@ -110,9 +110,14 @@ class VectorSettingsGeneralFragment : VectorSettingsBaseFragment() { } // Password - mPasswordPreference.onPreferenceClickListener = Preference.OnPreferenceClickListener { - onPasswordUpdateClick() - false + // Hide the preference if password can not be updated + if (session.getHomeServerCapabilities().canChangePassword) { + mPasswordPreference.onPreferenceClickListener = Preference.OnPreferenceClickListener { + onPasswordUpdateClick() + false + } + } else { + mPasswordPreference.isVisible = false } // Add Email