From 2a9e582db5fc0cad3a06b1fca4c9967389c28a5d Mon Sep 17 00:00:00 2001 From: ClaireG Date: Thu, 3 Mar 2022 22:40:26 +0100 Subject: [PATCH] Merge pull request #5405 from vector-im/cgizard/ISSUE-5402 [Create private room] Picture doesn't not displayed --- changelog.d/5402.bugfix | 1 + .../internal/session/content/FileUploader.kt | 18 +++++---- .../session/profile/DefaultProfileService.kt | 8 ++-- .../room/create/CreateRoomBodyBuilder.kt | 37 +++++++++---------- 4 files changed, 33 insertions(+), 31 deletions(-) create mode 100644 changelog.d/5402.bugfix diff --git a/changelog.d/5402.bugfix b/changelog.d/5402.bugfix new file mode 100644 index 0000000000..fde9e7e74f --- /dev/null +++ b/changelog.d/5402.bugfix @@ -0,0 +1 @@ +[Create room] Setting an avatar when creating a room had no effect \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/FileUploader.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/FileUploader.kt index b988f2253c..e9cb423893 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/FileUploader.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/FileUploader.kt @@ -30,6 +30,7 @@ import okhttp3.RequestBody import okhttp3.RequestBody.Companion.toRequestBody import okio.BufferedSink import okio.source +import org.matrix.android.sdk.api.MatrixCoroutineDispatchers import org.matrix.android.sdk.api.extensions.tryOrNull import org.matrix.android.sdk.api.failure.Failure import org.matrix.android.sdk.api.failure.MatrixError @@ -53,6 +54,7 @@ internal class FileUploader @Inject constructor( private val homeServerCapabilitiesService: HomeServerCapabilitiesService, private val context: Context, private val temporaryFileCreator: TemporaryFileCreator, + private val coroutineDispatchers: MatrixCoroutineDispatchers, contentUrlResolver: ContentUrlResolver, moshi: Moshi ) { @@ -146,14 +148,16 @@ internal class FileUploader @Inject constructor( .post(requestBody) .build() - return okHttpClient.newCall(request).awaitResponse().use { response -> - if (!response.isSuccessful) { - throw response.toFailure(globalErrorReceiver) - } else { - response.body?.source()?.let { - responseAdapter.fromJson(it) + return withContext(coroutineDispatchers.io) { + okHttpClient.newCall(request).awaitResponse().use { response -> + if (!response.isSuccessful) { + throw response.toFailure(globalErrorReceiver) + } else { + response.body?.source()?.let { + responseAdapter.fromJson(it) + } + ?: throw IOException() } - ?: throw IOException() } } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/profile/DefaultProfileService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/profile/DefaultProfileService.kt index caf4158657..6f99577ac2 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/profile/DefaultProfileService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/profile/DefaultProfileService.kt @@ -68,11 +68,9 @@ internal class DefaultProfileService @Inject constructor(private val taskExecuto } override suspend fun updateAvatar(userId: String, newAvatarUri: Uri, fileName: String) { - withContext(coroutineDispatchers.io) { - val response = fileUploader.uploadFromUri(newAvatarUri, fileName, MimeTypes.Jpeg) - setAvatarUrlTask.execute(SetAvatarUrlTask.Params(userId = userId, newAvatarUrl = response.contentUri)) - userStore.updateAvatar(userId, response.contentUri) - } + val response = fileUploader.uploadFromUri(newAvatarUri, fileName, MimeTypes.Jpeg) + setAvatarUrlTask.execute(SetAvatarUrlTask.Params(userId = userId, newAvatarUrl = response.contentUri)) + userStore.updateAvatar(userId, response.contentUri) } override suspend fun getAvatarUrl(userId: String): Optional { diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/create/CreateRoomBodyBuilder.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/create/CreateRoomBodyBuilder.kt index 84261e6ebf..c9914449c3 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/create/CreateRoomBodyBuilder.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/create/CreateRoomBodyBuilder.kt @@ -112,19 +112,18 @@ internal class CreateRoomBodyBuilder @Inject constructor( private suspend fun buildAvatarEvent(params: CreateRoomParams): Event? { return params.avatarUri?.let { avatarUri -> // First upload the image, ignoring any error - tryOrNull { + tryOrNull("Failed to upload image") { fileUploader.uploadFromUri( uri = avatarUri, filename = UUID.randomUUID().toString(), mimeType = MimeTypes.Jpeg) } - ?.let { response -> - Event( - type = EventType.STATE_ROOM_AVATAR, - stateKey = "", - content = mapOf("url" to response.contentUri) - ) - } + }?.let { response -> + Event( + type = EventType.STATE_ROOM_AVATAR, + stateKey = "", + content = mapOf("url" to response.contentUri) + ) } } @@ -180,19 +179,19 @@ internal class CreateRoomBodyBuilder @Inject constructor( params.invite3pids.isEmpty() && params.invitedUserIds.isNotEmpty() && params.invitedUserIds.let { userIds -> - val keys = deviceListManager.downloadKeys(userIds, forceDownload = false) + val keys = deviceListManager.downloadKeys(userIds, forceDownload = false) - userIds.all { userId -> - keys.map[userId].let { deviceMap -> - if (deviceMap.isNullOrEmpty()) { - // A user has no device, so do not enable encryption - false - } else { - // Check that every user's device have at least one key - deviceMap.values.all { !it.keys.isNullOrEmpty() } + userIds.all { userId -> + keys.map[userId].let { deviceMap -> + if (deviceMap.isNullOrEmpty()) { + // A user has no device, so do not enable encryption + false + } else { + // Check that every user's device have at least one key + deviceMap.values.all { !it.keys.isNullOrEmpty() } + } + } } } - } - } } }