-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathChatroom.java
More file actions
336 lines (296 loc) · 14.4 KB
/
Copy pathChatroom.java
File metadata and controls
336 lines (296 loc) · 14.4 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package io.rong.methods.chatroom;
import io.rong.RongCloud;
import io.rong.methods.chatroom.ban.Ban;
import io.rong.methods.chatroom.ban.BanAllMember;
import io.rong.methods.chatroom.ban.BanAllMemberWhitelist;
import io.rong.methods.chatroom.distribute.Distribute;
import io.rong.methods.chatroom.entry.ChatroomEntry;
import io.rong.methods.chatroom.gag.Gag;
import io.rong.methods.chatroom.keepalive.Keepalive;
import io.rong.methods.chatroom.demotion.Demotion;
import io.rong.methods.chatroom.mute.MuteMembers;
import io.rong.methods.chatroom.whitelist.Whitelist;
import io.rong.methods.chatroom.block.Block;
import io.rong.models.*;
import io.rong.models.chatroom.*;
import io.rong.models.response.ChatroomUserQueryResult;
import io.rong.models.response.CheckChatRoomUserResult;
import io.rong.models.response.CheckChatRoomUsersResult;
import io.rong.models.response.ChatroomQueryResult;
import io.rong.models.response.ResponseResult;
import io.rong.models.response.TokenResult;
import io.rong.models.user.UserModel;
import io.rong.util.CommonUtil;
import io.rong.util.GsonUtil;
import io.rong.util.HttpUtil;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
/**
* Chatroom Service
*/
public class Chatroom {
private static final String UTF8 = "UTF-8";
private static final String PATH = "chatroom";
private String appKey;
private String appSecret;
public Block block;
public Gag gag;
public MuteMembers muteMembers;
public Ban ban;
public BanAllMember banAllMember;
public BanAllMemberWhitelist banAllMemberWhitelist;
public Keepalive keepalive;
public Demotion demotion;
public Whitelist whiteList;
public Distribute distribute;
public ChatroomEntry entry;
private RongCloud rongCloud;
public RongCloud getRongCloud() {
return rongCloud;
}
public void setRongCloud(RongCloud rongCloud) {
this.rongCloud = rongCloud;
gag.setRongCloud(rongCloud);
keepalive.setRongCloud(rongCloud);
demotion.setRongCloud(rongCloud);
whiteList.setRongCloud(rongCloud);
block.setRongCloud(rongCloud);
demotion.setRongCloud(rongCloud);
distribute.setRongCloud(rongCloud);
ban.setRongCloud(rongCloud);
banAllMember.setRongCloud(rongCloud);
banAllMemberWhitelist.setRongCloud(rongCloud);
entry.setRongCloud(rongCloud);
}
public Chatroom(String appKey, String appSecret, RongCloud rongCloud) {
this.appKey = appKey;
this.appSecret = appSecret;
this.gag = new Gag(appKey, appSecret);
this.keepalive = new Keepalive(appKey, appSecret);
this.demotion = new Demotion(appKey, appSecret);
this.whiteList = new Whitelist(appKey, appSecret);
this.block = new Block(appKey, appSecret);
this.distribute = new Distribute(appKey, appSecret);
this.ban = new Ban(appKey, appSecret);
this.banAllMember = new BanAllMember(appKey, appSecret);
this.banAllMemberWhitelist = new BanAllMemberWhitelist(appKey, appSecret);
this.muteMembers = new MuteMembers(appKey, appSecret, rongCloud);
this.entry = new ChatroomEntry(appKey, appSecret);
}
/**
* Creates a chatroom.
*
* @param chatrooms: chatroom.id and name (required)
* @return ResponseResult
**/
@Deprecated
public ResponseResult create(ChatroomModel[] chatrooms) throws Exception {
if (chatrooms == null) {
return new ResponseResult(1002, "Paramer 'chatrooms' is required");
}
for (ChatroomModel chatroom : chatrooms) {
String message = CommonUtil.checkFiled(chatroom, PATH, CheckMethod.CREATE);
if (null != message) {
return (ResponseResult) GsonUtil.fromJson(message, ResponseResult.class);
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < chatrooms.length; i++) {
ChatroomModel chatroom = chatrooms[i];
sb.append("&chatroom[" + chatroom.getId() + "]=").append(URLEncoder.encode(chatroom.getName(), UTF8));
}
String body = sb.toString();
if (body.indexOf("&") == 0) {
body = body.substring(1);
}
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(rongCloud.getConfig(), appKey, appSecret, "/chatroom/create.json",
"application/x-www-form-urlencoded");
HttpUtil.setBodyParameter(body, conn, rongCloud.getConfig());
return (ResponseResult) GsonUtil.fromJson(
CommonUtil.getResponseByCode(PATH, CheckMethod.CREATE, HttpUtil.returnResult(conn, rongCloud.getConfig())), ResponseResult.class);
}
/**
* Create chatroom method V2
*
* @param chatroom
* @return
* @throws Exception
*/
public ResponseResult createV2(ChatroomDataModel chatroom) throws Exception {
if (chatroom == null) {
return new ResponseResult(1002, "Paramer 'chatrooms' is required");
}
String message = CommonUtil.checkFiled(chatroom, PATH, CheckMethod.CREATEV2);
if (null != message) {
return (ResponseResult) GsonUtil.fromJson(message, ResponseResult.class);
}
StringBuilder sb = new StringBuilder();
sb.append("chatroomId=").append(URLEncoder.encode(chatroom.getId().toString(), UTF8));
if (null != chatroom.getDestroyType()) {
sb.append("&destroyType=").append(URLEncoder.encode(chatroom.getDestroyType().toString(), UTF8));
}
if (null != chatroom.getDestroyTime()) {
sb.append("&destroyTime=").append(URLEncoder.encode(chatroom.getDestroyTime().toString(), UTF8));
}
if (null != chatroom.getIsBan()) {
sb.append("&isBan=").append(URLEncoder.encode(chatroom.getIsBan().toString(), UTF8));
}
if (null != chatroom.getEntryOwnerId()) {
sb.append("&entryOwnerId=").append(URLEncoder.encode(chatroom.getEntryOwnerId().toString(), UTF8));
}
if (null != chatroom.getWhiteUserIds() && chatroom.getWhiteUserIds().length > 0) {
for (String user : chatroom.getWhiteUserIds()) {
sb.append("&whiteUserIds=").append(URLEncoder.encode(user, UTF8));
}
}
if (null != chatroom.getEntryInfo() && chatroom.getEntryInfo().size() > 0) {
sb.append("&entryInfo=").append(URLEncoder.encode(GsonUtil.toJson(chatroom.getEntryInfo()), UTF8));
}
String body = sb.toString();
if (body.indexOf("&") == 0) {
body = body.substring(1, body.length());
}
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(rongCloud.getConfig(), appKey, appSecret, "/chatroom/create_new.json",
"application/x-www-form-urlencoded");
HttpUtil.setBodyParameter(body, conn, rongCloud.getConfig());
ResponseResult result = (ResponseResult) GsonUtil.fromJson(
CommonUtil.getResponseByCode(PATH, CheckMethod.CREATEV2, HttpUtil.returnResult(conn, rongCloud.getConfig())), ResponseResult.class);
result.setReqBody(body);
return result;
}
/**
* Set the chatroom destruction type
*
* @param destroyType: Specifies the destruction type
* @return ResponseResult
**/
public ResponseResult setDestroyType(ChatroomDestroyTypeModel destroyType) throws Exception {
String message = CommonUtil.checkFiled(destroyType, PATH, CheckMethod.DESTORY);
if (null != message) {
return (ResponseResult) GsonUtil.fromJson(message, ResponseResult.class);
}
StringBuilder sb = new StringBuilder();
sb.append("chatroomId=").append(URLEncoder.encode(destroyType.getId().toString(), UTF8));
if (null != destroyType.getDestroyType()) {
sb.append("&destroyType=").append(URLEncoder.encode(destroyType.getDestroyType().toString(), UTF8));
}
if (null != destroyType.getDestroyTime()) {
sb.append("&destroyTime=").append(URLEncoder.encode(destroyType.getDestroyTime().toString(), UTF8));
}
String body = sb.toString();
if (body.indexOf("&") == 0) {
body = body.substring(1, body.length());
}
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(rongCloud.getConfig(), appKey, appSecret, "/chatroom/destroy/set.json",
"application/x-www-form-urlencoded");
HttpUtil.setBodyParameter(body, conn, rongCloud.getConfig());
ResponseResult result = (ResponseResult) GsonUtil.fromJson(
CommonUtil.getResponseByCode(PATH, CheckMethod.DESTORY, HttpUtil.returnResult(conn, rongCloud.getConfig())), ResponseResult.class);
result.setReqBody(body);
return result;
}
/**
* Method to destroy a chatroom
*
* @param chatroom: The ID of the chatroom to be destroyed (required).
* @return ResponseResult
**/
public ResponseResult destroy(ChatroomModel chatroom) throws Exception {
String message = CommonUtil.checkFiled(chatroom, PATH, CheckMethod.DESTORY);
if (null != message) {
return (ResponseResult) GsonUtil.fromJson(message, ResponseResult.class);
}
String body = "chatroomId=" + URLEncoder.encode(chatroom.getId(), UTF8);
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(rongCloud.getConfig(), appKey, appSecret, "/chatroom/destroy.json",
"application/x-www-form-urlencoded");
HttpUtil.setBodyParameter(body, conn, rongCloud.getConfig());
return (ResponseResult) GsonUtil.fromJson(
CommonUtil.getResponseByCode(PATH, CheckMethod.DESTORY, HttpUtil.returnResult(conn, rongCloud.getConfig())), ResponseResult.class);
}
/**
* Method to query users in a chatroom
*
* @param chatroom: Chatroom ID, count, and order (required).
* @return ChatroomUserQueryResult
**/
public ChatroomUserQueryResult get(ChatroomModel chatroom) throws Exception {
String message = CommonUtil.checkFiled(chatroom, PATH, CheckMethod.GET);
if (null != message) {
return (ChatroomUserQueryResult) GsonUtil.fromJson(message, ChatroomUserQueryResult.class);
}
StringBuilder sb = new StringBuilder();
sb.append("&chatroomId=").append(URLEncoder.encode(chatroom.getId(), UTF8));
sb.append("&count=").append(URLEncoder.encode(chatroom.getCount().toString(), UTF8));
sb.append("&order=").append(URLEncoder.encode(chatroom.getOrder().toString(), UTF8));
String body = sb.toString();
if (body.indexOf("&") == 0) {
body = body.substring(1);
}
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(rongCloud.getConfig(), appKey, appSecret, "/chatroom/user/query.json",
"application/x-www-form-urlencoded");
HttpUtil.setBodyParameter(body, conn, rongCloud.getConfig());
return (ChatroomUserQueryResult) GsonUtil.fromJson(
CommonUtil.getResponseByCode(PATH, CheckMethod.GET, HttpUtil.returnResult(conn, rongCloud.getConfig())), ChatroomUserQueryResult.class);
}
/**
* Check if a user exists in a chatroom
*
* @param chatroomModel: Chatroom member. (Required)
* @return ResponseResult
**/
public CheckChatRoomUsersResult isExists(ChatroomModel chatroomModel) throws Exception {
String message = CommonUtil.checkFiled(chatroomModel, PATH, CheckMethod.ISEXISTS);
if (null != message) {
return (CheckChatRoomUsersResult) GsonUtil.fromJson(message, CheckChatRoomUsersResult.class);
}
StringBuilder sb = new StringBuilder();
sb.append("&chatroomId=").append(URLEncoder.encode(chatroomModel.getId(), UTF8));
ChatroomMember[] members = chatroomModel.getMembers();
for (ChatroomMember member : members) {
sb.append("&userId=").append(URLEncoder.encode(member.getId(), UTF8));
}
String body = sb.toString();
if (body.indexOf("&") == 0) {
body = body.substring(1, body.length());
}
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(rongCloud.getConfig(), appKey, appSecret, "/chatroom/users/exist.json",
"application/x-www-form-urlencoded");
HttpUtil.setBodyParameter(body, conn, rongCloud.getConfig());
return (CheckChatRoomUsersResult) GsonUtil.fromJson(
CommonUtil.getResponseByCode(PATH, CheckMethod.ISEXISTS, HttpUtil.returnResult(conn, rongCloud.getConfig())),
CheckChatRoomUsersResult.class);
}
/**
* Check if a user exists in a chatroom
*
* @param member: The chatroom member. (Required)
* @return ResponseResult
**/
public CheckChatRoomUserResult isExist(ChatroomMember member) throws Exception {
String message = CommonUtil.checkFiled(member, PATH, CheckMethod.ISEXIST);
if (null != message) {
return (CheckChatRoomUserResult) GsonUtil.fromJson(message, CheckChatRoomUserResult.class);
}
StringBuilder sb = new StringBuilder();
sb.append("chatroomId=").append(URLEncoder.encode(member.chatroomId, UTF8));
sb.append("&userId=").append(URLEncoder.encode(member.id, UTF8));
String body = sb.toString();
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(rongCloud.getConfig(), appKey, appSecret, "/chatroom/user/exist.json",
"application/x-www-form-urlencoded");
HttpUtil.setBodyParameter(body, conn, rongCloud.getConfig());
return (CheckChatRoomUserResult) GsonUtil.fromJson(
CommonUtil.getResponseByCode(PATH, CheckMethod.ISEXIST, HttpUtil.returnResult(conn, rongCloud.getConfig())), CheckChatRoomUserResult.class);
}
public ChatroomQueryResult query(ChatroomModel chatroomModel) throws Exception {
String message = CommonUtil.checkFiled(chatroomModel, PATH, CheckMethod.QUERY);
if (null != message) {
return (ChatroomQueryResult) GsonUtil.fromJson(message, ChatroomQueryResult.class);
}
String body = "chatroomId=" + URLEncoder.encode(chatroomModel.getId(), UTF8);
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(rongCloud.getConfig(), appKey, appSecret, "/chatroom/get.json",
"application/x-www-form-urlencoded");
HttpUtil.setBodyParameter(body, conn, rongCloud.getConfig());
return (ChatroomQueryResult) GsonUtil.fromJson(
CommonUtil.getResponseByCode(PATH, CheckMethod.QUERY, HttpUtil.returnResult(conn, rongCloud.getConfig())), ChatroomQueryResult.class);
}
}