From 646f00f3fcbdc9d06d149d772c41e1b14fee1dc1 Mon Sep 17 00:00:00 2001 From: ganfra Date: Thu, 17 Jun 2021 20:49:08 +0200 Subject: [PATCH] First test for auto accept invite [WIP] --- .../java/im/vector/app/AppStateHandler.kt | 61 ++++++++++++++++--- .../features/call/lookup/CallUserMapper.kt | 4 -- .../app/features/home/HomeDetailViewModel.kt | 17 +----- .../home/UnreadMessagesSharedViewModel.kt | 11 +--- .../room/list/GroupRoomListSectionBuilder.kt | 19 ------ .../room/list/SpaceRoomListSectionBuilder.kt | 37 ----------- .../NotificationDrawerManager.kt | 4 +- .../features/spaces/SpacesListViewModel.kt | 5 +- 8 files changed, 59 insertions(+), 99 deletions(-) diff --git a/vector/src/main/java/im/vector/app/AppStateHandler.kt b/vector/src/main/java/im/vector/app/AppStateHandler.kt index c5eac7e3e0..eb60a66436 100644 --- a/vector/src/main/java/im/vector/app/AppStateHandler.kt +++ b/vector/src/main/java/im/vector/app/AppStateHandler.kt @@ -24,13 +24,20 @@ import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.utils.BehaviorDataSource import im.vector.app.features.session.coroutineScope import im.vector.app.features.ui.UiStateRepository +import io.reactivex.Observable import io.reactivex.disposables.CompositeDisposable import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import org.matrix.android.sdk.api.extensions.tryOrNull import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.group.model.GroupSummary +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.RoomSummary +import org.matrix.android.sdk.api.session.room.roomSummaryQueryParams +import org.matrix.android.sdk.rx.rx +import timber.log.Timber +import java.util.concurrent.TimeUnit import javax.inject.Inject import javax.inject.Singleton @@ -49,19 +56,22 @@ fun RoomGroupingMethod.group() = (this as? RoomGroupingMethod.ByLegacyGroup)?.gr // TODO Keep this class for now, will maybe be used fro Space @Singleton class AppStateHandler @Inject constructor( - sessionDataSource: ActiveSessionDataSource, + private val sessionDataSource: ActiveSessionDataSource, private val uiStateRepository: UiStateRepository, private val activeSessionHolder: ActiveSessionHolder ) : LifecycleObserver { private val compositeDisposable = CompositeDisposable() - private val selectedSpaceDataSource = BehaviorDataSource>(Option.empty()) val selectedRoomGroupingObservable = selectedSpaceDataSource.observe() fun getCurrentRoomGroupingMethod(): RoomGroupingMethod? = selectedSpaceDataSource.currentValue?.orNull() + init { + observeActiveSession() + } + fun setCurrentSpace(spaceId: String?, session: Session? = null) { val uSession = session ?: activeSessionHolder.getSafeActiveSession() ?: return if (selectedSpaceDataSource.currentValue?.orNull() is RoomGroupingMethod.BySpace @@ -92,12 +102,13 @@ class AppStateHandler @Inject constructor( } } - init { + private fun observeActiveSession(){ sessionDataSource.observe() .distinctUntilChanged() .subscribe { - // sessionDataSource could already return a session while acitveSession holder still returns null + // sessionDataSource could already return a session while activeSession holder still returns null it.orNull()?.let { session -> + observeInvitesForAutoAccept(session) if (uiStateRepository.isGroupingMethodSpace(session.sessionId)) { setCurrentSpace(uiStateRepository.getSelectedSpace(session.sessionId), session) } else { @@ -109,6 +120,41 @@ class AppStateHandler @Inject constructor( } } + private fun observeInvitesForAutoAccept(session: Session?) { + if (session == null) return + val roomQueryParams = roomSummaryQueryParams { + this.memberships = listOf(Membership.INVITE) + } + val rxSession = session.rx() + Observable + .combineLatest( + rxSession.liveRoomSummaries(roomQueryParams).debounce(1, TimeUnit.SECONDS), + rxSession.liveRoomChangeMembershipState().debounce(1, TimeUnit.SECONDS), + { invitedRooms, membershipsChanged -> + val roomIdsToJoin = mutableListOf() + for (room in invitedRooms) { + val roomMembershipChanged = membershipsChanged[room.roomId] ?: ChangeMembershipState.Unknown + if (roomMembershipChanged != ChangeMembershipState.Joined && !roomMembershipChanged.isInProgress()) { + roomIdsToJoin.add(room.roomId) + } + } + roomIdsToJoin + } + ) + .doOnNext { roomIdsToJoin -> + session.coroutineScope.launch { + for (roomId in roomIdsToJoin) { + Timber.v("Auto accept invite for room: $roomId") + tryOrNull { session.joinRoom(roomId) } + } + } + } + .subscribe() + .also { + compositeDisposable.add(it) + } + } + fun safeActiveSpaceId(): String? { return (selectedSpaceDataSource.currentValue?.orNull() as? RoomGroupingMethod.BySpace)?.spaceSummary?.roomId } @@ -117,16 +163,11 @@ class AppStateHandler @Inject constructor( return (selectedSpaceDataSource.currentValue?.orNull() as? RoomGroupingMethod.ByLegacyGroup)?.groupSummary?.groupId } - @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) - fun entersForeground() { - } - @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) fun entersBackground() { - compositeDisposable.clear() val session = activeSessionHolder.getSafeActiveSession() ?: return when (val currentMethod = selectedSpaceDataSource.currentValue?.orNull() ?: RoomGroupingMethod.BySpace(null)) { - is RoomGroupingMethod.BySpace -> { + is RoomGroupingMethod.BySpace -> { uiStateRepository.storeGroupingMethod(true, session.sessionId) uiStateRepository.storeSelectedSpace(currentMethod.spaceSummary?.roomId, session.sessionId) } diff --git a/vector/src/main/java/im/vector/app/features/call/lookup/CallUserMapper.kt b/vector/src/main/java/im/vector/app/features/call/lookup/CallUserMapper.kt index 04177bd2b0..243f1ab6fb 100644 --- a/vector/src/main/java/im/vector/app/features/call/lookup/CallUserMapper.kt +++ b/vector/src/main/java/im/vector/app/features/call/lookup/CallUserMapper.kt @@ -57,10 +57,6 @@ class CallUserMapper(private val session: Session, private val protocolsChecker: // will make sure we know where how to map calls and also allow us know not to display // it in the future. invitedRoom.markVirtual(nativeRoomId) - // also auto-join the virtual room if we have a matching native room - // (possibly we should only join if we've also joined the native room, then we'd also have - // to make sure we joined virtual rooms on joining a native one) - session.joinRoom(invitedRoomId) } } } diff --git a/vector/src/main/java/im/vector/app/features/home/HomeDetailViewModel.kt b/vector/src/main/java/im/vector/app/features/home/HomeDetailViewModel.kt index b6210ae019..d636259512 100644 --- a/vector/src/main/java/im/vector/app/features/home/HomeDetailViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/home/HomeDetailViewModel.kt @@ -204,21 +204,8 @@ class HomeDetailViewModel @AssistedInject constructor(@Assisted initialState: Ho } is RoomGroupingMethod.BySpace -> { val activeSpaceRoomId = groupingMethod.spaceSummary?.roomId - val dmInvites = session.getRoomSummaries( - roomSummaryQueryParams { - memberships = listOf(Membership.INVITE) - roomCategoryFilter = RoomCategoryFilter.ONLY_DM - activeSpaceFilter = activeSpaceRoomId?.let { ActiveSpaceFilter.ActiveSpace(it) } ?: ActiveSpaceFilter.None - } - ).size - - val roomsInvite = session.getRoomSummaries( - roomSummaryQueryParams { - memberships = listOf(Membership.INVITE) - roomCategoryFilter = RoomCategoryFilter.ONLY_ROOMS - activeSpaceFilter = ActiveSpaceFilter.ActiveSpace(groupingMethod.spaceSummary?.roomId) - } - ).size + val dmInvites = 0 + val roomsInvite = 0 val dmRooms = session.getNotificationCountForRooms( roomSummaryQueryParams { diff --git a/vector/src/main/java/im/vector/app/features/home/UnreadMessagesSharedViewModel.kt b/vector/src/main/java/im/vector/app/features/home/UnreadMessagesSharedViewModel.kt index 9b506b6ed7..44a2f8cf09 100644 --- a/vector/src/main/java/im/vector/app/features/home/UnreadMessagesSharedViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/home/UnreadMessagesSharedViewModel.kt @@ -92,12 +92,7 @@ class UnreadMessagesSharedViewModel @AssistedInject constructor(@Assisted initia this.activeSpaceFilter = ActiveSpaceFilter.ActiveSpace(null) } ) - val invites = session.getRoomSummaries( - roomSummaryQueryParams { - this.memberships = listOf(Membership.INVITE) - this.activeSpaceFilter = ActiveSpaceFilter.ActiveSpace(null) - } - ).size + val invites = 0 copy( homeSpaceUnread = RoomAggregateNotificationCount( counts.notificationCount + invites, @@ -129,9 +124,7 @@ class UnreadMessagesSharedViewModel @AssistedInject constructor(@Assisted initia is RoomGroupingMethod.BySpace -> { val selectedSpace = appStateHandler.safeActiveSpaceId() - val inviteCount = session.getRoomSummaries( - roomSummaryQueryParams { this.memberships = listOf(Membership.INVITE) } - ).size + val inviteCount = 0 val totalCount = session.getNotificationCountForRooms( roomSummaryQueryParams { diff --git a/vector/src/main/java/im/vector/app/features/home/room/list/GroupRoomListSectionBuilder.kt b/vector/src/main/java/im/vector/app/features/home/room/list/GroupRoomListSectionBuilder.kt index 22b0eb091c..62b0612d4b 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/list/GroupRoomListSectionBuilder.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/list/GroupRoomListSectionBuilder.kt @@ -115,16 +115,6 @@ class GroupRoomListSectionBuilder( private fun buildRoomsSections(sections: MutableList, activeSpaceAwareQueries: MutableList, actualGroupId: String?) { - addSection( - sections, - activeSpaceAwareQueries, - R.string.invitations_header, - true - ) { - it.memberships = listOf(Membership.INVITE) - it.roomCategoryFilter = RoomCategoryFilter.ONLY_ROOMS - it.activeGroupId = actualGroupId - } addSection( sections, @@ -180,15 +170,6 @@ class GroupRoomListSectionBuilder( activeSpaceAwareQueries: MutableList, actualGroupId: String? ) { - addSection(sections, - activeSpaceAwareQueries, - R.string.invitations_header, - true - ) { - it.memberships = listOf(Membership.INVITE) - it.roomCategoryFilter = RoomCategoryFilter.ONLY_DM - it.activeGroupId = actualGroupId - } addSection( sections, diff --git a/vector/src/main/java/im/vector/app/features/home/room/list/SpaceRoomListSectionBuilder.kt b/vector/src/main/java/im/vector/app/features/home/room/list/SpaceRoomListSectionBuilder.kt index 266adf6b0c..e1b205eca2 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/list/SpaceRoomListSectionBuilder.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/list/SpaceRoomListSectionBuilder.kt @@ -88,21 +88,6 @@ class SpaceRoomListSectionBuilder( ) } RoomListDisplayMode.NOTIFICATIONS -> { - addSection( - sections = sections, - activeSpaceUpdaters = activeSpaceAwareQueries, - nameRes = R.string.invitations_header, - notifyOfLocalEcho = true, - spaceFilterStrategy = if (onlyOrphansInHome) { - RoomListViewModel.SpaceFilterStrategy.ORPHANS_IF_SPACE_NULL - } else { - RoomListViewModel.SpaceFilterStrategy.ALL_IF_SPACE_NULL - }, - countRoomAsNotif = true - ) { - it.memberships = listOf(Membership.INVITE) - it.roomCategoryFilter = RoomCategoryFilter.ALL - } addSection( sections = sections, @@ -136,18 +121,6 @@ class SpaceRoomListSectionBuilder( } private fun buildRoomsSections(sections: MutableList, activeSpaceAwareQueries: MutableList) { - addSection( - sections = sections, - activeSpaceUpdaters = activeSpaceAwareQueries, - nameRes = R.string.invitations_header, - notifyOfLocalEcho = true, - spaceFilterStrategy = RoomListViewModel.SpaceFilterStrategy.ALL_IF_SPACE_NULL, - countRoomAsNotif = true - ) { - it.memberships = listOf(Membership.INVITE) - it.roomCategoryFilter = RoomCategoryFilter.ONLY_ROOMS - } - addSection( sections, activeSpaceAwareQueries, @@ -253,16 +226,6 @@ class SpaceRoomListSectionBuilder( } private fun buildDmSections(sections: MutableList, activeSpaceAwareQueries: MutableList) { - addSection(sections = sections, - activeSpaceUpdaters = activeSpaceAwareQueries, - nameRes = R.string.invitations_header, - notifyOfLocalEcho = true, - spaceFilterStrategy = RoomListViewModel.SpaceFilterStrategy.ALL_IF_SPACE_NULL, - countRoomAsNotif = true - ) { - it.memberships = listOf(Membership.INVITE) - it.roomCategoryFilter = RoomCategoryFilter.ONLY_DM - } addSection(sections, activeSpaceAwareQueries, diff --git a/vector/src/main/java/im/vector/app/features/notifications/NotificationDrawerManager.kt b/vector/src/main/java/im/vector/app/features/notifications/NotificationDrawerManager.kt index 7ac9b28b9a..cf49b5ce0f 100644 --- a/vector/src/main/java/im/vector/app/features/notifications/NotificationDrawerManager.kt +++ b/vector/src/main/java/im/vector/app/features/notifications/NotificationDrawerManager.kt @@ -253,7 +253,9 @@ class NotificationDrawerManager @Inject constructor(private val context: Context roomEvents.add(event) } } - is InviteNotifiableEvent -> invitationEvents.add(event) + is InviteNotifiableEvent -> { + //invitationEvents.add(event) + } is SimpleNotifiableEvent -> simpleEvents.add(event) else -> Timber.w("Type not handled") } diff --git a/vector/src/main/java/im/vector/app/features/spaces/SpacesListViewModel.kt b/vector/src/main/java/im/vector/app/features/spaces/SpacesListViewModel.kt index d7d7af4ca5..c77a0f36c9 100644 --- a/vector/src/main/java/im/vector/app/features/spaces/SpacesListViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/spaces/SpacesListViewModel.kt @@ -119,10 +119,7 @@ class SpacesListViewModel @AssistedInject constructor(@Assisted initialState: Sp .throttleFirst(300, TimeUnit.MILLISECONDS) .observeOn(Schedulers.computation()) .subscribe { - val inviteCount = session.getRoomSummaries( - roomSummaryQueryParams { this.memberships = listOf(Membership.INVITE) } - ).size - + val inviteCount = 0 val totalCount = session.getNotificationCountForRooms( roomSummaryQueryParams { this.memberships = listOf(Membership.JOIN)