Skip to content

Commit 53d0d5e

Browse files
ashishkumar468misaochan
authored andcommitted
On Logout, fetch the CSRF token and then make the post logout call (commons-app#3182)
* Api call for logout * call clear cached onCompleteSessionLogout
1 parent 958bbbd commit 53d0d5e

File tree

5 files changed

+122
-45
lines changed

5 files changed

+122
-45
lines changed

app/src/main/java/fr/free/nrw/commons/CommonsApplication.java

+27-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package fr.free.nrw.commons;
22

3+
import static org.acra.ReportField.ANDROID_VERSION;
4+
import static org.acra.ReportField.APP_VERSION_CODE;
5+
import static org.acra.ReportField.APP_VERSION_NAME;
6+
import static org.acra.ReportField.PHONE_MODEL;
7+
import static org.acra.ReportField.STACK_TRACE;
8+
import static org.acra.ReportField.USER_COMMENT;
9+
310
import android.annotation.SuppressLint;
411
import android.app.Application;
512
import android.app.NotificationChannel;
@@ -9,27 +16,12 @@
916
import android.os.Build;
1017
import android.os.Process;
1118
import android.util.Log;
12-
1319
import androidx.annotation.NonNull;
14-
1520
import com.facebook.drawee.backends.pipeline.Fresco;
21+
import com.facebook.imagepipeline.core.ImagePipeline;
1622
import com.facebook.imagepipeline.core.ImagePipelineConfig;
1723
import com.squareup.leakcanary.LeakCanary;
1824
import com.squareup.leakcanary.RefWatcher;
19-
20-
import org.acra.ACRA;
21-
import org.acra.annotation.AcraCore;
22-
import org.acra.annotation.AcraDialog;
23-
import org.acra.annotation.AcraMailSender;
24-
import org.acra.data.StringFormat;
25-
import org.wikipedia.AppAdapter;
26-
import org.wikipedia.language.AppLanguageLookUpTable;
27-
28-
import java.io.File;
29-
30-
import javax.inject.Inject;
31-
import javax.inject.Named;
32-
3325
import fr.free.nrw.commons.auth.SessionManager;
3426
import fr.free.nrw.commons.bookmarks.locations.BookmarkLocationsDao;
3527
import fr.free.nrw.commons.bookmarks.pictures.BookmarkPicturesDao;
@@ -48,15 +40,18 @@
4840
import io.reactivex.internal.functions.Functions;
4941
import io.reactivex.plugins.RxJavaPlugins;
5042
import io.reactivex.schedulers.Schedulers;
43+
import java.io.File;
44+
import javax.inject.Inject;
45+
import javax.inject.Named;
46+
import org.acra.ACRA;
47+
import org.acra.annotation.AcraCore;
48+
import org.acra.annotation.AcraDialog;
49+
import org.acra.annotation.AcraMailSender;
50+
import org.acra.data.StringFormat;
51+
import org.wikipedia.AppAdapter;
52+
import org.wikipedia.language.AppLanguageLookUpTable;
5153
import timber.log.Timber;
5254

53-
import static org.acra.ReportField.ANDROID_VERSION;
54-
import static org.acra.ReportField.APP_VERSION_CODE;
55-
import static org.acra.ReportField.APP_VERSION_NAME;
56-
import static org.acra.ReportField.PHONE_MODEL;
57-
import static org.acra.ReportField.STACK_TRACE;
58-
import static org.acra.ReportField.USER_COMMENT;
59-
6055
@AcraCore(
6156
buildConfigClass = BuildConfig.class,
6257
resReportSendSuccessToast = R.string.crash_dialog_ok_toast,
@@ -250,6 +245,7 @@ public void clearApplicationData(Context context, LogoutListener logoutListener)
250245
.observeOn(AndroidSchedulers.mainThread())
251246
.subscribe(() -> {
252247
Timber.d("All accounts have been removed");
248+
clearImageCache();
253249
//TODO: fix preference manager
254250
defaultPrefs.clearAll();
255251
defaultPrefs.putBoolean("firstrun", false);
@@ -258,6 +254,14 @@ public void clearApplicationData(Context context, LogoutListener logoutListener)
258254
});
259255
}
260256

257+
/**
258+
* Clear all images cache held by Fresco
259+
*/
260+
private void clearImageCache() {
261+
ImagePipeline imagePipeline = Fresco.getImagePipeline();
262+
imagePipeline.clearCaches();
263+
}
264+
261265
/**
262266
* Deletes all tables and re-creates them.
263267
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package fr.free.nrw.commons.auth;
2+
3+
4+
import io.reactivex.Observable;
5+
import java.util.Objects;
6+
import javax.inject.Inject;
7+
import javax.inject.Named;
8+
import javax.inject.Singleton;
9+
import org.wikipedia.dataclient.Service;
10+
import org.wikipedia.dataclient.ServiceFactory;
11+
import org.wikipedia.dataclient.WikiSite;
12+
import org.wikipedia.dataclient.mwapi.MwPostResponse;
13+
14+
/**
15+
* Handler for logout
16+
*/
17+
@Singleton
18+
public class LogoutClient {
19+
20+
private final Service service;
21+
22+
@Inject
23+
public LogoutClient(@Named("commons-wikisite")
24+
WikiSite commonsWikiSite) {
25+
service = ServiceFactory.get(commonsWikiSite);
26+
}
27+
28+
/**
29+
* Fetches the CSRF token and uses that to post the logout api call
30+
* @return
31+
*/
32+
public Observable<MwPostResponse> postLogout() {
33+
return service.getCsrfToken().concatMap(tokenResponse -> service.postLogout(
34+
Objects.requireNonNull(Objects.requireNonNull(tokenResponse.query()).csrfToken())));
35+
}
36+
}

app/src/main/java/fr/free/nrw/commons/auth/SessionManager.java

-9
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,6 @@ public Completable logout() {
144144
.map(a -> accountManager.removeAccount(a, null, null).getResult()))
145145
.doOnComplete(() -> {
146146
currentAccount = null;
147-
clearImageCache();
148147
});
149148
}
150-
151-
/**
152-
* Clear all images cache held by Fresco
153-
*/
154-
private void clearImageCache(){
155-
ImagePipeline imagePipeline = Fresco.getImagePipeline();
156-
imagePipeline.clearCaches();
157-
}
158149
}

app/src/main/java/fr/free/nrw/commons/contributions/ContributionViewHolder.java

+1-10
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void init(int position, DisplayableContribution contribution) {
106106
* @param contribution
107107
*/
108108
private void fetchAndDisplayThumbnail(DisplayableContribution contribution) {
109-
String keyForLRUCache = getKeyForLRUCache(contribution.getContentUri());
109+
String keyForLRUCache = contribution.getFilename();
110110
String cacheUrl = thumbnailCache.get(keyForLRUCache);
111111
if (!StringUtils.isBlank(cacheUrl)) {
112112
imageView.setImageURI(cacheUrl);
@@ -132,15 +132,6 @@ private void fetchAndDisplayThumbnail(DisplayableContribution contribution) {
132132

133133
}
134134

135-
/**
136-
* Returns image key for the LRU cache, basically the id of the image, (the content uri is the ""+/id)
137-
* @param contentUri
138-
* @return
139-
*/
140-
private String getKeyForLRUCache(Uri contentUri) {
141-
return contentUri.getLastPathSegment();
142-
}
143-
144135
public void clear() {
145136
compositeDisposable.clear();
146137
}

app/src/main/java/fr/free/nrw/commons/theme/NavigationBaseActivity.java

+58-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package fr.free.nrw.commons.theme;
22

3+
import static fr.free.nrw.commons.di.NetworkingModule.NAMED_COMMONS_WIKI_SITE;
4+
35
import android.accounts.Account;
46
import android.accounts.AccountManager;
57
import android.app.ActivityManager;
8+
import android.app.ProgressDialog;
69
import android.content.ActivityNotFoundException;
710
import android.content.Context;
811
import android.content.Intent;
@@ -22,6 +25,10 @@
2225
import android.widget.TextView;
2326
import android.widget.Toast;
2427

28+
import fr.free.nrw.commons.auth.LogoutClient;
29+
import io.reactivex.android.schedulers.AndroidSchedulers;
30+
import io.reactivex.disposables.CompositeDisposable;
31+
import io.reactivex.schedulers.Schedulers;
2532
import javax.inject.Inject;
2633
import javax.inject.Named;
2734

@@ -40,6 +47,8 @@
4047
import fr.free.nrw.commons.logging.CommonsLogSender;
4148
import fr.free.nrw.commons.review.ReviewActivity;
4249
import fr.free.nrw.commons.settings.SettingsActivity;
50+
import org.wikipedia.dataclient.Service;
51+
import org.wikipedia.dataclient.WikiSite;
4352
import timber.log.Timber;
4453

4554
public abstract class NavigationBaseActivity extends BaseActivity
@@ -61,6 +70,15 @@ public abstract class NavigationBaseActivity extends BaseActivity
6170

6271
private ActionBarDrawerToggle toggle;
6372

73+
@Inject
74+
LogoutClient logoutClient;
75+
76+
77+
private CompositeDisposable disposable = new CompositeDisposable();
78+
private Service service;
79+
80+
private ProgressDialog progressDialog;
81+
6482
public void initDrawer() {
6583
navigationView.setNavigationItemSelectedListener(this);
6684

@@ -201,9 +219,7 @@ public boolean onNavigationItemSelected(@NonNull final MenuItem item) {
201219
.setMessage(R.string.logout_verification)
202220
.setCancelable(false)
203221
.setPositiveButton(R.string.yes, (dialog, which) -> {
204-
BaseLogoutListener logoutListener = new BaseLogoutListener();
205-
CommonsApplication app = (CommonsApplication) getApplication();
206-
app.clearApplicationData(this, logoutListener);
222+
handleLogout();
207223
})
208224
.setNegativeButton(R.string.no, (dialog, which) -> dialog.cancel())
209225
.show();
@@ -227,6 +243,36 @@ public boolean onNavigationItemSelected(@NonNull final MenuItem item) {
227243
}
228244
}
229245

246+
/**
247+
* Ask the logout client to post the logout api
248+
*/
249+
private void handleLogout() {
250+
if (null == progressDialog) {
251+
progressDialog = new ProgressDialog(this);
252+
progressDialog.setMessage(getString(R.string.please_wait));
253+
}
254+
255+
progressDialog.show();
256+
257+
disposable.add(logoutClient.postLogout()
258+
.subscribeOn(Schedulers.io())
259+
.observeOn(AndroidSchedulers.mainThread())
260+
.subscribe(mwQueryResponse -> {
261+
BaseLogoutListener logoutListener = new BaseLogoutListener();
262+
CommonsApplication app = (CommonsApplication) getApplication();
263+
app.clearApplicationData(this, logoutListener);
264+
progressDialog.cancel();
265+
},
266+
t -> {
267+
progressDialog.cancel();
268+
Toast.makeText(NavigationBaseActivity.this,
269+
t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
270+
Timber.e(t, "Something went wrong with post logout api: %s", t
271+
.getLocalizedMessage());
272+
}
273+
));
274+
}
275+
230276
private class BaseLogoutListener implements CommonsApplication.LogoutListener {
231277
@Override
232278
public void onLogoutComplete() {
@@ -294,4 +340,13 @@ public void onBackPressed() {
294340
super.onBackPressed();
295341
}
296342
}
343+
344+
@Override
345+
protected void onStop() {
346+
super.onStop();
347+
disposable.clear();
348+
if (progressDialog != null && progressDialog.isShowing()) {
349+
progressDialog.cancel();
350+
}
351+
}
297352
}

0 commit comments

Comments
 (0)