Convert API clients to kotlin (#5567)

* Convert UserClient to kotlin

* Added tests for WikiBaseClient

* Removed superfluous dao tests in review helper and got the proper ReviewDaoTest running in the unit test suite

* Improved tests for ReviewHelper

* Convert the ReviewHelper to kotlin

* Convert the WikiBaseClient to kotlin

* Convert the WikidataClient to kotlin
This commit is contained in:
Paul Hawke 2024-02-19 18:23:11 -06:00 committed by GitHub
parent c43405267a
commit 728712c4e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 496 additions and 526 deletions

View file

@ -1,46 +0,0 @@
package fr.free.nrw.commons.mwapi;
import fr.free.nrw.commons.wikidata.mwapi.MwQueryResponse;
import fr.free.nrw.commons.wikidata.mwapi.MwQueryResult;
import fr.free.nrw.commons.wikidata.mwapi.UserInfo;
import fr.free.nrw.commons.utils.DateUtil;
import java.util.Collections;
import java.util.Date;
import javax.inject.Inject;
import io.reactivex.Observable;
import io.reactivex.Single;
public class UserClient {
private final UserInterface userInterface;
@Inject
public UserClient(UserInterface userInterface) {
this.userInterface = userInterface;
}
/**
* Checks to see if a user is currently blocked from Commons
*
* @return whether or not the user is blocked from Commons
*/
public Single<Boolean> isUserBlockedFromCommons() {
return userInterface.getUserBlockInfo()
.map(MwQueryResponse::query)
.map(MwQueryResult::userInfo)
.map(UserInfo::blockexpiry)
.map(blockExpiry -> {
if (blockExpiry.isEmpty())
return false;
else if ("infinite".equals(blockExpiry))
return true;
else {
Date endDate = DateUtil.iso8601DateParse(blockExpiry);
Date current = new Date();
return endDate.after(current);
}
}).single(false);
}
}

View file

@ -0,0 +1,36 @@
package fr.free.nrw.commons.mwapi
import fr.free.nrw.commons.utils.DateUtil
import fr.free.nrw.commons.wikidata.mwapi.MwQueryResponse
import fr.free.nrw.commons.wikidata.mwapi.MwQueryResult
import fr.free.nrw.commons.wikidata.mwapi.UserInfo
import io.reactivex.Single
import java.text.ParseException
import java.util.Date
import javax.inject.Inject
class UserClient @Inject constructor(private val userInterface: UserInterface) {
/**
* Checks to see if a user is currently blocked from Commons
*
* @return whether or not the user is blocked from Commons
*/
fun isUserBlockedFromCommons(): Single<Boolean> =
userInterface.getUserBlockInfo()
.map(::processBlockExpiry)
.single(false)
@Throws(ParseException::class)
private fun processBlockExpiry(response: MwQueryResponse): Boolean {
val blockExpiry = response.query()?.userInfo()?.blockexpiry()
return when {
blockExpiry.isNullOrEmpty() -> false
"infinite" == blockExpiry -> true
else -> {
val endDate = DateUtil.iso8601DateParse(blockExpiry)
val current = Date()
endDate.after(current)
}
}
}
}