Skip to content

Commit d1fa98d

Browse files
committed
wip
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent f30e859 commit d1fa98d

3 files changed

Lines changed: 43 additions & 65 deletions

File tree

app/src/main/java/com/nextcloud/client/database/dao/ShareDao.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import androidx.room.Insert
1212
import androidx.room.OnConflictStrategy
1313
import androidx.room.Query
1414
import com.nextcloud.client.database.entity.ShareEntity
15+
import com.nextcloud.client.database.entity.model.ShareeKey
1516

1617
@Dao
1718
interface ShareDao {
@@ -21,4 +22,10 @@ interface ShareDao {
2122

2223
@Query("DELETE FROM ocshares WHERE owner_share = :accountName")
2324
suspend fun clearSharesForAccount(accountName: String)
25+
26+
@Query(
27+
"SELECT path, shate_with, share_type FROM ocshares " +
28+
"WHERE path IN (:paths) AND owner_share = :accountName AND share_type IN (:shareTypes)"
29+
)
30+
fun getShareeKeys(paths: List<String>, accountName: String, shareTypes: List<Int>): List<ShareeKey>
2431
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Nextcloud - Android Client
3+
*
4+
* SPDX-FileCopyrightText: 2026 Alper Ozturk <alper.ozturk@nextcloud.com>
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
6+
*/
7+
8+
package com.nextcloud.client.database.entity.model
9+
10+
import androidx.room.ColumnInfo
11+
import com.owncloud.android.lib.resources.shares.ShareType
12+
13+
data class ShareeKey(
14+
@ColumnInfo(name = "path") val path: String,
15+
@ColumnInfo(name = "shate_with") val shareWith: String?,
16+
@ColumnInfo(name = "share_type") val shareType: Int
17+
) {
18+
companion object {
19+
val shareableShareTypeValues = listOf(
20+
ShareType.USER,
21+
ShareType.GROUP,
22+
ShareType.EMAIL,
23+
ShareType.FEDERATED,
24+
ShareType.FEDERATED_GROUP,
25+
ShareType.ROOM,
26+
ShareType.CIRCLE
27+
).map { it.value }
28+
}
29+
}

app/src/main/java/com/nextcloud/utils/extensions/FileDataStorageManagerExtensions.kt

Lines changed: 7 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,16 @@
77

88
package com.nextcloud.utils.extensions
99

10-
import android.os.RemoteException
10+
import com.nextcloud.client.database.entity.model.ShareeKey
1111
import com.nextcloud.client.database.entity.toOCCapability
1212
import com.owncloud.android.datamodel.FileDataStorageManager
1313
import com.owncloud.android.datamodel.OCFile
14-
import com.owncloud.android.db.ProviderMeta.ProviderTableMeta
15-
import com.owncloud.android.lib.common.utils.Log_OC
1614
import com.owncloud.android.lib.resources.files.model.RemoteFile
1715
import com.owncloud.android.lib.resources.shares.OCShare
18-
import com.owncloud.android.lib.resources.shares.ShareType
1916
import com.owncloud.android.lib.resources.status.OCCapability
2017
import kotlinx.coroutines.Dispatchers
2118
import kotlinx.coroutines.withContext
22-
import kotlin.collections.asSequence
23-
24-
// matches FileDataStorageManager.getSharesWithForAFile, excludes public link shares
25-
private val shareableShareTypes = listOf(
26-
ShareType.USER,
27-
ShareType.GROUP,
28-
ShareType.EMAIL,
29-
ShareType.FEDERATED,
30-
ShareType.FEDERATED_GROUP,
31-
ShareType.ROOM,
32-
ShareType.CIRCLE
33-
)
19+
3420

3521
// keeps queries well under SQLite's bound-parameter limit
3622
private const val SHARE_PATH_QUERY_CHUNK_SIZE = 400
@@ -50,61 +36,17 @@ fun FileDataStorageManager.areShareesChanged(remoteFiles: List<RemoteFile>): Boo
5036
.toSet()
5137
}
5238

53-
val existingShareesByPath = queryLocalShareeKeysByPath(newShareesByPath.keys, user.accountName)
39+
val existingShareesByPath = newShareesByPath.keys
40+
.chunked(SHARE_PATH_QUERY_CHUNK_SIZE)
41+
.flatMap { chunk -> shareDao.getShareeKeys(chunk, user.accountName, ShareeKey.shareableShareTypeValues) }
42+
.groupBy(ShareeKey::path) { "${it.shareWith}:${it.shareType}" }
43+
.mapValues { (_, keys) -> keys.toSet() }
5444

5545
return newShareesByPath.keys.any { path ->
5646
newShareesByPath[path].orEmpty() != existingShareesByPath[path].orEmpty()
5747
}
5848
}
5949

60-
private fun FileDataStorageManager.queryLocalShareeKeysByPath(
61-
paths: Set<String>,
62-
accountName: String
63-
): Map<String, Set<String>> {
64-
val result = mutableMapOf<String, MutableSet<String>>()
65-
66-
paths.toList().chunked(SHARE_PATH_QUERY_CHUNK_SIZE).forEach { chunk ->
67-
queryLocalShareeKeysChunk(chunk, accountName, result)
68-
}
69-
70-
return result
71-
}
72-
73-
private fun FileDataStorageManager.queryLocalShareeKeysChunk(
74-
paths: List<String>,
75-
accountName: String,
76-
result: MutableMap<String, MutableSet<String>>
77-
) {
78-
val pathPlaceholders = paths.joinToString(",") { "?" }
79-
val shareTypeFilter = shareableShareTypes.joinToString(" OR ") { "${ProviderTableMeta.OCSHARES_SHARE_TYPE} = ?" }
80-
val selection = "${ProviderTableMeta.OCSHARES_PATH} IN ($pathPlaceholders) AND " +
81-
"${ProviderTableMeta.OCSHARES_ACCOUNT_OWNER} = ? AND ($shareTypeFilter)"
82-
val selectionArgs = (paths + accountName + shareableShareTypes.map { it.value.toString() }).toTypedArray()
83-
84-
val cursor = if (contentResolver != null) {
85-
contentResolver.query(ProviderTableMeta.CONTENT_URI_SHARE, null, selection, selectionArgs, null)
86-
} else {
87-
try {
88-
contentProviderClient?.query(ProviderTableMeta.CONTENT_URI_SHARE, null, selection, selectionArgs, null)
89-
} catch (e: RemoteException) {
90-
Log_OC.e(javaClass.simpleName, "Could not get list of shares: ${e.message}", e)
91-
null
92-
}
93-
}
94-
95-
cursor?.use {
96-
val pathIndex = it.getColumnIndex(ProviderTableMeta.OCSHARES_PATH)
97-
val shareWithIndex = it.getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_WITH)
98-
val shareTypeIndex = it.getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_TYPE)
99-
100-
while (it.moveToNext()) {
101-
val path = it.getString(pathIndex) ?: continue
102-
val key = "${it.getString(shareWithIndex)}:${it.getInt(shareTypeIndex)}"
103-
result.getOrPut(path) { mutableSetOf() }.add(key)
104-
}
105-
}
106-
}
107-
10850
suspend fun FileDataStorageManager.saveShares(shares: List<OCShare>, accountName: String) {
10951
withContext(Dispatchers.IO) {
11052
val entities = shares.map { share ->

0 commit comments

Comments
 (0)