First test for auto accept invite [WIP]

This commit is contained in:
ganfra 2021-06-17 20:49:08 +02:00
parent da19992f3f
commit 646f00f3fc
8 changed files with 59 additions and 99 deletions

View File

@ -24,13 +24,20 @@ import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.core.utils.BehaviorDataSource import im.vector.app.core.utils.BehaviorDataSource
import im.vector.app.features.session.coroutineScope import im.vector.app.features.session.coroutineScope
import im.vector.app.features.ui.UiStateRepository import im.vector.app.features.ui.UiStateRepository
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable import io.reactivex.disposables.CompositeDisposable
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.extensions.tryOrNull import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.session.Session 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.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.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.Inject
import javax.inject.Singleton 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 // TODO Keep this class for now, will maybe be used fro Space
@Singleton @Singleton
class AppStateHandler @Inject constructor( class AppStateHandler @Inject constructor(
sessionDataSource: ActiveSessionDataSource, private val sessionDataSource: ActiveSessionDataSource,
private val uiStateRepository: UiStateRepository, private val uiStateRepository: UiStateRepository,
private val activeSessionHolder: ActiveSessionHolder private val activeSessionHolder: ActiveSessionHolder
) : LifecycleObserver { ) : LifecycleObserver {
private val compositeDisposable = CompositeDisposable() private val compositeDisposable = CompositeDisposable()
private val selectedSpaceDataSource = BehaviorDataSource<Option<RoomGroupingMethod>>(Option.empty()) private val selectedSpaceDataSource = BehaviorDataSource<Option<RoomGroupingMethod>>(Option.empty())
val selectedRoomGroupingObservable = selectedSpaceDataSource.observe() val selectedRoomGroupingObservable = selectedSpaceDataSource.observe()
fun getCurrentRoomGroupingMethod(): RoomGroupingMethod? = selectedSpaceDataSource.currentValue?.orNull() fun getCurrentRoomGroupingMethod(): RoomGroupingMethod? = selectedSpaceDataSource.currentValue?.orNull()
init {
observeActiveSession()
}
fun setCurrentSpace(spaceId: String?, session: Session? = null) { fun setCurrentSpace(spaceId: String?, session: Session? = null) {
val uSession = session ?: activeSessionHolder.getSafeActiveSession() ?: return val uSession = session ?: activeSessionHolder.getSafeActiveSession() ?: return
if (selectedSpaceDataSource.currentValue?.orNull() is RoomGroupingMethod.BySpace if (selectedSpaceDataSource.currentValue?.orNull() is RoomGroupingMethod.BySpace
@ -92,12 +102,13 @@ class AppStateHandler @Inject constructor(
} }
} }
init { private fun observeActiveSession(){
sessionDataSource.observe() sessionDataSource.observe()
.distinctUntilChanged() .distinctUntilChanged()
.subscribe { .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 -> it.orNull()?.let { session ->
observeInvitesForAutoAccept(session)
if (uiStateRepository.isGroupingMethodSpace(session.sessionId)) { if (uiStateRepository.isGroupingMethodSpace(session.sessionId)) {
setCurrentSpace(uiStateRepository.getSelectedSpace(session.sessionId), session) setCurrentSpace(uiStateRepository.getSelectedSpace(session.sessionId), session)
} else { } 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<String>()
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? { fun safeActiveSpaceId(): String? {
return (selectedSpaceDataSource.currentValue?.orNull() as? RoomGroupingMethod.BySpace)?.spaceSummary?.roomId 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 return (selectedSpaceDataSource.currentValue?.orNull() as? RoomGroupingMethod.ByLegacyGroup)?.groupSummary?.groupId
} }
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun entersForeground() {
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun entersBackground() { fun entersBackground() {
compositeDisposable.clear()
val session = activeSessionHolder.getSafeActiveSession() ?: return val session = activeSessionHolder.getSafeActiveSession() ?: return
when (val currentMethod = selectedSpaceDataSource.currentValue?.orNull() ?: RoomGroupingMethod.BySpace(null)) { when (val currentMethod = selectedSpaceDataSource.currentValue?.orNull() ?: RoomGroupingMethod.BySpace(null)) {
is RoomGroupingMethod.BySpace -> { is RoomGroupingMethod.BySpace -> {
uiStateRepository.storeGroupingMethod(true, session.sessionId) uiStateRepository.storeGroupingMethod(true, session.sessionId)
uiStateRepository.storeSelectedSpace(currentMethod.spaceSummary?.roomId, session.sessionId) uiStateRepository.storeSelectedSpace(currentMethod.spaceSummary?.roomId, session.sessionId)
} }

View File

@ -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 // will make sure we know where how to map calls and also allow us know not to display
// it in the future. // it in the future.
invitedRoom.markVirtual(nativeRoomId) 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)
} }
} }
} }

View File

@ -204,21 +204,8 @@ class HomeDetailViewModel @AssistedInject constructor(@Assisted initialState: Ho
} }
is RoomGroupingMethod.BySpace -> { is RoomGroupingMethod.BySpace -> {
val activeSpaceRoomId = groupingMethod.spaceSummary?.roomId val activeSpaceRoomId = groupingMethod.spaceSummary?.roomId
val dmInvites = session.getRoomSummaries( val dmInvites = 0
roomSummaryQueryParams { val roomsInvite = 0
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 dmRooms = session.getNotificationCountForRooms( val dmRooms = session.getNotificationCountForRooms(
roomSummaryQueryParams { roomSummaryQueryParams {

View File

@ -92,12 +92,7 @@ class UnreadMessagesSharedViewModel @AssistedInject constructor(@Assisted initia
this.activeSpaceFilter = ActiveSpaceFilter.ActiveSpace(null) this.activeSpaceFilter = ActiveSpaceFilter.ActiveSpace(null)
} }
) )
val invites = session.getRoomSummaries( val invites = 0
roomSummaryQueryParams {
this.memberships = listOf(Membership.INVITE)
this.activeSpaceFilter = ActiveSpaceFilter.ActiveSpace(null)
}
).size
copy( copy(
homeSpaceUnread = RoomAggregateNotificationCount( homeSpaceUnread = RoomAggregateNotificationCount(
counts.notificationCount + invites, counts.notificationCount + invites,
@ -129,9 +124,7 @@ class UnreadMessagesSharedViewModel @AssistedInject constructor(@Assisted initia
is RoomGroupingMethod.BySpace -> { is RoomGroupingMethod.BySpace -> {
val selectedSpace = appStateHandler.safeActiveSpaceId() val selectedSpace = appStateHandler.safeActiveSpaceId()
val inviteCount = session.getRoomSummaries( val inviteCount = 0
roomSummaryQueryParams { this.memberships = listOf(Membership.INVITE) }
).size
val totalCount = session.getNotificationCountForRooms( val totalCount = session.getNotificationCountForRooms(
roomSummaryQueryParams { roomSummaryQueryParams {

View File

@ -115,16 +115,6 @@ class GroupRoomListSectionBuilder(
private fun buildRoomsSections(sections: MutableList<RoomsSection>, private fun buildRoomsSections(sections: MutableList<RoomsSection>,
activeSpaceAwareQueries: MutableList<UpdatableLivePageResult>, activeSpaceAwareQueries: MutableList<UpdatableLivePageResult>,
actualGroupId: String?) { actualGroupId: String?) {
addSection(
sections,
activeSpaceAwareQueries,
R.string.invitations_header,
true
) {
it.memberships = listOf(Membership.INVITE)
it.roomCategoryFilter = RoomCategoryFilter.ONLY_ROOMS
it.activeGroupId = actualGroupId
}
addSection( addSection(
sections, sections,
@ -180,15 +170,6 @@ class GroupRoomListSectionBuilder(
activeSpaceAwareQueries: MutableList<UpdatableLivePageResult>, activeSpaceAwareQueries: MutableList<UpdatableLivePageResult>,
actualGroupId: String? actualGroupId: String?
) { ) {
addSection(sections,
activeSpaceAwareQueries,
R.string.invitations_header,
true
) {
it.memberships = listOf(Membership.INVITE)
it.roomCategoryFilter = RoomCategoryFilter.ONLY_DM
it.activeGroupId = actualGroupId
}
addSection( addSection(
sections, sections,

View File

@ -88,21 +88,6 @@ class SpaceRoomListSectionBuilder(
) )
} }
RoomListDisplayMode.NOTIFICATIONS -> { 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( addSection(
sections = sections, sections = sections,
@ -136,18 +121,6 @@ class SpaceRoomListSectionBuilder(
} }
private fun buildRoomsSections(sections: MutableList<RoomsSection>, activeSpaceAwareQueries: MutableList<RoomListViewModel.ActiveSpaceQueryUpdater>) { private fun buildRoomsSections(sections: MutableList<RoomsSection>, activeSpaceAwareQueries: MutableList<RoomListViewModel.ActiveSpaceQueryUpdater>) {
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( addSection(
sections, sections,
activeSpaceAwareQueries, activeSpaceAwareQueries,
@ -253,16 +226,6 @@ class SpaceRoomListSectionBuilder(
} }
private fun buildDmSections(sections: MutableList<RoomsSection>, activeSpaceAwareQueries: MutableList<RoomListViewModel.ActiveSpaceQueryUpdater>) { private fun buildDmSections(sections: MutableList<RoomsSection>, activeSpaceAwareQueries: MutableList<RoomListViewModel.ActiveSpaceQueryUpdater>) {
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, addSection(sections,
activeSpaceAwareQueries, activeSpaceAwareQueries,

View File

@ -253,7 +253,9 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
roomEvents.add(event) roomEvents.add(event)
} }
} }
is InviteNotifiableEvent -> invitationEvents.add(event) is InviteNotifiableEvent -> {
//invitationEvents.add(event)
}
is SimpleNotifiableEvent -> simpleEvents.add(event) is SimpleNotifiableEvent -> simpleEvents.add(event)
else -> Timber.w("Type not handled") else -> Timber.w("Type not handled")
} }

View File

@ -119,10 +119,7 @@ class SpacesListViewModel @AssistedInject constructor(@Assisted initialState: Sp
.throttleFirst(300, TimeUnit.MILLISECONDS) .throttleFirst(300, TimeUnit.MILLISECONDS)
.observeOn(Schedulers.computation()) .observeOn(Schedulers.computation())
.subscribe { .subscribe {
val inviteCount = session.getRoomSummaries( val inviteCount = 0
roomSummaryQueryParams { this.memberships = listOf(Membership.INVITE) }
).size
val totalCount = session.getNotificationCountForRooms( val totalCount = session.getNotificationCountForRooms(
roomSummaryQueryParams { roomSummaryQueryParams {
this.memberships = listOf(Membership.JOIN) this.memberships = listOf(Membership.JOIN)