Skip to content

Commit 90d4e49

Browse files
authored
Migrated isUserBlockedFromCommons to retrofit (commons-app#3085)
* Switched wikimedia-android-data-client for new features * Added UserCLient and UserInterface * Migrated isUserBlockedFromCommons to retrofit * Added tests and removed dead code * Implemented ban checking functionality in UploadActivity * Removed unused class AuthenticatedActivity * Fixed tests * Fixed NullPointerException when a user accessed image details without logging in.
1 parent c6794f3 commit 90d4e49

11 files changed

+185
-255
lines changed

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

Lines changed: 0 additions & 40 deletions
This file was deleted.

app/src/main/java/fr/free/nrw/commons/di/CommonsApplicationModule.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.HashMap;
1616
import java.util.List;
1717
import java.util.Map;
18+
import java.util.Objects;
1819

1920
import javax.inject.Named;
2021
import javax.inject.Singleton;
@@ -194,6 +195,6 @@ public Scheduler providesMainThread() {
194195
@Named("username")
195196
@Provides
196197
public String provideLoggedInUsername() {
197-
return AppAdapter.get().getUserName();
198+
return Objects.toString(AppAdapter.get().getUserName(), "");
198199
}
199200
}

app/src/main/java/fr/free/nrw/commons/di/NetworkingModule.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ public MediaInterface provideMediaInterface(@Named(NAMED_COMMONS_WIKI_SITE) Wiki
230230
public CategoryInterface provideCategoryInterface(@Named(NAMED_COMMONS_WIKI_SITE) WikiSite commonsWikiSite) {
231231
return ServiceFactory.get(commonsWikiSite, BuildConfig.COMMONS_URL, CategoryInterface.class);
232232
}
233+
233234
@Provides
234235
@Singleton
235236
public UserInterface provideUserInterface(@Named(NAMED_COMMONS_WIKI_SITE) WikiSite commonsWikiSite) {

app/src/main/java/fr/free/nrw/commons/mwapi/ApacheHttpClientMediaWikiApi.java

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -49,47 +49,6 @@ public ApacheHttpClientMediaWikiApi(String apiURL) {
4949
api = new CustomMwApi(apiURL, httpClient);
5050
}
5151

52-
/**
53-
* Checks to see if a user is currently blocked from Commons
54-
*
55-
* @return whether or not the user is blocked from Commons
56-
*/
57-
@Override
58-
public boolean isUserBlockedFromCommons() {
59-
boolean userBlocked = false;
60-
try {
61-
CustomApiResult result = api.action("query")
62-
.param("action", "query")
63-
.param("format", "xml")
64-
.param("meta", "userinfo")
65-
.param("uiprop", "blockinfo")
66-
.get();
67-
if (result != null) {
68-
String blockEnd = result.getString("/api/query/userinfo/@blockexpiry");
69-
if (blockEnd.equals("infinite")) {
70-
userBlocked = true;
71-
} else if (!blockEnd.isEmpty()) {
72-
Date endDate = parseMWDate(blockEnd);
73-
Date current = new Date();
74-
userBlocked = endDate.after(current);
75-
}
76-
77-
}
78-
} catch (Exception e) {
79-
e.printStackTrace();
80-
}
81-
82-
return userBlocked;
83-
}
84-
85-
private Date parseMWDate(String mwDate) {
86-
try {
87-
return DateUtil.iso8601DateParse(mwDate);
88-
} catch (ParseException e) {
89-
throw new RuntimeException(e);
90-
}
91-
}
92-
9352
/**
9453
* Calls media wiki's logout API
9554
*/

app/src/main/java/fr/free/nrw/commons/mwapi/MediaWikiApi.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
public interface MediaWikiApi {
1111

12-
boolean isUserBlockedFromCommons();
13-
1412
void logout();
1513

1614
// Single<CampaignResponseDTO> getCampaigns();

app/src/main/java/fr/free/nrw/commons/mwapi/UserClient.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
package fr.free.nrw.commons.mwapi;
22

3+
import java.util.Collections;
4+
5+
import javax.inject.Inject;
6+
7+
import io.reactivex.Observable;
8+
39
import org.wikipedia.dataclient.mwapi.MwQueryLogEvent;
410
import org.wikipedia.dataclient.mwapi.MwQueryResponse;
511
import org.wikipedia.dataclient.mwapi.MwQueryResult;
12+
import org.wikipedia.dataclient.mwapi.UserInfo;
13+
import org.wikipedia.util.DateUtil;
614

7-
import java.util.Collections;
15+
import java.util.Date;
816

917
import javax.inject.Inject;
1018

11-
import io.reactivex.Observable;
19+
import io.reactivex.Single;
1220

1321
public class UserClient {
1422
private final UserInterface userInterface;
@@ -18,6 +26,29 @@ public UserClient(UserInterface userInterface) {
1826
this.userInterface = userInterface;
1927
}
2028

29+
/**
30+
* Checks to see if a user is currently blocked from Commons
31+
*
32+
* @return whether or not the user is blocked from Commons
33+
*/
34+
public Single<Boolean> isUserBlockedFromCommons() {
35+
return userInterface.getUserBlockInfo()
36+
.map(MwQueryResponse::query)
37+
.map(MwQueryResult::userInfo)
38+
.map(UserInfo::blockexpiry)
39+
.map(blockExpiry -> {
40+
if (blockExpiry.isEmpty())
41+
return false;
42+
else if ("infinite".equals(blockExpiry))
43+
return true;
44+
else {
45+
Date endDate = DateUtil.iso8601DateParse(blockExpiry);
46+
Date current = new Date();
47+
return endDate.after(current);
48+
}
49+
}).single(false);
50+
}
51+
2152
public Observable<MwQueryLogEvent> logEvents(String user) {
2253
try {
2354
return userInterface.getUserLogEvents(user, Collections.emptyMap())

app/src/main/java/fr/free/nrw/commons/mwapi/UserInterface.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@
1212
import static org.wikipedia.dataclient.Service.MW_API_PREFIX;
1313

1414
public interface UserInterface {
15+
16+
/**
17+
* Gets the log events of user
18+
* @param user name of user without prefix
19+
* @param continuation continuation params returned in previous query
20+
* @return query response
21+
*/
22+
1523
@GET(MW_API_PREFIX+"action=query&list=logevents&letype=upload&leprop=title|timestamp|ids&lelimit=500")
1624
Observable<MwQueryResponse> getUserLogEvents(@Query("leuser") String user, @QueryMap Map<String, String> continuation);
25+
26+
/**
27+
* Checks to see if a user is currently blocked from Commons
28+
*/
29+
@GET(MW_API_PREFIX + "action=query&meta=userinfo&uiprop=blockinfo")
30+
Observable<MwQueryResponse> getUserBlockInfo();
1731
}

0 commit comments

Comments
 (0)