Updated isUserBlocked method to check the expiration time instead of just if an expiration time exists. Updated tests accordingly

This commit is contained in:
Sean Nemann 2018-05-31 11:28:56 -04:00
parent 01a3a233a9
commit fcfae643c3
2 changed files with 42 additions and 3 deletions

View file

@ -37,6 +37,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;
@ -630,8 +631,17 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
.param("uiprop", "blockinfo")
.get();
if(result != null) {
// the blockexpiry field will only be present if the user is currently blocked
userBlocked = !result.getString("/api/query/userinfo/@blockexpiry").isEmpty();
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();
@ -642,10 +652,12 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
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);
}
}
}