change the ReviewHelper for api called

This commit is contained in:
24-S2-2-C-Interactive 2024-10-26 06:04:15 +11:00
parent 66f917b189
commit 55e59b8ec7

View file

@ -23,13 +23,17 @@ class ReviewHelper
@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
*/
/**
* Gets multiple random media items for review.
* - Fetches recent changes and filters them
* - Checks if files are nominated for deletion
* - Filters out already reviewed images
*
* @param count Number of media items to fetch
* @return Observable of Media items
*/
private fun getRecentChanges() =
reviewInterface
.getRecentChanges()
@ -38,19 +42,40 @@ class ReviewHelper
.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 multiple random media items for review.
* - Fetches recent changes and filters them
* - Checks if files are nominated for deletion
* - Filters out already reviewed images
*
* @param count Number of media items to fetch
* @return Observable of Media items
*/
fun getRandomMediaBatch(count: Int): Observable<Media> =
getRecentChanges()
.flatMapSingle(::getRandomMediaFromRecentChange)
.filter { media ->
!media.filename.isNullOrBlank() &&
!getReviewStatus(media.pageId)
}
.take(count.toLong())
.onErrorResumeNext { error: Throwable ->
Timber.e(error, "Error getting random media batch")
Observable.empty()
}
/**
* Gets a random file change for review.
*
* @return Random file change
*/
fun getRandomMedia(): Single<Media> =
getRandomMediaBatch(1)
.firstOrError()
/**
* Returns a proper Media object if the file is not already nominated for deletion
@ -108,6 +133,20 @@ class ReviewHelper
.getGlobalUsageInfo(filename)
.map { it.query()?.firstPage()?.checkWhetherFileIsUsedInWikis() }
fun checkFileUsageBatch(filenames: List<String>): Observable<Pair<String, Boolean>> =
Observable.fromIterable(filenames)
.flatMap { filename ->
checkFileUsage(filename)
.map { isUsed -> Pair(filename, isUsed) }
}
.onErrorResumeNext { error: Throwable ->
Timber.e(error, "Error checking file usage batch")
Observable.empty()
}
/**
* Checks if the change is reviewable or not.
* - checks the type and revisionId of the change
@ -145,5 +184,6 @@ class ReviewHelper
companion object {
private val imageExtensions = arrayOf(".jpg", ".jpeg", ".png")
private const val MAX_RETRIES = 3
}
}