diff --git a/app/src/main/java/fr/free/nrw/commons/actions/PageEditClient.java b/app/src/main/java/fr/free/nrw/commons/actions/PageEditClient.java deleted file mode 100644 index e5a560911..000000000 --- a/app/src/main/java/fr/free/nrw/commons/actions/PageEditClient.java +++ /dev/null @@ -1,80 +0,0 @@ -package fr.free.nrw.commons.actions; - -import io.reactivex.Single; -import io.reactivex.SingleOnSubscribe; -import org.wikipedia.csrf.CsrfTokenClient; -import org.wikipedia.dataclient.Service; - -import io.reactivex.Observable; - -/** - * This class acts as a Client to facilitate wiki page editing - * services to various dependency providing modules such as the Network module, the Review Controller ,etc - * - * The methods provided by this class will post to the Media wiki api - * documented at: https://commons.wikimedia.org/w/api.php?action=help&modules=edit - */ -public class PageEditClient { - - private final CsrfTokenClient csrfTokenClient; - private final PageEditInterface pageEditInterface; - private final Service service; - - public PageEditClient(CsrfTokenClient csrfTokenClient, - PageEditInterface pageEditInterface, - Service service) { - this.csrfTokenClient = csrfTokenClient; - this.pageEditInterface = pageEditInterface; - this.service = service; - } - - /** - * This method is used when the content of the page is to be replaced by new content received - * @param pageTitle Title of the page to edit - * @param text Holds the page content - * @param summary Edit summary - */ - public Observable edit(String pageTitle, String text, String summary) { - try { - return pageEditInterface.postEdit(pageTitle, summary, text, csrfTokenClient.getTokenBlocking()) - .map(editResponse -> editResponse.edit().editSucceeded()); - } catch (Throwable throwable) { - return Observable.just(false); - } - } - - /** - * This method is used when we need to append something to the end of wiki page content - * @param pageTitle Title of the page to edit - * @param appendText The received page content is added to beginning of the page - * @param summary Edit summary - */ - public Observable appendEdit(String pageTitle, String appendText, String summary) { - return Single.create((SingleOnSubscribe) emitter -> { - try { - emitter.onSuccess(csrfTokenClient.getTokenBlocking()); - } catch (Throwable throwable) { - emitter.onError(throwable); - throwable.printStackTrace(); - } - }).flatMapObservable(token -> pageEditInterface.postAppendEdit(pageTitle, summary, appendText, token) - .map(editResponse -> editResponse.edit().editSucceeded())); - - } - - /** - * This method is used when we need to add something to the starting of the page - * @param pageTitle Title of the page to edit - * @param prependText The received page content is added to beginning of the page - * @param summary Edit summary - */ - public Observable prependEdit(String pageTitle, String prependText, String summary) { - try { - return pageEditInterface.postPrependEdit(pageTitle, summary, prependText, csrfTokenClient.getTokenBlocking()) - .map(editResponse -> editResponse.edit().editSucceeded()); - } catch (Throwable throwable) { - return Observable.just(false); - } - } - -} diff --git a/app/src/main/java/fr/free/nrw/commons/actions/PageEditClient.kt b/app/src/main/java/fr/free/nrw/commons/actions/PageEditClient.kt new file mode 100644 index 000000000..0ae7e42de --- /dev/null +++ b/app/src/main/java/fr/free/nrw/commons/actions/PageEditClient.kt @@ -0,0 +1,65 @@ +package fr.free.nrw.commons.actions + +import io.reactivex.Observable +import org.wikipedia.csrf.CsrfTokenClient + +/** + * This class acts as a Client to facilitate wiki page editing + * services to various dependency providing modules such as the Network module, the Review Controller ,etc + * + * The methods provided by this class will post to the Media wiki api + * documented at: https://commons.wikimedia.org/w/api.php?action=help&modules=edit + */ +class PageEditClient( + private val csrfTokenClient: CsrfTokenClient, + private val pageEditInterface: PageEditInterface +) { + + /** + * Replace the content of a wiki page + * @param pageTitle Title of the page to edit + * @param text Holds the page content + * @param summary Edit summary + * @return whether the edit was successful + */ + fun edit(pageTitle: String, text: String, summary: String): Observable { + return try { + pageEditInterface.postEdit(pageTitle, summary, text, csrfTokenClient.tokenBlocking) + .map { editResponse -> editResponse.edit()!!.editSucceeded() } + } catch (throwable: Throwable) { + Observable.just(false) + } + } + + /** + * Append text to the end of a wiki page + * @param pageTitle Title of the page to edit + * @param appendText The received page content is added to the end of the page + * @param summary Edit summary + * @return whether the edit was successful + */ + fun appendEdit(pageTitle: String, appendText: String, summary: String): Observable { + return try { + pageEditInterface.postAppendEdit(pageTitle, summary, appendText, csrfTokenClient.tokenBlocking) + .map { editResponse -> editResponse.edit()!!.editSucceeded() } + } catch (throwable: Throwable) { + Observable.just(false) + } + } + + /** + * Prepend text to the beginning of a wiki page + * @param pageTitle Title of the page to edit + * @param prependText The received page content is added to the beginning of the page + * @param summary Edit summary + * @return whether the edit was successful + */ + fun prependEdit(pageTitle: String, prependText: String, summary: String): Observable { + return try { + pageEditInterface.postPrependEdit(pageTitle, summary, prependText, csrfTokenClient.tokenBlocking) + .map { editResponse -> editResponse.edit()!!.editSucceeded() } + } catch (throwable: Throwable) { + Observable.just(false) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/fr/free/nrw/commons/actions/PageEditInterface.java b/app/src/main/java/fr/free/nrw/commons/actions/PageEditInterface.java deleted file mode 100644 index 8f4faf01f..000000000 --- a/app/src/main/java/fr/free/nrw/commons/actions/PageEditInterface.java +++ /dev/null @@ -1,76 +0,0 @@ -package fr.free.nrw.commons.actions; - -import androidx.annotation.NonNull; - -import org.wikipedia.edit.Edit; - -import io.reactivex.Observable; -import retrofit2.http.Field; -import retrofit2.http.FormUrlEncoded; -import retrofit2.http.Headers; -import retrofit2.http.POST; - -import static org.wikipedia.dataclient.Service.MW_API_PREFIX; - -/** - * This interface facilitates wiki commons page editing services to the Networking module - * which provides all network related services used by the app. - * - * This interface posts a form encoded request to the wikimedia API - * with editing action as argument to edit a particular page - */ -public interface PageEditInterface { - - /** - * This method posts such that the Content which the page - * has will be completely replaced by the value being passed to the - * "text" field of the encoded form data - * @param title Title of the page to edit. Cannot be used together with pageid. - * @param summary Edit summary. Also section title when section=new and sectiontitle is not set - * @param text Holds the page content - * @param token A "csrf" token - */ - @FormUrlEncoded - @Headers("Cache-Control: no-cache") - @POST(MW_API_PREFIX + "action=edit") - @NonNull - Observable postEdit(@NonNull @Field("title") String title, - @NonNull @Field("summary") String summary, - @NonNull @Field("text") String text, - // NOTE: This csrf shold always be sent as the last field of form data - @NonNull @Field("token") String token); - - /** - * This method posts such that the Content which the page - * has will be completely replaced by the value being passed to the - * "text" field of the encoded form data - * @param title Title of the page to edit. Cannot be used together with pageid. - * @param summary Edit summary. Also section title when section=new and sectiontitle is not set - * @param text The received page content is added to beginning of the page - * @param token A "csrf" token - */ - @FormUrlEncoded - @Headers("Cache-Control: no-cache") - @POST(MW_API_PREFIX + "action=edit") - @NonNull Observable postAppendEdit(@NonNull @Field("title") String title, - @NonNull @Field("summary") String summary, - @NonNull @Field("appendtext") String text, - @NonNull @Field("token") String token); - - /** - * This method posts such that the Content which the page - * has will be completely replaced by the value being passed to the - * "text" field of the encoded form data - * @param title Title of the page to edit. Cannot be used together with pageid. - * @param summary Edit summary. Also section title when section=new and sectiontitle is not set - * @param text The received page content is added to beginning of the page - * @param token A "csrf" token - */ - @FormUrlEncoded - @Headers("Cache-Control: no-cache") - @POST(MW_API_PREFIX + "action=edit") - @NonNull Observable postPrependEdit(@NonNull @Field("title") String title, - @NonNull @Field("summary") String summary, - @NonNull @Field("prependtext") String text, - @NonNull @Field("token") String token); -} diff --git a/app/src/main/java/fr/free/nrw/commons/actions/PageEditInterface.kt b/app/src/main/java/fr/free/nrw/commons/actions/PageEditInterface.kt new file mode 100644 index 000000000..8b27d1288 --- /dev/null +++ b/app/src/main/java/fr/free/nrw/commons/actions/PageEditInterface.kt @@ -0,0 +1,76 @@ +package fr.free.nrw.commons.actions + +import io.reactivex.Observable +import org.wikipedia.dataclient.Service +import org.wikipedia.edit.Edit +import retrofit2.http.Field +import retrofit2.http.FormUrlEncoded +import retrofit2.http.Headers +import retrofit2.http.POST + +/** + * This interface facilitates wiki commons page editing services to the Networking module + * which provides all network related services used by the app. + * + * This interface posts a form encoded request to the wikimedia API + * with editing action as argument to edit a particular page + */ +interface PageEditInterface { + /** + * This method posts such that the Content which the page + * has will be completely replaced by the value being passed to the + * "text" field of the encoded form data + * @param title Title of the page to edit. Cannot be used together with pageid. + * @param summary Edit summary. Also section title when section=new and sectiontitle is not set + * @param text Holds the page content + * @param token A "csrf" token + */ + @FormUrlEncoded + @Headers("Cache-Control: no-cache") + @POST(Service.MW_API_PREFIX + "action=edit") + fun postEdit( + @Field("title") title: String, + @Field("summary") summary: String, + @Field("text") text: String, + // NOTE: This csrf shold always be sent as the last field of form data + @Field("token") token: String + ): Observable + + /** + * This method posts such that the Content which the page + * has will be appended with the value being passed to the + * "appendText" field of the encoded form data + * @param title Title of the page to edit. Cannot be used together with pageid. + * @param summary Edit summary. Also section title when section=new and sectiontitle is not set + * @param appendText Text to add to the end of the page + * @param token A "csrf" token + */ + @FormUrlEncoded + @Headers("Cache-Control: no-cache") + @POST(Service.MW_API_PREFIX + "action=edit") + fun postAppendEdit( + @Field("title") title: String, + @Field("summary") summary: String, + @Field("appendtext") appendText: String, + @Field("token") token: String + ): Observable + + /** + * This method posts such that the Content which the page + * has will be prepended with the value being passed to the + * "prependText" field of the encoded form data + * @param title Title of the page to edit. Cannot be used together with pageid. + * @param summary Edit summary. Also section title when section=new and sectiontitle is not set + * @param prependText Text to add to the beginning of the page + * @param token A "csrf" token + */ + @FormUrlEncoded + @Headers("Cache-Control: no-cache") + @POST(Service.MW_API_PREFIX + "action=edit") + fun postPrependEdit( + @Field("title") title: String, + @Field("summary") summary: String, + @Field("prependtext") prependText: String, + @Field("token") token: String + ): Observable +} \ No newline at end of file diff --git a/app/src/main/java/fr/free/nrw/commons/actions/ThanksClient.java b/app/src/main/java/fr/free/nrw/commons/actions/ThanksClient.java deleted file mode 100644 index 2b754ef92..000000000 --- a/app/src/main/java/fr/free/nrw/commons/actions/ThanksClient.java +++ /dev/null @@ -1,48 +0,0 @@ -package fr.free.nrw.commons.actions; - -import org.wikipedia.csrf.CsrfTokenClient; -import org.wikipedia.dataclient.Service; - -import javax.inject.Inject; -import javax.inject.Named; -import javax.inject.Singleton; - -import fr.free.nrw.commons.CommonsApplication; -import io.reactivex.Observable; - -/** - * Facilitates the Wkikimedia Thanks api extension, as described in the - * api documentation: "The Thanks extension includes an API for sending thanks" - * - * In simple terms this class is used by a user to thank someone for adding - * contribution to the commons platform - */ -@Singleton -public class ThanksClient { - - private final CsrfTokenClient csrfTokenClient; - private final Service service; - - @Inject - public ThanksClient(@Named("commons-csrf") CsrfTokenClient csrfTokenClient, - @Named("commons-service") Service service) { - this.csrfTokenClient = csrfTokenClient; - this.service = service; - } - - /** - * Handles the Thanking logic - * @param revisionID The revision ID you would like to thank someone for - * @return if thanks was successfully sent to intended recipient, returned as a boolean observable - */ - public Observable thank(long revisionId) { - try { - return service.thank(String.valueOf(revisionId), null, - csrfTokenClient.getTokenBlocking(), - CommonsApplication.getInstance().getUserAgent()) - .map(mwQueryResponse -> mwQueryResponse.getSuccessVal() == 1); - } catch (Throwable throwable) { - return Observable.just(false); - } - } -} diff --git a/app/src/main/java/fr/free/nrw/commons/actions/ThanksClient.kt b/app/src/main/java/fr/free/nrw/commons/actions/ThanksClient.kt new file mode 100644 index 000000000..b2fb06242 --- /dev/null +++ b/app/src/main/java/fr/free/nrw/commons/actions/ThanksClient.kt @@ -0,0 +1,36 @@ +package fr.free.nrw.commons.actions + +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 + +/** + * Client for the Wkikimedia Thanks API extension + * Thanks are used by a user to show gratitude to another user for their contributions + */ +@Singleton +class ThanksClient @Inject constructor( + @param:Named(NAMED_COMMONS_CSRF) private val csrfTokenClient: CsrfTokenClient, + @param:Named("commons-service") private val service: Service +) { + /** + * Thanks a user for a particular revision + * @param revisionId The revision ID the user would like to thank someone for + * @return if thanks was successfully sent to intended recipient + */ + fun thank(revisionId: Long): Observable { + return try { + service.thank(revisionId.toString(), null, csrfTokenClient.tokenBlocking, CommonsApplication.getInstance().userAgent) + .map { mwQueryResponse -> mwQueryResponse.successVal == 1 } + } catch (throwable: Throwable) { + Observable.just(false) + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/fr/free/nrw/commons/di/NetworkingModule.java b/app/src/main/java/fr/free/nrw/commons/di/NetworkingModule.java index 33291c6cd..ec68c3eae 100644 --- a/app/src/main/java/fr/free/nrw/commons/di/NetworkingModule.java +++ b/app/src/main/java/fr/free/nrw/commons/di/NetworkingModule.java @@ -216,9 +216,8 @@ public class NetworkingModule { @Provides @Singleton public PageEditClient provideCommonsPageEditClient(@Named(NAMED_COMMONS_CSRF) CsrfTokenClient csrfTokenClient, - @Named("commons-page-edit-service") PageEditInterface pageEditInterface, - @Named("commons-service") Service service) { - return new PageEditClient(csrfTokenClient, pageEditInterface, service); + @Named("commons-page-edit-service") PageEditInterface pageEditInterface) { + return new PageEditClient(csrfTokenClient, pageEditInterface); } @Provides diff --git a/app/src/test/kotlin/fr/free/nrw/commons/actions/PageEditClientTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/actions/PageEditClientTest.kt index 186b57610..5477d9549 100644 --- a/app/src/test/kotlin/fr/free/nrw/commons/actions/PageEditClientTest.kt +++ b/app/src/test/kotlin/fr/free/nrw/commons/actions/PageEditClientTest.kt @@ -18,8 +18,6 @@ class PageEditClientTest { private lateinit var csrfTokenClient: CsrfTokenClient @Mock private lateinit var pageEditInterface: PageEditInterface - @Mock - private lateinit var service: Service private lateinit var pageEditClient: PageEditClient @@ -36,7 +34,7 @@ class PageEditClientTest { @Throws(Exception::class) fun setUp() { MockitoAnnotations.initMocks(this) - pageEditClient = PageEditClient(csrfTokenClient, pageEditInterface, service) + pageEditClient = PageEditClient(csrfTokenClient, pageEditInterface) } /**