Skip to content

Commit

Permalink
Fix for Incorrect group permissions handling. Closes #152.
Browse files Browse the repository at this point in the history
Upping versionCode to 97
  • Loading branch information
Dima-Android committed Sep 2, 2024
1 parent b25c77a commit 2b5aded
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
15 changes: 14 additions & 1 deletion app/src/main/java/org/zotero/android/architecture/Defaults.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ open class Defaults @Inject constructor(
private val lastUsedCreatorNamePresentation = "LastUsedCreatorNamePresentation"
private val itemsSortType = "ItemsSortType"
private val showCollectionItemCounts = "showCollectionItemCounts"
private val performFullSyncGuardKey = "performFullSyncGuardKey"
private val didPerformFullSyncFix = "didPerformFullSyncFix"
private val tagPickerShowAutomaticTags = "tagPickerShowAutomaticTags"
private val tagPickerDisplayAllTags = "tagPickerDisplayAllTags"
Expand Down Expand Up @@ -54,7 +55,6 @@ open class Defaults @Inject constructor(
private val webDavScheme = "webDavScheme"
private val webDavPassword = "webDavPassword"


private val sharedPreferences: SharedPreferences by lazy {
context.getSharedPreferences(
sharedPrefsFile,
Expand Down Expand Up @@ -352,8 +352,21 @@ open class Defaults @Inject constructor(
return dataMarshaller.unmarshal(json)
}

val currentPerformFullSyncGuard = 1

fun setPerformFullSyncGuard(newValue: Int) {
sharedPreferences.edit { putInt(performFullSyncGuardKey, newValue) }
}

fun performFullSyncGuard(): Int {
if (!sharedPreferences.contains(performFullSyncGuardKey)) {
if (sharedPreferences.contains(didPerformFullSyncFix)) {
return currentPerformFullSyncGuard - 1
}
return currentPerformFullSyncGuard
}
return sharedPreferences.getInt(performFullSyncGuardKey, 1)
}

fun reset() {
setUsername("")
Expand Down
16 changes: 15 additions & 1 deletion app/src/main/java/org/zotero/android/database/Database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.realm.annotations.RealmModule
import org.zotero.android.database.objects.AllItemsDbRow
import org.zotero.android.database.objects.FieldKeys
import org.zotero.android.database.objects.ItemTypes
import org.zotero.android.database.objects.ObjectSyncState
import org.zotero.android.database.objects.RCollection
import org.zotero.android.database.objects.RCondition
import org.zotero.android.database.objects.RCreator
Expand All @@ -34,7 +35,7 @@ import java.io.File

class Database {
companion object {
private const val schemaVersion = 2L //From now on must only increase by 1 whenever db schema changes
private const val schemaVersion = 3L //From now on must only increase by 1 whenever db schema changes

fun mainConfiguration(dbFile: File): RealmConfiguration {
val builder = RealmConfiguration.Builder()
Expand All @@ -53,6 +54,9 @@ class Database {
if (oldVersion < 2) {
migrateAllItemsDbRowTypeIconNameTypeChange(dynamicRealm)
}
if (oldVersion < 3) {
markAllNonLocalGroupsAsOutdatedToTriggerResync(dynamicRealm)
}
}

override fun equals(other: Any?): Boolean {
Expand All @@ -70,6 +74,16 @@ class Database {

}

private fun markAllNonLocalGroupsAsOutdatedToTriggerResync(dynamicRealm: DynamicRealm) {
val groups = dynamicRealm
.where(RGroup::class.java.simpleName)
.equalTo("isLocalOnly", false)
.findAll()
for (group in groups) {
group.setString("syncState", ObjectSyncState.outdated.name)
}
}

private fun migrateAllItemsDbRowTypeIconNameTypeChange(dynamicRealm: DynamicRealm) {
val realmSchema = dynamicRealm.schema

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ class StoreGroupDbRequest(
database.createEmbeddedObject(RVersions::class.java, group, "versions")
}

val isOwner = response.data.owner == userId
val canEditMetadata: Boolean
val canEditFiles: Boolean

if (response.data.libraryEditing == "admins") {
canEditMetadata = (response.data.admins ?: emptyList()).contains(userId)
canEditMetadata = isOwner || (response.data.admins ?: emptyList()).contains(userId)
} else {
canEditMetadata = true
}
Expand All @@ -54,15 +55,15 @@ class StoreGroupDbRequest(
}

"admins" -> {
canEditFiles = (response.data.admins ?: emptyList()).contains(userId)
canEditFiles = isOwner || (response.data.admins ?: emptyList()).contains(userId)
}

"members" -> {
canEditFiles = true
}

else -> {
canEditFiles = false
canEditFiles = isOwner
}
}

Expand Down
19 changes: 16 additions & 3 deletions app/src/main/java/org/zotero/android/sync/UserControllers.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.zotero.android.sync

import InitializeCustomLibrariesDbRequest
import android.content.Context
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
Expand All @@ -15,8 +14,8 @@ import org.zotero.android.architecture.coroutines.Dispatchers
import org.zotero.android.attachmentdownloader.AttachmentDownloader
import org.zotero.android.database.DbWrapperMain
import org.zotero.android.database.requests.CleanupUnusedTags
import org.zotero.android.files.FileStore
import org.zotero.android.websocket.ChangeWsResponse
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton

Expand All @@ -32,6 +31,7 @@ class UserControllers @Inject constructor(
private val webSocketController: WebSocketController,
private val changeWsResponseKindEventStream: ChangeWsResponseKindEventStream,
private val fileDownloader: AttachmentDownloader,
private val defaults: Defaults,
) {

private lateinit var changeObserver: ObjectUserChangeObserver
Expand Down Expand Up @@ -75,6 +75,15 @@ class UserControllers @Inject constructor(
}

fun enableSync(apiKey: String) {
Timber.i("UserControllers: performFullSyncGuard: ${defaults.performFullSyncGuard()}; currentPerformFullSyncGuard: ${defaults.currentPerformFullSyncGuard}")
if (defaults.performFullSyncGuard() < defaults.currentPerformFullSyncGuard) {
defaults.setDidPerformFullSyncFix(false)
defaults.setPerformFullSyncGuard(defaults.currentPerformFullSyncGuard)
} else {
defaults.setDidPerformFullSyncFix(true)
}
Timber.i("UserControllers: didPerformFullSyncFix: ${defaults.didPerformFullSyncFix()}")

objectUserChangeEventStream.flow()
.debounce(3000)
.onEach { changedLibraries ->
Expand All @@ -99,7 +108,11 @@ class UserControllers @Inject constructor(

this.webSocketController.connect(apiKey = apiKey, completed = {
//TODO backgroundUploadObserver.updateSessions()
val type: SyncKind = SyncKind.normal
val type = if(defaults.didPerformFullSyncFix()) {
SyncKind.normal
} else {
SyncKind.full
}
this.syncScheduler.request(type = type, libraries = Libraries.all)
})

Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/BuildConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ object BuildConfig {
const val compileSdkVersion = 34
const val targetSdk = 34

val versionCode = 96 // Must be updated on every build
val versionCode = 97 // Must be updated on every build
val version = Version(
major = 1,
minor = 0,
Expand Down

0 comments on commit 2b5aded

Please sign in to comment.