join and leave methods moved from MembershipService to RoomService an… (#5183)

This commit is contained in:
fedrunov 2022-02-10 10:05:03 +01:00 committed by GitHub
parent e71063222b
commit 06b5563ff6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 52 additions and 63 deletions

1
changelog.d/5183.sdk Normal file
View file

@ -0,0 +1 @@
`join` and `leave` methods moved from MembershipService to RoomService and SpaceService to split logic for rooms and spaces

View file

@ -344,7 +344,6 @@ class SpaceHierarchyTest : InstrumentedTest {
// Test part one of the rooms // Test part one of the rooms
val bRoomId = spaceBInfo.roomIds.first() val bRoomId = spaceBInfo.roomIds.first()
val bRoom = session.getRoom(bRoomId)
commonTestHelper.waitWithLatch { latch -> commonTestHelper.waitWithLatch { latch ->
val flatAChildren = session.getFlattenRoomSummaryChildrenOfLive(spaceAInfo.spaceId) val flatAChildren = session.getFlattenRoomSummaryChildrenOfLive(spaceAInfo.spaceId)
@ -360,7 +359,7 @@ class SpaceHierarchyTest : InstrumentedTest {
} }
// part from b room // part from b room
bRoom!!.leave(null) session.leaveRoom(bRoomId)
// The room should have disapear from flat children // The room should have disapear from flat children
flatAChildren.observeForever(childObserver) flatAChildren.observeForever(childObserver)
} }

View file

@ -76,6 +76,13 @@ interface RoomService {
thirdPartySigned: SignInvitationResult thirdPartySigned: SignInvitationResult
) )
/**
* Leave the room, or reject an invitation.
* @param roomId the roomId of the room to leave
* @param reason optional reason for leaving the room
*/
suspend fun leaveRoom(roomId: String, reason: String? = null)
/** /**
* Get a room from a roomId * Get a room from a roomId
* @param roomId the roomId to look for. * @param roomId the roomId to look for.

View file

@ -81,14 +81,4 @@ interface MembershipService {
@Deprecated("Use remove instead", ReplaceWith("remove(userId, reason)")) @Deprecated("Use remove instead", ReplaceWith("remove(userId, reason)"))
suspend fun kick(userId: String, reason: String? = null) = remove(userId, reason) suspend fun kick(userId: String, reason: String? = null) = remove(userId, reason)
/**
* Join the room, or accept an invitation.
*/
suspend fun join(reason: String? = null, viaServers: List<String> = emptyList())
/**
* Leave the room, or reject an invitation.
*/
suspend fun leave(reason: String? = null)
} }

View file

@ -26,8 +26,6 @@ interface Space {
val spaceId: String val spaceId: String
suspend fun leave(reason: String? = null)
/** /**
* A current snapshot of [RoomSummary] associated with the space * A current snapshot of [RoomSummary] associated with the space
*/ */

View file

@ -87,6 +87,13 @@ interface SpaceService {
suspend fun rejectInvite(spaceId: String, reason: String?) suspend fun rejectInvite(spaceId: String, reason: String?)
/**
* Leave the space, or reject an invitation.
* @param spaceId the spaceId of the space to leave
* @param reason optional reason for leaving the space
*/
suspend fun leaveSpace(spaceId: String, reason: String? = null)
// fun getSpaceParentsOfRoom(roomId: String) : List<SpaceSummary> // fun getSpaceParentsOfRoom(roomId: String) : List<SpaceSummary>
/** /**

View file

@ -46,6 +46,7 @@ import org.matrix.android.sdk.internal.session.room.create.CreateRoomTask
import org.matrix.android.sdk.internal.session.room.membership.RoomChangeMembershipStateDataSource import org.matrix.android.sdk.internal.session.room.membership.RoomChangeMembershipStateDataSource
import org.matrix.android.sdk.internal.session.room.membership.RoomMemberHelper import org.matrix.android.sdk.internal.session.room.membership.RoomMemberHelper
import org.matrix.android.sdk.internal.session.room.membership.joining.JoinRoomTask 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.peeking.PeekRoomTask import org.matrix.android.sdk.internal.session.room.peeking.PeekRoomTask
import org.matrix.android.sdk.internal.session.room.peeking.ResolveRoomStateTask import org.matrix.android.sdk.internal.session.room.peeking.ResolveRoomStateTask
import org.matrix.android.sdk.internal.session.room.read.MarkAllRoomsReadTask import org.matrix.android.sdk.internal.session.room.read.MarkAllRoomsReadTask
@ -66,7 +67,8 @@ internal class DefaultRoomService @Inject constructor(
private val peekRoomTask: PeekRoomTask, private val peekRoomTask: PeekRoomTask,
private val roomGetter: RoomGetter, private val roomGetter: RoomGetter,
private val roomSummaryDataSource: RoomSummaryDataSource, private val roomSummaryDataSource: RoomSummaryDataSource,
private val roomChangeMembershipStateDataSource: RoomChangeMembershipStateDataSource private val roomChangeMembershipStateDataSource: RoomChangeMembershipStateDataSource,
private val leaveRoomTask: LeaveRoomTask,
) : RoomService { ) : RoomService {
override suspend fun createRoom(createRoomParams: CreateRoomParams): String { override suspend fun createRoom(createRoomParams: CreateRoomParams): String {
@ -133,6 +135,10 @@ internal class DefaultRoomService @Inject constructor(
joinRoomTask.execute(JoinRoomTask.Params(roomId, reason, thirdPartySigned = thirdPartySigned)) joinRoomTask.execute(JoinRoomTask.Params(roomId, reason, thirdPartySigned = thirdPartySigned))
} }
override suspend fun leaveRoom(roomId: String, reason: String?) {
leaveRoomTask.execute(LeaveRoomTask.Params(roomId, reason))
}
override suspend fun markAllAsRead(roomIds: List<String>) { override suspend fun markAllAsRead(roomIds: List<String>) {
markAllRoomsReadTask.execute(MarkAllRoomsReadTask.Params(roomIds)) markAllRoomsReadTask.execute(MarkAllRoomsReadTask.Params(roomIds))
} }

View file

@ -37,8 +37,6 @@ import org.matrix.android.sdk.internal.query.QueryStringValueProcessor
import org.matrix.android.sdk.internal.query.process import org.matrix.android.sdk.internal.query.process
import org.matrix.android.sdk.internal.session.room.membership.admin.MembershipAdminTask import org.matrix.android.sdk.internal.session.room.membership.admin.MembershipAdminTask
import org.matrix.android.sdk.internal.session.room.membership.joining.InviteTask import org.matrix.android.sdk.internal.session.room.membership.joining.InviteTask
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.session.room.membership.threepid.InviteThreePidTask
import org.matrix.android.sdk.internal.util.fetchCopied import org.matrix.android.sdk.internal.util.fetchCopied
@ -48,8 +46,6 @@ internal class DefaultMembershipService @AssistedInject constructor(
private val loadRoomMembersTask: LoadRoomMembersTask, private val loadRoomMembersTask: LoadRoomMembersTask,
private val inviteTask: InviteTask, private val inviteTask: InviteTask,
private val inviteThreePidTask: InviteThreePidTask, private val inviteThreePidTask: InviteThreePidTask,
private val joinTask: JoinRoomTask,
private val leaveRoomTask: LeaveRoomTask,
private val membershipAdminTask: MembershipAdminTask, private val membershipAdminTask: MembershipAdminTask,
@UserId @UserId
private val userId: String, private val userId: String,
@ -139,14 +135,4 @@ internal class DefaultMembershipService @AssistedInject constructor(
val params = InviteThreePidTask.Params(roomId, threePid) val params = InviteThreePidTask.Params(roomId, threePid)
return inviteThreePidTask.execute(params) return inviteThreePidTask.execute(params)
} }
override suspend fun join(reason: String?, viaServers: List<String>) {
val params = JoinRoomTask.Params(roomId, reason, viaServers)
joinTask.execute(params)
}
override suspend fun leave(reason: String?) {
val params = LeaveRoomTask.Params(roomId, reason)
leaveRoomTask.execute(params)
}
} }

View file

@ -40,10 +40,6 @@ internal class DefaultSpace(
override val spaceId = room.roomId override val spaceId = room.roomId
override suspend fun leave(reason: String?) {
return room.leave(reason)
}
override fun spaceSummary(): RoomSummary? { override fun spaceSummary(): RoomSummary? {
return spaceSummaryDataSource.getSpaceSummary(room.roomId) return spaceSummaryDataSource.getSpaceSummary(room.roomId)
} }

View file

@ -184,6 +184,10 @@ internal class DefaultSpaceService @Inject constructor(
return joinSpaceTask.execute(JoinSpaceTask.Params(spaceIdOrAlias, reason, viaServers)) return joinSpaceTask.execute(JoinSpaceTask.Params(spaceIdOrAlias, reason, viaServers))
} }
override suspend fun leaveSpace(spaceId: String, reason: String?) {
leaveRoomTask.execute(LeaveRoomTask.Params(spaceId, reason))
}
override suspend fun rejectInvite(spaceId: String, reason: String?) { override suspend fun rejectInvite(spaceId: String, reason: String?) {
leaveRoomTask.execute(LeaveRoomTask.Params(spaceId, reason)) leaveRoomTask.execute(LeaveRoomTask.Params(spaceId, reason))
} }

View file

@ -801,14 +801,14 @@ class TimelineViewModel @AssistedInject constructor(
private fun handleRejectInvite() { private fun handleRejectInvite() {
viewModelScope.launch { viewModelScope.launch {
tryOrNull { room.leave(null) } tryOrNull { session.leaveRoom(room.roomId) }
} }
} }
private fun handleAcceptInvite() { private fun handleAcceptInvite() {
viewModelScope.launch { viewModelScope.launch {
tryOrNull { tryOrNull {
room.join() session.joinRoom(room.roomId)
analyticsTracker.capture(room.roomSummary().toAnalyticsJoinedRoom()) analyticsTracker.capture(room.roomSummary().toAnalyticsJoinedRoom())
} }
} }

View file

@ -440,7 +440,7 @@ class MessageComposerViewModel @AssistedInject constructor(
is ParsedCommand.LeaveRoom -> { is ParsedCommand.LeaveRoom -> {
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
try { try {
session.getRoom(slashCommandResult.roomId)?.leave(null) session.leaveRoom(slashCommandResult.roomId)
popDraft() popDraft()
_viewEvents.post(MessageComposerViewEvents.SlashCommandResultOk()) _viewEvents.post(MessageComposerViewEvents.SlashCommandResultOk())
} catch (failure: Throwable) { } catch (failure: Throwable) {
@ -683,7 +683,9 @@ class MessageComposerViewModel @AssistedInject constructor(
?.roomId ?.roomId
?.let { session.getRoom(it) } ?.let { session.getRoom(it) }
} }
?.leave(reason = null) ?.let {
session.leaveRoom(it.roomId)
}
} }
} }

View file

@ -222,10 +222,9 @@ class RoomListViewModel @AssistedInject constructor(
) )
} }
val room = session.getRoom(roomId) ?: return@withState
viewModelScope.launch { viewModelScope.launch {
try { try {
room.join() session.joinRoom(roomId)
analyticsTracker.capture(action.roomSummary.toAnalyticsJoinedRoom()) analyticsTracker.capture(action.roomSummary.toAnalyticsJoinedRoom())
// We do not update the joiningRoomsIds here, because, the room is not joined yet regarding the sync data. // We do not update the joiningRoomsIds here, because, the room is not joined yet regarding the sync data.
// Instead, we wait for the room to be joined // Instead, we wait for the room to be joined
@ -245,10 +244,9 @@ class RoomListViewModel @AssistedInject constructor(
return@withState return@withState
} }
val room = session.getRoom(roomId) ?: return@withState
viewModelScope.launch { viewModelScope.launch {
try { try {
room.leave(null) session.leaveRoom(roomId)
// We do not update the rejectingRoomsIds here, because, the room is not rejected yet regarding the sync data. // We do not update the rejectingRoomsIds here, because, the room is not rejected yet regarding the sync data.
// Instead, we wait for the room to be rejected // Instead, we wait for the room to be rejected
// Known bug: if the user is invited again (after rejecting the first invitation), the loading will be displayed instead of the buttons. // Known bug: if the user is invited again (after rejecting the first invitation), the loading will be displayed instead of the buttons.
@ -333,9 +331,8 @@ class RoomListViewModel @AssistedInject constructor(
private fun handleLeaveRoom(action: RoomListAction.LeaveRoom) { private fun handleLeaveRoom(action: RoomListAction.LeaveRoom) {
_viewEvents.post(RoomListViewEvents.Loading(null)) _viewEvents.post(RoomListViewEvents.Loading(null))
val room = session.getRoom(action.roomId) ?: return
viewModelScope.launch { viewModelScope.launch {
val value = runCatching { room.leave(null) } val value = runCatching { session.leaveRoom(action.roomId) }
.fold({ RoomListViewEvents.Done }, { RoomListViewEvents.Failure(it) }) .fold({ RoomListViewEvents.Done }, { RoomListViewEvents.Failure(it) })
_viewEvents.post(value) _viewEvents.post(value)
} }

View file

@ -34,7 +34,6 @@ import kotlinx.coroutines.sync.withPermit
import org.matrix.android.sdk.api.extensions.orFalse import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.failure.Failure import org.matrix.android.sdk.api.failure.Failure
import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.room.Room
import org.matrix.android.sdk.api.session.room.members.ChangeMembershipState import org.matrix.android.sdk.api.session.room.members.ChangeMembershipState
import org.matrix.android.sdk.api.session.room.model.Membership import org.matrix.android.sdk.api.session.room.model.Membership
import org.matrix.android.sdk.api.session.room.roomSummaryQueryParams import org.matrix.android.sdk.api.session.room.roomSummaryQueryParams
@ -109,7 +108,7 @@ class InvitesAcceptor @Inject constructor(
private suspend fun Session.joinRoomSafely(roomId: String) { private suspend fun Session.joinRoomSafely(roomId: String) {
if (shouldRejectRoomIds.contains(roomId)) { if (shouldRejectRoomIds.contains(roomId)) {
getRoom(roomId)?.rejectInviteSafely() rejectInviteSafely(roomId)
return return
} }
val roomMembershipChanged = getChangeMemberships(roomId) val roomMembershipChanged = getChangeMemberships(roomId)
@ -126,16 +125,16 @@ class InvitesAcceptor @Inject constructor(
// if the inviting user is on the same HS, there can only be one cause: they left, so we try to reject the invite. // if the inviting user is on the same HS, there can only be one cause: they left, so we try to reject the invite.
if (inviterId?.endsWith(sessionParams.credentials.homeServer.orEmpty()).orFalse()) { if (inviterId?.endsWith(sessionParams.credentials.homeServer.orEmpty()).orFalse()) {
shouldRejectRoomIds.add(roomId) shouldRejectRoomIds.add(roomId)
room.rejectInviteSafely() rejectInviteSafely(roomId)
} }
} }
} }
} }
} }
private suspend fun Room.rejectInviteSafely() { private suspend fun Session.rejectInviteSafely(roomId: String) {
try { try {
leave(null) leaveRoom(roomId)
shouldRejectRoomIds.remove(roomId) shouldRejectRoomIds.remove(roomId)
} catch (failure: Throwable) { } catch (failure: Throwable) {
Timber.v("Fail rejecting invite for room: $roomId") Timber.v("Fail rejecting invite for room: $roomId")

View file

@ -83,7 +83,7 @@ class NotificationBroadcastReceiver : BroadcastReceiver() {
if (room != null) { if (room != null) {
session.coroutineScope.launch { session.coroutineScope.launch {
tryOrNull { tryOrNull {
room.join() session.joinRoom(room.roomId)
analyticsTracker.capture(room.roomSummary().toAnalyticsJoinedRoom()) analyticsTracker.capture(room.roomSummary().toAnalyticsJoinedRoom())
} }
} }
@ -93,11 +93,8 @@ class NotificationBroadcastReceiver : BroadcastReceiver() {
private fun handleRejectRoom(roomId: String) { private fun handleRejectRoom(roomId: String) {
activeSessionHolder.getSafeActiveSession()?.let { session -> activeSessionHolder.getSafeActiveSession()?.let { session ->
val room = session.getRoom(roomId) session.coroutineScope.launch {
if (room != null) { tryOrNull { session.leaveRoom(roomId) }
session.coroutineScope.launch {
tryOrNull { room.leave() }
}
} }
} }
} }

View file

@ -186,7 +186,7 @@ class RoomProfileViewModel @AssistedInject constructor(
_viewEvents.post(RoomProfileViewEvents.Loading(stringProvider.getString(R.string.room_profile_leaving_room))) _viewEvents.post(RoomProfileViewEvents.Loading(stringProvider.getString(R.string.room_profile_leaving_room)))
viewModelScope.launch { viewModelScope.launch {
try { try {
room.leave(null) session.leaveRoom(room.roomId)
// Do nothing, we will be closing the room automatically when it will get back from sync // Do nothing, we will be closing the room automatically when it will get back from sync
} catch (failure: Throwable) { } catch (failure: Throwable) {
_viewEvents.post(RoomProfileViewEvents.Failure(failure)) _viewEvents.post(RoomProfileViewEvents.Failure(failure))

View file

@ -256,7 +256,7 @@ class SpaceListViewModel @AssistedInject constructor(@Assisted initialState: Spa
private fun handleLeaveSpace(action: SpaceListAction.LeaveSpace) { private fun handleLeaveSpace(action: SpaceListAction.LeaveSpace) {
viewModelScope.launch { viewModelScope.launch {
tryOrNull("Failed to leave space ${action.spaceSummary.roomId}") { tryOrNull("Failed to leave space ${action.spaceSummary.roomId}") {
session.spaceService().getSpace(action.spaceSummary.roomId)?.leave(null) session.spaceService().leaveSpace(action.spaceSummary.roomId)
} }
} }
} }

View file

@ -131,7 +131,7 @@ class SpaceMenuViewModel @AssistedInject constructor(
session.coroutineScope.launch { session.coroutineScope.launch {
try { try {
if (state.leaveMode == SpaceMenuState.LeaveMode.LEAVE_NONE) { if (state.leaveMode == SpaceMenuState.LeaveMode.LEAVE_NONE) {
session.getRoom(initialState.spaceId)?.leave(null) session.spaceService().leaveSpace(initialState.spaceId)
} else if (state.leaveMode == SpaceMenuState.LeaveMode.LEAVE_ALL) { } else if (state.leaveMode == SpaceMenuState.LeaveMode.LEAVE_ALL) {
// need to find all child rooms that i have joined // need to find all child rooms that i have joined
@ -143,13 +143,13 @@ class SpaceMenuViewModel @AssistedInject constructor(
} }
).forEach { ).forEach {
try { try {
session.getRoom(it.roomId)?.leave(null) session.spaceService().leaveSpace(it.roomId)
} catch (failure: Throwable) { } catch (failure: Throwable) {
// silently ignore? // silently ignore?
Timber.e(failure, "Fail to leave sub rooms/spaces") Timber.e(failure, "Fail to leave sub rooms/spaces")
} }
} }
session.getRoom(initialState.spaceId)?.leave(null) session.spaceService().leaveSpace(initialState.spaceId)
} }
// We observe the membership and to dismiss when we have remote echo of leaving // We observe the membership and to dismiss when we have remote echo of leaving

View file

@ -104,7 +104,7 @@ class SpaceInviteBottomSheetViewModel @AssistedInject constructor(
setState { copy(joinActionState = Loading()) } setState { copy(joinActionState = Loading()) }
session.coroutineScope.launch(Dispatchers.IO) { session.coroutineScope.launch(Dispatchers.IO) {
try { try {
session.getRoom(initialState.spaceId)?.join() session.spaceService().joinSpace(initialState.spaceId)
setState { copy(joinActionState = Success(Unit)) } setState { copy(joinActionState = Success(Unit)) }
} catch (failure: Throwable) { } catch (failure: Throwable) {
setState { copy(joinActionState = Fail(failure)) } setState { copy(joinActionState = Fail(failure)) }
@ -116,7 +116,7 @@ class SpaceInviteBottomSheetViewModel @AssistedInject constructor(
setState { copy(rejectActionState = Loading()) } setState { copy(rejectActionState = Loading()) }
session.coroutineScope.launch(Dispatchers.IO) { session.coroutineScope.launch(Dispatchers.IO) {
try { try {
session.getRoom(initialState.spaceId)?.leave() session.spaceService().leaveSpace(initialState.spaceId)
setState { copy(rejectActionState = Success(Unit)) } setState { copy(rejectActionState = Success(Unit)) }
} catch (failure: Throwable) { } catch (failure: Throwable) {
setState { copy(rejectActionState = Fail(failure)) } setState { copy(rejectActionState = Fail(failure)) }

View file

@ -72,14 +72,14 @@ class SpaceLeaveAdvancedViewModel @AssistedInject constructor(
try { try {
state.selectedRooms.forEach { state.selectedRooms.forEach {
try { try {
session.getRoom(it)?.leave(null) session.leaveRoom(it)
} catch (failure: Throwable) { } catch (failure: Throwable) {
// silently ignore? // silently ignore?
Timber.e(failure, "Fail to leave sub rooms/spaces") Timber.e(failure, "Fail to leave sub rooms/spaces")
} }
} }
session.getRoom(initialState.spaceId)?.leave(null) session.spaceService().leaveSpace(initialState.spaceId)
// We observe the membership and to dismiss when we have remote echo of leaving // We observe the membership and to dismiss when we have remote echo of leaving
} catch (failure: Throwable) { } catch (failure: Throwable) {
setState { copy(leaveState = Fail(failure)) } setState { copy(leaveState = Fail(failure)) }