mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 21:03:54 +01:00
Merge pull request #1573 from seannemann21/show-block-status
Show Block Status if use is blocked
This commit is contained in:
commit
b4d556a648
6 changed files with 145 additions and 0 deletions
|
|
@ -4,8 +4,13 @@ import android.os.Bundle;
|
|||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import fr.free.nrw.commons.R;
|
||||
import fr.free.nrw.commons.mwapi.MediaWikiApi;
|
||||
import fr.free.nrw.commons.theme.NavigationBaseActivity;
|
||||
import fr.free.nrw.commons.utils.ViewUtil;
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
|
||||
import static fr.free.nrw.commons.auth.AccountUtil.AUTH_COOKIE;
|
||||
|
||||
|
|
@ -34,6 +39,8 @@ public abstract class AuthenticatedActivity extends NavigationBaseActivity {
|
|||
if (savedInstanceState != null) {
|
||||
authCookie = savedInstanceState.getString(AUTH_COOKIE);
|
||||
}
|
||||
|
||||
showBlockStatus();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -45,4 +52,20 @@ public abstract class AuthenticatedActivity extends NavigationBaseActivity {
|
|||
protected abstract void onAuthCookieAcquired(String authCookie);
|
||||
|
||||
protected abstract void onAuthFailure();
|
||||
|
||||
/**
|
||||
* Makes API call to check if user is blocked from Commons. If the user is blocked, a snackbar
|
||||
* is created to notify the user
|
||||
*/
|
||||
protected void showBlockStatus()
|
||||
{
|
||||
Observable.fromCallable(() -> mediaWikiApi.isUserBlockedFromCommons())
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.filter(result -> result)
|
||||
.subscribe(result -> {
|
||||
ViewUtil.showSnackbar(findViewById(android.R.id.content), R.string.block_notification);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -166,6 +166,7 @@ public class LoginActivity extends AccountAuthenticatorActivity {
|
|||
if (sessionManager.getCurrentAccount() != null
|
||||
&& sessionManager.isUserLoggedIn()
|
||||
&& sessionManager.getCachedAuthCookie() != null) {
|
||||
sessionManager.revalidateAuthToken();
|
||||
startMainActivity();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ import java.util.Collections;
|
|||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import fr.free.nrw.commons.BuildConfig;
|
||||
|
|
@ -724,12 +725,48 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if a user is currently blocked from Commons
|
||||
* @return whether or not the user is blocked from Commons
|
||||
*/
|
||||
@Override
|
||||
public boolean isUserBlockedFromCommons() {
|
||||
boolean userBlocked = false;
|
||||
try {
|
||||
ApiResult result = api.action("query")
|
||||
.param("action", "query")
|
||||
.param("format", "xml")
|
||||
.param("meta", "userinfo")
|
||||
.param("uiprop", "blockinfo")
|
||||
.get();
|
||||
if(result != null) {
|
||||
String blockEnd = result.getString("/api/query/userinfo/@blockexpiry");
|
||||
if(blockEnd.equals("infinite"))
|
||||
{
|
||||
userBlocked = true;
|
||||
}
|
||||
else if (!blockEnd.isEmpty()) {
|
||||
Date endDate = parseMWDate(blockEnd);
|
||||
Date current = new Date();
|
||||
userBlocked = endDate.after(current);
|
||||
}
|
||||
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return userBlocked;
|
||||
}
|
||||
|
||||
private Date parseMWDate(String mwDate) {
|
||||
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ENGLISH); // Assuming MW always gives me UTC
|
||||
isoFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
try {
|
||||
return isoFormat.parse(mwDate);
|
||||
} catch (ParseException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -85,6 +85,8 @@ public interface MediaWikiApi {
|
|||
@NonNull
|
||||
Single<Integer> getUploadCount(String userName);
|
||||
|
||||
boolean isUserBlockedFromCommons();
|
||||
|
||||
interface ProgressListener {
|
||||
void onProgress(long transferred, long total);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue