[WIP] add SendThankTask

This commit is contained in:
Yusuke Matsubara 2018-05-19 20:29:59 +02:00 committed by maskara
parent aa479dc9b4
commit 6f731ce23e
8 changed files with 196 additions and 10 deletions

View file

@ -101,19 +101,19 @@ public class DeleteTask extends AsyncTask<Void, Integer, Boolean> {
publishProgress(1);
mwApi.prependEdit(editToken,fileDeleteString+"\n",
media.getFilename(),summary);
media.getFilename(), summary);
publishProgress(2);
mwApi.edit(editToken,subpageString+"\n",
"Commons:Deletion_requests/"+media.getFilename(),summary);
"Commons:Deletion_requests/"+media.getFilename(), summary);
publishProgress(3);
mwApi.appendEdit(editToken,logPageString+"\n",
"Commons:Deletion_requests/"+date,summary);
"Commons:Deletion_requests/"+date, summary);
publishProgress(4);
mwApi.appendEdit(editToken,userPageString+"\n",
"User_Talk:"+sessionManager.getCurrentAccount().name,summary);
"User_Talk:"+sessionManager.getCurrentAccount().name, summary);
publishProgress(5);
}
catch (Exception e) {

View file

@ -13,6 +13,7 @@ import fr.free.nrw.commons.contributions.ContributionsSyncAdapter;
import fr.free.nrw.commons.delete.DeleteTask;
import fr.free.nrw.commons.modifications.ModificationsSyncAdapter;
import fr.free.nrw.commons.review.CheckCategoryTask;
import fr.free.nrw.commons.review.SendThankTask;
import fr.free.nrw.commons.settings.SettingsFragment;
import fr.free.nrw.commons.nearby.PlaceRenderer;
import fr.free.nrw.commons.settings.SettingsFragment;
@ -46,6 +47,8 @@ public interface CommonsApplicationComponent extends AndroidInjector<Application
void inject(CheckCategoryTask checkCategoryTask);
void inject(SendThankTask sendThankTask);
void inject(SettingsFragment fragment);
@Override

View file

@ -52,7 +52,11 @@ import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
import fr.free.nrw.commons.location.LatLng;
import fr.free.nrw.commons.mwapi.MediaWikiApi;
import fr.free.nrw.commons.review.CheckCategoryTask;
import fr.free.nrw.commons.review.SendThankTask;
import fr.free.nrw.commons.ui.widget.CompatTextView;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import timber.log.Timber;
import static android.content.Context.CLIPBOARD_SERVICE;
@ -434,10 +438,8 @@ public class MediaDetailFragment extends CommonsDaggerSupportFragment {
alert.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String reason = input.getText().toString();
// TODO: RESTORE THIS IMMEDIATELY AFTER TESTING
DeleteTask deleteTask = new DeleteTask(getActivity(), media, reason);
//CheckCategoryTask deleteTask = new CheckCategoryTask(getActivity(), media);
DeleteTask deleteTask = new DeleteTask(getActivity(), media, reason);
deleteTask.execute();
enableDeleteButton(false);
}

View file

@ -294,6 +294,19 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
.getString("/api/query/pages/page/@_idx")) != -1;
}
@Override
public boolean thank(String editToken, String revision) throws IOException {
ApiResult res = api.action("thank")
.param("rev", revision)
.param("token", editToken)
.param("source", getUserAgent())
.post();
String r = res.getString("/api/result/@success");
// Does this correctly check the success/failure?
// The docs https://www.mediawiki.org/wiki/Extension:Thanks seems unclear about that.
return r.equals("success");
}
@Override
@Nullable
public String edit(String editToken, String processedPageContent, String filename, String summary) throws IOException {
@ -592,6 +605,20 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
.getString("/api/query/pages/page/revisions/rev");
}
@Override
@Nullable
public String firstRevisionOfFile(String filename) throws IOException {
String res = api.action("query")
.param("prop", "revisions")
.param("rvprop", "timestamp|ids")
.param("titles", filename)
.param("rvdir", "newer")
.param("rvlimit", "1")
.get()
.getString("/api/query/pages/page/revisions/rev/@revid");
return res;
}
@Override
@NonNull
public List<Notification> getNotifications() {

View file

@ -108,6 +108,10 @@ public interface MediaWikiApi {
Single<CampaignResponseDTO> getCampaigns();
boolean thank(String editToken, String revision) throws IOException;
String firstRevisionOfFile(String filename) throws IOException;
interface ProgressListener {
void onProgress(long transferred, long total);
}

View file

@ -19,6 +19,9 @@ import timber.log.Timber;
import static android.support.v4.app.NotificationCompat.DEFAULT_ALL;
import static android.support.v4.app.NotificationCompat.PRIORITY_HIGH;
// Example code:
// CheckCategoryTask deleteTask = new CheckCategoryTask(getActivity(), media);
public class CheckCategoryTask extends AsyncTask<Void, Integer, Boolean> {
@Inject
@ -26,7 +29,7 @@ public class CheckCategoryTask extends AsyncTask<Void, Integer, Boolean> {
@Inject
SessionManager sessionManager;
public static final int NOTIFICATION_CHECK_CATEGORY = 1;
public static final int NOTIFICATION_CHECK_CATEGORY = 0x101;
private NotificationManager notificationManager;
private NotificationCompat.Builder notificationBuilder;
@ -71,7 +74,7 @@ public class CheckCategoryTask extends AsyncTask<Void, Integer, Boolean> {
}
publishProgress(1);
mwApi.appendEdit(editToken, "\n{{subst:chc}}\n", media.getFilename(),summary);
mwApi.appendEdit(editToken, "\n{{subst:chc}}\n", media.getFilename(), summary);
publishProgress(2);
}
catch (Exception e) {
@ -91,7 +94,7 @@ public class CheckCategoryTask extends AsyncTask<Void, Integer, Boolean> {
message = context.getString(messages[values[0]]);
}
notificationBuilder.setContentTitle(context.getString(R.string.check_category_notification_title))
notificationBuilder.setContentTitle(context.getString(R.string.check_category_notification_title, media.getDisplayTitle()))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))
.setSmallIcon(R.drawable.ic_launcher)

View file

@ -0,0 +1,140 @@
package fr.free.nrw.commons.review;
import android.app.NotificationManager;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v4.app.NotificationCompat;
import android.view.Gravity;
import android.widget.Toast;
import javax.inject.Inject;
import fr.free.nrw.commons.Media;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.auth.SessionManager;
import fr.free.nrw.commons.di.ApplicationlessInjection;
import fr.free.nrw.commons.mwapi.MediaWikiApi;
import timber.log.Timber;
import static android.support.v4.app.NotificationCompat.DEFAULT_ALL;
import static android.support.v4.app.NotificationCompat.PRIORITY_HIGH;
// example code:
//
// media = new Media("File:Iru.png");
// Observable.fromCallable(() -> mwApi.firstRevisionOfFile(media.getFilename()))
// .subscribeOn(Schedulers.io())
// .observeOn(AndroidSchedulers.mainThread())
// .subscribe(revision -> {
// SendThankTask task = new SendThankTask(getActivity(), media, revision);
// task.execute();
// });
public class SendThankTask extends AsyncTask<Void, Integer, Boolean> {
@Inject
MediaWikiApi mwApi;
@Inject
SessionManager sessionManager;
public static final int NOTIFICATION_SEND_THANK = 0x102;
private NotificationManager notificationManager;
private NotificationCompat.Builder notificationBuilder;
private Context context;
private Media media;
private String revision;
public SendThankTask(Context context, Media media, String revision){
this.context = context;
this.media = media;
this.revision = revision;
}
@Override
protected void onPreExecute(){
ApplicationlessInjection
.getInstance(context.getApplicationContext())
.getCommonsApplicationComponent()
.inject(this);
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationBuilder = new NotificationCompat.Builder(context);
Toast toast = new Toast(context);
toast.setGravity(Gravity.CENTER,0,0);
toast = Toast.makeText(context, context.getString(R.string.send_thank_toast, media.getDisplayTitle()), Toast.LENGTH_SHORT);
toast.show();
}
@Override
protected Boolean doInBackground(Void ...voids) {
publishProgress(0);
String editToken;
String authCookie;
authCookie = sessionManager.getAuthCookie();
mwApi.setAuthCookie(authCookie);
try {
editToken = mwApi.getEditToken();
if (editToken.equals("+\\")) {
return false;
}
publishProgress(1);
mwApi.thank(editToken, revision);
publishProgress(2);
}
catch (Exception e) {
Timber.d(e.getMessage());
return false;
}
return true;
}
@Override
protected void onProgressUpdate (Integer... values){
super.onProgressUpdate(values);
int[] messages = new int[]{R.string.getting_edit_token, R.string.send_thank_send};
String message = "";
if (0 < values[0] && values[0] < messages.length) {
message = context.getString(messages[values[0]]);
}
notificationBuilder.setContentTitle(context.getString(R.string.send_thank_notification_title))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))
.setSmallIcon(R.drawable.ic_launcher)
.setProgress(messages.length, values[0], false)
.setOngoing(true);
notificationManager.notify(NOTIFICATION_SEND_THANK, notificationBuilder.build());
}
@Override
protected void onPostExecute(Boolean result) {
String message = "";
String title = "";
if (result){
title = context.getString(R.string.send_thank_success_title);
message = context.getString(R.string.send_thank_success_message, media.getDisplayTitle());
}
else {
title = context.getString(R.string.send_thank_failure_title);
message = context.getString(R.string.send_thank_failure_message, media.getDisplayTitle());
}
notificationBuilder.setDefaults(DEFAULT_ALL)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))
.setSmallIcon(R.drawable.ic_launcher)
.setProgress(0,0,false)
.setOngoing(false)
.setPriority(PRIORITY_HIGH);
notificationManager.notify(NOTIFICATION_SEND_THANK, notificationBuilder.build());
}
}

View file

@ -469,6 +469,13 @@ Upload your first media by touching the camera or gallery icon above.</string>
<string name="nominate_for_deletion_edit_deletion_request_log">Adding file to Delete requests log</string>
<string name="nominate_for_deletion_create_deletion_request">Creating Delete requests subpage</string>
<string name="notsure">Not sure</string>
<string name="send_thank_success_title">Sending Thanks: Success</string>
<string name="send_thank_success_message">Successfully sent thanks to %1$s</string>
<string name="send_thank_failure_message">Failed to send thanks %1$s</string>
<string name="send_thank_failure_title">Sending Thanks: Failure</string>
<string name="send_thank_send">Sending thanks</string>
<string name="send_thank_notification_title">Sending thanks</string>
<string name="send_thank_toast">Sending Thanks for %1$s</string>
<string name="review_copyright">Is this a copyright violation?</string>
<string name="review_category">Is this mis-categorized?</string>