Skip to content

Commit ef47b70

Browse files
pshnicolas-raoul
andauthored
Move thanks API into main commons codebase (commons-app#5463)
* Move thanks API into main commons codebase * KDoc * KDoc --------- Co-authored-by: Nicolas Raoul <nicolas.raoul@gmail.com>
1 parent 2c086b3 commit ef47b70

File tree

7 files changed

+61
-54
lines changed

7 files changed

+61
-54
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package fr.free.nrw.commons.actions
2+
3+
import org.wikipedia.dataclient.mwapi.MwResponse
4+
5+
/**
6+
* Response of the Thanks API.
7+
* Context:
8+
* The Commons Android app lets you thank other contributors who have uploaded a great picture.
9+
* See https://www.mediawiki.org/wiki/Extension:Thanks
10+
*/
11+
class MwThankPostResponse : MwResponse() {
12+
var result: Result? = null
13+
14+
inner class Result {
15+
var success: Int? = null
16+
var recipient: String? = null
17+
}
18+
}

app/src/main/java/fr/free/nrw/commons/actions/ThanksClient.kt

+9-6
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import fr.free.nrw.commons.CommonsApplication
44
import fr.free.nrw.commons.di.NetworkingModule.NAMED_COMMONS_CSRF
55
import io.reactivex.Observable
66
import org.wikipedia.csrf.CsrfTokenClient
7-
import org.wikipedia.dataclient.Service
8-
import org.wikipedia.dataclient.mwapi.MwPostResponse
97
import javax.inject.Inject
108
import javax.inject.Named
119
import javax.inject.Singleton
@@ -17,7 +15,7 @@ import javax.inject.Singleton
1715
@Singleton
1816
class ThanksClient @Inject constructor(
1917
@param:Named(NAMED_COMMONS_CSRF) private val csrfTokenClient: CsrfTokenClient,
20-
@param:Named("commons-service") private val service: Service
18+
private val service: ThanksInterface
2119
) {
2220
/**
2321
* Thanks a user for a particular revision
@@ -26,11 +24,16 @@ class ThanksClient @Inject constructor(
2624
*/
2725
fun thank(revisionId: Long): Observable<Boolean> {
2826
return try {
29-
service.thank(revisionId.toString(), null, csrfTokenClient.tokenBlocking, CommonsApplication.getInstance().userAgent)
30-
.map { mwThankPostResponse -> mwThankPostResponse.result.success== 1 }
27+
service.thank(
28+
revisionId.toString(), // Rev
29+
null, // Log
30+
csrfTokenClient.tokenBlocking, // Token
31+
CommonsApplication.getInstance().userAgent // Source
32+
).map {
33+
mwThankPostResponse -> mwThankPostResponse.result?.success == 1
34+
}
3135
} catch (throwable: Throwable) {
3236
Observable.just(false)
3337
}
3438
}
35-
3639
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package fr.free.nrw.commons.actions
2+
3+
import io.reactivex.Observable
4+
import org.wikipedia.dataclient.Service
5+
import retrofit2.http.Field
6+
import retrofit2.http.FormUrlEncoded
7+
import retrofit2.http.POST
8+
9+
/**
10+
* Thanks API.
11+
* Context:
12+
* The Commons Android app lets you thank another contributor who has uploaded a great picture.
13+
* See https://www.mediawiki.org/wiki/Extension:Thanks
14+
*/
15+
interface ThanksInterface {
16+
@FormUrlEncoded
17+
@POST(Service.MW_API_PREFIX + "action=thank")
18+
fun thank(
19+
@Field("rev") rev: String?,
20+
@Field("log") log: String?,
21+
@Field("token") token: String,
22+
@Field("source") source: String?
23+
): Observable<MwThankPostResponse?>
24+
}

app/src/main/java/fr/free/nrw/commons/di/NetworkingModule.java

+9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import fr.free.nrw.commons.BuildConfig;
1010
import fr.free.nrw.commons.actions.PageEditClient;
1111
import fr.free.nrw.commons.actions.PageEditInterface;
12+
import fr.free.nrw.commons.actions.ThanksInterface;
1213
import fr.free.nrw.commons.category.CategoryInterface;
1314
import fr.free.nrw.commons.explore.depictions.DepictsClient;
1415
import fr.free.nrw.commons.kvstore.JsonKvStore;
@@ -256,6 +257,14 @@ public CategoryInterface provideCategoryInterface(
256257
.get(commonsWikiSite, BuildConfig.COMMONS_URL, CategoryInterface.class);
257258
}
258259

260+
@Provides
261+
@Singleton
262+
public ThanksInterface provideThanksInterface(
263+
@Named(NAMED_COMMONS_WIKI_SITE) WikiSite commonsWikiSite) {
264+
return ServiceFactory
265+
.get(commonsWikiSite, BuildConfig.COMMONS_URL, ThanksInterface.class);
266+
}
267+
259268
@Provides
260269
@Singleton
261270
public UserInterface provideUserInterface(@Named(NAMED_COMMONS_WIKI_SITE) WikiSite commonsWikiSite) {

app/src/test/kotlin/fr/free/nrw/commons/actions/ThanksClientTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ThanksClientTest {
2525
@Mock
2626
private lateinit var csrfTokenClient: CsrfTokenClient
2727
@Mock
28-
private lateinit var service: Service
28+
private lateinit var service: ThanksInterface
2929

3030
@Mock
3131
private lateinit var commonsApplication: CommonsApplication

data-client/src/main/java/org/wikipedia/dataclient/Service.java

-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.wikipedia.dataclient.mwapi.page.MwMobileViewPageLead;
1212
import org.wikipedia.dataclient.mwapi.page.MwMobileViewPageRemaining;
1313
import org.wikipedia.dataclient.mwapi.page.MwQueryPageSummary;
14-
import org.wikipedia.dataclient.mwapi.page.MwThankPostResponse;
1514
import org.wikipedia.edit.Edit;
1615
import org.wikipedia.edit.preview.EditPreview;
1716
import org.wikipedia.login.LoginClient;
@@ -191,14 +190,6 @@ public interface Service {
191190
@NonNull Observable<MwQueryResponse> getCategoryMembers(@NonNull @Query("cmtitle") String title,
192191
@Nullable @Query("cmcontinue") String continueStr);
193192

194-
@FormUrlEncoded
195-
@POST(MW_API_PREFIX + "action=thank")
196-
@NonNull Observable<MwThankPostResponse> thank(@Nullable @Field("rev") String rev,
197-
@Nullable @Field("log") String log,
198-
@NonNull @Field("token") String token,
199-
@Nullable @Field("source") String source);
200-
201-
202193
// ------- CSRF, Login, and Create Account -------
203194

204195
@Headers("Cache-Control: no-cache")

data-client/src/main/java/org/wikipedia/dataclient/mwapi/page/MwThankPostResponse.java

-38
This file was deleted.

0 commit comments

Comments
 (0)