Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update API calls to receive null avatars if the users have not set any #1230

Merged
merged 6 commits into from
Mar 13, 2024
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
42 changes: 3 additions & 39 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,7 +19,6 @@ package com.infomaniak.drive.data.api

import androidx.collection.arrayMapOf
import com.google.gson.JsonElement
import com.infomaniak.drive.data.api.ApiRoutes.activitiesWithExtraQuery
import com.infomaniak.drive.data.models.*
import com.infomaniak.drive.data.models.ArchiveUUID.ArchiveBody
import com.infomaniak.drive.data.models.drive.Category
Expand All @@ -41,39 +40,6 @@ object ApiRepository : ApiRepositoryCore() {

var PER_PAGE = 200

private const val ACTIONS = "&actions[]=file_create" +
"&actions[]=file_rename" +
"&actions[]=file_move" +
"&actions[]=file_move_out" +
"&actions[]=file_trash" +
"&actions[]=file_restore" +
"&actions[]=file_delete" +
"&actions[]=file_update" +
"&actions[]=file_favorite_create" +
"&actions[]=file_favorite_remove" +
"&actions[]=file_share_create" +
"&actions[]=file_share_update" +
"&actions[]=file_share_delete" +
"&actions[]=file_categorize" +
"&actions[]=file_uncategorize" +
"&actions[]=file_color_update" +
"&actions[]=file_color_delete" +
"&actions[]=share_link_create" +
"&actions[]=share_link_update" +
"&actions[]=share_link_delete" +
"&actions[]=collaborative_folder_create" +
"&actions[]=collaborative_folder_update" +
"&actions[]=collaborative_folder_delete"

private const val ADDITIONAL_ACTIONS = "&actions[]=file_access" +
"&actions[]=comment_create" +
"&actions[]=comment_update" +
"&actions[]=comment_delete" +
"&actions[]=comment_like" +
"&actions[]=comment_unlike" +
"&actions[]=comment_resolve" +
"&actions[]=share_link_show"

fun getAllDrivesData(
okHttpClient: OkHttpClient
): ApiResponse<DriveInfo> {
Expand Down Expand Up @@ -101,17 +67,15 @@ object ApiRepository : ApiRepositoryCore() {
return callApi(url, GET, okHttpClient = okHttpClient)
}

// Increase timeout for this api call because it can take more than 10s to process data
// 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}&$activitiesWithExtraQuery" else "&with=user"
val url = "${ApiRoutes.getFileActivities(file)}?${pagination(page)}$queries$ACTIONS" +
if (forFileList) "" else ADDITIONAL_ACTIONS

val pagination = pagination(page)
val url = ApiRoutes.getFileActivities(file, forFileList, pagination)
return callApi(url, GET, okHttpClient = okHttpClient)
}

Expand Down
69 changes: 57 additions & 12 deletions app/src/main/java/com/infomaniak/drive/data/api/ApiRoutes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,46 @@ import com.infomaniak.drive.utils.FileId

object ApiRoutes {

private const val fileWithQuery = "with=capabilities,categories,conversion_capabilities,dropbox,dropbox.capabilities," +
"external_import,is_favorite,path,sharelink,sorted_name"
private const val fileWithQuery = "with=capabilities,categories,conversion_capabilities," +
"dropbox,dropbox.capabilities,external_import,is_favorite,path,sharelink,sorted_name"
private const val fileExtraWithQuery = "$fileWithQuery,users,version"
private const val activitiesWithQuery = "with=file,file.capabilities,file.categories,file.conversion_capabilities," +
"file.dropbox,file.dropbox.capabilities,file.is_favorite,file.sharelink,file.sorted_name"
const val activitiesWithExtraQuery = "$activitiesWithQuery,file.external_import"
private const val activitiesWithExtraQuery = "$activitiesWithQuery,file.external_import"
private const val noAvatar = "no_avatar_default=1"

private const val ACTIONS = "&actions[]=file_create" +
"&actions[]=file_rename" +
"&actions[]=file_move" +
"&actions[]=file_move_out" +
"&actions[]=file_trash" +
"&actions[]=file_restore" +
"&actions[]=file_delete" +
"&actions[]=file_update" +
"&actions[]=file_favorite_create" +
"&actions[]=file_favorite_remove" +
"&actions[]=file_share_create" +
"&actions[]=file_share_update" +
"&actions[]=file_share_delete" +
"&actions[]=file_categorize" +
"&actions[]=file_uncategorize" +
"&actions[]=file_color_update" +
"&actions[]=file_color_delete" +
"&actions[]=share_link_create" +
"&actions[]=share_link_update" +
"&actions[]=share_link_delete" +
"&actions[]=collaborative_folder_create" +
"&actions[]=collaborative_folder_update" +
"&actions[]=collaborative_folder_delete"

private const val ADDITIONAL_ACTIONS = "&actions[]=file_access" +
"&actions[]=comment_create" +
"&actions[]=comment_update" +
"&actions[]=comment_delete" +
"&actions[]=comment_like" +
"&actions[]=comment_unlike" +
"&actions[]=comment_resolve" +
"&actions[]=share_link_show"

private fun orderQuery(order: SortType) = "order_for[${order.orderBy}]=${order.order}&order_by=${order.orderBy}"

Expand All @@ -45,7 +79,7 @@ object ApiRoutes {

/** V1 */
//region V1
fun getAllDrivesData() = "${DRIVE_API_V1}init?with=drives,users,teams,ips,categories"
fun getAllDrivesData() = "${DRIVE_API_V1}init?${noAvatar}&with=drives,users,teams,ips,categories"
//endregion

/** Archive */
Expand All @@ -61,7 +95,7 @@ object ApiRoutes {

fun fileInvitationAccess(file: File, invitationId: Int) = "${driveURL(file.driveId)}/files/invitations/$invitationId"

fun getFileShare(file: File) = "${accessUrl(file)}?with=user"
fun getFileShare(file: File) = "${accessUrl(file)}?${noAvatar}&with=user"

fun checkFileShare(file: File) = "${accessUrl(file)}/check"

Expand All @@ -86,14 +120,25 @@ object ApiRoutes {
"&actions[]=comment_create"

fun getLastActivities(driveId: Int) =
"${filesURL(driveId)}/activities?$activitiesWithQuery,user&depth=unlimited&$activitiesActions"
"${filesURL(driveId)}/activities?${activitiesWithQuery},user&depth=unlimited&${activitiesActions}&${noAvatar}"

fun getFileActivities(file: File, forFileList: Boolean, pagination: String): String {

val baseUrl = "${fileURL(file)}/activities"
val baseParameters = "?${noAvatar}&${pagination}"
val sourceDependentParameters = if (forFileList) {
"&depth=children&from_date=${file.responseAt}&${activitiesWithExtraQuery}"
} else {
"&with=user"
}
val actionsParameters = ACTIONS + if (forFileList) "" else ADDITIONAL_ACTIONS

fun getFileActivities(file: File) = "${fileURL(file)}/activities"
return baseUrl + baseParameters + sourceDependentParameters + actionsParameters
}

fun getFileActivities(driveId: Int, fileIds: String, fromDate: Long) =
"${filesURL(driveId)}/activities/batch?$activitiesWithQuery&file_ids=$fileIds&from_date=$fromDate" +
"&actions[]=file_rename" +
"&actions[]=file_update"
fun getFileActivities(driveId: Int, fileIds: String, fromDate: Long) = "${filesURL(driveId)}/activities/batch" +
"?${noAvatar}&${activitiesWithQuery}&file_ids=${fileIds}" +
"&from_date=${fromDate}&actions[]=file_rename&actions[]=file_update"

fun getTrashedFilesActivities(file: File) = "${trashURL(file)}/activities"
//endregion
Expand All @@ -112,7 +157,7 @@ object ApiRoutes {

/** Comment */
//region Comment
private const val withComments = "with=user,likes,responses,responses.user,responses.likes"
private const val withComments = "${noAvatar}&with=user,likes,responses,responses.user,responses.likes"

fun fileComments(file: File) = "${fileURL(file)}/comments?$withComments"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -783,11 +783,11 @@ object FileController {

fun getFolderActivities(folder: File, page: Int, userDrive: UserDrive? = null): Map<out Int, FileActivity> {
return getRealmInstance(userDrive).use { realm ->
getFolderActivitiesRec(realm, folder, page, userDrive)
getFolderActivitiesRecursive(realm, folder, page, userDrive)
}
}

private fun getFolderActivitiesRec(
private fun getFolderActivitiesRecursive(
tevincent marked this conversation as resolved.
Show resolved Hide resolved
realm: Realm,
folder: File,
page: Int,
Expand All @@ -797,7 +797,7 @@ object FileController {
userDrive?.userId?.let { AccountUtils.getHttpClient(it, 30) } ?: HttpClient.okHttpClientLongTimeout
}
val returnResponse = arrayMapOf<Int, FileActivity>()
val apiResponse = ApiRepository.getFileActivities(folder, page, true, okHttpClient)
val apiResponse = ApiRepository.getFileActivities(folder, page, forFileList = true, okHttpClient)
if (!apiResponse.isSuccess()) return returnResponse

return if (apiResponse.data?.isNotEmpty() == true) {
Expand All @@ -816,7 +816,7 @@ object FileController {
}
returnResponse

} else returnResponse.apply { putAll(getFolderActivitiesRec(realm, folder, page + 1, userDrive)) }
} else returnResponse.apply { putAll(getFolderActivitiesRecursive(realm, folder, page + 1, userDrive)) }
} else {
if (apiResponse.responseAt > 0L) {
updateFile(folder.id, realm) { file -> file.responseAt = apiResponse.responseAt }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ open class DriveUser(
var avatarUrl: String? = "",
@SerializedName("display_name")
var displayName: String = "",
var avatar: String = "",
private var avatar: String? = "",
var email: String = "",
@SerializedName("role")
private var _role: String = "",
Expand All @@ -50,7 +50,7 @@ open class DriveUser(
displayName = user.displayName ?: ""
}

fun getUserAvatar() = avatar.ifBlank { avatarUrl.toString() }
fun getUserAvatar() = avatar?.ifBlank { avatarUrl.toString() } ?: avatarUrl.toString()

fun getInitials(): String {
displayName.split(" ").let { initials ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class FileDetailsViewModel : ViewModel() {
getFileActivitiesJob = Job()

return liveData(Dispatchers.IO + getFileActivitiesJob) {
manageRecursiveApiResponse(file) { file, page -> ApiRepository.getFileActivities(file, page, false) }
manageRecursiveApiResponse(file) { file, page -> ApiRepository.getFileActivities(file, page, forFileList = false) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class FileShareAddUserDialog : FullScreenBottomSheetDialog() {
background = getBackgroundColorBasedOnId(item.id)
)
val request = ImageRequest.Builder(this)
.data(item.avatar)
.data(item.getUserAvatar())
.transformations(CircleCropTransformation())
.fallback(fallback)
.error(fallback)
Expand Down
Loading