Skip to content

Commit 97c1aab

Browse files
authored
Merge pull request #217 from leancloud/feat_friend_block
add friendship block interface and update friendship query
2 parents 02258e5 + 0c86926 commit 97c1aab

File tree

8 files changed

+533
-23
lines changed

8 files changed

+533
-23
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cn.leancloud;
2+
3+
import cn.leancloud.annotation.LCClassName;
4+
5+
@LCClassName("_Blocklist")
6+
public class LCBlockRelation extends LCObject {
7+
public static final String CLASS_NAME = "_Blocklist";
8+
9+
public static final String ATTR_BLOCK_USER = "blockedUser";// user who was added in block list
10+
11+
public LCBlockRelation() {
12+
super(CLASS_NAME);
13+
}
14+
15+
public LCBlockRelation(LCObject object) {
16+
super(object);
17+
setClassName(CLASS_NAME);
18+
}
19+
20+
public LCUser getUser(){
21+
return getLCObject(ATTR_BLOCK_USER);
22+
}
23+
24+
public void setUser(LCUser blockedUser){
25+
put(ATTR_BLOCK_USER, blockedUser);
26+
}
27+
28+
29+
}

core/src/main/java/cn/leancloud/LCQuery.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@
22

33
import cn.leancloud.cache.QueryResultCache;
44
import cn.leancloud.core.PaasClient;
5-
import cn.leancloud.query.LCCloudQueryResult;
65
import cn.leancloud.query.QueryConditions;
76
import cn.leancloud.query.QueryOperation;
87
import cn.leancloud.types.LCGeoPoint;
98
import cn.leancloud.types.LCNull;
109
import cn.leancloud.utils.LCUtils;
1110
import cn.leancloud.utils.LogUtil;
1211
import cn.leancloud.utils.StringUtil;
13-
14-
import java.util.*;
15-
1612
import io.reactivex.Observable;
1713
import io.reactivex.ObservableSource;
1814
import io.reactivex.functions.Function;
1915

16+
import java.util.*;
17+
2018
public class LCQuery<T extends LCObject> implements Cloneable {
2119
private static final LCLogger LOGGER = LogUtil.getLogger(LCQuery.class);
2220

@@ -27,6 +25,8 @@ public enum CachePolicy {
2725

2826
private Class<T> clazz;
2927
private String className;
28+
//custom endPoint , if set , query url will be changed to custom path by append the endPoint , else use common className
29+
private String endPoint;
3030
private java.lang.Boolean isRunning;
3131
private CachePolicy cachePolicy = CachePolicy.IGNORE_CACHE;
3232
private long maxCacheAge = -1;
@@ -54,6 +54,7 @@ public LCQuery clone() throws CloneNotSupportedException {
5454
query.isRunning = false;
5555
query.cachePolicy = this.cachePolicy;
5656
query.maxCacheAge = this.maxCacheAge;
57+
query.endPoint = this.endPoint;
5758
query.conditions = null != this.conditions? this.conditions.clone(): null;
5859
return query;
5960
}
@@ -65,6 +66,11 @@ public LCQuery clone() throws CloneNotSupportedException {
6566
this.conditions = new QueryConditions();
6667
}
6768

69+
LCQuery(String theClassName, Class<T> clazz, String endPoint) {
70+
this(theClassName, clazz);
71+
this.endPoint = endPoint;
72+
}
73+
6874
/**
6975
* Constructs a query. A default query with no further parameters will retrieve all AVObjects of
7076
* the provided class.
@@ -124,6 +130,10 @@ Map<String, List<QueryOperation>> getWhere() {
124130
return conditions.getWhere();
125131
}
126132

133+
void setEndPoint(String endPoint) {
134+
this.endPoint = endPoint;
135+
}
136+
127137
/**
128138
* Get class name
129139
* @return class name.
@@ -942,7 +952,7 @@ protected Observable<List<T>> findInBackground(LCUser asAuthenticatedUser, int e
942952
query.put("limit", Integer.toString(explicitLimit));
943953
}
944954
LOGGER.d("Query: " + query);
945-
return PaasClient.getStorageClient().queryObjects(asAuthenticatedUser, getClassName(), query, this.cachePolicy, this.maxCacheAge)
955+
return PaasClient.getStorageClient().queryObjects(asAuthenticatedUser, getClassName(), endPoint, query, this.cachePolicy, this.maxCacheAge)
946956
.map(new Function<List<LCObject>, List<T>>() {
947957
public List<T> apply(List<LCObject> var1) throws Exception {
948958
LOGGER.d("invoke within AVQuery.findInBackground(). resultSize=" + var1.size());

core/src/main/java/cn/leancloud/LCUser.java

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,21 @@
22

33
import cn.leancloud.annotation.LCClassName;
44
import cn.leancloud.cache.PersistenceUtil;
5-
import cn.leancloud.callback.LCCallback;
65
import cn.leancloud.callback.FollowersAndFolloweesCallback;
6+
import cn.leancloud.callback.LCCallback;
77
import cn.leancloud.core.AppConfiguration;
88
import cn.leancloud.core.PaasClient;
9-
import cn.leancloud.core.StorageClient;
109
import cn.leancloud.gson.ObjectDeserializer;
10+
import cn.leancloud.json.JSON;
11+
import cn.leancloud.json.JSONObject;
1112
import cn.leancloud.ops.Utils;
1213
import cn.leancloud.query.QueryConditions;
1314
import cn.leancloud.sms.LCSMS;
1415
import cn.leancloud.sms.LCSMSOption;
1516
import cn.leancloud.types.LCNull;
16-
import cn.leancloud.utils.LCUtils;
1717
import cn.leancloud.utils.ErrorUtils;
18+
import cn.leancloud.utils.LCUtils;
1819
import cn.leancloud.utils.StringUtil;
19-
import cn.leancloud.json.JSON;
20-
import cn.leancloud.json.JSONObject;
2120
import io.reactivex.Observable;
2221
import io.reactivex.Observer;
2322
import io.reactivex.annotations.NonNull;
@@ -1337,6 +1336,8 @@ public LCQuery<LCFriendship> friendshipQuery(boolean isFollowerDirection) {
13371336
} else {
13381337
query.whereEqualTo(LCFriendship.ATTR_USER, LCUser.createWithoutData(CLASS_NAME, userObjectId));
13391338
query.include(LCFriendship.ATTR_FOLLOWEE);
1339+
// filter blocked user
1340+
query.setEndPoint("users/self/friends");
13401341
}
13411342
query.whereEqualTo(LCFriendship.ATTR_FRIEND_STATUS, true);
13421343
return query;
@@ -1368,6 +1369,7 @@ public Observable<List<LCFriendship>> queryFriendship(int offset, int limit, Str
13681369
if (!StringUtil.isEmpty(orderBy)) {
13691370
conditions.setOrder(orderBy);
13701371
}
1372+
conditions.include(LCFriendship.ATTR_FOLLOWEE);
13711373
return PaasClient.getStorageClient().queryFriendship(this, conditions.assembleParameters());
13721374
}
13731375

@@ -1455,6 +1457,65 @@ public Observable<LCFriendship> updateFriendship(LCUser asAuthenticatedUser, LCF
14551457
getObjectId(), friendship.getFollowee().getObjectId(), param);
14561458
}
14571459

1460+
/**
1461+
* block somebody in background.
1462+
* @param objectId target user objectId.
1463+
* @return observable instance.
1464+
*/
1465+
public Observable<JSONObject> blockFriendInBackground(String objectId){
1466+
if (!checkUserAuthentication(null)) {
1467+
return Observable.error(ErrorUtils.propagateException(LCException.SESSION_MISSING,
1468+
"No valid session token, make sure signUp or login has been called."));
1469+
}
1470+
if (StringUtil.isEmpty(objectId)) {
1471+
return Observable.error(ErrorUtils.propagateException(LCException.INVALID_PARAMETER,
1472+
"objectId is invalid."));
1473+
}
1474+
1475+
return PaasClient.getStorageClient().blockFriend(this, objectId);
1476+
1477+
}
1478+
1479+
/**
1480+
* unblock somebody in background.
1481+
* @param objectId target user objectId.
1482+
* @return observable instance.
1483+
*/
1484+
public Observable<JSONObject> unblockFriendInBackground(String objectId){
1485+
if (!checkUserAuthentication(null)) {
1486+
return Observable.error(ErrorUtils.propagateException(LCException.SESSION_MISSING,
1487+
"No valid session token, make sure signUp or login has been called."));
1488+
}
1489+
if (StringUtil.isEmpty(objectId)) {
1490+
return Observable.error(ErrorUtils.propagateException(LCException.INVALID_PARAMETER,
1491+
"objectId is invalid."));
1492+
}
1493+
1494+
return PaasClient.getStorageClient().unblockFriend(this, objectId);
1495+
1496+
}
1497+
1498+
/**
1499+
* get block query.
1500+
* @return query instance.
1501+
*/
1502+
public LCQuery<LCObject> friendshipBlockQuery() {
1503+
return LCUser.friendshipBlockQuery(LCObject.class);
1504+
}
1505+
1506+
/**
1507+
* get block query.
1508+
* @param clazz result class.
1509+
* @param <T> template type.
1510+
* @return query instance.
1511+
*/
1512+
public static <T extends LCObject> LCQuery<T> friendshipBlockQuery(Class<T> clazz){
1513+
LCQuery<T> query = new LCQuery<>(LCBlockRelation.CLASS_NAME, clazz);
1514+
query.include("blockedUser");
1515+
return query;
1516+
}
1517+
1518+
14581519
/**
14591520
* accept a friendship.
14601521
* @param request friendship request.

core/src/main/java/cn/leancloud/Transformer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public static String getSubClassName(Class clazz) {
3232
return LCFile.CLASS_NAME;
3333
} else if (LCFriendship.class.isAssignableFrom(clazz)) {
3434
return LCFriendship.CLASS_NAME;
35+
} else if (LCBlockRelation.class.isAssignableFrom(clazz)) {
36+
return LCBlockRelation.CLASS_NAME;
3537
} else if (LCFriendshipRequest.class.isAssignableFrom(clazz)) {
3638
return LCFriendshipRequest.CLASS_NAME;
3739
} else {
@@ -83,6 +85,8 @@ public static <T extends LCObject> T transform(LCObject rawObj, Class<T> clazz)
8385
result = new LCFriendshipRequest();
8486
} else if (LCFriendship.class.isAssignableFrom(clazz)) {
8587
result = new LCFriendship();
88+
} else if (LCBlockRelation.class.isAssignableFrom(clazz)) {
89+
result = new LCBlockRelation();
8690
} else {
8791
result = new LCObject(clazz.getSimpleName());
8892
}
@@ -113,6 +117,8 @@ public static LCObject objectFromClassName(String className) {
113117
result = new LCFriendshipRequest();
114118
} else if (LCFriendship.CLASS_NAME.equals(className)) {
115119
result = new LCFriendship();
120+
} else if (LCBlockRelation.CLASS_NAME.equals(className)) {
121+
result = new LCBlockRelation();
116122
} else if (subClassesMAP.containsKey(className)) {
117123
try {
118124
result = subClassesMAP.get(className).newInstance();

core/src/main/java/cn/leancloud/core/StorageClient.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
public class StorageClient {
3131
private static LCLogger LOGGER = LogUtil.getLogger(StorageClient.class);
32+
private static final String CUSTOM_ENDPOINT_PREFIX = "CUSTOM_ENDPOINT_";
3233

3334
private APIService apiService = null;
3435
private boolean asynchronized = false;
@@ -127,9 +128,15 @@ private Observable<LCQueryResult> queryRemoteServer(final LCUser authenticatedUs
127128
String authenticatedSession = getSessionToken(authenticatedUser);
128129
if (LCUser.CLASS_NAME.equalsIgnoreCase(className)) {
129130
return wrapObservable(apiService.queryUsers(authenticatedSession, query));
130-
} else {
131+
}else if(LCBlockRelation.CLASS_NAME.equalsIgnoreCase(className)){
132+
return wrapObservable(apiService.getBlockListOfFriend(authenticatedSession, query));
133+
} else if(className.startsWith(CUSTOM_ENDPOINT_PREFIX)){
134+
String endPoint = className.substring(CUSTOM_ENDPOINT_PREFIX.length());
135+
return wrapObservable(apiService.queryObjectsByCustomEndPoint(authenticatedSession, endPoint, query));
136+
} else{
131137
return wrapObservable(apiService.queryObjects(authenticatedSession, className, query));
132138
}
139+
133140
}
134141

135142
public Observable<List<LCUser>> strictlyQueryUsers(final LCUser authenticatedUser,
@@ -161,11 +168,14 @@ public List<LCUser> apply(List<LCObject> var1) throws Exception {
161168

162169

163170
public Observable<List<LCObject>> queryObjects(final LCUser authenticatedUser,
164-
final String className, final Map<String, String> query,
171+
final String className, final String endPoint, final Map<String, String> query,
165172
LCQuery.CachePolicy cachePolicy, final long maxAgeInMilliseconds) {
166173
final String cacheKey = QueryResultCache.generateKeyForQueryCondition(className, query);
167174
Observable<List<LCObject>> result = null;
168175
Observable<LCQueryResult> queryResult = null;
176+
//check whether use endPoint
177+
final String validPath = (endPoint == null || endPoint.length() == 0) ? className : (CUSTOM_ENDPOINT_PREFIX+endPoint);
178+
169179
switch (cachePolicy) {
170180
case CACHE_ONLY:
171181
result = wrapObservable(
@@ -178,8 +188,7 @@ public Observable<List<LCObject>> queryObjects(final LCUser authenticatedUser,
178188
@Override
179189
public ObservableSource<? extends List<LCObject>> apply(Throwable throwable) throws Exception {
180190
LOGGER.d("failed to query local cache, cause: " + throwable.getMessage() + ", try to query networking");
181-
182-
return queryRemoteServer(authenticatedUser, className, query)
191+
return queryRemoteServer(authenticatedUser, validPath, query)
183192
.map(new Function<LCQueryResult, List<LCObject>>() {
184193
public List<LCObject> apply(LCQueryResult o) throws Exception {
185194
o.setClassName(className);
@@ -196,7 +205,7 @@ public List<LCObject> apply(LCQueryResult o) throws Exception {
196205
});
197206
break;
198207
case NETWORK_ELSE_CACHE:
199-
queryResult = queryRemoteServer(authenticatedUser, className, query);
208+
queryResult = queryRemoteServer(authenticatedUser, validPath, query);
200209
if (null != queryResult) {
201210
result = queryResult.map(new Function<LCQueryResult, List<LCObject>>() {
202211
public List<LCObject> apply(LCQueryResult o) throws Exception {
@@ -221,7 +230,7 @@ public ObservableSource<? extends List<LCObject>> apply(Throwable throwable) thr
221230
break;
222231
case IGNORE_CACHE:
223232
default:
224-
queryResult = queryRemoteServer(authenticatedUser, className, query);
233+
queryResult = queryRemoteServer(authenticatedUser, validPath, query);
225234
if (null != queryResult) {
226235
result = queryResult.map(new Function<LCQueryResult, List<LCObject>>() {
227236
public List<LCObject> apply(LCQueryResult o) throws Exception {
@@ -616,7 +625,7 @@ public Observable<JSONObject> getFollowersAndFollowees(final LCUser authenticate
616625
public Observable<List<LCFriendship>> queryFriendship(final LCUser authenticatedUser, Map<String, String> conditions) {
617626
String authenticatedSession = getSessionToken(authenticatedUser);
618627
return wrapObservable(
619-
apiService.getFollowees(authenticatedSession, authenticatedUser.getObjectId(), conditions)
628+
apiService.getFriends(authenticatedSession, conditions)
620629
.map(new Function<LCQueryResult, List<LCFriendship>>() {
621630
@Override
622631
public List<LCFriendship> apply(LCQueryResult result) throws Exception {
@@ -633,6 +642,16 @@ public List<LCFriendship> apply(LCQueryResult result) throws Exception {
633642
}));
634643
}
635644

645+
public Observable<JSONObject> blockFriend(final LCUser authenticatedUser, String objectId) {
646+
String authenticatedSession = getSessionToken(authenticatedUser);
647+
return wrapObservable(apiService.blockFriendByObjectId(authenticatedSession, objectId));
648+
}
649+
650+
public Observable<JSONObject> unblockFriend(final LCUser authenticatedUser, String objectId) {
651+
String authenticatedSession = getSessionToken(authenticatedUser);
652+
return wrapObservable(apiService.unblockFriendByObjectId(authenticatedSession, objectId));
653+
}
654+
636655
public Observable<LCStatus> postStatus(final LCUser authenticatedUser, Map<String, Object> param) {
637656
String authenticatedSession = getSessionToken(authenticatedUser);
638657
return wrapObservable(apiService.postStatus(authenticatedSession, param));

core/src/main/java/cn/leancloud/service/APIService.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ Observable<List<? extends LCObject>> findObjects(@Header(HEADER_KEY_LC_SESSIONTO
3131
Observable<LCQueryResult> queryObjects(@Header(HEADER_KEY_LC_SESSIONTOKEN) String sessionToken,
3232
@Path("className") String className, @QueryMap Map<String, String> query);
3333

34+
@GET("/1.1/{endPoint}")
35+
Observable<LCQueryResult> queryObjectsByCustomEndPoint(@Header(HEADER_KEY_LC_SESSIONTOKEN) String sessionToken,
36+
@Path(value="endPoint", encoded = true) String endPoint, @QueryMap Map<String, String> query);
37+
3438
@GET("/1.1/cloudQuery")
3539
Observable<LCQueryResult> cloudQuery(@Header(HEADER_KEY_LC_SESSIONTOKEN) String sessionToken,
3640
@QueryMap Map<String, String> query);
@@ -225,9 +229,8 @@ Observable<LCQueryResult> getFollowers(@Header(HEADER_KEY_LC_SESSIONTOKEN) Strin
225229
@Path("userId") String userId,
226230
@QueryMap Map<String, String> query);
227231

228-
@GET("/1.1/users/{userId}/followees")
229-
Observable<LCQueryResult> getFollowees(@Header(HEADER_KEY_LC_SESSIONTOKEN) String sessionToken,
230-
@Path("userId") String userId,
232+
@GET("/1.1/users/self/friends")
233+
Observable<LCQueryResult> getFriends(@Header(HEADER_KEY_LC_SESSIONTOKEN) String sessionToken,
231234
@QueryMap Map<String, String> query);
232235

233236
@GET("/1.1/users/{userId}/followersAndFollowees")
@@ -351,4 +354,15 @@ Observable<LCLeaderboardResult> getLeaderboardAroundResults(@Path("memberType")
351354
@GET("/1.1/users/strictlyQuery")
352355
Observable<LCQueryResult> strictlyQueryUsers(@Header(HEADER_KEY_LC_SESSIONTOKEN) String sessionToken,
353356
@QueryMap Map<String, String> query);
357+
358+
@POST("/1.1/users/self/friendBlocklist/{objectId}")
359+
Observable<JSONObject> blockFriendByObjectId(@Header(HEADER_KEY_LC_SESSIONTOKEN) String sessionToken, @Path("objectId") String objectId);
360+
361+
@DELETE("/1.1/users/self/friendBlocklist/{objectId}")
362+
Observable<JSONObject> unblockFriendByObjectId(@Header(HEADER_KEY_LC_SESSIONTOKEN) String sessionToken, @Path("objectId") String objectId);
363+
364+
@GET("/1.1/users/self/friendBlocklist")
365+
Observable<LCQueryResult> getBlockListOfFriend(@Header(HEADER_KEY_LC_SESSIONTOKEN) String sessionToken, @QueryMap Map<String, String> query);
366+
367+
354368
}

0 commit comments

Comments
 (0)