Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit b518f6a3 authored by tobiasKaminsky's avatar tobiasKaminsky
Browse files

retrieve recommendations

parent 1a8e497c
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
/*
 * Nextcloud Android Library
 *
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2024 Tobias Kaminsky <tobias@kaminsky.me>
 * SPDX-License-Identifier: MIT
 */
package com.nextcloud.android.lib.resources.recommendations

import com.owncloud.android.AbstractIT
import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation
import com.owncloud.android.lib.resources.status.NextcloudVersion
import org.junit.Assert.assertTrue
import org.junit.Test

class GetRecommendationsRemoteOperationIT : AbstractIT() {
    @Test
    fun getRecommendations() {
        testOnlyOnServer(NextcloudVersion.nextcloud_31)
        assertTrue(CreateFolderRemoteOperation("/test/", true).execute(client).isSuccess)

        val result = GetRecommendationsRemoteOperation().execute(nextcloudClient).resultData

        assertTrue(result.enabled)
        assertTrue(result.recommendations.isNotEmpty())
    }
}
+68 −0
Original line number Diff line number Diff line
/*
 * Nextcloud Android Library
 *
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2024 Tobias Kaminsky <tobias@kaminsky.me>
 * SPDX-License-Identifier: MIT
 */
package com.nextcloud.android.lib.resources.recommendations

import com.google.gson.reflect.TypeToken
import com.nextcloud.common.NextcloudClient
import com.nextcloud.operations.GetMethod
import com.owncloud.android.lib.common.operations.RemoteOperationResult
import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.lib.ocs.ServerResponse
import com.owncloud.android.lib.resources.OCSRemoteOperation
import org.apache.commons.httpclient.HttpStatus

/**
 * Get recommendation of an user
 */
class GetRecommendationsRemoteOperation :
    OCSRemoteOperation<RecommendationResponse>() {
    @Suppress("TooGenericExceptionCaught")
    override fun run(client: NextcloudClient): RemoteOperationResult<RecommendationResponse> {
        var result: RemoteOperationResult<RecommendationResponse>
        var getMethod: GetMethod? = null
        try {
            getMethod =
                GetMethod(
                    client.baseUri.toString() + ENDPOINT + JSON_FORMAT,
                    true
                )
            val status = client.execute(getMethod)
            if (status == HttpStatus.SC_OK) {
                val map =
                    getServerResponse(
                        getMethod,
                        object : TypeToken<ServerResponse<RecommendationResponse>>() {}
                    )?.ocs?.data

                if (map != null) {
                    result = RemoteOperationResult(true, getMethod)
                    result.setResultData(map)
                } else {
                    result = RemoteOperationResult(false, getMethod)
                }
            } else {
                result = RemoteOperationResult(false, getMethod)
            }
        } catch (e: Exception) {
            result = RemoteOperationResult(e)
            Log_OC.e(
                TAG,
                "Get recommendations failed: " + result.logMessage,
                result.exception
            )
        } finally {
            getMethod?.releaseConnection()
        }
        return result
    }

    companion object {
        private val TAG = GetRecommendationsRemoteOperation::class.java.simpleName
        private const val ENDPOINT = "/ocs/v2.php/apps/recommendations/api/v1/recommendations"
    }
}
+20 −0
Original line number Diff line number Diff line
/*
 * Nextcloud Android Library
 *
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2024 Tobias Kaminsky <tobias@kaminsky.me>
 * SPDX-License-Identifier: MIT
 */

package com.nextcloud.android.lib.resources.recommendations

data class Recommendation(
    val id: Long,
    val timestamp: Long,
    val name: String,
    val directory: String,
    val extension: String,
    val mimeType: String,
    val hasPreview: Boolean,
    val reason: String
)
+11 −0
Original line number Diff line number Diff line
/*
 * Nextcloud Android Library
 *
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2024 Tobias Kaminsky <tobias@kaminsky.me>
 * SPDX-License-Identifier: MIT
 */

package com.nextcloud.android.lib.resources.recommendations

data class RecommendationResponse(val enabled: Boolean, val recommendations: ArrayList<Recommendation>)
+3 −0
Original line number Diff line number Diff line
@@ -38,6 +38,9 @@ class NextcloudVersion : OwnCloudVersion {

        @JvmField
        val nextcloud_30 = NextcloudVersion(0x1E000000) // 30.0
        
        @JvmField
        val nextcloud_31 = NextcloudVersion(0x1F000000) // 31.0
    }

    constructor(string: String) : super(string)