Skip to content

Commit 89ddcfc

Browse files
Merge pull request #1758 from nextcloud/remove-share-attributes
Remove share attributes & use OCShare extensions
2 parents ccc7ae5 + 0303be5 commit 89ddcfc

File tree

5 files changed

+69
-111
lines changed

5 files changed

+69
-111
lines changed

library/src/androidTest/java/com/owncloud/android/lib/resources/shares/CreateShareRemoteOperationIT.kt

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ package com.owncloud.android.lib.resources.shares
99

1010
import com.owncloud.android.AbstractIT
1111
import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation
12-
import com.owncloud.android.lib.resources.shares.attributes.ShareAttributes
13-
import com.owncloud.android.lib.resources.shares.attributes.ShareAttributesJsonHandler
1412
import com.owncloud.android.lib.resources.status.GetStatusRemoteOperation
1513
import com.owncloud.android.lib.resources.status.NextcloudVersion
1614
import com.owncloud.android.lib.resources.status.OwnCloudVersion
@@ -30,18 +28,6 @@ class CreateShareRemoteOperationIT : AbstractIT() {
3028
Assume.assumeTrue(ownCloudVersion.isNewerOrEqual(NextcloudVersion.nextcloud_24))
3129
}
3230

33-
@Test
34-
fun createShareWithNoteAndAttributes() {
35-
val attributes = listOf(ShareAttributes.createDownloadAttributes(true))
36-
val note = "Note with attributes"
37-
val path = "/shareWithAttributes/"
38-
39-
createFolder(path)
40-
val share = createShare(path, "admin", note, ShareAttributesJsonHandler.toJson(attributes))
41-
assertEquals(note, share.note)
42-
assertEquals(attributes, ShareAttributesJsonHandler.toList(share.attributes))
43-
}
44-
4531
@Test
4632
fun createShareWithNote() {
4733
val note = "This is the note"

library/src/main/java/com/owncloud/android/lib/resources/shares/attributes/ShareAttributes.kt

Lines changed: 0 additions & 25 deletions
This file was deleted.

library/src/main/java/com/owncloud/android/lib/resources/shares/attributes/ShareAttributesDeserializer.kt

Lines changed: 0 additions & 36 deletions
This file was deleted.

library/src/main/java/com/owncloud/android/lib/resources/shares/attributes/ShareAttributesJsonHandler.kt

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Nextcloud Android Library
3+
*
4+
* SPDX-FileCopyrightText: 2025 Alper Ozturk <[email protected]>
5+
* SPDX-License-Identifier: MIT
6+
*/
7+
8+
package com.owncloud.android.lib.resources.shares.extensions
9+
10+
import com.owncloud.android.lib.resources.shares.OCShare
11+
import org.json.JSONArray
12+
import org.json.JSONObject
13+
14+
private const val KEY = "key"
15+
private const val SCOPE_KEY = "scope"
16+
private const val DOWNLOAD_KEY = "download"
17+
private const val PERMISSIONS_KEY = "permissions"
18+
private const val VALUE_KEY = "value"
19+
private const val ENABLED_KEY = "enabled"
20+
21+
fun toggleAllowDownloadAndSync(
22+
attributes: String?,
23+
isChecked: Boolean,
24+
useV2DownloadAttributes: Boolean
25+
): String? {
26+
var jsonArray = JSONArray()
27+
if (!attributes.isNullOrEmpty()) {
28+
jsonArray = JSONArray(attributes)
29+
}
30+
31+
val downloadAttr = jsonArray.findDownloadAttribute()
32+
val enabledKey = getEnabledKey(useV2DownloadAttributes)
33+
34+
if (downloadAttr != null) {
35+
downloadAttr.put(enabledKey, isChecked)
36+
} else {
37+
jsonArray.put(
38+
JSONObject().apply {
39+
put(KEY, DOWNLOAD_KEY)
40+
put(SCOPE_KEY, PERMISSIONS_KEY)
41+
put(enabledKey, isChecked)
42+
}
43+
)
44+
}
45+
46+
return jsonArray.toString()
47+
}
48+
49+
@Suppress("ReturnCount")
50+
fun OCShare?.isAllowDownloadAndSyncEnabled(useV2DownloadAttributes: Boolean): Boolean {
51+
if (this?.attributes.isNullOrEmpty()) return false
52+
53+
val jsonArray = JSONArray(this.attributes)
54+
val downloadAttr = jsonArray.findDownloadAttribute() ?: return false
55+
val enabledKey = getEnabledKey(useV2DownloadAttributes)
56+
57+
return downloadAttr.optBoolean(enabledKey, false)
58+
}
59+
60+
private fun JSONArray.findDownloadAttribute(): JSONObject? =
61+
(0 until length())
62+
.asSequence()
63+
.map { getJSONObject(it) }
64+
.find {
65+
it.optString(KEY) == DOWNLOAD_KEY &&
66+
it.optString(SCOPE_KEY) == PERMISSIONS_KEY
67+
}
68+
69+
private fun getEnabledKey(isV2: Boolean): String = if (isV2) VALUE_KEY else ENABLED_KEY

0 commit comments

Comments
 (0)