Skip to content

Commit

Permalink
Merge pull request #586 from Infomaniak/fix-search-timeout
Browse files Browse the repository at this point in the history
Fix getFileActivities timeout
  • Loading branch information
sirambd authored Feb 23, 2022
2 parents 53a3eca + 3df97f4 commit 9a96b94
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Core
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import com.infomaniak.drive.data.api.ApiRepository.putFileShareLink
import com.infomaniak.drive.data.api.ApiRepository.removeCategory
import com.infomaniak.drive.data.api.ApiRepository.updateDropBox
import com.infomaniak.drive.data.api.ApiRoutes.postFileShare
import com.infomaniak.drive.data.api.ErrorCode.Companion.translateError
import com.infomaniak.drive.data.models.File
import com.infomaniak.drive.utils.ApiTestUtils.assertApiResponseData
import com.infomaniak.drive.utils.ApiTestUtils.createDropBoxForTest
Expand All @@ -66,9 +65,7 @@ import com.infomaniak.drive.utils.ApiTestUtils.deleteTestFile
import com.infomaniak.drive.utils.ApiTestUtils.getCategory
import com.infomaniak.drive.utils.ApiTestUtils.getShareLink
import com.infomaniak.drive.utils.ApiTestUtils.putNewFileInTrash
import com.infomaniak.drive.utils.KDriveHttpClient
import com.infomaniak.drive.utils.Utils.ROOT_ID
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.*
import org.junit.jupiter.api.Assertions.*
import java.util.*
Expand Down Expand Up @@ -277,8 +274,7 @@ class ApiRepositoryTest : KDriveTest() {
@Test
@DisplayName("Check if the file activities are correctly retrieved")
fun getFileActivities() {
val okHttpClientWithTimeout = runBlocking { KDriveHttpClient.getHttpClient(user.id, 30) }
with(getFileActivities(okHttpClientWithTimeout, testFile, 1)) {
with(getFileActivities(testFile, 1, false)) {
if (isSuccess()) {
assertApiResponseData(this)
} else {
Expand Down
18 changes: 10 additions & 8 deletions app/src/main/java/com/infomaniak/drive/data/api/ApiRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.infomaniak.drive.data.api

import androidx.collection.arrayMapOf
import com.google.gson.JsonElement
import com.infomaniak.drive.data.api.ApiRoutes.withFile
import com.infomaniak.drive.data.models.*
import com.infomaniak.drive.data.models.drive.Category
import com.infomaniak.drive.data.models.drive.DriveInfo
Expand Down Expand Up @@ -90,9 +91,15 @@ object ApiRepository {
return callApi(url, GET, okHttpClient = okHttpClient)
}

fun getFileActivities(okHttpClient: OkHttpClient, file: File, page: Int): ApiResponse<ArrayList<FileActivity>> {
val url = "${ApiRoutes.getFileActivities(file)}?${pagination(page)}&depth=children&from_date=${file.responseAt}" +
"&with=file,rights,collaborative_folder,favorite,share_link,mobile,categories" + ACTIONS
// Increase timeout for this api call because it can take more than 10s to process data
fun getFileActivities(
file: File,
page: Int,
forFileList: Boolean,
okHttpClient: OkHttpClient = HttpClient.okHttpClientLongTimeout,
): ApiResponse<ArrayList<FileActivity>> {
val queries = if (forFileList) "&depth=children&from_date=${file.responseAt}&$withFile" else "&with=user"
val url = "${ApiRoutes.getFileActivities(file)}?${pagination(page)}$queries$ACTIONS"
return callApi(url, GET, okHttpClient = okHttpClient)
}

Expand Down Expand Up @@ -197,11 +204,6 @@ object ApiRepository {
return callApi(ApiRoutes.getFileCount(file), GET)
}

fun getFileActivities(file: File, page: Int): ApiResponse<ArrayList<FileActivity>> {
val url = "${ApiRoutes.getFileActivities(file)}?with=user&${pagination(page, 25)}" + ACTIONS
return callApi(url, GET)
}

fun getFileComments(file: File, page: Int): ApiResponse<ArrayList<FileComment>> {
val url = "${ApiRoutes.commentFile(file)}?with=like,response&${pagination(page)}"
return callApi(url, GET)
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/com/infomaniak/drive/data/api/ApiRoutes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import com.infomaniak.drive.data.models.Team

object ApiRoutes {

private const val with = "with=children,rights,collaborative_folder,favorite,mobile,share_link,categories"
private val with = with("children")
val withFile = with("file")

private fun with(target: String) = "with=$target,rights,collaborative_folder,favorite,mobile,share_link,categories"

private fun fileURL(file: File) = "${DRIVE_API}${file.driveId}/file/${file.id}"

Expand Down Expand Up @@ -111,7 +114,7 @@ object ApiRoutes {
fun dropBox(file: File) = "${fileURL(file)}/collaborate"

fun getLastActivities(driveId: Int) =
"${DRIVE_API}$driveId/file/activity?with=file,rights,collaborative_folder,favorite,mobile,share_link,categories" +
"${DRIVE_API}$driveId/file/activity?$withFile" +
"&depth=unlimited" +
"&actions[]=file_create" +
"&actions[]=file_update" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -749,10 +749,10 @@ object FileController {
userDrive: UserDrive? = null
): Map<out Int, FileActivity> {
val okHttpClient = runBlocking {
userDrive?.userId?.let { KDriveHttpClient.getHttpClient(it) } ?: HttpClient.okHttpClient
userDrive?.userId?.let { KDriveHttpClient.getHttpClient(it, 30) } ?: HttpClient.okHttpClientLongTimeout
}
val returnResponse = arrayMapOf<Int, FileActivity>()
val apiResponse = ApiRepository.getFileActivities(okHttpClient, folder, page)
val apiResponse = ApiRepository.getFileActivities(folder, page, true, okHttpClient)
if (!apiResponse.isSuccess()) return returnResponse

return if (apiResponse.data?.isNotEmpty() == true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class FileDetailsViewModel : ViewModel() {

return liveData(Dispatchers.IO + getFileActivitiesJob) {
suspend fun recursive(page: Int) {
val apiRepository = ApiRepository.getFileActivities(file, page)
val apiRepository = ApiRepository.getFileActivities(file, page, false)
if (apiRepository.isSuccess()) {
when {
apiRepository.data?.isNullOrEmpty() == true -> emit(null)
Expand Down

0 comments on commit 9a96b94

Please sign in to comment.