Skip to content

Commit d1a9cca

Browse files
committed
adding support for the homeserver display name and avatar capabilities
- MSC3283 matrix-org/synapse#11933 - includes session database migration
1 parent a5f4413 commit d1a9cca

File tree

7 files changed

+81
-8
lines changed

7 files changed

+81
-8
lines changed

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/homeserver/HomeServerCapabilities.kt

+13
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ data class HomeServerCapabilities(
2121
* True if it is possible to change the password of the account.
2222
*/
2323
val canChangePassword: Boolean = true,
24+
/**
25+
* True if it is possible to change the display name of the account.
26+
*/
27+
val canChangeDisplayName: Boolean = true,
28+
/**
29+
* True if it is possible to change the avatar of the account.
30+
*/
31+
val canChangeAvatar: Boolean = true,
32+
/**
33+
* True if it is possible to change the 3pid associations of the account.
34+
*/
35+
val canChange3pid: Boolean = true,
2436
/**
2537
* Max size of file which can be uploaded to the homeserver in bytes. [MAX_UPLOAD_FILE_SIZE_UNKNOWN] if unknown or not retrieved yet
2638
*/
@@ -76,6 +88,7 @@ data class HomeServerCapabilities(
7688
}
7789
}
7890
}
91+
7992
fun isFeatureSupported(feature: String, byRoomVersion: String): Boolean {
8093
if (roomVersions?.capabilities == null) return false
8194
val info = roomVersions.capabilities[feature] ?: return false

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/RealmSessionStoreMigration.kt

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo021
4242
import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo022
4343
import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo023
4444
import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo024
45+
import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo025
4546
import org.matrix.android.sdk.internal.util.Normalizer
4647
import timber.log.Timber
4748
import javax.inject.Inject
@@ -85,5 +86,6 @@ internal class RealmSessionStoreMigration @Inject constructor(
8586
if (oldVersion < 22) MigrateSessionTo022(realm).perform()
8687
if (oldVersion < 23) MigrateSessionTo023(realm).perform()
8788
if (oldVersion < 24) MigrateSessionTo024(realm).perform()
89+
if (oldVersion < 25) MigrateSessionTo025(realm).perform()
8890
}
8991
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/mapper/HomeServerCapabilitiesMapper.kt

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ internal object HomeServerCapabilitiesMapper {
3535
fun map(entity: HomeServerCapabilitiesEntity): HomeServerCapabilities {
3636
return HomeServerCapabilities(
3737
canChangePassword = entity.canChangePassword,
38+
canChangeDisplayName = entity.canChangeDisplayName,
39+
canChangeAvatar = entity.canChangeAvatar,
40+
canChange3pid = entity.canChange3Pid,
3841
maxUploadFileSize = entity.maxUploadFileSize,
3942
lastVersionIdentityServerSupported = entity.lastVersionIdentityServerSupported,
4043
defaultIdentityServerUrl = entity.defaultIdentityServerUrl,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2022 The Matrix.org Foundation C.I.C.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.matrix.android.sdk.internal.database.migration
18+
19+
import io.realm.DynamicRealm
20+
import org.matrix.android.sdk.internal.database.model.HomeServerCapabilitiesEntityFields
21+
import org.matrix.android.sdk.internal.util.database.RealmMigrator
22+
23+
class MigrateSessionTo025(realm: DynamicRealm) : RealmMigrator(realm, 25) {
24+
25+
override fun doMigrate(realm: DynamicRealm) {
26+
realm.schema.get("HomeServerCapabilitiesEntity")
27+
?.addField(HomeServerCapabilitiesEntityFields.CAN_CHANGE_DISPLAY_NAME, Boolean::class.java)
28+
?.addField(HomeServerCapabilitiesEntityFields.CAN_CHANGE_AVATAR, Boolean::class.java)
29+
?.addField(HomeServerCapabilitiesEntityFields.CAN_CHANGE3PID, Boolean::class.java)
30+
}
31+
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/HomeServerCapabilitiesEntity.kt

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilities
2121

2222
internal open class HomeServerCapabilitiesEntity(
2323
var canChangePassword: Boolean = true,
24+
var canChangeDisplayName: Boolean = true,
25+
var canChangeAvatar: Boolean = true,
26+
var canChange3Pid: Boolean = true,
2427
var roomVersionsJson: String? = null,
2528
var maxUploadFileSize: Long = HomeServerCapabilities.MAX_UPLOAD_FILE_SIZE_UNKNOWN,
2629
var lastVersionIdentityServerSupported: Boolean = false,

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/homeserver/GetCapabilitiesResult.kt

+19-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package org.matrix.android.sdk.internal.session.homeserver
1818

1919
import com.squareup.moshi.Json
2020
import com.squareup.moshi.JsonClass
21-
import org.matrix.android.sdk.api.extensions.orTrue
2221
import org.matrix.android.sdk.api.util.JsonDict
2322

2423
/**
@@ -42,6 +41,25 @@ internal data class Capabilities(
4241
@Json(name = "m.change_password")
4342
val changePassword: BooleanCapability? = null,
4443

44+
/**
45+
* Capability to indicate if the user can change their display name.
46+
* True if the user can change their display name, false otherwise.
47+
*/
48+
@Json(name = "m.set_displayname")
49+
val changeDisplayName: BooleanCapability? = null,
50+
51+
/**
52+
* Capability to indicate if the user can change their avatar.
53+
* True if the user can change their avatar, false otherwise.
54+
*/
55+
@Json(name = "m.set_avatar_url")
56+
val changeAvatar: BooleanCapability? = null,
57+
/**
58+
* Capability to indicate if the user can change add, remove or change 3PID associations.
59+
* True if the user can change their 3PID associations, false otherwise.
60+
*/
61+
@Json(name = "m.3pid_changes")
62+
val change3pid: BooleanCapability? = null,
4563
/**
4664
* This capability describes the default and available room versions a server supports, and at what level of stability.
4765
* 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(
88106
@Json(name = "org.matrix.msc3244.room_capabilities")
89107
val roomCapabilities: JsonDict? = null
90108
)
91-
92-
// The spec says: If not present, the client should assume that password changes are possible via the API
93-
internal fun GetCapabilitiesResult.canChangePassword(): Boolean {
94-
return capabilities?.changePassword?.enabled.orTrue()
95-
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/homeserver/GetHomeServerCapabilitiesTask.kt

+10-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import com.zhuinden.monarchy.Monarchy
2020
import org.matrix.android.sdk.api.MatrixPatterns.getDomain
2121
import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig
2222
import org.matrix.android.sdk.api.auth.wellknown.WellknownResult
23+
import org.matrix.android.sdk.api.extensions.orTrue
2324
import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilities
2425
import org.matrix.android.sdk.internal.auth.version.Versions
2526
import org.matrix.android.sdk.internal.auth.version.isLoginAndRegistrationSupportedBySdk
@@ -108,9 +109,16 @@ internal class DefaultGetHomeServerCapabilitiesTask @Inject constructor(
108109
val homeServerCapabilitiesEntity = HomeServerCapabilitiesEntity.getOrCreate(realm)
109110

110111
if (getCapabilitiesResult != null) {
111-
homeServerCapabilitiesEntity.canChangePassword = getCapabilitiesResult.canChangePassword()
112+
val capabilities = getCapabilitiesResult.capabilities
112113

113-
homeServerCapabilitiesEntity.roomVersionsJson = getCapabilitiesResult.capabilities?.roomVersions?.let {
114+
// The spec says: If not present, the client should assume that
115+
// password, display name, avatar changes and 3pid changes are possible via the API
116+
homeServerCapabilitiesEntity.canChangePassword = capabilities?.changePassword?.enabled.orTrue()
117+
homeServerCapabilitiesEntity.canChangeDisplayName = capabilities?.changeDisplayName?.enabled.orTrue()
118+
homeServerCapabilitiesEntity.canChangeAvatar = capabilities?.changeAvatar?.enabled.orTrue()
119+
homeServerCapabilitiesEntity.canChange3Pid = capabilities?.change3pid?.enabled.orTrue()
120+
121+
homeServerCapabilitiesEntity.roomVersionsJson = capabilities?.roomVersions?.let {
114122
MoshiProvider.providesMoshi().adapter(RoomVersions::class.java).toJson(it)
115123
}
116124
}

0 commit comments

Comments
 (0)