mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 12:23:58 +01:00
Move thanks API into main commons codebase (#5463)
* Move thanks API into main commons codebase * KDoc * KDoc --------- Co-authored-by: Nicolas Raoul <nicolas.raoul@gmail.com>
This commit is contained in:
parent
2c086b3d79
commit
ef47b7025e
7 changed files with 61 additions and 54 deletions
|
|
@ -0,0 +1,18 @@
|
|||
package fr.free.nrw.commons.actions
|
||||
|
||||
import org.wikipedia.dataclient.mwapi.MwResponse
|
||||
|
||||
/**
|
||||
* Response of the Thanks API.
|
||||
* Context:
|
||||
* The Commons Android app lets you thank other contributors who have uploaded a great picture.
|
||||
* See https://www.mediawiki.org/wiki/Extension:Thanks
|
||||
*/
|
||||
class MwThankPostResponse : MwResponse() {
|
||||
var result: Result? = null
|
||||
|
||||
inner class Result {
|
||||
var success: Int? = null
|
||||
var recipient: String? = null
|
||||
}
|
||||
}
|
||||
|
|
@ -4,8 +4,6 @@ import fr.free.nrw.commons.CommonsApplication
|
|||
import fr.free.nrw.commons.di.NetworkingModule.NAMED_COMMONS_CSRF
|
||||
import io.reactivex.Observable
|
||||
import org.wikipedia.csrf.CsrfTokenClient
|
||||
import org.wikipedia.dataclient.Service
|
||||
import org.wikipedia.dataclient.mwapi.MwPostResponse
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Named
|
||||
import javax.inject.Singleton
|
||||
|
|
@ -17,7 +15,7 @@ import javax.inject.Singleton
|
|||
@Singleton
|
||||
class ThanksClient @Inject constructor(
|
||||
@param:Named(NAMED_COMMONS_CSRF) private val csrfTokenClient: CsrfTokenClient,
|
||||
@param:Named("commons-service") private val service: Service
|
||||
private val service: ThanksInterface
|
||||
) {
|
||||
/**
|
||||
* Thanks a user for a particular revision
|
||||
|
|
@ -26,11 +24,16 @@ class ThanksClient @Inject constructor(
|
|||
*/
|
||||
fun thank(revisionId: Long): Observable<Boolean> {
|
||||
return try {
|
||||
service.thank(revisionId.toString(), null, csrfTokenClient.tokenBlocking, CommonsApplication.getInstance().userAgent)
|
||||
.map { mwThankPostResponse -> mwThankPostResponse.result.success== 1 }
|
||||
service.thank(
|
||||
revisionId.toString(), // Rev
|
||||
null, // Log
|
||||
csrfTokenClient.tokenBlocking, // Token
|
||||
CommonsApplication.getInstance().userAgent // Source
|
||||
).map {
|
||||
mwThankPostResponse -> mwThankPostResponse.result?.success == 1
|
||||
}
|
||||
} catch (throwable: Throwable) {
|
||||
Observable.just(false)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package fr.free.nrw.commons.actions
|
||||
|
||||
import io.reactivex.Observable
|
||||
import org.wikipedia.dataclient.Service
|
||||
import retrofit2.http.Field
|
||||
import retrofit2.http.FormUrlEncoded
|
||||
import retrofit2.http.POST
|
||||
|
||||
/**
|
||||
* Thanks API.
|
||||
* Context:
|
||||
* The Commons Android app lets you thank another contributor who has uploaded a great picture.
|
||||
* See https://www.mediawiki.org/wiki/Extension:Thanks
|
||||
*/
|
||||
interface ThanksInterface {
|
||||
@FormUrlEncoded
|
||||
@POST(Service.MW_API_PREFIX + "action=thank")
|
||||
fun thank(
|
||||
@Field("rev") rev: String?,
|
||||
@Field("log") log: String?,
|
||||
@Field("token") token: String,
|
||||
@Field("source") source: String?
|
||||
): Observable<MwThankPostResponse?>
|
||||
}
|
||||
|
|
@ -9,6 +9,7 @@ import fr.free.nrw.commons.BetaConstants;
|
|||
import fr.free.nrw.commons.BuildConfig;
|
||||
import fr.free.nrw.commons.actions.PageEditClient;
|
||||
import fr.free.nrw.commons.actions.PageEditInterface;
|
||||
import fr.free.nrw.commons.actions.ThanksInterface;
|
||||
import fr.free.nrw.commons.category.CategoryInterface;
|
||||
import fr.free.nrw.commons.explore.depictions.DepictsClient;
|
||||
import fr.free.nrw.commons.kvstore.JsonKvStore;
|
||||
|
|
@ -256,6 +257,14 @@ public class NetworkingModule {
|
|||
.get(commonsWikiSite, BuildConfig.COMMONS_URL, CategoryInterface.class);
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
public ThanksInterface provideThanksInterface(
|
||||
@Named(NAMED_COMMONS_WIKI_SITE) WikiSite commonsWikiSite) {
|
||||
return ServiceFactory
|
||||
.get(commonsWikiSite, BuildConfig.COMMONS_URL, ThanksInterface.class);
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
public UserInterface provideUserInterface(@Named(NAMED_COMMONS_WIKI_SITE) WikiSite commonsWikiSite) {
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ class ThanksClientTest {
|
|||
@Mock
|
||||
private lateinit var csrfTokenClient: CsrfTokenClient
|
||||
@Mock
|
||||
private lateinit var service: Service
|
||||
private lateinit var service: ThanksInterface
|
||||
|
||||
@Mock
|
||||
private lateinit var commonsApplication: CommonsApplication
|
||||
|
|
|
|||
|
|
@ -11,7 +11,6 @@ import org.wikipedia.dataclient.mwapi.SiteMatrix;
|
|||
import org.wikipedia.dataclient.mwapi.page.MwMobileViewPageLead;
|
||||
import org.wikipedia.dataclient.mwapi.page.MwMobileViewPageRemaining;
|
||||
import org.wikipedia.dataclient.mwapi.page.MwQueryPageSummary;
|
||||
import org.wikipedia.dataclient.mwapi.page.MwThankPostResponse;
|
||||
import org.wikipedia.edit.Edit;
|
||||
import org.wikipedia.edit.preview.EditPreview;
|
||||
import org.wikipedia.login.LoginClient;
|
||||
|
|
@ -191,14 +190,6 @@ public interface Service {
|
|||
@NonNull Observable<MwQueryResponse> getCategoryMembers(@NonNull @Query("cmtitle") String title,
|
||||
@Nullable @Query("cmcontinue") String continueStr);
|
||||
|
||||
@FormUrlEncoded
|
||||
@POST(MW_API_PREFIX + "action=thank")
|
||||
@NonNull Observable<MwThankPostResponse> thank(@Nullable @Field("rev") String rev,
|
||||
@Nullable @Field("log") String log,
|
||||
@NonNull @Field("token") String token,
|
||||
@Nullable @Field("source") String source);
|
||||
|
||||
|
||||
// ------- CSRF, Login, and Create Account -------
|
||||
|
||||
@Headers("Cache-Control: no-cache")
|
||||
|
|
|
|||
|
|
@ -1,38 +0,0 @@
|
|||
package org.wikipedia.dataclient.mwapi.page;
|
||||
|
||||
|
||||
import org.wikipedia.dataclient.mwapi.MwResponse;
|
||||
|
||||
public class MwThankPostResponse extends MwResponse {
|
||||
private Result result;
|
||||
|
||||
public Result getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setResult(Result result) {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public class Result {
|
||||
private Integer success;
|
||||
private String recipient;
|
||||
|
||||
public Integer getSuccess() {
|
||||
return success;
|
||||
}
|
||||
|
||||
public void setSuccess(Integer success) {
|
||||
this.success = success;
|
||||
}
|
||||
|
||||
public String getRecipient() {
|
||||
return recipient;
|
||||
}
|
||||
|
||||
public void setRecipient(String recipient) {
|
||||
this.recipient = recipient;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue