Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move thanks API into main commons codebase #5463

Merged
merged 3 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
}
}
15 changes: 9 additions & 6 deletions app/src/main/java/fr/free/nrw/commons/actions/ThanksClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
}
}

}
24 changes: 24 additions & 0 deletions app/src/main/java/fr/free/nrw/commons/actions/ThanksInterface.kt
Original file line number Diff line number Diff line change
@@ -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?>
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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;
Expand Down Expand Up @@ -256,6 +257,14 @@ public CategoryInterface provideCategoryInterface(
.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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
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;
Expand Down Expand Up @@ -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")
Expand Down

This file was deleted.