mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Some correction in the code
This commit is contained in:
parent
058c0482da
commit
e60037300a
3 changed files with 87 additions and 95 deletions
|
|
@ -481,7 +481,7 @@ class UploadWorker(
|
|||
contribution.media.captions,
|
||||
)
|
||||
|
||||
if (revisionID != null) {
|
||||
if (null != revisionID) {
|
||||
withContext(Dispatchers.IO) {
|
||||
// Fetch and update place details
|
||||
val place = placesRepository.fetchPlace(wikiDataPlace.id)
|
||||
|
|
|
|||
|
|
@ -17,84 +17,83 @@ import javax.inject.Singleton
|
|||
*/
|
||||
@Singleton
|
||||
class WikiBaseClient
|
||||
@Inject
|
||||
constructor(
|
||||
private val wikiBaseInterface: WikiBaseInterface,
|
||||
@param:Named(NetworkingModule.NAMED_COMMONS_CSRF) private val csrfTokenClient: CsrfTokenClient,
|
||||
) {
|
||||
fun postEditEntity(
|
||||
fileEntityId: String?,
|
||||
data: String?,
|
||||
): Observable<Boolean> =
|
||||
csrfToken().switchMap { editToken ->
|
||||
wikiBaseInterface
|
||||
.postEditEntity(fileEntityId!!, editToken, data!!)
|
||||
.map { response: MwPostResponse -> response.successVal == 1 }
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes the server call for posting new depicts
|
||||
*
|
||||
* @param filename name of the file
|
||||
* @param data data of the depicts to be uploaded
|
||||
* @return Observable<Boolean>
|
||||
</Boolean> */
|
||||
fun postEditEntityByFilename(
|
||||
filename: String?,
|
||||
data: String?,
|
||||
): Observable<Boolean> =
|
||||
csrfToken().switchMap { editToken ->
|
||||
wikiBaseInterface
|
||||
.postEditEntityByFilename(filename!!, editToken, data!!)
|
||||
.map { response: MwPostResponse -> response.successVal == 1 }
|
||||
}
|
||||
|
||||
fun getClaimIdsByProperty(
|
||||
fileEntityId: String,
|
||||
property: String,
|
||||
): Observable<List<String>> =
|
||||
wikiBaseInterface.getClaimsByProperty(fileEntityId, property).map { claimsResponse ->
|
||||
claimsResponse.claims[property]?.mapNotNull { claim -> claim.id } ?: emptyList()
|
||||
}
|
||||
|
||||
fun postDeleteClaims(
|
||||
entityId: String,
|
||||
data: String?,
|
||||
): Observable<Boolean> =
|
||||
csrfToken().switchMap { editToken ->
|
||||
wikiBaseInterface
|
||||
.postDeleteClaims(editToken, entityId, data!!)
|
||||
.map { response: MwPostResponse -> response.successVal == 1 }
|
||||
}
|
||||
|
||||
fun getFileEntityId(uploadResult: UploadResult): Observable<Long> =
|
||||
@Inject
|
||||
constructor(
|
||||
private val wikiBaseInterface: WikiBaseInterface,
|
||||
@param:Named(NetworkingModule.NAMED_COMMONS_CSRF) private val csrfTokenClient: CsrfTokenClient,
|
||||
) {
|
||||
fun postEditEntity(
|
||||
fileEntityId: String?,
|
||||
data: String?,
|
||||
): Observable<Boolean> =
|
||||
csrfToken().switchMap { editToken ->
|
||||
wikiBaseInterface
|
||||
.getFileEntityId(uploadResult.createCanonicalFileName())
|
||||
.map { response: MwQueryResponse ->
|
||||
response
|
||||
.query()!!
|
||||
.pages()!![0]
|
||||
.pageId()
|
||||
.toLong()
|
||||
}
|
||||
|
||||
fun addLabelsToWikidata(
|
||||
fileEntityId: Long,
|
||||
languageCode: String?,
|
||||
captionValue: String?
|
||||
): Observable<MwPostResponse> {
|
||||
return csrfToken().switchMap { editToken ->
|
||||
wikiBaseInterface.addLabelstoWikidata(
|
||||
PAGE_ID_PREFIX + fileEntityId,
|
||||
editToken,
|
||||
languageCode,
|
||||
captionValue
|
||||
)
|
||||
}
|
||||
.postEditEntity(fileEntityId!!, editToken, data!!)
|
||||
.map { response: MwPostResponse -> response.successVal == 1 }
|
||||
}
|
||||
|
||||
private fun csrfToken(): Observable<String> =
|
||||
Observable.fromCallable {
|
||||
csrfTokenClient.getTokenBlocking()
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Makes the server call for posting new depicts
|
||||
*
|
||||
* @param filename name of the file
|
||||
* @param data data of the depicts to be uploaded
|
||||
* @return Observable<Boolean>
|
||||
</Boolean> */
|
||||
fun postEditEntityByFilename(
|
||||
filename: String?,
|
||||
data: String?,
|
||||
): Observable<Boolean> =
|
||||
csrfToken().switchMap { editToken ->
|
||||
wikiBaseInterface
|
||||
.postEditEntityByFilename(filename!!, editToken, data!!)
|
||||
.map { response: MwPostResponse -> response.successVal == 1 }
|
||||
}
|
||||
|
||||
fun getClaimIdsByProperty(
|
||||
fileEntityId: String,
|
||||
property: String,
|
||||
): Observable<List<String>> =
|
||||
wikiBaseInterface.getClaimsByProperty(fileEntityId, property).map { claimsResponse ->
|
||||
claimsResponse.claims[property]?.mapNotNull { claim -> claim.id } ?: emptyList()
|
||||
}
|
||||
|
||||
fun postDeleteClaims(
|
||||
entityId: String,
|
||||
data: String?,
|
||||
): Observable<Boolean> =
|
||||
csrfToken().switchMap { editToken ->
|
||||
wikiBaseInterface
|
||||
.postDeleteClaims(editToken, entityId, data!!)
|
||||
.map { response: MwPostResponse -> response.successVal == 1 }
|
||||
}
|
||||
|
||||
fun getFileEntityId(uploadResult: UploadResult): Observable<Long> =
|
||||
wikiBaseInterface
|
||||
.getFileEntityId(uploadResult.createCanonicalFileName())
|
||||
.map { response: MwQueryResponse ->
|
||||
response
|
||||
.query()!!
|
||||
.pages()!![0]
|
||||
.pageId()
|
||||
.toLong()
|
||||
}
|
||||
|
||||
fun addLabelsToWikidata(
|
||||
fileEntityId: Long,
|
||||
languageCode: String?,
|
||||
captionValue: String?,
|
||||
): Observable<MwPostResponse> =
|
||||
csrfToken().switchMap { editToken ->
|
||||
wikiBaseInterface.addLabelstoWikidata(
|
||||
PAGE_ID_PREFIX + fileEntityId,
|
||||
editToken,
|
||||
languageCode,
|
||||
captionValue,
|
||||
)
|
||||
}
|
||||
|
||||
private fun csrfToken(): Observable<String> =
|
||||
Observable.fromCallable {
|
||||
csrfTokenClient.getTokenBlocking()
|
||||
}
|
||||
}
|
||||
|
|
@ -37,6 +37,7 @@ import javax.inject.Inject
|
|||
import javax.inject.Named
|
||||
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
|
||||
|
|
@ -54,7 +55,6 @@ class WikidataEditService @Inject constructor(
|
|||
fileEntityId: String,
|
||||
depictedItems: List<String>
|
||||
): Observable<Boolean> {
|
||||
|
||||
val data = EditClaim.from(
|
||||
if (isBetaFlavour) listOf("Q10") else depictedItems, DEPICTS.propertyName
|
||||
)
|
||||
|
|
@ -79,7 +79,6 @@ class WikidataEditService @Inject constructor(
|
|||
fileEntityId: String?,
|
||||
depictedItems: List<String>
|
||||
): Observable<Boolean> {
|
||||
|
||||
val entityId: String = PAGE_ID_PREFIX + fileEntityId
|
||||
val claimIds = getDepictionsClaimIds(entityId)
|
||||
|
||||
|
|
@ -164,8 +163,7 @@ class WikidataEditService @Inject constructor(
|
|||
)
|
||||
return null
|
||||
}
|
||||
val result = addImageAndMediaLegends(wikidataPlace!!, fileName, captions)
|
||||
return result
|
||||
return addImageAndMediaLegends(wikidataPlace!!, fileName, captions)
|
||||
}
|
||||
|
||||
fun addImageAndMediaLegends(
|
||||
|
|
@ -197,8 +195,7 @@ class WikidataEditService @Inject constructor(
|
|||
), Arrays.asList(MEDIA_LEGENDS.propertyName)
|
||||
)
|
||||
|
||||
val result = wikidataClient.setClaim(claim, COMMONS_APP_TAG).blockingSingle()
|
||||
return result
|
||||
return wikidataClient.setClaim(claim, COMMONS_APP_TAG).blockingSingle()
|
||||
}
|
||||
|
||||
fun handleImageClaimResult(wikidataItem: WikidataItem, revisionId: Long?) {
|
||||
|
|
@ -242,22 +239,18 @@ class WikidataEditService @Inject constructor(
|
|||
|
||||
@SuppressLint("NewApi")
|
||||
private fun captionEdits(contribution: Contribution, fileEntityId: Long): Observable<Boolean> {
|
||||
val result = Observable.fromIterable(contribution.media.captions.entries)
|
||||
return Observable.fromIterable(contribution.media.captions.entries)
|
||||
.concatMap { addCaption(fileEntityId, it.key, it.value) }
|
||||
return result
|
||||
}
|
||||
|
||||
private fun depictionEdits(
|
||||
contribution: Contribution,
|
||||
fileEntityId: Long
|
||||
): Observable<Boolean> {
|
||||
val result = addDepictsProperty(fileEntityId.toString(), buildList {
|
||||
for ((_, _, _, _, _, _, id) in contribution.depictedItems) {
|
||||
add(id)
|
||||
}
|
||||
})
|
||||
return result
|
||||
}
|
||||
): Observable<Boolean> = addDepictsProperty(fileEntityId.toString(), buildList {
|
||||
for ((_, _, _, _, _, _, id) in contribution.depictedItems) {
|
||||
add(id)
|
||||
}
|
||||
})
|
||||
|
||||
companion object {
|
||||
const val COMMONS_APP_TAG: String = "wikimedia-commons-app"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue