Description and thumbnail is showing for the categories that are associated with Wikidata (#4803)

* Depiction.commons categories showing with description and thumbnail

* Test added

* Test added
This commit is contained in:
Ayan Sarkar 2022-02-14 16:15:59 +05:30 committed by GitHub
parent ecc6b1bf28
commit 15fa93e8d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 95 additions and 3 deletions

View file

@ -5,6 +5,7 @@ import fr.free.nrw.commons.upload.GpsCategoryModel
import fr.free.nrw.commons.upload.structure.depictions.DepictedItem
import fr.free.nrw.commons.utils.StringSortingUtils
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.functions.Function4
import timber.log.Timber
import java.util.*
@ -96,8 +97,27 @@ class CategoriesModel @Inject constructor(
.toObservable()
}
private fun categoriesFromDepiction(selectedDepictions: List<DepictedItem>) =
Observable.just(selectedDepictions.map { it.commonsCategories }.flatten())
/**
* Fetches details of every category associated with selected depictions, converts them into
* CategoryItem and returns them in a list.
*
* @param selectedDepictions selected DepictItems
* @return List of CategoryItem associated with selected depictions
*/
private fun categoriesFromDepiction(selectedDepictions: List<DepictedItem>):
Observable<MutableList<CategoryItem>>? {
return Observable.fromIterable(
selectedDepictions.map { it.commonsCategories }.flatten())
.map { categoryItem ->
categoryClient.getCategoriesByName(categoryItem.name,
categoryItem.name, SEARCH_CATS_LIMIT).map {
CategoryItem(it[0].name, it[0].description,
it[0].thumbnail, it[0].isSelected)
}.blockingGet()
}.toList().toObservable()
}
private fun combine(
depictionCategories: List<CategoryItem>,

View file

@ -48,6 +48,24 @@ class CategoryClient @Inject constructor(private val categoryInterface: Category
)
}
/**
* Fetches categories starting and ending with a specified name.
*
* @param startingCategoryName Name of the category to start
* @param endingCategoryName Name of the category to end
* @param itemLimit How many categories to return
* @param offset offset
* @return MwQueryResponse
*/
@JvmOverloads
fun getCategoriesByName(startingCategoryName: String?, endingCategoryName: String?,
itemLimit: Int, offset: Int = 0): Single<List<CategoryItem>> {
return responseMapper(
categoryInterface.getCategoriesByName(startingCategoryName, endingCategoryName,
itemLimit, offset)
)
}
/**
* The method takes categoryName as input and returns a List of Subcategories
* It uses the generator query API to get the subcategories in a category, 500 at a time.

View file

@ -40,6 +40,23 @@ public interface CategoryInterface {
@Query("gaclimit") int itemLimit,
@Query("gacoffset") int offset);
/**
* Fetches categories starting and ending with a specified name.
*
* @param startingCategory Name of the category to start
* @param endingCategory Name of the category to end
* @param itemLimit How many categories to return
* @param offset offset
* @return MwQueryResponse
*/
@GET("w/api.php?action=query&format=json&formatversion=2"
+ "&generator=allcategories&prop=categoryinfo|description|pageimages&piprop=thumbnail"
+ "&pithumbsize=70")
Single<MwQueryResponse> getCategoriesByName(@Query("gacfrom") String startingCategory,
@Query("gacto") String endingCategory,
@Query("gaclimit") int itemLimit,
@Query("gacoffset") int offset);
@GET("w/api.php?action=query&format=json&formatversion=2"
+ "&generator=categorymembers&gcmtype=subcat"
+ "&prop=info&gcmlimit=50")

View file

@ -84,13 +84,16 @@ class CategoriesModelTest {
"titleSearch", "", "", false))))
whenever(categoryDao.recentCategories(25)).thenReturn(listOf(CategoryItem(
"recentCategories","","", false)))
whenever(categoryClient.getCategoriesByName("depictionCategory",
"depictionCategory", 25)).thenReturn(Single.just(listOf(CategoryItem(
"commonsCategories","","", false))))
val imageTitleList = listOf("Test")
CategoriesModel(categoryClient, categoryDao, gpsCategoryModel)
.searchAll("", imageTitleList, listOf(depictedItem))
.test()
.assertValue(
listOf(
categoryItem("depictionCategory"),
categoryItem("commonsCategories"),
categoryItem("gpsCategory"),
categoryItem("titleSearch"),
categoryItem("recentCategories")

View file

@ -77,6 +77,40 @@ class CategoryClientTest {
.test()
.assertValues(emptyList())
}
@Test
fun getCategoriesByNameFound() {
val mockResponse = withMockResponse("Category:Test")
whenever(categoryInterface.getCategoriesByName(anyString(), anyString(),
anyInt(), anyInt()))
.thenReturn(Single.just(mockResponse))
categoryClient.getCategoriesByName("tes", "tes", 10)
.test()
.assertValues(listOf(CategoryItem("Test", "",
"", false)))
categoryClient.getCategoriesByName("tes" , "tes",
10, 10)
.test()
.assertValues(listOf(CategoryItem("Test", "",
"", false)))
}
@Test
fun getCategoriesByNameNull() {
val mockResponse = withNullPages()
whenever(categoryInterface.getCategoriesByName(anyString(), anyString(),
anyInt(), anyInt()))
.thenReturn(Single.just(mockResponse))
categoryClient.getCategoriesByName("tes", "tes",
10)
.test()
.assertValues(emptyList())
categoryClient.getCategoriesByName("tes", "tes",
10, 10)
.test()
.assertValues(emptyList())
}
@Test
fun getParentCategoryListFound() {
val mockResponse = withMockResponse("Category:Test")