mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +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 javax.inject.Inject;
|
||||||
|
|
||||||
|
import fr.free.nrw.commons.R;
|
||||||
import fr.free.nrw.commons.mwapi.MediaWikiApi;
|
import fr.free.nrw.commons.mwapi.MediaWikiApi;
|
||||||
import fr.free.nrw.commons.theme.NavigationBaseActivity;
|
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;
|
import static fr.free.nrw.commons.auth.AccountUtil.AUTH_COOKIE;
|
||||||
|
|
||||||
|
|
@ -34,6 +39,8 @@ public abstract class AuthenticatedActivity extends NavigationBaseActivity {
|
||||||
if (savedInstanceState != null) {
|
if (savedInstanceState != null) {
|
||||||
authCookie = savedInstanceState.getString(AUTH_COOKIE);
|
authCookie = savedInstanceState.getString(AUTH_COOKIE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
showBlockStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -45,4 +52,20 @@ public abstract class AuthenticatedActivity extends NavigationBaseActivity {
|
||||||
protected abstract void onAuthCookieAcquired(String authCookie);
|
protected abstract void onAuthCookieAcquired(String authCookie);
|
||||||
|
|
||||||
protected abstract void onAuthFailure();
|
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
|
if (sessionManager.getCurrentAccount() != null
|
||||||
&& sessionManager.isUserLoggedIn()
|
&& sessionManager.isUserLoggedIn()
|
||||||
&& sessionManager.getCachedAuthCookie() != null) {
|
&& sessionManager.getCachedAuthCookie() != null) {
|
||||||
|
sessionManager.revalidateAuthToken();
|
||||||
startMainActivity();
|
startMainActivity();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -39,6 +39,7 @@ import java.util.Collections;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.TimeZone;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
|
|
||||||
import fr.free.nrw.commons.BuildConfig;
|
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) {
|
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
|
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 {
|
try {
|
||||||
return isoFormat.parse(mwDate);
|
return isoFormat.parse(mwDate);
|
||||||
} catch (ParseException e) {
|
} catch (ParseException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -85,6 +85,8 @@ public interface MediaWikiApi {
|
||||||
@NonNull
|
@NonNull
|
||||||
Single<Integer> getUploadCount(String userName);
|
Single<Integer> getUploadCount(String userName);
|
||||||
|
|
||||||
|
boolean isUserBlockedFromCommons();
|
||||||
|
|
||||||
interface ProgressListener {
|
interface ProgressListener {
|
||||||
void onProgress(long transferred, long total);
|
void onProgress(long transferred, long total);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -279,6 +279,7 @@
|
||||||
<string name="error_loading_images">Error occurred while loading images.</string>
|
<string name="error_loading_images">Error occurred while loading images.</string>
|
||||||
<string name="image_uploaded_by">Uploaded by: %1$s</string>
|
<string name="image_uploaded_by">Uploaded by: %1$s</string>
|
||||||
|
|
||||||
|
<string name="block_notification">You are blocked from editing Commons</string>
|
||||||
<string name="share_app_title">Share App</string>
|
<string name="share_app_title">Share App</string>
|
||||||
<string name="share_coordinates_not_present">Coordinates were not specified during image selection</string>
|
<string name="share_coordinates_not_present">Coordinates were not specified during image selection</string>
|
||||||
<string name="error_fetching_nearby_places">Error fetching nearby places.</string>
|
<string name="error_fetching_nearby_places">Error fetching nearby places.</string>
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ import org.robolectric.RobolectricTestRunner
|
||||||
import org.robolectric.RuntimeEnvironment
|
import org.robolectric.RuntimeEnvironment
|
||||||
import org.robolectric.annotation.Config
|
import org.robolectric.annotation.Config
|
||||||
import java.net.URLDecoder
|
import java.net.URLDecoder
|
||||||
|
import java.text.SimpleDateFormat
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner::class)
|
@RunWith(RobolectricTestRunner::class)
|
||||||
|
|
@ -225,6 +226,86 @@ class ApacheHttpClientMediaWikiApiTest {
|
||||||
assertEquals(23, testObserver.values()[0])
|
assertEquals(23, testObserver.values()[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun isUserBlockedFromCommonsForInfinitelyBlockedUser() {
|
||||||
|
server.enqueue(MockResponse().setBody("<?xml version=\"1.0\"?><api><query><userinfo id=\"1000\" name=\"testusername\" blockid=\"3000\" blockedby=\"blockerusername\" blockedbyid=\"1001\" blockreason=\"testing\" blockedtimestamp=\"2018-05-24T15:32:09Z\" blockexpiry=\"infinite\"></userinfo></query></api>"))
|
||||||
|
|
||||||
|
val result = testObject.isUserBlockedFromCommons();
|
||||||
|
|
||||||
|
assertBasicRequestParameters(server, "GET").let { userBlockedRequest ->
|
||||||
|
parseQueryParams(userBlockedRequest).let { body ->
|
||||||
|
assertEquals("xml", body["format"])
|
||||||
|
assertEquals("query", body["action"])
|
||||||
|
assertEquals("userinfo", body["meta"])
|
||||||
|
assertEquals("blockinfo", body["uiprop"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun isUserBlockedFromCommonsForTimeBlockedUser() {
|
||||||
|
val currentDate = Date()
|
||||||
|
val expiredDate = Date(currentDate.time + 10000)
|
||||||
|
val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
|
||||||
|
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"))
|
||||||
|
server.enqueue(MockResponse().setBody("<?xml version=\"1.0\"?><api><query><userinfo id=\"1000\" name=\"testusername\" blockid=\"3000\" blockedby=\"blockerusername\" blockedbyid=\"1001\" blockreason=\"testing\" blockedtimestamp=\"2018-05-24T15:32:09Z\" blockexpiry=\"" + dateFormat.format(expiredDate) + "\"></userinfo></query></api>"))
|
||||||
|
|
||||||
|
val result = testObject.isUserBlockedFromCommons();
|
||||||
|
|
||||||
|
assertBasicRequestParameters(server, "GET").let { userBlockedRequest ->
|
||||||
|
parseQueryParams(userBlockedRequest).let { body ->
|
||||||
|
assertEquals("xml", body["format"])
|
||||||
|
assertEquals("query", body["action"])
|
||||||
|
assertEquals("userinfo", body["meta"])
|
||||||
|
assertEquals("blockinfo", body["uiprop"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assertTrue(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun isUserBlockedFromCommonsForExpiredBlockedUser() {
|
||||||
|
val currentDate = Date()
|
||||||
|
val expiredDate = Date(currentDate.time - 10000)
|
||||||
|
val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
|
||||||
|
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"))
|
||||||
|
server.enqueue(MockResponse().setBody("<?xml version=\"1.0\"?><api><query><userinfo id=\"1000\" name=\"testusername\" blockid=\"3000\" blockedby=\"blockerusername\" blockedbyid=\"1001\" blockreason=\"testing\" blockedtimestamp=\"2018-05-24T15:32:09Z\" blockexpiry=\"" + dateFormat.format(expiredDate) + "\"></userinfo></query></api>"))
|
||||||
|
|
||||||
|
val result = testObject.isUserBlockedFromCommons();
|
||||||
|
|
||||||
|
assertBasicRequestParameters(server, "GET").let { userBlockedRequest ->
|
||||||
|
parseQueryParams(userBlockedRequest).let { body ->
|
||||||
|
assertEquals("xml", body["format"])
|
||||||
|
assertEquals("query", body["action"])
|
||||||
|
assertEquals("userinfo", body["meta"])
|
||||||
|
assertEquals("blockinfo", body["uiprop"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assertFalse(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun isUserBlockedFromCommonsForNotBlockedUser() {
|
||||||
|
server.enqueue(MockResponse().setBody("<?xml version=\"1.0\"?><api><query><userinfo id=\"1000\" name=\"testusername\"></userinfo></query></api>"))
|
||||||
|
|
||||||
|
val result = testObject.isUserBlockedFromCommons();
|
||||||
|
|
||||||
|
assertBasicRequestParameters(server, "GET").let { userBlockedRequest ->
|
||||||
|
parseQueryParams(userBlockedRequest).let { body ->
|
||||||
|
assertEquals("xml", body["format"])
|
||||||
|
assertEquals("query", body["action"])
|
||||||
|
assertEquals("userinfo", body["meta"])
|
||||||
|
assertEquals("blockinfo", body["uiprop"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assertFalse(result)
|
||||||
|
}
|
||||||
|
|
||||||
private fun assertBasicRequestParameters(server: MockWebServer, method: String): RecordedRequest = server.takeRequest().let {
|
private fun assertBasicRequestParameters(server: MockWebServer, method: String): RecordedRequest = server.takeRequest().let {
|
||||||
assertEquals("/", it.requestUrl.encodedPath())
|
assertEquals("/", it.requestUrl.encodedPath())
|
||||||
assertEquals(method, it.method)
|
assertEquals(method, it.method)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue