Skip to content

Commit 5dc45a5

Browse files
maskaravivekneslihanturan
authored andcommitted
Fix bugs in peer review flow (commons-app#3039)
* Fix bugs in peer review flow * Fix tests * Bug fixes * Fix remaining issues with peer review
1 parent 6673725 commit 5dc45a5

File tree

8 files changed

+131
-116
lines changed

8 files changed

+131
-116
lines changed

app/src/main/java/fr/free/nrw/commons/delete/DeleteHelper.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package fr.free.nrw.commons.delete;
22

3+
import android.annotation.SuppressLint;
34
import android.content.Context;
45
import android.content.Intent;
56
import android.net.Uri;
67

8+
import androidx.appcompat.app.AlertDialog;
9+
710
import java.text.SimpleDateFormat;
811
import java.util.ArrayList;
912
import java.util.Calendar;
@@ -12,17 +15,16 @@
1215
import javax.inject.Inject;
1316
import javax.inject.Singleton;
1417

15-
import androidx.appcompat.app.AlertDialog;
1618
import fr.free.nrw.commons.BuildConfig;
1719
import fr.free.nrw.commons.Media;
1820
import fr.free.nrw.commons.auth.SessionManager;
1921
import fr.free.nrw.commons.mwapi.MediaWikiApi;
2022
import fr.free.nrw.commons.notification.NotificationHelper;
21-
import fr.free.nrw.commons.review.ReviewActivity;
22-
import fr.free.nrw.commons.utils.ViewUtil;
2323
import fr.free.nrw.commons.review.ReviewController;
2424
import fr.free.nrw.commons.utils.ViewUtilWrapper;
2525
import io.reactivex.Single;
26+
import io.reactivex.android.schedulers.AndroidSchedulers;
27+
import io.reactivex.schedulers.Schedulers;
2628
import timber.log.Timber;
2729

2830
import static fr.free.nrw.commons.notification.NotificationHelper.NOTIFICATION_DELETE;
@@ -57,7 +59,6 @@ public DeleteHelper(MediaWikiApi mwApi,
5759
*/
5860
public Single<Boolean> makeDeletion(Context context, Media media, String reason) {
5961
viewUtil.showShortToast(context, "Trying to nominate " + media.getDisplayTitle() + " for deletion");
60-
6162
return Single.fromCallable(() -> delete(media, reason))
6263
.flatMap(result -> Single.fromCallable(() ->
6364
showDeletionNotification(context, media, result)));
@@ -99,7 +100,8 @@ private boolean delete(Media media, String reason) {
99100

100101
try {
101102
editToken = mwApi.getEditToken();
102-
if (editToken.equals("+\\")) {
103+
104+
if(editToken == null) {
103105
return false;
104106
}
105107

@@ -143,7 +145,12 @@ private boolean showDeletionNotification(Context context, Media media, boolean r
143145
* @param question
144146
* @param problem
145147
*/
146-
public void askReasonAndExecute(Media media, Context context, String question, ReviewController.DeleteReason problem) {
148+
@SuppressLint("CheckResult")
149+
public void askReasonAndExecute(Media media,
150+
Context context,
151+
String question,
152+
ReviewController.DeleteReason problem,
153+
ReviewController.ReviewCallback reviewCallback) {
147154
AlertDialog.Builder alert = new AlertDialog.Builder(context);
148155
alert.setTitle(question);
149156

@@ -183,12 +190,19 @@ public void askReasonAndExecute(Media media, Context context, String question, R
183190
}
184191
}
185192

186-
((ReviewActivity) context).reviewController.swipeToNext();
187-
((ReviewActivity) context).runRandomizer();
193+
makeDeletion(context, media, reason)
194+
.subscribeOn(Schedulers.io())
195+
.observeOn(AndroidSchedulers.mainThread())
196+
.subscribe(aBoolean -> {
197+
if (aBoolean) {
198+
reviewCallback.onSuccess();
199+
} else {
200+
reviewCallback.onFailure();
201+
}
202+
});
188203

189-
makeDeletion(context, media, reason);
190204
});
191-
alert.setNegativeButton("Cancel", null);
205+
alert.setNegativeButton("Cancel", (dialog, which) -> reviewCallback.onFailure());
192206
AlertDialog d = alert.create();
193207
d.show();
194208
}

app/src/main/java/fr/free/nrw/commons/review/ReviewActivity.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import com.google.android.material.navigation.NavigationView;
2121
import com.viewpagerindicator.CirclePageIndicator;
2222

23-
import java.util.ArrayList;
24-
2523
import javax.inject.Inject;
2624

2725
import butterknife.BindView;
@@ -92,6 +90,9 @@ public static void startYourself(Context context, String title) {
9290

9391
private CompositeDisposable compositeDisposable = new CompositeDisposable();
9492

93+
public Media getMedia() {
94+
return media;
95+
}
9596

9697
@Override
9798
protected void onAuthCookieAcquired(String authCookie) {
@@ -163,23 +164,18 @@ private void updateImage(Media media) {
163164

164165
simpleDraweeView.setImageURI(media.getImageUrl());
165166

166-
reviewController.onImageRefreshed(fileName); //file name is updated
167+
reviewController.onImageRefreshed(media); //file name is updated
167168
compositeDisposable.add(reviewHelper.getFirstRevisionOfFile(fileName)
168169
.subscribeOn(Schedulers.io())
169170
.observeOn(AndroidSchedulers.mainThread())
170171
.subscribe(revision -> {
171172
reviewController.firstRevision = revision;
172-
reviewPagerAdapter.updateFileInformation(fileName);
173-
imageCaption.setText(fileName + " is uploaded by: " + revision.getUser());
173+
reviewPagerAdapter.updateFileInformation();
174+
String caption = String.format(getString(R.string.review_is_uploaded_by), fileName, revision.getUser());
175+
imageCaption.setText(caption);
174176
progressBar.setVisibility(View.GONE);
175177
}));
176178
reviewPager.setCurrentItem(0);
177-
updateCategories(media.getCategories());
178-
}
179-
180-
private void updateCategories(ArrayList<String> categories) {
181-
reviewController.onCategoriesRefreshed(categories);
182-
reviewPagerAdapter.updateCategories();
183179
}
184180

185181
public void swipeToNext() {

app/src/main/java/fr/free/nrw/commons/review/ReviewController.java

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@
77
import android.view.Gravity;
88
import android.widget.Toast;
99

10-
import org.wikipedia.dataclient.mwapi.MwQueryPage;
10+
import androidx.annotation.NonNull;
11+
import androidx.annotation.Nullable;
12+
import androidx.core.app.NotificationCompat;
1113

12-
import java.util.ArrayList;
14+
import org.wikipedia.dataclient.mwapi.MwQueryPage;
1315

1416
import javax.inject.Inject;
1517
import javax.inject.Singleton;
1618

17-
import androidx.annotation.NonNull;
18-
import androidx.annotation.Nullable;
19-
import androidx.core.app.NotificationCompat;
20-
import androidx.viewpager.widget.ViewPager;
19+
import fr.free.nrw.commons.CommonsApplication;
2120
import fr.free.nrw.commons.Media;
2221
import fr.free.nrw.commons.R;
2322
import fr.free.nrw.commons.auth.SessionManager;
@@ -31,13 +30,11 @@
3130

3231
@Singleton
3332
public class ReviewController {
34-
private String fileName;
35-
public static final int NOTIFICATION_SEND_THANK = 0x102;
36-
protected static ArrayList<String> categories;
37-
public static final int NOTIFICATION_CHECK_CATEGORY = 0x101;
33+
private static final int NOTIFICATION_SEND_THANK = 0x102;
34+
private static final int NOTIFICATION_CHECK_CATEGORY = 0x101;
3835
private final DeleteHelper deleteHelper;
3936
@Nullable
40-
public MwQueryPage.Revision firstRevision; // TODO: maybe we can expand this class to include fileName
37+
MwQueryPage.Revision firstRevision; // TODO: maybe we can expand this class to include fileName
4138
@Inject
4239
MediaWikiApi mwApi;
4340
@Inject
@@ -46,63 +43,52 @@ public class ReviewController {
4643
private NotificationCompat.Builder notificationBuilder;
4744
private Media media;
4845

49-
private ViewPager viewPager;
50-
private ReviewActivity reviewActivity;
51-
5246
ReviewController(DeleteHelper deleteHelper, Context context) {
5347
this.deleteHelper = deleteHelper;
54-
reviewActivity = (ReviewActivity) context;
55-
viewPager = ((ReviewActivity) context).reviewPager;
56-
}
57-
58-
public void onImageRefreshed(String fileName) {
59-
this.fileName = fileName;
60-
media = new Media("File:" + fileName);
61-
ReviewController.categories = new ArrayList<>();
48+
CommonsApplication.createNotificationChannel(context.getApplicationContext());
49+
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
50+
notificationBuilder = new NotificationCompat.Builder(context, CommonsApplication.NOTIFICATION_CHANNEL_ID_ALL);
6251
}
6352

64-
public void onCategoriesRefreshed(ArrayList<String> categories) {
65-
ReviewController.categories = categories;
53+
void onImageRefreshed(Media media) {
54+
this.media = media;
6655
}
6756

68-
public void swipeToNext() {
69-
int nextPos = viewPager.getCurrentItem() + 1;
70-
if (nextPos <= 3) {
71-
viewPager.setCurrentItem(nextPos);
72-
} else {
73-
reviewActivity.runRandomizer();
74-
}
57+
public Media getMedia() {
58+
return media;
7559
}
7660

7761
public enum DeleteReason {
7862
SPAM,
7963
COPYRIGHT_VIOLATION
8064
}
8165

82-
public void reportSpam(@NonNull Activity activity) {
83-
deleteHelper.askReasonAndExecute(new Media("File:" + fileName),
66+
void reportSpam(@NonNull Activity activity, ReviewCallback reviewCallback) {
67+
Timber.d("Report spam for %s", media.getFilename());
68+
deleteHelper.askReasonAndExecute(media,
8469
activity,
8570
activity.getResources().getString(R.string.review_spam_report_question),
86-
DeleteReason.SPAM);
71+
DeleteReason.SPAM,
72+
reviewCallback);
8773
}
8874

89-
public void reportPossibleCopyRightViolation(@NonNull Activity activity) {
90-
deleteHelper.askReasonAndExecute(new Media("File:" + fileName),
75+
void reportPossibleCopyRightViolation(@NonNull Activity activity, ReviewCallback reviewCallback) {
76+
Timber.d("Report spam for %s", media.getFilename());
77+
deleteHelper.askReasonAndExecute(media,
9178
activity,
9279
activity.getResources().getString(R.string.review_c_violation_report_question),
93-
DeleteReason.COPYRIGHT_VIOLATION);
80+
DeleteReason.COPYRIGHT_VIOLATION,
81+
reviewCallback);
9482
}
9583

9684
@SuppressLint("CheckResult")
97-
public void reportWrongCategory(@NonNull Activity activity) {
85+
void reportWrongCategory(@NonNull Activity activity, ReviewCallback reviewCallback) {
9886
Context context = activity.getApplicationContext();
9987
ApplicationlessInjection
10088
.getInstance(context)
10189
.getCommonsApplicationComponent()
10290
.inject(this);
10391

104-
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
105-
notificationBuilder = new NotificationCompat.Builder(context);
10692
Toast toast = new Toast(context);
10793
toast.setGravity(Gravity.CENTER, 0, 0);
10894
toast = Toast.makeText(context, context.getString(R.string.check_category_toast, media.getDisplayTitle()), Toast.LENGTH_SHORT);
@@ -120,7 +106,8 @@ public void reportWrongCategory(@NonNull Activity activity) {
120106

121107
try {
122108
editToken = mwApi.getEditToken();
123-
if (editToken.equals("+\\")) {
109+
110+
if (editToken == null) {
124111
return false;
125112
}
126113
publishProgress(context, 1);
@@ -136,15 +123,17 @@ public void reportWrongCategory(@NonNull Activity activity) {
136123
.subscribeOn(Schedulers.io())
137124
.observeOn(AndroidSchedulers.mainThread())
138125
.subscribe((result) -> {
139-
String message = "";
140-
String title = "";
126+
String message;
127+
String title;
141128

142129
if (result) {
143130
title = context.getString(R.string.check_category_success_title);
144131
message = context.getString(R.string.check_category_success_message, media.getDisplayTitle());
132+
reviewCallback.onSuccess();
145133
} else {
146134
title = context.getString(R.string.check_category_failure_title);
147135
message = context.getString(R.string.check_category_failure_message, media.getDisplayTitle());
136+
reviewCallback.onFailure();
148137
}
149138

150139
notificationBuilder.setDefaults(NotificationCompat.DEFAULT_ALL)
@@ -176,15 +165,13 @@ private void publishProgress(@NonNull Context context, int i) {
176165
notificationManager.notify(NOTIFICATION_CHECK_CATEGORY, notificationBuilder.build());
177166
}
178167

179-
@SuppressLint("CheckResult")
180-
public void sendThanks(@NonNull Activity activity) {
168+
@SuppressLint({"CheckResult", "StringFormatInvalid"})
169+
void sendThanks(@NonNull Activity activity) {
181170
Context context = activity.getApplicationContext();
182171
ApplicationlessInjection
183172
.getInstance(context)
184173
.getCommonsApplicationComponent()
185174
.inject(this);
186-
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
187-
notificationBuilder = new NotificationCompat.Builder(context);
188175
Toast toast = new Toast(context);
189176
toast.setGravity(Gravity.CENTER, 0, 0);
190177
toast = Toast.makeText(context, context.getString(R.string.send_thank_toast, media.getDisplayTitle()), Toast.LENGTH_SHORT);
@@ -200,7 +187,7 @@ public void sendThanks(@NonNull Activity activity) {
200187

201188
try {
202189
editToken = mwApi.getEditToken();
203-
if (editToken.equals("+\\")) {
190+
if (editToken == null) {
204191
return false;
205192
}
206193
publishProgress(context, 1);
@@ -238,4 +225,10 @@ public void sendThanks(@NonNull Activity activity) {
238225

239226
}, Timber::e);
240227
}
228+
229+
public interface ReviewCallback {
230+
void onSuccess();
231+
232+
void onFailure();
233+
}
241234
}

app/src/main/java/fr/free/nrw/commons/review/ReviewHelper.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.wikipedia.dataclient.mwapi.RecentChange;
77
import org.wikipedia.util.DateUtil;
88

9+
import java.util.Collections;
910
import java.util.Date;
1011
import java.util.Random;
1112

@@ -53,7 +54,7 @@ private Observable<RecentChange> getRecentChanges() {
5354
return reviewInterface.getRecentChanges(rcStart)
5455
.map(mwQueryResponse -> mwQueryResponse.query().getRecentChanges())
5556
.map(recentChanges -> {
56-
//Collections.shuffle(recentChanges);
57+
Collections.shuffle(recentChanges);
5758
return recentChanges;
5859
})
5960
.flatMapIterable(changes -> changes)
@@ -113,7 +114,8 @@ Observable<MwQueryPage.Revision> getFirstRevisionOfFile(String filename) {
113114
* @return
114115
*/
115116
private boolean isChangeReviewable(RecentChange recentChange) {
116-
if (recentChange.getType().equals("log") && !(recentChange.getOldRevisionId() == 0)) {
117+
if ((recentChange.getType().equals("log") && !(recentChange.getOldRevisionId() == 0))
118+
|| !recentChange.getType().equals("log")) {
117119
return false;
118120
}
119121

0 commit comments

Comments
 (0)