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 // Kotlin + coroutines
implementation(libs.androidx.work.runtime.ktx) implementation(libs.androidx.work.runtime.ktx)
implementation(libs.androidx.work.runtime) implementation(libs.androidx.work.runtime)
implementation(libs.kotlinx.coroutines.rx2)
testImplementation(libs.androidx.work.testing) testImplementation(libs.androidx.work.testing)
//Glide //Glide

View file

@ -2,22 +2,19 @@ package fr.free.nrw.commons.delete
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Context 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.Media
import fr.free.nrw.commons.R 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.auth.SessionManager
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient
import fr.free.nrw.commons.utils.DateUtil
import fr.free.nrw.commons.utils.ViewUtilWrapper import fr.free.nrw.commons.utils.ViewUtilWrapper
import io.reactivex.Single import io.reactivex.Single
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.rx2.rxSingle
import timber.log.Timber 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 * This class handles the reason for deleting a Media object
@ -30,6 +27,8 @@ class ReasonBuilder @Inject constructor(
private val viewUtilWrapper: ViewUtilWrapper private val viewUtilWrapper: ViewUtilWrapper
) { ) {
private val defaultFileUsagePageSize = 10
/** /**
* To process the reason and append the media's upload date and uploaded_by_me string * To process the reason and append the media's upload date and uploaded_by_me string
* @param media * @param media
@ -41,7 +40,7 @@ class ReasonBuilder @Inject constructor(
return Single.just("Not known") return Single.just("Not known")
} }
Timber.d("Fetching article number") 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> { private fun getAndAppendFileUsage(media: Media, reason: String): Single<String> {
return if (checkAccount()) { return rxSingle(context = Dispatchers.IO) {
Timber.d("Fetching achievements for ${sessionManager.userName}") if (!checkAccount()) return@rxSingle ""
okHttpJsonApiClient
.getAchievements(sessionManager.userName) try {
.map { feedbackResponse -> val globalFileUsage = okHttpJsonApiClient.getGlobalFileUsages(
Timber.d("Feedback Response: $feedbackResponse") fileName = media.filename,
appendArticlesUsed(feedbackResponse, media, reason) pageSize = defaultFileUsagePageSize
} )
} else { val globalUsages = globalFileUsage?.query?.pages?.sumOf { it.fileUsage.size } ?: 0
Single.just("")
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 * 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 media whose upload data is to be fetched
* @param reason * @param reason string to be appended
*/ */
@SuppressLint("StringFormatInvalid") @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) 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) } .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_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="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 <string name="no_uploads">Welcome to Commons!\n
Upload your first media by tapping on the add button.</string> Upload your first media by tapping on the add button.</string>

View file

@ -16,6 +16,7 @@ constraintlayout = "1.1.3"
coordinates2country = "1.8" coordinates2country = "1.8"
dexcount = "4.0.0" dexcount = "4.0.0"
githubTripletPlay = "2.7.2" githubTripletPlay = "2.7.2"
kotlinxCoroutinesRx2 = "1.8.0"
osmdroidAndroid = "6.1.17" osmdroidAndroid = "6.1.17"
testCore = "1.4.0" testCore = "1.4.0"
coreKtx = "1.9.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" } facebook-fresco = { module = "com.facebook.fresco:fresco", version.ref = "frescoVersion" }
glide-compiler = { module = "com.github.bumptech.glide:compiler", version.ref = "glide" } glide-compiler = { module = "com.github.bumptech.glide:compiler", version.ref = "glide" }
glide = { module = "com.github.bumptech.glide:glide", 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" } photoview = { module = "com.github.chrisbanes:PhotoView", version.ref = "photoviewVersion" }
# RxJava and Reactive Programming # RxJava and Reactive Programming