Issue-5662-kotlinstyle (#5833)

* *.kt: bulk correction of formatting using ktlint --format

* *.kt: replace wildcard imports and second stage auto format ktlint --format

* QuizQuestionTest.kt: modified property names to camel case to meet ktlint standard

* LevelControllerTest.kt: modified property names to camel case to meet ktlint standard

* QuizActivityUnitTest.kt: modified property names to camel case to meet ktlint standard

* MediaDetailFragmentUnitTests.kt: modified property names to camel case to meet ktlint standard

* UploadWorker.kt: modified property names to camel case to meet ktlint standard

* UploadClient.kt: modified property names to camel case to meet ktlint standard

* BasePagingPresenter.kt: modified property names to camel case to meet ktlint standard

* DescriptionEditActivity.kt: modified property names to camel case to meet ktlint standard

* OnSwipeTouchListener.kt: modified property names to camel case to meet ktlint standard

* MediaDetailFragmentUnitTests.kt: corrected excessive line length to meet ktlint standard

* DepictedItem.kt: corrected property name format and catch format to for  ktlint standard

* UploadCategoryAdapter.kt: corrected class definition format to meet ktlint standard

* CustomSelectorActivity.kt: reformatted function names to first letter lowercase to meet ktlint standard

* MediaDetailFragmentUnitTests.kt: fix string literal indentation to meet ktlint standard

* NotForUploadDao.kt: file renamed to match class name, new file NotForUploadStatusDao.kt

* UploadedDao.kt: file renamed to match class name, new file UploadedStatusDao.kt

* Urls.kt: fixed excessive line length for ktLint standard

* Snak_partial.kt & Statement_partial.kt: refactored to remove underscores in class names to meet ktLint standard

* *.kt: fixed consecutive KDOC error for ktLint

* PageableBaseDataSourceTest.kt & UploadPresenterTest.kt: fixed excessive line lengths to meet ktLint standard

* CheckboxTriStatesTest.kt: renamed file to match class name to meet ktLint standard

* .kt: resolved backing-property-naming error in ktLint, made matching properties public, matched names and refactored

* TestConnectionFactory.kt: fixed property naming to adhere to ktLint standard
This commit is contained in:
tristan 2024-09-19 14:56:45 +10:00 committed by GitHub
parent 950539c55c
commit 2d82a430c4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
405 changed files with 11032 additions and 9137 deletions

View file

@ -1,138 +1,149 @@
package fr.free.nrw.commons.review
import androidx.annotation.VisibleForTesting
import fr.free.nrw.commons.Media
import fr.free.nrw.commons.media.MediaClient
import fr.free.nrw.commons.wikidata.mwapi.MwQueryPage
import fr.free.nrw.commons.wikidata.mwapi.MwQueryPage.Revision
import fr.free.nrw.commons.wikidata.mwapi.MwQueryResponse
import io.reactivex.Completable
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import org.apache.commons.lang3.StringUtils
import timber.log.Timber
import java.util.Collections
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class ReviewHelper @Inject constructor(
private val mediaClient: MediaClient,
private val reviewInterface: ReviewInterface
) {
@JvmField @Inject var dao: ReviewDao? = null
class ReviewHelper
@Inject
constructor(
private val mediaClient: MediaClient,
private val reviewInterface: ReviewInterface,
) {
@JvmField @Inject
var dao: ReviewDao? = null
/**
* Fetches recent changes from MediaWiki API
* Calls the API to get the latest 50 changes
* When more results are available, the query gets continued beyond this range
*
* @return
*/
private fun getRecentChanges() = reviewInterface.getRecentChanges()
.map { it.query()?.pages() }
.map(MutableList<MwQueryPage>::shuffled)
.flatMapIterable { changes: List<MwQueryPage>? -> changes }
.filter { isChangeReviewable(it) }
/**
* Fetches recent changes from MediaWiki API
* Calls the API to get the latest 50 changes
* When more results are available, the query gets continued beyond this range
*
* @return
*/
private fun getRecentChanges() =
reviewInterface
.getRecentChanges()
.map { it.query()?.pages() }
.map(MutableList<MwQueryPage>::shuffled)
.flatMapIterable { changes: List<MwQueryPage>? -> changes }
.filter { isChangeReviewable(it) }
/**
* Gets a random file change for review. Checks if the image has already been shown to the user
* - Picks a random file from those changes
* - Checks if the file is nominated for deletion
* - Retries upto 5 times for getting a file which is not nominated for deletion
*
* @return Random file change
*/
fun getRandomMedia(): Single<Media> = getRecentChanges()
.flatMapSingle(::getRandomMediaFromRecentChange)
.filter { !it.filename.isNullOrBlank() && !getReviewStatus(it.pageId) }
.firstOrError()
/**
* Gets a random file change for review. Checks if the image has already been shown to the user
* - Picks a random file from those changes
* - Checks if the file is nominated for deletion
* - Retries upto 5 times for getting a file which is not nominated for deletion
*
* @return Random file change
*/
fun getRandomMedia(): Single<Media> =
getRecentChanges()
.flatMapSingle(::getRandomMediaFromRecentChange)
.filter { !it.filename.isNullOrBlank() && !getReviewStatus(it.pageId) }
.firstOrError()
/**
* Returns a proper Media object if the file is not already nominated for deletion
* Else it returns an empty Media object
*
* @param recentChange
* @return
*/
private fun getRandomMediaFromRecentChange(recentChange: MwQueryPage) =
Single.just(recentChange)
.flatMap { mediaClient.checkPageExistsUsingTitle("Commons:Deletion_requests/${it.title()}") }
.flatMap {
if (it) {
Single.error(Exception("${recentChange.title()} is deleted"))
} else {
mediaClient.getMedia(recentChange.title())
/**
* Returns a proper Media object if the file is not already nominated for deletion
* Else it returns an empty Media object
*
* @param recentChange
* @return
*/
private fun getRandomMediaFromRecentChange(recentChange: MwQueryPage) =
Single
.just(recentChange)
.flatMap { mediaClient.checkPageExistsUsingTitle("Commons:Deletion_requests/${it.title()}") }
.flatMap {
if (it) {
Single.error(Exception("${recentChange.title()} is deleted"))
} else {
mediaClient.getMedia(recentChange.title())
}
}
/**
* Checks if the image exists in the reviewed images entity
*
* @param image
* @return
*/
fun getReviewStatus(image: String?): Boolean = dao?.isReviewedAlready(image) ?: false
/**
* Gets the first revision of the file from filename
*
* @param filename
* @return
*/
fun getFirstRevisionOfFile(filename: String?): Observable<Revision> =
reviewInterface
.getFirstRevisionOfFile(filename)
.map {
it
.query()
?.firstPage()
?.revisions()
?.get(0)
}
/**
* Checks Whether Given File is used in any Wiki page or not
* by calling api for given file
*
* @param filename
* @return
*/
fun checkFileUsage(filename: String?): Observable<Boolean> =
reviewInterface
.getGlobalUsageInfo(filename)
.map { it.query()?.firstPage()?.checkWhetherFileIsUsedInWikis() }
/**
* Checks if the change is reviewable or not.
* - checks the type and revisionId of the change
* - checks supported image extensions
*
* @param recentChange
* @return
*/
private fun isChangeReviewable(recentChange: MwQueryPage): Boolean {
for (extension in imageExtensions) {
if (recentChange.title().endsWith(extension)) {
return true
}
}
/**
* Checks if the image exists in the reviewed images entity
*
* @param image
* @return
*/
fun getReviewStatus(image: String?): Boolean =
dao?.isReviewedAlready(image) ?: false
/**
* Gets the first revision of the file from filename
*
* @param filename
* @return
*/
fun getFirstRevisionOfFile(filename: String?): Observable<Revision> =
reviewInterface.getFirstRevisionOfFile(filename)
.map { it.query()?.firstPage()?.revisions()?.get(0) }
/**
* Checks Whether Given File is used in any Wiki page or not
* by calling api for given file
*
* @param filename
* @return
*/
fun checkFileUsage(filename: String?): Observable<Boolean> =
reviewInterface.getGlobalUsageInfo(filename)
.map { it.query()?.firstPage()?.checkWhetherFileIsUsedInWikis() }
/**
* Checks if the change is reviewable or not.
* - checks the type and revisionId of the change
* - checks supported image extensions
*
* @param recentChange
* @return
*/
private fun isChangeReviewable(recentChange: MwQueryPage): Boolean {
for (extension in imageExtensions) {
if (recentChange.title().endsWith(extension)) {
return true
}
return false
}
return false
}
/**
* Adds reviewed/skipped images to the database
*
* @param imageId
*/
fun addViewedImagesToDB(imageId: String?) {
Completable.fromAction { dao!!.insert(ReviewEntity(imageId)) }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
// Inserted successfully
Timber.i("Image inserted successfully.")
}
) { throwable: Throwable? -> Timber.e("Image not inserted into the reviewed images database") }
}
/**
* Adds reviewed/skipped images to the database
*
* @param imageId
*/
fun addViewedImagesToDB(imageId: String?) {
Completable
.fromAction { dao!!.insert(ReviewEntity(imageId)) }
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{
// Inserted successfully
Timber.i("Image inserted successfully.")
},
) { throwable: Throwable? -> Timber.e("Image not inserted into the reviewed images database") }
}
companion object {
private val imageExtensions = arrayOf(".jpg", ".jpeg", ".png")
companion object {
private val imageExtensions = arrayOf(".jpg", ".jpeg", ".png")
}
}
}

View file

@ -18,12 +18,18 @@ interface ReviewInterface {
* the limit is increased from 10 to 50 using gcmlimit
*
*/
@GET("w/api.php?action=query&format=json&formatversion=2&generator=categorymembers&gcmtype=file&gcmsort=timestamp&gcmdir=desc&gcmtitle=Category:Uploaded_with_Mobile/Android&gcmlimit=50")
@GET(
"w/api.php?action=query&format=json&formatversion=2&generator=categorymembers&gcmtype=file&gcmsort=timestamp&gcmdir=desc&gcmtitle=Category:Uploaded_with_Mobile/Android&gcmlimit=50",
)
fun getRecentChanges(): Observable<MwQueryResponse>
@GET("w/api.php?action=query&format=json&formatversion=2&prop=revisions&rvprop=timestamp|ids|user&rvdir=newer&rvlimit=1")
fun getFirstRevisionOfFile(@Query("titles") titles: String?): Observable<MwQueryResponse>
fun getFirstRevisionOfFile(
@Query("titles") titles: String?,
): Observable<MwQueryResponse>
@GET("w/api.php?action=query&format=json&formatversion=2&prop=fileusage|globalusage")
fun getGlobalUsageInfo(@Query("titles") title: String?): Observable<MwQueryResponse>
fun getGlobalUsageInfo(
@Query("titles") title: String?,
): Observable<MwQueryResponse>
}