From d8d6358d15cc2aefd454d6f21ec76b9bd64fc008 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Fri, 11 Feb 2022 11:57:45 +0000 Subject: [PATCH] adding support for the homeserver display name and avatar capabilities - MSC3283 https://github.com/matrix-org/synapse/pull/11933 - includes session database migration --- .../homeserver/HomeServerCapabilities.kt | 13 ++++++++ .../database/RealmSessionStoreMigration.kt | 2 ++ .../mapper/HomeServerCapabilitiesMapper.kt | 3 ++ .../database/migration/MigrateSessionTo025.kt | 31 +++++++++++++++++++ .../model/HomeServerCapabilitiesEntity.kt | 3 ++ .../homeserver/GetCapabilitiesResult.kt | 25 +++++++++++---- .../GetHomeServerCapabilitiesTask.kt | 12 +++++-- 7 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/migration/MigrateSessionTo025.kt diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/homeserver/HomeServerCapabilities.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/homeserver/HomeServerCapabilities.kt index 3ed6a7ebb2..2256dfb8f0 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/homeserver/HomeServerCapabilities.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/homeserver/HomeServerCapabilities.kt @@ -21,6 +21,18 @@ data class HomeServerCapabilities( * True if it is possible to change the password of the account. */ val canChangePassword: Boolean = true, + /** + * True if it is possible to change the display name of the account. + */ + val canChangeDisplayName: Boolean = true, + /** + * True if it is possible to change the avatar of the account. + */ + val canChangeAvatar: Boolean = true, + /** + * True if it is possible to change the 3pid associations of the account. + */ + val canChange3pid: 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 */ @@ -76,6 +88,7 @@ data class HomeServerCapabilities( } } } + fun isFeatureSupported(feature: String, byRoomVersion: String): Boolean { if (roomVersions?.capabilities == null) return false val info = roomVersions.capabilities[feature] ?: return false diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/RealmSessionStoreMigration.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/RealmSessionStoreMigration.kt index f4a8ae2c67..4bf352c06c 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/RealmSessionStoreMigration.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/RealmSessionStoreMigration.kt @@ -42,6 +42,7 @@ import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo021 import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo022 import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo023 import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo024 +import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo025 import org.matrix.android.sdk.internal.util.Normalizer import timber.log.Timber import javax.inject.Inject @@ -85,5 +86,6 @@ internal class RealmSessionStoreMigration @Inject constructor( if (oldVersion < 22) MigrateSessionTo022(realm).perform() if (oldVersion < 23) MigrateSessionTo023(realm).perform() if (oldVersion < 24) MigrateSessionTo024(realm).perform() + if (oldVersion < 25) MigrateSessionTo025(realm).perform() } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/mapper/HomeServerCapabilitiesMapper.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/mapper/HomeServerCapabilitiesMapper.kt index 8b6d263f8c..7869506015 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/mapper/HomeServerCapabilitiesMapper.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/mapper/HomeServerCapabilitiesMapper.kt @@ -35,6 +35,9 @@ internal object HomeServerCapabilitiesMapper { fun map(entity: HomeServerCapabilitiesEntity): HomeServerCapabilities { return HomeServerCapabilities( canChangePassword = entity.canChangePassword, + canChangeDisplayName = entity.canChangeDisplayName, + canChangeAvatar = entity.canChangeAvatar, + canChange3pid = entity.canChange3pid, maxUploadFileSize = entity.maxUploadFileSize, lastVersionIdentityServerSupported = entity.lastVersionIdentityServerSupported, defaultIdentityServerUrl = entity.defaultIdentityServerUrl, diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/migration/MigrateSessionTo025.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/migration/MigrateSessionTo025.kt new file mode 100644 index 0000000000..35267d2a16 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/migration/MigrateSessionTo025.kt @@ -0,0 +1,31 @@ +/* + * Copyright (c) 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.database.migration + +import io.realm.DynamicRealm +import org.matrix.android.sdk.internal.database.model.HomeServerCapabilitiesEntityFields +import org.matrix.android.sdk.internal.util.database.RealmMigrator + +class MigrateSessionTo025(realm: DynamicRealm) : RealmMigrator(realm, 25) { + + override fun doMigrate(realm: DynamicRealm) { + realm.schema.get("HomeServerCapabilitiesEntity") + ?.addField(HomeServerCapabilitiesEntityFields.CAN_CHANGE_DISPLAY_NAME, Boolean::class.java) + ?.addField(HomeServerCapabilitiesEntityFields.CAN_CHANGE_AVATAR, Boolean::class.java) + ?.addField(HomeServerCapabilitiesEntityFields.CAN_CHANGE3PID, Boolean::class.java) + } +} diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/HomeServerCapabilitiesEntity.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/HomeServerCapabilitiesEntity.kt index 980449ddfb..08ecd5995e 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/HomeServerCapabilitiesEntity.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/HomeServerCapabilitiesEntity.kt @@ -21,6 +21,9 @@ import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilities internal open class HomeServerCapabilitiesEntity( var canChangePassword: Boolean = true, + var canChangeDisplayName: Boolean = true, + var canChangeAvatar: Boolean = true, + var canChange3pid: Boolean = true, var roomVersionsJson: String? = null, var maxUploadFileSize: Long = HomeServerCapabilities.MAX_UPLOAD_FILE_SIZE_UNKNOWN, var lastVersionIdentityServerSupported: Boolean = false, diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/homeserver/GetCapabilitiesResult.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/homeserver/GetCapabilitiesResult.kt index 9e68309dad..830a58cd12 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/homeserver/GetCapabilitiesResult.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/homeserver/GetCapabilitiesResult.kt @@ -18,7 +18,6 @@ package org.matrix.android.sdk.internal.session.homeserver import com.squareup.moshi.Json import com.squareup.moshi.JsonClass -import org.matrix.android.sdk.api.extensions.orTrue import org.matrix.android.sdk.api.util.JsonDict /** @@ -42,6 +41,25 @@ internal data class Capabilities( @Json(name = "m.change_password") val changePassword: BooleanCapability? = null, + /** + * Capability to indicate if the user can change their display name. + * True if the user can change their display name, false otherwise. + */ + @Json(name = "m.set_displayname") + val changeDisplayName: BooleanCapability? = null, + + /** + * Capability to indicate if the user can change their avatar. + * True if the user can change their avatar, false otherwise. + */ + @Json(name = "m.set_avatar_url") + val changeAvatar: BooleanCapability? = null, + /** + * Capability to indicate if the user can change add, remove or change 3PID associations. + * True if the user can change their 3PID associations, false otherwise. + */ + @Json(name = "m.3pid_changes") + val change3pid: BooleanCapability? = null, /** * This capability describes the default and available room versions a server supports, and at what level of stability. * Clients should make use of this capability to determine if users need to be encouraged to upgrade their rooms. @@ -88,8 +106,3 @@ internal data class RoomVersions( @Json(name = "org.matrix.msc3244.room_capabilities") val roomCapabilities: JsonDict? = null ) - -// The spec says: If not present, the client should assume that password changes are possible via the API -internal fun GetCapabilitiesResult.canChangePassword(): Boolean { - return capabilities?.changePassword?.enabled.orTrue() -} diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/homeserver/GetHomeServerCapabilitiesTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/homeserver/GetHomeServerCapabilitiesTask.kt index 612b98f863..e822cbdcdb 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/homeserver/GetHomeServerCapabilitiesTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/homeserver/GetHomeServerCapabilitiesTask.kt @@ -20,6 +20,7 @@ import com.zhuinden.monarchy.Monarchy import org.matrix.android.sdk.api.MatrixPatterns.getDomain import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig import org.matrix.android.sdk.api.auth.wellknown.WellknownResult +import org.matrix.android.sdk.api.extensions.orTrue import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilities import org.matrix.android.sdk.internal.auth.version.Versions import org.matrix.android.sdk.internal.auth.version.isLoginAndRegistrationSupportedBySdk @@ -108,9 +109,16 @@ internal class DefaultGetHomeServerCapabilitiesTask @Inject constructor( val homeServerCapabilitiesEntity = HomeServerCapabilitiesEntity.getOrCreate(realm) if (getCapabilitiesResult != null) { - homeServerCapabilitiesEntity.canChangePassword = getCapabilitiesResult.canChangePassword() + val capabilities = getCapabilitiesResult.capabilities - homeServerCapabilitiesEntity.roomVersionsJson = getCapabilitiesResult.capabilities?.roomVersions?.let { + // The spec says: If not present, the client should assume that + // password, display name, avatar changes and 3pid changes are possible via the API + homeServerCapabilitiesEntity.canChangePassword = capabilities?.changePassword?.enabled.orTrue() + homeServerCapabilitiesEntity.canChangeDisplayName = capabilities?.changeDisplayName?.enabled.orTrue() + homeServerCapabilitiesEntity.canChangeAvatar = capabilities?.changeAvatar?.enabled.orTrue() + homeServerCapabilitiesEntity.canChange3pid = capabilities?.change3pid?.enabled.orTrue() + + homeServerCapabilitiesEntity.roomVersionsJson = capabilities?.roomVersions?.let { MoshiProvider.providesMoshi().adapter(RoomVersions::class.java).toJson(it) } }