From eec65fb622cb6bea6f3fd8c765e970ce73b5486d Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 20 Feb 2020 18:17:57 +0100 Subject: [PATCH] Join room by roomId or room alias --- .../src/main/java/im/vector/matrix/rx/RxSession.kt | 4 ++-- .../android/api/session/room/RoomDirectoryService.kt | 4 ++-- .../matrix/android/api/session/room/RoomService.kt | 4 ++-- .../session/room/model/create/CreateRoomResponse.kt | 7 ++++++- .../session/room/DefaultRoomDirectoryService.kt | 4 ++-- .../internal/session/room/DefaultRoomService.kt | 4 ++-- .../matrix/android/internal/session/room/RoomAPI.kt | 5 +++-- .../internal/session/room/create/CreateRoomTask.kt | 2 +- .../session/room/membership/joining/JoinRoomTask.kt | 12 +++++++----- 9 files changed, 27 insertions(+), 19 deletions(-) diff --git a/matrix-sdk-android-rx/src/main/java/im/vector/matrix/rx/RxSession.kt b/matrix-sdk-android-rx/src/main/java/im/vector/matrix/rx/RxSession.kt index 82e1d71f44..87ff6f0390 100644 --- a/matrix-sdk-android-rx/src/main/java/im/vector/matrix/rx/RxSession.kt +++ b/matrix-sdk-android-rx/src/main/java/im/vector/matrix/rx/RxSession.kt @@ -95,10 +95,10 @@ class RxSession(private val session: Session) { session.searchUsersDirectory(search, limit, excludedUserIds, it) } - fun joinRoom(roomId: String, + fun joinRoom(roomIdOrAlias: String, reason: String? = null, viaServers: List = emptyList()): Single = singleBuilder { - session.joinRoom(roomId, reason, viaServers, it) + session.joinRoom(roomIdOrAlias, reason, viaServers, it) } fun getRoomIdByAlias(roomAlias: String, diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/RoomDirectoryService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/RoomDirectoryService.kt index c0e413f83b..0273c789dd 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/RoomDirectoryService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/RoomDirectoryService.kt @@ -35,9 +35,9 @@ interface RoomDirectoryService { callback: MatrixCallback): Cancelable /** - * Join a room by id + * Join a room by id, or room alias */ - fun joinRoom(roomId: String, + fun joinRoom(roomIdOrAlias: String, reason: String? = null, callback: MatrixCallback): Cancelable diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/RoomService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/RoomService.kt index 9fec605bd9..93761dfd26 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/RoomService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/RoomService.kt @@ -36,11 +36,11 @@ interface RoomService { /** * Join a room by id - * @param roomId the roomId of the room to join + * @param roomIdOrAlias the roomId or the room alias of the room to join * @param reason optional reason for joining the room * @param viaServers the servers to attempt to join the room through. One of the servers must be participating in the room. */ - fun joinRoom(roomId: String, + fun joinRoom(roomIdOrAlias: String, reason: String? = null, viaServers: List = emptyList(), callback: MatrixCallback): Cancelable diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/create/CreateRoomResponse.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/create/CreateRoomResponse.kt index c437bcfbf1..29c8cb830d 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/create/CreateRoomResponse.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/create/CreateRoomResponse.kt @@ -21,5 +21,10 @@ import com.squareup.moshi.JsonClass @JsonClass(generateAdapter = true) internal data class CreateRoomResponse( - @Json(name = "room_id") var roomId: String? = null + /** + * Required. The created room's ID. + */ + @Json(name = "room_id") var roomId: String ) + +internal typealias JoinRoomResponse = CreateRoomResponse diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomDirectoryService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomDirectoryService.kt index 711e2bd97c..ef55702de6 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomDirectoryService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomDirectoryService.kt @@ -44,9 +44,9 @@ internal class DefaultRoomDirectoryService @Inject constructor(private val getPu .executeBy(taskExecutor) } - override fun joinRoom(roomId: String, reason: String?, callback: MatrixCallback): Cancelable { + override fun joinRoom(roomIdOrAlias: String, reason: String?, callback: MatrixCallback): Cancelable { return joinRoomTask - .configureWith(JoinRoomTask.Params(roomId, reason)) { + .configureWith(JoinRoomTask.Params(roomIdOrAlias, reason)) { this.callback = callback } .executeBy(taskExecutor) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomService.kt index e049c1869d..84fc357160 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomService.kt @@ -139,9 +139,9 @@ internal class DefaultRoomService @Inject constructor( .executeBy(taskExecutor) } - override fun joinRoom(roomId: String, reason: String?, viaServers: List, callback: MatrixCallback): Cancelable { + override fun joinRoom(roomIdOrAlias: String, reason: String?, viaServers: List, callback: MatrixCallback): Cancelable { return joinRoomTask - .configureWith(JoinRoomTask.Params(roomId, reason, viaServers)) { + .configureWith(JoinRoomTask.Params(roomIdOrAlias, reason, viaServers)) { this.callback = callback } .executeBy(taskExecutor) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomAPI.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomAPI.kt index 9be63565fe..f5ddf6ae4b 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomAPI.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/RoomAPI.kt @@ -20,6 +20,7 @@ import im.vector.matrix.android.api.session.events.model.Content import im.vector.matrix.android.api.session.events.model.Event import im.vector.matrix.android.api.session.room.model.create.CreateRoomParams import im.vector.matrix.android.api.session.room.model.create.CreateRoomResponse +import im.vector.matrix.android.api.session.room.model.create.JoinRoomResponse import im.vector.matrix.android.api.session.room.model.roomdirectory.PublicRoomsParams import im.vector.matrix.android.api.session.room.model.roomdirectory.PublicRoomsResponse import im.vector.matrix.android.api.session.room.model.thirdparty.ThirdPartyProtocol @@ -223,13 +224,13 @@ internal interface RoomAPI { * Join the given room. * * @param roomIdOrAlias the room id or alias - * @param server_name the servers to attempt to join the room through + * @param viaServers the servers to attempt to join the room through * @param params the request body */ @POST(NetworkConstants.URI_API_PREFIX_PATH_R0 + "join/{roomIdOrAlias}") fun join(@Path("roomIdOrAlias") roomIdOrAlias: String, @Query("server_name") viaServers: List, - @Body params: Map): Call + @Body params: Map): Call /** * Leave the given room. diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/create/CreateRoomTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/create/CreateRoomTask.kt index f120130739..7314f55961 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/create/CreateRoomTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/create/CreateRoomTask.kt @@ -66,7 +66,7 @@ internal class DefaultCreateRoomTask @Inject constructor( val createRoomResponse = executeRequest(eventBus) { apiCall = roomAPI.createRoom(createRoomParams) } - val roomId = createRoomResponse.roomId!! + val roomId = createRoomResponse.roomId // Wait for room to come back from the sync (but it can maybe be in the DB if the sync response is received before) try { awaitNotEmptyResult(realmConfiguration, TimeUnit.MINUTES.toMillis(1L)) { realm -> diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/joining/JoinRoomTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/joining/JoinRoomTask.kt index 0153930226..635f3955c2 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/joining/JoinRoomTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/membership/joining/JoinRoomTask.kt @@ -17,6 +17,7 @@ package im.vector.matrix.android.internal.session.room.membership.joining import im.vector.matrix.android.api.session.room.failure.JoinRoomFailure +import im.vector.matrix.android.api.session.room.model.create.JoinRoomResponse import im.vector.matrix.android.internal.database.awaitNotEmptyResult import im.vector.matrix.android.internal.database.model.RoomEntity import im.vector.matrix.android.internal.database.model.RoomEntityFields @@ -33,7 +34,7 @@ import javax.inject.Inject internal interface JoinRoomTask : Task { data class Params( - val roomId: String, + val roomIdOrAlias: String, val reason: String?, val viaServers: List = emptyList() ) @@ -48,19 +49,20 @@ internal class DefaultJoinRoomTask @Inject constructor( ) : JoinRoomTask { override suspend fun execute(params: JoinRoomTask.Params) { - executeRequest(eventBus) { - apiCall = roomAPI.join(params.roomId, params.viaServers, mapOf("reason" to params.reason)) + val joinRoomResponse = executeRequest(eventBus) { + apiCall = roomAPI.join(params.roomIdOrAlias, params.viaServers, mapOf("reason" to params.reason)) } // Wait for room to come back from the sync (but it can maybe be in the DB is the sync response is received before) + val roomId = joinRoomResponse.roomId try { awaitNotEmptyResult(realmConfiguration, TimeUnit.MINUTES.toMillis(1L)) { realm -> realm.where(RoomEntity::class.java) - .equalTo(RoomEntityFields.ROOM_ID, params.roomId) + .equalTo(RoomEntityFields.ROOM_ID, roomId) } } catch (exception: TimeoutCancellationException) { throw JoinRoomFailure.JoinedWithTimeout } - setReadMarkers(params.roomId) + setReadMarkers(roomId) } private suspend fun setReadMarkers(roomId: String) {