Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ package com.owncloud.android.lib.resources.shares

import com.owncloud.android.AbstractIT
import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation
import com.owncloud.android.lib.resources.shares.attributes.ShareAttributes
import com.owncloud.android.lib.resources.shares.attributes.ShareAttributesJsonHandler
import com.owncloud.android.lib.resources.status.GetStatusRemoteOperation
import com.owncloud.android.lib.resources.status.NextcloudVersion
import com.owncloud.android.lib.resources.status.OwnCloudVersion
Expand All @@ -30,18 +28,6 @@ class CreateShareRemoteOperationIT : AbstractIT() {
Assume.assumeTrue(ownCloudVersion.isNewerOrEqual(NextcloudVersion.nextcloud_24))
}

@Test
fun createShareWithNoteAndAttributes() {
val attributes = listOf(ShareAttributes.createDownloadAttributes(true))
val note = "Note with attributes"
val path = "/shareWithAttributes/"

createFolder(path)
val share = createShare(path, "admin", note, ShareAttributesJsonHandler.toJson(attributes))
assertEquals(note, share.note)
assertEquals(attributes, ShareAttributesJsonHandler.toList(share.attributes))
}

@Test
fun createShareWithNote() {
val note = "This is the note"
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2025 Alper Ozturk <[email protected]>
* SPDX-License-Identifier: MIT
*/

package com.owncloud.android.lib.resources.shares.extensions

import com.owncloud.android.lib.resources.shares.OCShare
import org.json.JSONArray
import org.json.JSONObject

private const val KEY = "key"
private const val SCOPE_KEY = "scope"
private const val DOWNLOAD_KEY = "download"
private const val PERMISSIONS_KEY = "permissions"
private const val VALUE_KEY = "value"
private const val ENABLED_KEY = "enabled"

fun toggleAllowDownloadAndSync(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cannot be implemented as an extension, as the share object does not exist at the time of share creation. Therefore, optional attributes should be passed as a string parameter instead.

attributes: String?,
isChecked: Boolean,
useV2DownloadAttributes: Boolean
): String? {
var jsonArray = JSONArray()
if (!attributes.isNullOrEmpty()) {
jsonArray = JSONArray(attributes)
}

val downloadAttr = jsonArray.findDownloadAttribute()
val enabledKey = getEnabledKey(useV2DownloadAttributes)

if (downloadAttr != null) {
downloadAttr.put(enabledKey, isChecked)
} else {
jsonArray.put(
JSONObject().apply {
put(KEY, DOWNLOAD_KEY)
put(SCOPE_KEY, PERMISSIONS_KEY)
put(enabledKey, isChecked)
}
)
}

return jsonArray.toString()
}

@Suppress("ReturnCount")
fun OCShare?.isAllowDownloadAndSyncEnabled(useV2DownloadAttributes: Boolean): Boolean {
if (this?.attributes.isNullOrEmpty()) return false

val jsonArray = JSONArray(this.attributes)
val downloadAttr = jsonArray.findDownloadAttribute() ?: return false
val enabledKey = getEnabledKey(useV2DownloadAttributes)

return downloadAttr.optBoolean(enabledKey, false)
}

private fun JSONArray.findDownloadAttribute(): JSONObject? =
(0 until length())
.asSequence()
.map { getJSONObject(it) }
.find {
it.optString(KEY) == DOWNLOAD_KEY &&
it.optString(SCOPE_KEY) == PERMISSIONS_KEY
}

private fun getEnabledKey(isV2: Boolean): String = if (isV2) VALUE_KEY else ENABLED_KEY
Loading