-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathFriend.java
More file actions
executable file
·207 lines (164 loc) · 6.87 KB
/
Copy pathFriend.java
File metadata and controls
executable file
·207 lines (164 loc) · 6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package io.rong.methods.profile;
import io.rong.RongCloud;
import io.rong.methods.BaseMethod;
import io.rong.models.CheckMethod;
import io.rong.models.profile.FriendModel;
import io.rong.models.profile.FriendProfileModel;
import io.rong.models.profile.PagingGetFriendsModel;
import io.rong.models.response.CheckFriendsResult;
import io.rong.models.response.GetPermissionResult;
import io.rong.models.response.QueryFriendsResult;
import io.rong.models.response.ResponseResult;
import org.apache.commons.lang3.StringUtils;
/**
* Group Information Hosting Service
*/
public class Friend extends BaseMethod {
private static final String API_JSON_PATH = "profile/friend";
public Friend(String appKey, String appSecret, RongCloud rongCloud) {
this.appKey = appKey;
this.appSecret = appSecret;
this.rongCloud = rongCloud;
initPath();
}
@Override
protected void initPath() {
super.path = API_JSON_PATH;
}
/**
* Add a friend
**/
public ResponseResult add(FriendModel friend) throws Exception {
String method = CheckMethod.ADD_FRIEND;
ResponseResult result = checkFiled(friend, method, ResponseResult.class);
if (result != null) {
return result;
}
StringBuilder sb = new StringBuilder();
addFormParam(sb, "userId=", friend.getUserId());
addFormParam(sb, "&targetId=", friend.getTargetId());
addFormParam(sb, "&optType=", friend.getOptType());
addFormParam(sb, "&extra=", friend.getExtra());
String body = sb.toString();
return doRequest("/friend/add.json", body, method, ResponseResult.class);
}
/**
* Delete a friend
**/
public ResponseResult delete(String userId, String... targetIds) throws Exception {
String method = CheckMethod.DEL_FRIEND;
ResponseResult result = checkParam("userId", userId, method, ResponseResult.class);
if (result != null) {
return result;
}
result = checkParam("targetIds", targetIds, method, ResponseResult.class);
if (result != null) {
return result;
}
StringBuilder sb = new StringBuilder();
addFormParam(sb, "userId=", userId);
addFormParam(sb, "&targetIds=", StringUtils.join(removeDuplicates(targetIds), ","));
String body = sb.toString();
return doRequest("/friend/delete.json", body, method, ResponseResult.class);
}
/**
* Clear all friends
**/
public ResponseResult clean(String userId) throws Exception {
String method = CheckMethod.CLEAN_FRIEND;
ResponseResult result = checkParam("userId", userId, method, ResponseResult.class);
if (result != null) {
return result;
}
StringBuilder sb = new StringBuilder();
addFormParam(sb, "userId=", userId);
String body = sb.toString();
return doRequest("/friend/clean.json", body, method, ResponseResult.class);
}
/**
* Set friend profile
**/
public ResponseResult setProfile(FriendProfileModel profileModel) throws Exception {
String method = CheckMethod.SET_FRIEND_PROFILE;
ResponseResult result = checkFiled(profileModel, method, ResponseResult.class);
if (result != null) {
return result;
}
StringBuilder sb = new StringBuilder();
addFormParam(sb, "userId=", profileModel.getUserId());
addFormParam(sb, "&targetId=", profileModel.getTargetId());
addFormParam(sb, "&remarkName=", profileModel.getRemarkName());
addFormParam(sb, "&friendExtProfile=", profileModel.getFriendExtProfile());
String body = sb.toString();
return doRequest("/friend/profile/set.json", body, method, ResponseResult.class);
}
/**
* Paginate to get friend information
*/
public QueryFriendsResult pagingGetFriends(PagingGetFriendsModel getFriendsModel) throws Exception {
String method = CheckMethod.PAGING_GET_FRIENDS;
QueryFriendsResult result = checkFiled(getFriendsModel, method, QueryFriendsResult.class);
if (result != null) {
return result;
}
StringBuilder sb = new StringBuilder();
addFormParam(sb, "userId=", getFriendsModel.getUserId());
addFormParam(sb, "&pageToken=", getFriendsModel.getPageToken());
addFormParam(sb, "&size=", getFriendsModel.getSize());
addFormParam(sb, "&order=", getFriendsModel.getOrder());
String body = sb.toString();
return doRequest("/friend/get.json", body, method, QueryFriendsResult.class);
}
/**
* Check friend relationships
**/
public CheckFriendsResult checkFriends(String userId, String... targetIds) throws Exception {
String method = CheckMethod.CHECK_FRIENDS;
CheckFriendsResult result = checkParam("userId", userId, method, CheckFriendsResult.class);
if (result != null) {
return result;
}
result = checkParam("targetIds", targetIds, method, CheckFriendsResult.class);
if (result != null) {
return result;
}
StringBuilder sb = new StringBuilder();
addFormParam(sb, "userId=", userId);
addFormParam(sb, "&targetIds=", StringUtils.join(removeDuplicates(targetIds), ","));
String body = sb.toString();
return doRequest("/friend/check.json", body, method, CheckFriendsResult.class);
}
/**
* Set the permission for adding friends
**/
public ResponseResult setPermission(Integer permissionType, String... userIds) throws Exception {
String method = CheckMethod.SET_PERM;
ResponseResult result = checkParam("userIds", userIds, method, ResponseResult.class);
if (result != null) {
return result;
}
result = checkParam("permissionType", permissionType, method, ResponseResult.class);
if (result != null) {
return result;
}
StringBuilder sb = new StringBuilder();
addFormParam(sb, "userIds=", StringUtils.join(removeDuplicates(userIds), ","));
addFormParam(sb, "&permissionType=", permissionType);
String body = sb.toString();
return doRequest("/friend/permission/set.json", body, method, ResponseResult.class);
}
/**
* Get the permission for adding friends
**/
public GetPermissionResult getPermission(String... userIds) throws Exception {
String method = CheckMethod.GET_PERM;
GetPermissionResult result = checkParam("userIds", userIds, method, GetPermissionResult.class);
if (result != null) {
return result;
}
StringBuilder sb = new StringBuilder();
addFormParam(sb, "userIds=", StringUtils.join(removeDuplicates(userIds), ","));
String body = sb.toString();
return doRequest("/friend/permission/get.json", body, method, GetPermissionResult.class);
}
}