Skip to content

Commit

Permalink
adding support for the homeserver display name and avatar capabilities
Browse files Browse the repository at this point in the history
- MSC3283 matrix-org/synapse#11933
- includes session database migration
  • Loading branch information
ouchadam committed Feb 11, 2022
1 parent a5f4413 commit 2d0eb2c
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand All @@ -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.
Expand Down Expand Up @@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -108,9 +109,15 @@ 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 and avatar 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?.changeAvatar?.enabled.orTrue()

homeServerCapabilitiesEntity.roomVersionsJson = capabilities?.roomVersions?.let {
MoshiProvider.providesMoshi().adapter(RoomVersions::class.java).toJson(it)
}
}
Expand Down

0 comments on commit 2d0eb2c

Please sign in to comment.