refactor: use globalFileUsage instead of achievement to append in reason

Fetching achievements is a time consuming operation and globalFileUsage gives the similar result in optimal time
This commit is contained in:
Rohit Verma 2025-06-22 12:25:37 +05:30
parent ab2f71227f
commit aa16a99707
No known key found for this signature in database
GPG key ID: D64E53E770687D0E
4 changed files with 34 additions and 27 deletions

View file

@ -347,6 +347,7 @@ dependencies {
// Kotlin + coroutines
implementation(libs.androidx.work.runtime.ktx)
implementation(libs.androidx.work.runtime)
implementation(libs.kotlinx.coroutines.rx2)
testImplementation(libs.androidx.work.testing)
//Glide

View file

@ -2,22 +2,19 @@ package fr.free.nrw.commons.delete
import android.annotation.SuppressLint
import android.content.Context
import android.util.Log
import fr.free.nrw.commons.utils.DateUtil
import java.util.Locale
import javax.inject.Inject
import javax.inject.Singleton
import fr.free.nrw.commons.Media
import fr.free.nrw.commons.R
import fr.free.nrw.commons.profile.achievements.FeedbackResponse
import fr.free.nrw.commons.auth.SessionManager
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient
import fr.free.nrw.commons.utils.DateUtil
import fr.free.nrw.commons.utils.ViewUtilWrapper
import io.reactivex.Single
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.rx2.rxSingle
import timber.log.Timber
import java.util.Locale
import javax.inject.Inject
import javax.inject.Singleton
/**
* This class handles the reason for deleting a Media object
@ -30,6 +27,8 @@ class ReasonBuilder @Inject constructor(
private val viewUtilWrapper: ViewUtilWrapper
) {
private val defaultFileUsagePageSize = 10
/**
* To process the reason and append the media's upload date and uploaded_by_me string
* @param media
@ -41,7 +40,7 @@ class ReasonBuilder @Inject constructor(
return Single.just("Not known")
}
Timber.d("Fetching article number")
return fetchArticleNumber(media, reason)
return getAndAppendFileUsage(media, reason)
}
/**
@ -56,31 +55,36 @@ class ReasonBuilder @Inject constructor(
}
}
private fun fetchArticleNumber(media: Media, reason: String): Single<String> {
return if (checkAccount()) {
Timber.d("Fetching achievements for ${sessionManager.userName}")
okHttpJsonApiClient
.getAchievements(sessionManager.userName)
.map { feedbackResponse ->
Timber.d("Feedback Response: $feedbackResponse")
appendArticlesUsed(feedbackResponse, media, reason)
}
} else {
Single.just("")
private fun getAndAppendFileUsage(media: Media, reason: String): Single<String> {
return rxSingle(context = Dispatchers.IO) {
if (!checkAccount()) return@rxSingle ""
try {
val globalFileUsage = okHttpJsonApiClient.getGlobalFileUsages(
fileName = media.filename,
pageSize = defaultFileUsagePageSize
)
val globalUsages = globalFileUsage?.query?.pages?.sumOf { it.fileUsage.size } ?: 0
appendArticlesUsed(globalUsages, media, reason)
} catch (e: Exception) {
Timber.e(e, "Error fetching file usage")
throw e
}
}
}
/**
* Takes the uploaded_by_me string, the upload date, name of articles using images
* Takes the uploaded_by_me string, the upload date, no. of articles using images
* and appends it to the received reason
* @param feedBack object
* @param fileUsages No. of files/articles using this image
* @param media whose upload data is to be fetched
* @param reason
* @param reason string to be appended
*/
@SuppressLint("StringFormatInvalid")
private fun appendArticlesUsed(feedBack: FeedbackResponse, media: Media, reason: String): String {
private fun appendArticlesUsed(fileUsages: Int, media: Media, reason: String): String {
val reason1Template = context.getString(R.string.uploaded_by_myself)
return reason + String.format(Locale.getDefault(), reason1Template, prettyUploadedDate(media), feedBack.articlesUsingImages)
return reason + String.format(Locale.getDefault(), reason1Template, prettyUploadedDate(media), fileUsages)
.also { Timber.i("New Reason %s", it) }
}

View file

@ -431,7 +431,7 @@
<string name="deletion_reason_no_longer_want_public">I changed my mind, I don\'t want it to be publicly visible anymore</string>
<string name="deletion_reason_not_interesting">Sorry this picture is not interesting for an encyclopedia</string>
<string name="uploaded_by_myself">Uploaded by myself on %1$s, used in %2$d article(s).</string>
<string name="uploaded_by_myself">Uploaded by myself on %1$s, used in %2$d article(s) at least.</string>
<string name="no_uploads">Welcome to Commons!\n
Upload your first media by tapping on the add button.</string>

View file

@ -16,6 +16,7 @@ constraintlayout = "1.1.3"
coordinates2country = "1.8"
dexcount = "4.0.0"
githubTripletPlay = "2.7.2"
kotlinxCoroutinesRx2 = "1.8.0"
osmdroidAndroid = "6.1.17"
testCore = "1.4.0"
coreKtx = "1.9.0"
@ -127,6 +128,7 @@ dagger-compiler = { module = "com.google.dagger:dagger-compiler", version.ref =
facebook-fresco = { module = "com.facebook.fresco:fresco", version.ref = "frescoVersion" }
glide-compiler = { module = "com.github.bumptech.glide:compiler", version.ref = "glide" }
glide = { module = "com.github.bumptech.glide:glide", version.ref = "glide" }
kotlinx-coroutines-rx2 = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-rx2", version.ref = "kotlinxCoroutinesRx2" }
photoview = { module = "com.github.chrisbanes:PhotoView", version.ref = "photoviewVersion" }
# RxJava and Reactive Programming