This commit is contained in:
Sujal-Gupta-SG 2025-01-25 22:03:44 +05:30
parent c0a93b6fc2
commit b2092fa751

View file

@ -37,11 +37,6 @@ import javax.inject.Inject
import javax.inject.Named import javax.inject.Named
import javax.inject.Singleton import javax.inject.Singleton
/**
* This class is meant to handle the Wikidata edits made through the app It will talk with MediaWiki
* Apis to make the necessary calls, log the edits and fire listeners on successful edits
*/
@Singleton @Singleton
class WikidataEditService @Inject constructor( class WikidataEditService @Inject constructor(
private val context: Context, private val context: Context,
@ -55,6 +50,7 @@ class WikidataEditService @Inject constructor(
fileEntityId: String, fileEntityId: String,
depictedItems: List<String> depictedItems: List<String>
): Observable<Boolean> { ): Observable<Boolean> {
Timber.d("Entering addDepictsProperty with fileEntityId: %s and depictedItems: %s", fileEntityId, depictedItems)
val data = EditClaim.from( val data = EditClaim.from(
if (isBetaFlavour) listOf("Q10") else depictedItems, DEPICTS.propertyName if (isBetaFlavour) listOf("Q10") else depictedItems, DEPICTS.propertyName
) )
@ -72,6 +68,7 @@ class WikidataEditService @Inject constructor(
showLongToast(context, throwable.toString()) showLongToast(context, throwable.toString())
} }
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.doOnComplete { Timber.d("Exiting addDepictsProperty") }
} }
@SuppressLint("CheckResult") @SuppressLint("CheckResult")
@ -79,10 +76,10 @@ class WikidataEditService @Inject constructor(
fileEntityId: String?, fileEntityId: String?,
depictedItems: List<String> depictedItems: List<String>
): Observable<Boolean> { ): Observable<Boolean> {
Timber.d("Entering updateDepictsProperty with fileEntityId: %s and depictedItems: %s", fileEntityId, depictedItems)
val entityId: String = PAGE_ID_PREFIX + fileEntityId val entityId: String = PAGE_ID_PREFIX + fileEntityId
val claimIds = getDepictionsClaimIds(entityId) val claimIds = getDepictionsClaimIds(entityId)
/* Please consider removeClaim scenario for BetaDebug */
val data = RemoveClaim.from(if (isBetaFlavour) listOf("Q10") else claimIds) val data = RemoveClaim.from(if (isBetaFlavour) listOf("Q10") else claimIds)
return wikiBaseClient.postDeleteClaims(entityId, gson.toJson(data)) return wikiBaseClient.postDeleteClaims(entityId, gson.toJson(data))
@ -104,20 +101,26 @@ class WikidataEditService @Inject constructor(
return@switchMap Observable.empty<Boolean>() return@switchMap Observable.empty<Boolean>()
} }
} }
.doOnComplete { Timber.d("Exiting updateDepictsProperty") }
} }
@SuppressLint("CheckResult") @SuppressLint("CheckResult")
private fun getDepictionsClaimIds(entityId: String): List<String> { private fun getDepictionsClaimIds(entityId: String): List<String> {
return wikiBaseClient.getClaimIdsByProperty(entityId, DEPICTS.propertyName) Timber.d("Entering getDepictionsClaimIds with entityId: %s", entityId)
val claimIds = wikiBaseClient.getClaimIdsByProperty(entityId, DEPICTS.propertyName)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.blockingFirst() .blockingFirst()
Timber.d("Exiting getDepictionsClaimIds with claimIds: %s", claimIds)
return claimIds
} }
@SuppressLint("StringFormatInvalid") @SuppressLint("StringFormatInvalid")
private fun showSuccessToast(wikiItemName: String) { private fun showSuccessToast(wikiItemName: String) {
Timber.d("Entering showSuccessToast with wikiItemName: %s", wikiItemName)
val successStringTemplate = context.getString(R.string.successful_wikidata_edit) val successStringTemplate = context.getString(R.string.successful_wikidata_edit)
val successMessage = String.format(Locale.getDefault(), successStringTemplate, wikiItemName) val successMessage = String.format(Locale.getDefault(), successStringTemplate, wikiItemName)
showLongToast(context, successMessage) showLongToast(context, successMessage)
Timber.d("Exiting showSuccessToast")
} }
@RequiresApi(Build.VERSION_CODES.N) @RequiresApi(Build.VERSION_CODES.N)
@ -126,6 +129,7 @@ class WikidataEditService @Inject constructor(
fileEntityId: Long, languageCode: String, fileEntityId: Long, languageCode: String,
captionValue: String captionValue: String
): Observable<Boolean> { ): Observable<Boolean> {
Timber.d("Entering addCaption with fileEntityId: %s, languageCode: %s, captionValue: %s", fileEntityId, languageCode, captionValue)
return wikiBaseClient.addLabelsToWikidata(fileEntityId, languageCode, captionValue) return wikiBaseClient.addLabelsToWikidata(fileEntityId, languageCode, captionValue)
.doOnNext { mwPostResponse: MwPostResponse? -> .doOnNext { mwPostResponse: MwPostResponse? ->
onAddCaptionResponse( onAddCaptionResponse(
@ -141,33 +145,40 @@ class WikidataEditService @Inject constructor(
) )
} }
.map(Objects::nonNull) .map(Objects::nonNull)
.doOnComplete { Timber.d("Exiting addCaption") }
} }
private fun onAddCaptionResponse(fileEntityId: Long, response: MwPostResponse?) { private fun onAddCaptionResponse(fileEntityId: Long, response: MwPostResponse?) {
Timber.d("Entering onAddCaptionResponse with fileEntityId: %s and response: %s", fileEntityId, response)
if (response != null) { if (response != null) {
Timber.d("Caption successfully set, revision id = %s", response) Timber.d("Caption successfully set, revision id = %s", response)
} else { } else {
Timber.d("Error occurred while setting Captions, fileEntityId = %s", fileEntityId) Timber.d("Error occurred while setting Captions, fileEntityId = %s", fileEntityId)
} }
Timber.d("Exiting onAddCaptionResponse")
} }
fun createClaim( fun createClaim(
wikidataPlace: WikidataPlace?, fileName: String, wikidataPlace: WikidataPlace?, fileName: String,
captions: Map<String, String> captions: Map<String, String>
): Long? { ): Long? {
Timber.d("Entering createClaim with wikidataPlace: %s, fileName: %s, captions: %s", wikidataPlace, fileName, captions)
if (!(directKvStore.getBoolean("Picture_Has_Correct_Location", true))) { if (!(directKvStore.getBoolean("Picture_Has_Correct_Location", true))) {
Timber.d( Timber.d(
"Image location and nearby place location mismatched, so Wikidata item won't be edited" "Image location and nearby place location mismatched, so Wikidata item won't be edited"
) )
return null return null
} }
return addImageAndMediaLegends(wikidataPlace!!, fileName, captions) val result = addImageAndMediaLegends(wikidataPlace!!, fileName, captions)
Timber.d("Exiting createClaim with result: %s", result)
return result
} }
fun addImageAndMediaLegends( fun addImageAndMediaLegends(
wikidataItem: WikidataItem, fileName: String, wikidataItem: WikidataItem, fileName: String,
captions: Map<String, String> captions: Map<String, String>
): Long { ): Long {
Timber.d("Entering addImageAndMediaLegends with wikidataItem: %s, fileName: %s, captions: %s", wikidataItem, fileName, captions)
val p18 = SnakPartial( val p18 = SnakPartial(
"value", "value",
IMAGE.propertyName, IMAGE.propertyName,
@ -193,10 +204,13 @@ class WikidataEditService @Inject constructor(
), Arrays.asList(MEDIA_LEGENDS.propertyName) ), Arrays.asList(MEDIA_LEGENDS.propertyName)
) )
return wikidataClient.setClaim(claim, COMMONS_APP_TAG).blockingSingle() val result = wikidataClient.setClaim(claim, COMMONS_APP_TAG).blockingSingle()
Timber.d("Exiting addImageAndMediaLegends with result: %s", result)
return result
} }
fun handleImageClaimResult(wikidataItem: WikidataItem, revisionId: Long?) { fun handleImageClaimResult(wikidataItem: WikidataItem, revisionId: Long?) {
Timber.d("Entering handleImageClaimResult with wikidataItem: %s and revisionId: %s", wikidataItem, revisionId)
if (revisionId != null) { if (revisionId != null) {
wikidataEditListener?.onSuccessfulWikidataEdit() wikidataEditListener?.onSuccessfulWikidataEdit()
showSuccessToast(wikidataItem.name) showSuccessToast(wikidataItem.name)
@ -204,12 +218,14 @@ class WikidataEditService @Inject constructor(
Timber.d("Unable to make wiki data edit for entity %s", wikidataItem) Timber.d("Unable to make wiki data edit for entity %s", wikidataItem)
showLongToast(context, context.getString(R.string.wikidata_edit_failure)) showLongToast(context, context.getString(R.string.wikidata_edit_failure))
} }
Timber.d("Exiting handleImageClaimResult")
} }
fun addDepictionsAndCaptions( fun addDepictionsAndCaptions(
uploadResult: UploadResult, uploadResult: UploadResult,
contribution: Contribution contribution: Contribution
): Observable<Boolean> { ): Observable<Boolean> {
Timber.d("Entering addDepictionsAndCaptions with uploadResult: %s and contribution: %s", uploadResult, contribution)
return wikiBaseClient.getFileEntityId(uploadResult) return wikiBaseClient.getFileEntityId(uploadResult)
.doOnError { throwable: Throwable? -> .doOnError { throwable: Throwable? ->
Timber.e( Timber.e(
@ -233,25 +249,33 @@ class WikidataEditService @Inject constructor(
return@switchMap Observable.empty<Boolean>() return@switchMap Observable.empty<Boolean>()
} }
} }
.doOnComplete { Timber.d("Exiting addDepictionsAndCaptions") }
} }
@SuppressLint("NewApi") @SuppressLint("NewApi")
private fun captionEdits(contribution: Contribution, fileEntityId: Long): Observable<Boolean> { private fun captionEdits(contribution: Contribution, fileEntityId: Long): Observable<Boolean> {
return Observable.fromIterable(contribution.media.captions.entries) Timber.d("Entering captionEdits with contribution: %s and fileEntityId: %s", contribution, fileEntityId)
val result = Observable.fromIterable(contribution.media.captions.entries)
.concatMap { addCaption(fileEntityId, it.key, it.value) } .concatMap { addCaption(fileEntityId, it.key, it.value) }
Timber.d("Exiting captionEdits")
return result
} }
private fun depictionEdits( private fun depictionEdits(
contribution: Contribution, contribution: Contribution,
fileEntityId: Long fileEntityId: Long
): Observable<Boolean> = addDepictsProperty(fileEntityId.toString(), buildList { ): Observable<Boolean> {
for ((_, _, _, _, _, _, id) in contribution.depictedItems) { Timber.d("Entering depictionEdits with contribution: %s and fileEntityId: %s", contribution, fileEntityId)
add(id) val result = addDepictsProperty(fileEntityId.toString(), buildList {
} for ((_, _, _, _, _, _, id) in contribution.depictedItems) {
}) add(id)
}
})
Timber.d("Exiting depictionEdits")
return result
}
companion object { companion object {
const val COMMONS_APP_TAG: String = "wikimedia-commons-app" const val COMMONS_APP_TAG: String = "wikimedia-commons-app"
} }
} }