Skip to content

Commit

Permalink
Convert MembershipService to suspend functions
Browse files Browse the repository at this point in the history
Signed-off-by: Dominic Fischer <[email protected]>
  • Loading branch information
Dominaezzz committed Mar 29, 2021
1 parent 2045a16 commit bc68075
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import io.reactivex.Completable
import io.reactivex.Observable
import io.reactivex.Single
import kotlinx.coroutines.rx2.rxCompletable
import kotlinx.coroutines.rx2.rxSingle
import org.matrix.android.sdk.api.query.QueryStringValue
import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.api.session.identity.ThreePid
Expand Down Expand Up @@ -90,13 +91,13 @@ class RxRoom(private val room: Room) {
return room.getMyReadReceiptLive().asObservable()
}

fun loadRoomMembersIfNeeded(): Single<Unit> = singleBuilder {
room.loadRoomMembersIfNeeded(it)
fun loadRoomMembersIfNeeded(): Single<Unit> = rxSingle {
room.loadRoomMembersIfNeeded()
}

fun joinRoom(reason: String? = null,
viaServers: List<String> = emptyList()): Single<Unit> = singleBuilder {
room.join(reason, viaServers, it)
viaServers: List<String> = emptyList()): Single<Unit> = rxSingle {
room.join(reason, viaServers)
}

fun liveEventReadReceipts(eventId: String): Observable<List<ReadReceipt>> {
Expand All @@ -114,12 +115,12 @@ class RxRoom(private val room: Room) {
return room.getLiveRoomNotificationState().asObservable()
}

fun invite(userId: String, reason: String? = null): Completable = completableBuilder<Unit> {
room.invite(userId, reason, it)
fun invite(userId: String, reason: String? = null): Completable = rxCompletable {
room.invite(userId, reason)
}

fun invite3pid(threePid: ThreePid): Completable = completableBuilder<Unit> {
room.invite3pid(threePid, it)
fun invite3pid(threePid: ThreePid): Completable = rxCompletable {
room.invite3pid(threePid)
}

fun updateTopic(topic: String): Completable = rxCompletable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ class CryptoTestHelper(private val mTestHelper: CommonTestHelper) {
bobRoomSummariesLive.observeForever(newRoomObserver)
}

mTestHelper.doSync<Unit> {
aliceRoom.invite(bobSession.myUserId, callback = it)
mTestHelper.runBlockingTest {
aliceRoom.invite(bobSession.myUserId)
}

mTestHelper.await(lock1)
Expand Down Expand Up @@ -172,8 +172,8 @@ class CryptoTestHelper(private val mTestHelper: CommonTestHelper) {
fun createSamAccountAndInviteToTheRoom(room: Room): Session {
val samSession = mTestHelper.createAccount(TestConstants.USER_SAM, defaultSessionParams)

mTestHelper.doSync<Unit> {
room.invite(samSession.myUserId, null, it)
mTestHelper.runBlockingTest {
room.invite(samSession.myUserId, null)
}

mTestHelper.doSync<Unit> {
Expand Down Expand Up @@ -411,7 +411,7 @@ class CryptoTestHelper(private val mTestHelper: CommonTestHelper) {
val sessions = mutableListOf(aliceSession)
for (index in 1 until numberOfMembers) {
val session = mTestHelper.createAccount("User_$index", defaultSessionParams)
mTestHelper.doSync<Unit>(timeout = 600_000) { room.invite(session.myUserId, null, it) }
mTestHelper.runBlockingTest(timeout = 600_000) { room.invite(session.myUserId, null) }
println("TEST -> " + session.myUserId + " invited")
mTestHelper.doSync<Unit> { session.joinRoom(room.roomId, null, emptyList(), it) }
println("TEST -> " + session.myUserId + " joined")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,8 @@ class KeyShareTests : InstrumentedTest {
}

// Let alice invite bob
mTestHelper.doSync<Unit> {
roomAlicePov.invite(bobSession.myUserId, null, it)
mTestHelper.runBlockingTest {
roomAlicePov.invite(bobSession.myUserId, null)
}

mTestHelper.doSync<Unit> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
package org.matrix.android.sdk.api.session.room.members

import androidx.lifecycle.LiveData
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.session.identity.ThreePid
import org.matrix.android.sdk.api.session.room.model.RoomMemberSummary
import org.matrix.android.sdk.api.util.Cancelable

/**
* This interface defines methods to handling membership. It's implemented at the room level.
Expand All @@ -29,9 +27,8 @@ interface MembershipService {

/**
* This methods load all room members if it was done yet.
* @return a [Cancelable]
*/
fun loadRoomMembersIfNeeded(matrixCallback: MatrixCallback<Unit>): Cancelable
suspend fun loadRoomMembersIfNeeded()

/**
* Return the roomMember with userId or null.
Expand Down Expand Up @@ -60,47 +57,35 @@ interface MembershipService {
/**
* Invite a user in the room
*/
fun invite(userId: String,
reason: String? = null,
callback: MatrixCallback<Unit>): Cancelable
suspend fun invite(userId: String, reason: String? = null)

/**
* Invite a user with email or phone number in the room
*/
fun invite3pid(threePid: ThreePid,
callback: MatrixCallback<Unit>): Cancelable
suspend fun invite3pid(threePid: ThreePid)

/**
* Ban a user from the room
*/
fun ban(userId: String,
reason: String? = null,
callback: MatrixCallback<Unit>): Cancelable
suspend fun ban(userId: String, reason: String? = null)

/**
* Unban a user from the room
*/
fun unban(userId: String,
reason: String? = null,
callback: MatrixCallback<Unit>): Cancelable
suspend fun unban(userId: String, reason: String? = null)

/**
* Kick a user from the room
*/
fun kick(userId: String,
reason: String? = null,
callback: MatrixCallback<Unit>): Cancelable
suspend fun kick(userId: String, reason: String? = null)

/**
* Join the room, or accept an invitation.
*/
fun join(reason: String? = null,
viaServers: List<String> = emptyList(),
callback: MatrixCallback<Unit>): Cancelable
suspend fun join(reason: String? = null, viaServers: List<String> = emptyList())

/**
* Leave the room, or reject an invitation.
*/
fun leave(reason: String? = null,
callback: MatrixCallback<Unit>): Cancelable
suspend fun leave(reason: String? = null)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import dagger.assisted.AssistedFactory
import com.zhuinden.monarchy.Monarchy
import org.matrix.android.sdk.api.MatrixCallback
import org.matrix.android.sdk.api.session.identity.ThreePid
import org.matrix.android.sdk.api.session.room.members.MembershipService
import org.matrix.android.sdk.api.session.room.members.RoomMemberQueryParams
import org.matrix.android.sdk.api.session.room.model.Membership
import org.matrix.android.sdk.api.session.room.model.RoomMemberSummary
import org.matrix.android.sdk.api.util.Cancelable
import org.matrix.android.sdk.internal.database.mapper.asDomain
import org.matrix.android.sdk.internal.database.model.RoomMemberSummaryEntity
import org.matrix.android.sdk.internal.database.model.RoomMemberSummaryEntityFields
Expand All @@ -39,16 +37,13 @@ import org.matrix.android.sdk.internal.session.room.membership.joining.InviteTas
import org.matrix.android.sdk.internal.session.room.membership.joining.JoinRoomTask
import org.matrix.android.sdk.internal.session.room.membership.leaving.LeaveRoomTask
import org.matrix.android.sdk.internal.session.room.membership.threepid.InviteThreePidTask
import org.matrix.android.sdk.internal.task.TaskExecutor
import org.matrix.android.sdk.internal.task.configureWith
import org.matrix.android.sdk.internal.util.fetchCopied
import io.realm.Realm
import io.realm.RealmQuery

internal class DefaultMembershipService @AssistedInject constructor(
@Assisted private val roomId: String,
@SessionDatabase private val monarchy: Monarchy,
private val taskExecutor: TaskExecutor,
private val loadRoomMembersTask: LoadRoomMembersTask,
private val inviteTask: InviteTask,
private val inviteThreePidTask: InviteThreePidTask,
Expand All @@ -64,13 +59,9 @@ internal class DefaultMembershipService @AssistedInject constructor(
fun create(roomId: String): DefaultMembershipService
}

override fun loadRoomMembersIfNeeded(matrixCallback: MatrixCallback<Unit>): Cancelable {
override suspend fun loadRoomMembersIfNeeded() {
val params = LoadRoomMembersTask.Params(roomId, Membership.LEAVE)
return loadRoomMembersTask
.configureWith(params) {
this.callback = matrixCallback
}
.executeBy(taskExecutor)
loadRoomMembersTask.execute(params)
}

override fun getRoomMember(userId: String): RoomMemberSummary? {
Expand Down Expand Up @@ -120,66 +111,38 @@ internal class DefaultMembershipService @AssistedInject constructor(
}
}

override fun ban(userId: String, reason: String?, callback: MatrixCallback<Unit>): Cancelable {
override suspend fun ban(userId: String, reason: String?) {
val params = MembershipAdminTask.Params(MembershipAdminTask.Type.BAN, roomId, userId, reason)
return membershipAdminTask
.configureWith(params) {
this.callback = callback
}
.executeBy(taskExecutor)
membershipAdminTask.execute(params)
}

override fun unban(userId: String, reason: String?, callback: MatrixCallback<Unit>): Cancelable {
override suspend fun unban(userId: String, reason: String?) {
val params = MembershipAdminTask.Params(MembershipAdminTask.Type.UNBAN, roomId, userId, reason)
return membershipAdminTask
.configureWith(params) {
this.callback = callback
}
.executeBy(taskExecutor)
membershipAdminTask.execute(params)
}

override fun kick(userId: String, reason: String?, callback: MatrixCallback<Unit>): Cancelable {
override suspend fun kick(userId: String, reason: String?) {
val params = MembershipAdminTask.Params(MembershipAdminTask.Type.KICK, roomId, userId, reason)
return membershipAdminTask
.configureWith(params) {
this.callback = callback
}
.executeBy(taskExecutor)
membershipAdminTask.execute(params)
}

override fun invite(userId: String, reason: String?, callback: MatrixCallback<Unit>): Cancelable {
override suspend fun invite(userId: String, reason: String?) {
val params = InviteTask.Params(roomId, userId, reason)
return inviteTask
.configureWith(params) {
this.callback = callback
}
.executeBy(taskExecutor)
inviteTask.execute(params)
}

override fun invite3pid(threePid: ThreePid, callback: MatrixCallback<Unit>): Cancelable {
override suspend fun invite3pid(threePid: ThreePid) {
val params = InviteThreePidTask.Params(roomId, threePid)
return inviteThreePidTask
.configureWith(params) {
this.callback = callback
}
.executeBy(taskExecutor)
return inviteThreePidTask.execute(params)
}

override fun join(reason: String?, viaServers: List<String>, callback: MatrixCallback<Unit>): Cancelable {
override suspend fun join(reason: String?, viaServers: List<String>) {
val params = JoinRoomTask.Params(roomId, reason, viaServers)
return joinTask
.configureWith(params) {
this.callback = callback
}
.executeBy(taskExecutor)
joinTask.execute(params)
}

override fun leave(reason: String?, callback: MatrixCallback<Unit>): Cancelable {
override suspend fun leave(reason: String?) {
val params = LeaveRoomTask.Params(roomId, reason)
return leaveRoomTask
.configureWith(params) {
this.callback = callback
}
.executeBy(taskExecutor)
leaveRoomTask.execute(params)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -940,14 +940,14 @@ class RoomDetailViewModel @AssistedInject constructor(
}

private fun handleInviteSlashCommand(invite: ParsedCommand.Invite) {
launchSlashCommandFlow {
room.invite(invite.userId, invite.reason, it)
launchSlashCommandFlowSuspendable {
room.invite(invite.userId, invite.reason)
}
}

private fun handleInvite3pidSlashCommand(invite: ParsedCommand.Invite3Pid) {
launchSlashCommandFlow {
room.invite3pid(invite.threePid, it)
launchSlashCommandFlowSuspendable {
room.invite3pid(invite.threePid)
}
}

Expand All @@ -971,20 +971,20 @@ class RoomDetailViewModel @AssistedInject constructor(
}

private fun handleKickSlashCommand(kick: ParsedCommand.KickUser) {
launchSlashCommandFlow {
room.kick(kick.userId, kick.reason, it)
launchSlashCommandFlowSuspendable {
room.kick(kick.userId, kick.reason)
}
}

private fun handleBanSlashCommand(ban: ParsedCommand.BanUser) {
launchSlashCommandFlow {
room.ban(ban.userId, ban.reason, it)
launchSlashCommandFlowSuspendable {
room.ban(ban.userId, ban.reason)
}
}

private fun handleUnbanSlashCommand(unban: ParsedCommand.UnbanUser) {
launchSlashCommandFlow {
room.unban(unban.userId, unban.reason, it)
launchSlashCommandFlowSuspendable {
room.unban(unban.userId, unban.reason)
}
}

Expand Down Expand Up @@ -1088,11 +1088,21 @@ class RoomDetailViewModel @AssistedInject constructor(
}

private fun handleRejectInvite() {
room.leave(null, NoOpMatrixCallback())
viewModelScope.launch {
try {
room.leave(null)
} catch (_: Exception) {
}
}
}

private fun handleAcceptInvite() {
room.join(callback = NoOpMatrixCallback())
viewModelScope.launch {
try {
room.join()
} catch (_: Exception) {
}
}
}

private fun handleEditAction(action: RoomDetailAction.EnterEditMode) {
Expand Down
Loading

0 comments on commit bc68075

Please sign in to comment.