Request an initial sync when a user is unignored (#1899)

This commit is contained in:
Benoit Marty 2022-04-15 12:07:28 +02:00
parent 6ed1516d90
commit 6b9f2965da
6 changed files with 84 additions and 4 deletions

View File

@ -23,5 +23,10 @@ sealed class GlobalError {
data class InvalidToken(val softLogout: Boolean) : GlobalError()
data class ConsentNotGivenError(val consentUri: String) : GlobalError()
data class CertificateError(val fingerprint: Fingerprint) : GlobalError()
/**
* The SDK requires the app (which should request the user) to perform an initial sync.
*/
data class InitialSyncRequest(val reason: InitialSyncRequestReason) : GlobalError()
object ExpiredAccount : GlobalError()
}

View File

@ -0,0 +1,27 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.android.sdk.api.failure
/**
* This enum provide the reason why the SDK request an initial sync to the application
*/
enum class InitialSyncRequestReason {
/**
* The list of ignored users has changed, and at least one user who was ignored is not ignored anymore
*/
IGNORED_USERS_LIST_CHANGE,
}

View File

@ -298,6 +298,7 @@ interface Session :
* Possible cases:
* - The access token is not valid anymore,
* - a M_CONSENT_NOT_GIVEN error has been received from the homeserver
* See [GlobalError] for all the possible cases
*/
fun onGlobalError(session: Session, globalError: GlobalError) = Unit
}

View File

@ -20,6 +20,8 @@ import com.zhuinden.monarchy.Monarchy
import io.realm.Realm
import io.realm.RealmList
import io.realm.kotlin.where
import org.matrix.android.sdk.api.failure.GlobalError
import org.matrix.android.sdk.api.failure.InitialSyncRequestReason
import org.matrix.android.sdk.api.pushrules.RuleScope
import org.matrix.android.sdk.api.pushrules.RuleSetKey
import org.matrix.android.sdk.api.pushrules.rest.GetPushRulesResponse
@ -31,6 +33,7 @@ import org.matrix.android.sdk.api.session.room.model.RoomMemberContent
import org.matrix.android.sdk.api.session.room.model.RoomSummary
import org.matrix.android.sdk.api.session.sync.model.InvitedRoomSync
import org.matrix.android.sdk.api.session.sync.model.UserAccountDataSync
import org.matrix.android.sdk.internal.SessionManager
import org.matrix.android.sdk.internal.database.mapper.ContentMapper
import org.matrix.android.sdk.internal.database.mapper.PushRulesMapper
import org.matrix.android.sdk.internal.database.mapper.asDomain
@ -48,7 +51,10 @@ import org.matrix.android.sdk.internal.database.query.getDirectRooms
import org.matrix.android.sdk.internal.database.query.getOrCreate
import org.matrix.android.sdk.internal.database.query.where
import org.matrix.android.sdk.internal.di.SessionDatabase
import org.matrix.android.sdk.internal.di.SessionId
import org.matrix.android.sdk.internal.di.UserId
import org.matrix.android.sdk.internal.session.SessionListeners
import org.matrix.android.sdk.internal.session.dispatchTo
import org.matrix.android.sdk.internal.session.room.RoomAvatarResolver
import org.matrix.android.sdk.internal.session.room.membership.RoomDisplayNameResolver
import org.matrix.android.sdk.internal.session.room.membership.RoomMemberHelper
@ -67,7 +73,10 @@ internal class UserAccountDataSyncHandler @Inject constructor(
private val directChatsHelper: DirectChatsHelper,
private val updateUserAccountDataTask: UpdateUserAccountDataTask,
private val roomAvatarResolver: RoomAvatarResolver,
private val roomDisplayNameResolver: RoomDisplayNameResolver
private val roomDisplayNameResolver: RoomDisplayNameResolver,
@SessionId private val sessionId: String,
private val sessionManager: SessionManager,
private val sessionListeners: SessionListeners
) {
fun handle(realm: Realm, accountData: UserAccountDataSync?) {
@ -186,9 +195,10 @@ internal class UserAccountDataSyncHandler @Inject constructor(
private fun handleIgnoredUsers(realm: Realm, event: UserAccountDataEvent) {
val userIds = event.content.toModel<IgnoredUsersContent>()?.ignoredUsers?.keys ?: return
realm.where(IgnoredUserEntity::class.java)
.findAll()
.deleteAllFromRealm()
val currentIgnoredUsers = realm.where(IgnoredUserEntity::class.java).findAll()
val currentIgnoredUserIds = currentIgnoredUsers.map { it.userId }
// Delete the previous list
currentIgnoredUsers.deleteAllFromRealm()
// And save the new received list
userIds.forEach { realm.createObject(IgnoredUserEntity::class.java).apply { userId = it } }
@ -204,6 +214,20 @@ internal class UserAccountDataSyncHandler @Inject constructor(
.forEach {
it.deleteOnCascade(true)
}
// Handle the case when some users are unignored from another session
val mustRefreshCache = currentIgnoredUserIds.any { currentIgnoredUserId -> currentIgnoredUserId !in userIds }
if (mustRefreshCache) {
Timber.d("A user has been unignored from another session, an initial sync should be performed")
dispatchMustRefresh()
}
}
private fun dispatchMustRefresh() {
val session = sessionManager.getSessionComponent(sessionId)?.session()
session.dispatchTo(sessionListeners) { safeSession, listener ->
listener.onGlobalError(safeSession, GlobalError.InitialSyncRequest(InitialSyncRequestReason.IGNORED_USERS_LIST_CHANGE))
}
}
private fun handleBreadcrumbs(realm: Realm, event: UserAccountDataEvent) {

View File

@ -46,6 +46,7 @@ import androidx.viewbinding.ViewBinding
import com.airbnb.mvrx.MavericksView
import com.bumptech.glide.util.Util
import com.google.android.material.appbar.MaterialToolbar
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.snackbar.Snackbar
import dagger.hilt.android.EntryPointAccessors
import im.vector.app.BuildConfig
@ -86,6 +87,7 @@ import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.failure.GlobalError
import org.matrix.android.sdk.api.failure.InitialSyncRequestReason
import reactivecircus.flowbinding.android.view.clicks
import timber.log.Timber
import javax.inject.Inject
@ -266,9 +268,27 @@ abstract class VectorBaseActivity<VB : ViewBinding> : AppCompatActivity(), Maver
is GlobalError.CertificateError ->
handleCertificateError(globalError)
GlobalError.ExpiredAccount -> Unit // TODO Handle account expiration
is GlobalError.InitialSyncRequest -> handleInitialSyncRequest(globalError)
}
}
private fun handleInitialSyncRequest(initialSyncRequest: GlobalError.InitialSyncRequest) {
MaterialAlertDialogBuilder(this)
.setTitle(R.string.initial_sync_request_title)
.setMessage(
getString(R.string.initial_sync_request_content, getString(
when (initialSyncRequest.reason) {
InitialSyncRequestReason.IGNORED_USERS_LIST_CHANGE -> R.string.initial_sync_request_reason_unignored_users
}
))
)
.setPositiveButton(R.string.ok) { _, _ ->
MainActivity.restartApp(this, MainActivityArgs(clearCache = true))
}
.setNegativeButton(R.string.later, null)
.show()
}
private fun handleCertificateError(certificateError: GlobalError.CertificateError) {
singletonEntryPoint()
.unrecognizedCertificateDialog()

View File

@ -176,6 +176,9 @@
<string name="initial_sync_start_importing_account_groups">Initial sync:\nImporting communities</string>
<string name="initial_sync_start_importing_account_data">Initial sync:\nImporting account data</string>
<string name="initial_sync_request_title">Initial sync request</string>
<string name="initial_sync_request_content">${app_name} needs to perform a clear cache to be up to date, for the following reason:\n%s\n\nNote that this action will restart the app and it may take some time.</string>
<string name="initial_sync_request_reason_unignored_users">- Some users have been unignored</string>
<string name="event_status_sent_message">Message sent</string>
<string name="event_status_sending_message">Sending message…</string>