Merge pull request #5576 from vector-im/feature/aris/thread_labs_notice_users

Threads Migration
This commit is contained in:
ganfra 2022-03-22 14:57:07 +01:00 committed by GitHub
commit a2f64e7f3c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 178 additions and 14 deletions

View file

@ -64,7 +64,11 @@ data class MatrixConfiguration(
/**
* True to enable presence information sync (if available). False to disable regardless of server setting.
*/
val presenceSyncEnabled: Boolean = true
val presenceSyncEnabled: Boolean = true,
/**
* Thread messages default enable/disabled value
*/
val threadMessagesEnabledDefault: Boolean = false,
) {
/**

View file

@ -19,15 +19,19 @@ package org.matrix.android.sdk.internal.database.lightweight
import android.content.Context
import androidx.core.content.edit
import androidx.preference.PreferenceManager
import org.matrix.android.sdk.api.MatrixConfiguration
import javax.inject.Inject
/**
* The purpose of this class is to provide an alternative and lightweight way to store settings/data
* on the sdi without using the database. This should be used just for sdk/user preferences and
* on the sdk without using the database. This should be used just for sdk/user preferences and
* not for large data sets
*/
class LightweightSettingsStorage @Inject constructor(context: Context) {
class LightweightSettingsStorage @Inject constructor(
context: Context,
private val matrixConfiguration: MatrixConfiguration
) {
private val sdkDefaultPrefs = PreferenceManager.getDefaultSharedPreferences(context.applicationContext)
@ -38,7 +42,7 @@ class LightweightSettingsStorage @Inject constructor(context: Context) {
}
fun areThreadMessagesEnabled(): Boolean {
return sdkDefaultPrefs.getBoolean(MATRIX_SDK_SETTINGS_THREAD_MESSAGES_ENABLED, false)
return sdkDefaultPrefs.getBoolean(MATRIX_SDK_SETTINGS_THREAD_MESSAGES_ENABLED, matrixConfiguration.threadMessagesEnabledDefault)
}
companion object {

View file

@ -80,8 +80,8 @@ internal class WorkManagerProvider @Inject constructor(
workManager.enqueue(checkWorkerRequest)
val checkWorkerLiveState = workManager.getWorkInfoByIdLiveData(checkWorkerRequest.id)
val observer = object : Observer<WorkInfo> {
override fun onChanged(workInfo: WorkInfo) {
if (workInfo.state.isFinished) {
override fun onChanged(workInfo: WorkInfo?) {
if (workInfo?.state?.isFinished == true) {
checkWorkerLiveState.removeObserver(this)
if (workInfo.state == WorkInfo.State.FAILED) {
throw RuntimeException("MatrixWorkerFactory is not being set on your worker configuration.\n" +

View file

@ -44,7 +44,7 @@ internal interface FetchThreadSummariesTask : Task<FetchThreadSummariesTask.Para
data class Params(
val roomId: String,
val from: String = "",
val limit: Int = 100,
val limit: Int = 500,
val isUserParticipating: Boolean = true
)
}

View file

@ -36,8 +36,9 @@
<!-- Level 1: Security and Privacy -->
<!-- Level 1: Labs -->
<bool name="settings_labs_thread_messages_default">false</bool>
<!-- Level 1: Advcanced settings -->
<!-- Level 1: Advanced settings -->
<!-- Level 1: Help and about -->

View file

@ -46,6 +46,7 @@ import im.vector.app.features.navigation.Navigator
import im.vector.app.features.pin.PinCodeStore
import im.vector.app.features.pin.SharedPrefPinCodeStore
import im.vector.app.features.room.VectorRoomDisplayNameFallbackProvider
import im.vector.app.features.settings.VectorPreferences
import im.vector.app.features.ui.SharedPreferencesUiStateRepository
import im.vector.app.features.ui.UiStateRepository
import kotlinx.coroutines.CoroutineScope
@ -113,10 +114,13 @@ object VectorStaticModule {
}
@Provides
fun providesMatrixConfiguration(vectorRoomDisplayNameFallbackProvider: VectorRoomDisplayNameFallbackProvider): MatrixConfiguration {
fun providesMatrixConfiguration(
vectorPreferences: VectorPreferences,
vectorRoomDisplayNameFallbackProvider: VectorRoomDisplayNameFallbackProvider): MatrixConfiguration {
return MatrixConfiguration(
applicationFlavor = BuildConfig.FLAVOR_DESCRIPTION,
roomDisplayNameFallbackProvider = vectorRoomDisplayNameFallbackProvider,
threadMessagesEnabledDefault = vectorPreferences.areThreadMessagesEnabled(),
presenceSyncEnabled = BuildConfig.PRESENCE_SYNC_ENABLED
)
}

View file

@ -241,7 +241,7 @@ class MainActivity : VectorBaseActivity<ActivityMainBinding>(), UnlockedActivity
// We have a session.
// Check it can be opened
if (sessionHolder.getActiveSession().isOpenable) {
HomeActivity.newIntent(this)
HomeActivity.newIntent(this, existingSession = true)
} else {
// The token is still invalid
navigator.softLogout(this)

View file

@ -90,6 +90,7 @@ import javax.inject.Inject
data class HomeActivityArgs(
val clearNotification: Boolean,
val accountCreation: Boolean,
val hasExistingSession: Boolean = false,
val inviteNotificationRoomId: String? = null
) : Parcelable
@ -253,6 +254,8 @@ class HomeActivity :
HomeActivityViewEvents.PromptToEnableSessionPush -> handlePromptToEnablePush()
is HomeActivityViewEvents.OnCrossSignedInvalidated -> handleCrossSigningInvalidated(it)
HomeActivityViewEvents.ShowAnalyticsOptIn -> handleShowAnalyticsOptIn()
HomeActivityViewEvents.NotifyUserForThreadsMigration -> handleNotifyUserForThreadsMigration()
is HomeActivityViewEvents.MigrateThreads -> migrateThreadsIfNeeded(it.checkSession)
}.exhaustive
}
homeActivityViewModel.onEach { renderState(it) }
@ -269,6 +272,48 @@ class HomeActivity :
navigator.openAnalyticsOptIn(this)
}
/**
* Migrating from old threads io.element.thread to new m.thread needs an initial sync to
* sync and display existing messages appropriately
*/
private fun migrateThreadsIfNeeded(checkSession: Boolean) {
if (checkSession) {
// We should check session to ensure we will only clear cache if needed
val args = intent.getParcelableExtra<HomeActivityArgs>(Mavericks.KEY_ARG)
if (args?.hasExistingSession == true) {
// existingSession --> Will be true only if we came from an existing active session
Timber.i("----> Migrating threads from an existing session..")
handleThreadsMigration()
} else {
// We came from a new session and not an existing one,
// so there is no need to migrate threads while an initial synced performed
Timber.i("----> No thread migration needed, we are ok")
vectorPreferences.setShouldMigrateThreads(shouldMigrate = false)
}
} else {
// Proceed with migration
handleThreadsMigration()
}
}
/**
* Clear cache and restart to invoke an initial sync for threads migration
*/
private fun handleThreadsMigration() {
Timber.i("----> Threads Migration detected, clearing cache and sync...")
vectorPreferences.setShouldMigrateThreads(shouldMigrate = false)
MainActivity.restartApp(this, MainActivityArgs(clearCache = true))
}
private fun handleNotifyUserForThreadsMigration() {
MaterialAlertDialogBuilder(this)
.setTitle(R.string.threads_notice_migration_title)
.setMessage(R.string.threads_notice_migration_message)
.setCancelable(true)
.setPositiveButton(R.string.sas_got_it) { _, _ -> }
.show()
}
private fun handleIntent(intent: Intent?) {
intent?.dataString?.let { deepLink ->
val resolvedLink = when {
@ -546,11 +591,13 @@ class HomeActivity :
fun newIntent(context: Context,
clearNotification: Boolean = false,
accountCreation: Boolean = false,
existingSession: Boolean = false,
inviteNotificationRoomId: String? = null
): Intent {
val args = HomeActivityArgs(
clearNotification = clearNotification,
accountCreation = accountCreation,
hasExistingSession = existingSession,
inviteNotificationRoomId = inviteNotificationRoomId
)

View file

@ -25,4 +25,6 @@ sealed interface HomeActivityViewEvents : VectorViewEvents {
data class OnCrossSignedInvalidated(val userItem: MatrixItem.UserItem) : HomeActivityViewEvents
object PromptToEnableSessionPush : HomeActivityViewEvents
object ShowAnalyticsOptIn : HomeActivityViewEvents
object NotifyUserForThreadsMigration : HomeActivityViewEvents
data class MigrateThreads(val checkSession: Boolean) : HomeActivityViewEvents
}

View file

@ -51,6 +51,7 @@ import org.matrix.android.sdk.api.util.toMatrixItem
import org.matrix.android.sdk.flow.flow
import org.matrix.android.sdk.internal.crypto.model.CryptoDeviceInfo
import org.matrix.android.sdk.internal.crypto.model.MXUsersDevicesMap
import org.matrix.android.sdk.internal.database.lightweight.LightweightSettingsStorage
import org.matrix.android.sdk.internal.util.awaitCallback
import timber.log.Timber
import kotlin.coroutines.Continuation
@ -62,6 +63,7 @@ class HomeActivityViewModel @AssistedInject constructor(
private val activeSessionHolder: ActiveSessionHolder,
private val reAuthHelper: ReAuthHelper,
private val analyticsStore: AnalyticsStore,
private val lightweightSettingsStorage: LightweightSettingsStorage,
private val vectorPreferences: VectorPreferences
) : VectorViewModel<HomeActivityViewState, HomeActivityViewActions, HomeActivityViewEvents>(initialState) {
@ -84,6 +86,7 @@ class HomeActivityViewModel @AssistedInject constructor(
checkSessionPushIsOn()
observeCrossSigningReset()
observeAnalytics()
initThreadsMigration()
}
private fun observeAnalytics() {
@ -130,6 +133,46 @@ class HomeActivityViewModel @AssistedInject constructor(
.launchIn(viewModelScope)
}
/**
* Handle threads migration. The migration includes:
* - Notify users that had io.element.thread enabled from labs
* - Re-Enable m.thread to those users (that they had enabled labs threads)
* - Handle migration when threads are enabled by default
*/
private fun initThreadsMigration() {
// When we would like to enable threads for all users
// if(vectorPreferences.shouldMigrateThreads()) {
// vectorPreferences.setThreadMessagesEnabled()
// lightweightSettingsStorage.setThreadMessagesEnabled(vectorPreferences.areThreadMessagesEnabled())
// }
when {
// Notify users
vectorPreferences.shouldNotifyUserAboutThreads() && vectorPreferences.areThreadMessagesEnabled() -> {
Timber.i("----> Notify users about threads")
// Notify the user if needed that we migrated to support m.thread
// instead of io.element.thread so old thread messages will be displayed as normal timeline messages
_viewEvents.post(HomeActivityViewEvents.NotifyUserForThreadsMigration)
vectorPreferences.userNotifiedAboutThreads()
}
// Migrate users with enabled lab settings
vectorPreferences.shouldNotifyUserAboutThreads() && vectorPreferences.shouldMigrateThreads() -> {
Timber.i("----> Migrate threads with enabled labs")
// If user had io.element.thread enabled then enable the new thread support,
// clear cache to sync messages appropriately
vectorPreferences.setThreadMessagesEnabled()
lightweightSettingsStorage.setThreadMessagesEnabled(vectorPreferences.areThreadMessagesEnabled())
// Clear Cache
_viewEvents.post(HomeActivityViewEvents.MigrateThreads(checkSession = false))
}
// Enable all users
vectorPreferences.shouldMigrateThreads() && vectorPreferences.areThreadMessagesEnabled() -> {
Timber.i("----> Try to migrate threads")
_viewEvents.post(HomeActivityViewEvents.MigrateThreads(checkSession = true))
}
}
}
private fun observeInitialSync() {
val session = activeSessionHolder.getSafeActiveSession() ?: return

View file

@ -201,7 +201,13 @@ class VectorPreferences @Inject constructor(private val context: Context) {
private const val TAKE_PHOTO_VIDEO_MODE = "TAKE_PHOTO_VIDEO_MODE"
private const val SETTINGS_LABS_RENDER_LOCATIONS_IN_TIMELINE = "SETTINGS_LABS_RENDER_LOCATIONS_IN_TIMELINE"
const val SETTINGS_LABS_ENABLE_THREAD_MESSAGES = "SETTINGS_LABS_ENABLE_THREAD_MESSAGES"
// This key will be used to identify clients with the old thread support enabled io.element.thread
const val SETTINGS_LABS_ENABLE_THREAD_MESSAGES_OLD_CLIENTS = "SETTINGS_LABS_ENABLE_THREAD_MESSAGES"
// This key will be used to identify clients with the new thread support enabled m.thread
const val SETTINGS_LABS_ENABLE_THREAD_MESSAGES = "SETTINGS_LABS_ENABLE_THREAD_MESSAGES_FINAL"
const val SETTINGS_THREAD_MESSAGES_SYNCED = "SETTINGS_THREAD_MESSAGES_SYNCED"
// Possible values for TAKE_PHOTO_VIDEO_MODE
const val TAKE_PHOTO_VIDEO_MODE_ALWAYS_ASK = 0
@ -1006,7 +1012,56 @@ class VectorPreferences @Inject constructor(private val context: Context) {
return defaultPrefs.getBoolean(SETTINGS_LABS_RENDER_LOCATIONS_IN_TIMELINE, true)
}
/**
* Indicates whether or not thread messages are enabled
*/
fun areThreadMessagesEnabled(): Boolean {
return defaultPrefs.getBoolean(SETTINGS_LABS_ENABLE_THREAD_MESSAGES, false)
return defaultPrefs.getBoolean(SETTINGS_LABS_ENABLE_THREAD_MESSAGES, getDefault(R.bool.settings_labs_thread_messages_default))
}
/**
* Manually sets thread messages enabled, useful for migrating users from io.element.thread
*/
fun setThreadMessagesEnabled() {
defaultPrefs
.edit()
.putBoolean(SETTINGS_LABS_ENABLE_THREAD_MESSAGES, true)
.apply()
}
/**
* Indicates whether or not the user will be notified about the new thread support
* We should notify the user only if he had old thread support enabled
*/
fun shouldNotifyUserAboutThreads(): Boolean {
return defaultPrefs.getBoolean(SETTINGS_LABS_ENABLE_THREAD_MESSAGES_OLD_CLIENTS, false)
}
/**
* Indicates that the user have been notified about threads migration
*/
fun userNotifiedAboutThreads() {
defaultPrefs
.edit()
.putBoolean(SETTINGS_LABS_ENABLE_THREAD_MESSAGES_OLD_CLIENTS, false)
.apply()
}
/**
* Indicates whether or not we should clear cache for threads migration.
* Default value is true, for fresh installs and updates
*/
fun shouldMigrateThreads(): Boolean {
return defaultPrefs.getBoolean(SETTINGS_THREAD_MESSAGES_SYNCED, true)
}
/**
* Indicates that there no longer threads migration needed
*/
fun setShouldMigrateThreads(shouldMigrate: Boolean) {
defaultPrefs
.edit()
.putBoolean(SETTINGS_THREAD_MESSAGES_SYNCED, shouldMigrate)
.apply()
}
}

View file

@ -42,6 +42,8 @@ class VectorSettingsLabsFragment @Inject constructor(
// clear cache
findPreference<VectorSwitchPreference>(VectorPreferences.SETTINGS_LABS_ENABLE_THREAD_MESSAGES)?.let {
it.onPreferenceClickListener = Preference.OnPreferenceClickListener {
// We should migrate threads only if threads are disabled
vectorPreferences.setShouldMigrateThreads(!vectorPreferences.areThreadMessagesEnabled())
lightweightSettingsStorage.setThreadMessagesEnabled(vectorPreferences.areThreadMessagesEnabled())
displayLoadingView()
MainActivity.restartApp(requireActivity(), MainActivityArgs(clearCache = true))

View file

@ -731,6 +731,8 @@
<!-- Parameter %s will be replaced by the value of string reply_in_thread -->
<string name="thread_list_empty_notice">Tip: Long tap a message and use “%s”.</string>
<string name="search_thread_from_a_thread">From a Thread</string>
<string name="threads_notice_migration_title">Threads Approaching Beta 🎉</string>
<string name="threads_notice_migration_message">Were getting closer to releasing a public Beta for Threads.\n\nAs we prepare for it, we need to make some changes: threads created before this point will be displayed as regular replies.\n\nThis will be a one-off transition as Threads are now part of the Matrix specification.</string>
<!-- Search -->
<string name="search_hint">Search</string>

View file

@ -52,8 +52,8 @@
<!--</im.vector.app.core.preference.VectorPreferenceCategory>-->
<im.vector.app.core.preference.VectorSwitchPreference
android:defaultValue="false"
android:key="SETTINGS_LABS_ENABLE_THREAD_MESSAGES"
android:defaultValue="@bool/settings_labs_thread_messages_default"
android:key="SETTINGS_LABS_ENABLE_THREAD_MESSAGES_FINAL"
android:summary="@string/labs_enable_thread_messages_desc"
android:title="@string/labs_enable_thread_messages" />