Consume login client from data client library (#2894)

Fix actions for review client

Use data client library for notifications

With delete helper migrated to data client

With wikidata edits

With notifications and modifications migrated to data client

With upload migrated to retrofit

Delete unused code

Reuse thank interface from the library
This commit is contained in:
Vivek Maskara 2019-07-07 14:27:45 +05:30
parent 623c2f5467
commit d427a77c2a
35 changed files with 883 additions and 1668 deletions

View file

@ -0,0 +1,40 @@
package fr.free.nrw.commons.wikidata;
import org.wikipedia.csrf.CsrfTokenClient;
import org.wikipedia.dataclient.Service;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import io.reactivex.Observable;
import static fr.free.nrw.commons.di.NetworkingModule.NAMED_WIKI_DATA_CSRF;
@Singleton
public class WikidataClient {
private final Service service;
private final CsrfTokenClient csrfTokenClient;
@Inject
public WikidataClient(@Named("wikidata-service") Service service,
@Named(NAMED_WIKI_DATA_CSRF) CsrfTokenClient csrfTokenClient) {
this.service = service;
this.csrfTokenClient = csrfTokenClient;
}
public Observable<Long> createClaim(String entityId, String property, String snaktype, String value) {
try {
return service.postCreateClaim(entityId, snaktype, property, value, "en", csrfTokenClient.getTokenBlocking())
.map(mwPostResponse -> {
if (mwPostResponse.getSuccessVal() == 1) {
return 1L;
}
return -1L;
});
} catch (Throwable throwable) {
return Observable.just(-1L);
}
}
}

View file

@ -10,10 +10,10 @@ import javax.inject.Named;
import javax.inject.Singleton;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.actions.PageEditClient;
import fr.free.nrw.commons.kvstore.JsonKvStore;
import fr.free.nrw.commons.mwapi.MediaWikiApi;
import fr.free.nrw.commons.utils.ViewUtil;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import timber.log.Timber;
@ -26,20 +26,26 @@ import timber.log.Timber;
@Singleton
public class WikidataEditService {
private final static String COMMONS_APP_TAG = "wikimedia-commons-app";
private final static String COMMONS_APP_EDIT_REASON = "Add tag for edits made using Android Commons app";
private final Context context;
private final MediaWikiApi mediaWikiApi;
private final WikidataEditListener wikidataEditListener;
private final JsonKvStore directKvStore;
private final WikidataClient wikidataClient;
private final PageEditClient wikiDataPageEditClient;
@Inject
public WikidataEditService(Context context,
MediaWikiApi mediaWikiApi,
WikidataEditListener wikidataEditListener,
@Named("default_preferences") JsonKvStore directKvStore) {
@Named("default_preferences") JsonKvStore directKvStore,
WikidataClient wikidataClient,
@Named("wikidata-page-edit") PageEditClient wikiDataPageEditClient) {
this.context = context;
this.mediaWikiApi = mediaWikiApi;
this.wikidataEditListener = wikidataEditListener;
this.directKvStore = directKvStore;
this.wikidataClient = wikidataClient;
this.wikiDataPageEditClient = wikiDataPageEditClient;
}
/**
@ -77,13 +83,20 @@ public class WikidataEditService {
private void editWikidataProperty(String wikidataEntityId, String fileName) {
Timber.d("Upload successful with wiki data entity id as %s", wikidataEntityId);
Timber.d("Attempting to edit Wikidata property %s", wikidataEntityId);
Observable.fromCallable(() -> {
String propertyValue = getFileName(fileName);
return mediaWikiApi.wikidataCreateClaim(wikidataEntityId, "P18", "value", propertyValue);
})
String propertyValue = getFileName(fileName);
Timber.d(propertyValue);
wikidataClient.createClaim(wikidataEntityId, "P18", "value", propertyValue)
.flatMap(revisionId -> {
if (revisionId != -1) {
return wikiDataPageEditClient.addEditTag(revisionId, COMMONS_APP_TAG, COMMONS_APP_EDIT_REASON);
}
throw new RuntimeException("Unable to edit wikidata item");
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(revisionId -> handleClaimResult(wikidataEntityId, revisionId), throwable -> {
.subscribe(revisionId -> handleClaimResult(wikidataEntityId, String.valueOf(revisionId)), throwable -> {
Timber.e(throwable, "Error occurred while making claim");
ViewUtil.showLongToast(context, context.getString(R.string.wikidata_edit_failure));
});
@ -95,31 +108,12 @@ public class WikidataEditService {
wikidataEditListener.onSuccessfulWikidataEdit();
}
showSuccessToast();
logEdit(revisionId);
} else {
Timber.d("Unable to make wiki data edit for entity %s", wikidataEntityId);
ViewUtil.showLongToast(context, context.getString(R.string.wikidata_edit_failure));
}
}
/**
* Log the Wikidata edit by adding Wikimedia Commons App tag to the edit
* @param revisionId
*/
@SuppressLint("CheckResult")
private void logEdit(String revisionId) {
Observable.fromCallable(() -> mediaWikiApi.addWikidataEditTag(revisionId))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> {
if (result) {
Timber.d("Wikidata edit was tagged successfully");
} else {
Timber.d("Wikidata edit couldn't be tagged");
}
}, throwable -> Timber.e(throwable, "Error occurred while adding tag to the edit"));
}
/**
* Show a success toast when the edit is made successfully
*/