-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathConversation.java
More file actions
306 lines (266 loc) · 12.9 KB
/
Copy pathConversation.java
File metadata and controls
306 lines (266 loc) · 12.9 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
package io.rong.methods.conversation;
import io.rong.methods.BaseMethod;
import io.rong.models.CheckMethod;
import io.rong.models.Result;
import io.rong.models.agent.AgentModel;
import io.rong.models.conversation.ConversationAttrModel;
import io.rong.models.conversation.ConversationModel;
import io.rong.models.conversation.ConversationSetTopModel;
import io.rong.models.conversation.ConversationTagModel;
import io.rong.models.conversation.TagConversationsModel;
import io.rong.models.conversation.UserConversationTagModel;
import io.rong.models.response.ConversationNotificationResult;
import io.rong.models.response.ResponseResult;
import io.rong.models.response.ResultData;
import io.rong.util.CommonUtil;
import io.rong.util.GsonUtil;
import io.rong.util.HttpUtil;
import org.apache.commons.lang3.StringUtils;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* Conversation Do Not Disturb Service
*
* @version
* */
public class Conversation extends BaseMethod {
private static final String UTF8 = "UTF-8";
private static final String PATH = "conversation";
private static final Integer PARAM_ERROR = 1002;
private static final String ERR_REQUIRED_TEMPLATE = "The parameter %s is required.";
@Override
protected void initPath() {
super.path = PATH;
}
public Conversation(String appKey, String appSecret) {
this.appKey = appKey;
this.appSecret = appSecret;
initPath();
}
/**
* Set whether to receive message notifications for a specific conversation.
*
* @param conversation Conversation information, where type is required
* @return ResponseResult
**/
public ResponseResult mute(ConversationModel conversation) throws Exception {
String message = CommonUtil.checkFiled(conversation, PATH, CheckMethod.MUTE);
if (null != message) {
return (ResponseResult) GsonUtil.fromJson(message, ResponseResult.class);
}
StringBuilder sb = new StringBuilder();
sb.append("&conversationType=").append(URLEncoder.encode(conversation.type.toString(), UTF8));
sb.append("&requestId=").append(URLEncoder.encode(conversation.userId.toString(), UTF8));
sb.append("&targetId=").append(URLEncoder.encode(conversation.targetId.toString(), UTF8));
sb.append("&isMuted=").append(URLEncoder.encode("1", UTF8));
if (conversation.busChannel != null) {
sb.append("&busChannel=").append(URLEncoder.encode(conversation.getBusChannel(), UTF8));
}
sb.append("&unpushLevel=").append(URLEncoder.encode(String.valueOf(conversation.getUnpushLevel()), UTF8));
String body = sb.toString();
if (body.indexOf("&") == 0) {
body = body.substring(1, body.length());
}
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(rongCloud.getConfig(), appKey, appSecret, "/conversation/notification/set.json", "application/x-www-form-urlencoded");
HttpUtil.setBodyParameter(body, conn, rongCloud.getConfig());
return (ResponseResult) GsonUtil.fromJson(CommonUtil.getResponseByCode(PATH, CheckMethod.MUTE, HttpUtil.returnResult(conn, rongCloud.getConfig())), ResponseResult.class);
}
/**
* Set whether to receive message notifications for a specific conversation.
*
* @param conversation Conversation information, where type is required.
* @return ResponseResult
**/
public ResponseResult unMute(ConversationModel conversation) throws Exception {
String message = CommonUtil.checkFiled(conversation, PATH, CheckMethod.UNMUTE);
if (null != message) {
return (ResponseResult) GsonUtil.fromJson(message, ResponseResult.class);
}
StringBuilder sb = new StringBuilder();
sb.append("&conversationType=").append(URLEncoder.encode(conversation.type.toString(), UTF8));
sb.append("&requestId=").append(URLEncoder.encode(conversation.userId.toString(), UTF8));
sb.append("&targetId=").append(URLEncoder.encode(conversation.targetId.toString(), UTF8));
sb.append("&isMuted=").append(URLEncoder.encode("0", UTF8));
if (conversation.busChannel != null) {
sb.append("&busChannel=").append(URLEncoder.encode(conversation.getBusChannel(), UTF8));
}
sb.append("&unpushLevel=").append(URLEncoder.encode("0", UTF8));
String body = sb.toString();
if (body.indexOf("&") == 0) {
body = body.substring(1, body.length());
}
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(rongCloud.getConfig(), appKey, appSecret, "/conversation/notification/set.json", "application/x-www-form-urlencoded");
HttpUtil.setBodyParameter(body, conn, rongCloud.getConfig());
return (ResponseResult) GsonUtil.fromJson(CommonUtil.getResponseByCode(PATH,CheckMethod.UNMUTE,HttpUtil.returnResult(conn, rongCloud.getConfig())), ResponseResult.class);
}
/**
* Method to query the Do Not Disturb status of a conversation.
*
* @param conversation Conversation information, where type is required
* @return ResponseResult
**/
public Result get(ConversationModel conversation) throws Exception {
String message = CommonUtil.checkFiled(conversation,PATH,CheckMethod.GET);
if(null != message){
return (ResponseResult)GsonUtil.fromJson(message,ResponseResult.class);
}
StringBuilder sb = new StringBuilder();
sb.append("&conversationType=").append(URLEncoder.encode(conversation.type, UTF8));
sb.append("&requestId=").append(URLEncoder.encode(conversation.userId, UTF8));
sb.append("&targetId=").append(URLEncoder.encode(conversation.targetId, UTF8));
if(conversation.busChannel != null){
sb.append("&busChannel=").append(URLEncoder.encode(conversation.getBusChannel(), UTF8));
}
String body = sb.toString();
if (body.indexOf("&") == 0) {
body = body.substring(1, body.length());
}
HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(rongCloud.getConfig(), appKey, appSecret, "/conversation/notification/get.json", "application/x-www-form-urlencoded");
HttpUtil.setBodyParameter(body, conn, rongCloud.getConfig());
return (ConversationNotificationResult) GsonUtil.fromJson(CommonUtil.getResponseByCode(PATH,CheckMethod.GET,HttpUtil.returnResult(conn, rongCloud.getConfig())), ConversationNotificationResult.class);
}
/**
* Set conversation top
*/
public ResponseResult setTop(ConversationSetTopModel model) throws Exception {
String method = CheckMethod.SET_TOP;
ResponseResult result = checkFiled(model, method, ResponseResult.class);
if (result != null) {
return result;
}
StringBuilder sb = new StringBuilder();
addFormParam(sb, "userId=", model.getUserId());
addFormParam(sb, "&conversationType=", model.getConversationType());
addFormParam(sb, "&targetId=", model.getTargetId());
addFormParam(sb, "&setTop=", model.getSetTop());
String body = sb.toString();
return doRequest("/conversation/top/set.json", body, method, ResponseResult.class);
}
private ResponseResult required(String fieldName) {
return new ResponseResult(PARAM_ERROR, String.format(ERR_REQUIRED_TEMPLATE, fieldName));
}
/**
* Set User Conversation Tags.
*/
public ResponseResult setUserConversationTag(UserConversationTagModel model) throws Exception {
if (model == null){
return required("model");
}
if (StringUtils.isBlank(model.getUserId())) {
return required("userId");
}
if (model.getTags() == null || model.getTags().isEmpty()) {
return required("tags");
}
List<ConversationTagModel> createTagList = new ArrayList<>();
for (ConversationTagModel tag : model.getTags()) {
if (StringUtils.isBlank(tag.getTagId())) {
return required("tagId");
}
if (StringUtils.isBlank(tag.getTagName())) {
return required("tagName");
}
createTagList.add(new ConversationTagModel(tag.getTagId(), tag.getTagName()));
}
UserConversationTagModel param = new UserConversationTagModel(model.getUserId(), createTagList);
return doRequest("/user/conversation/tag/set.json", GsonUtil.toJson(param, UserConversationTagModel.class),
"", ResponseResult.class, CONTENT_TYPE_JSON);
}
/**
* Delete User Conversation Tags.
*/
public ResponseResult deleteUserConversationTag(String userId, List<String> tagIds) throws Exception {
if (StringUtils.isBlank(userId)) {
return required("userId");
}
if (tagIds == null || tagIds.isEmpty()) {
return required("tagIds");
}
Map<String, Object> param = new HashMap<>();
param.put("userId", userId);
param.put("tagIds", tagIds);
return doRequest("/user/conversation/tag/del.json", GsonUtil.toJson(param, Map.class),
"", ResponseResult.class, CONTENT_TYPE_JSON);
}
/**
* Query User Conversation Tags.
*/
public ResultData<UserConversationTagModel> queryUserConversationTags(String userId) throws Exception{
if (StringUtils.isBlank(userId)) {
return new ResultData<>(PARAM_ERROR, String.format(ERR_REQUIRED_TEMPLATE, "userId"));
}
String uri = "/user/conversation/tag/get.json?userId=" + URLEncoder.encode(userId, UTF8);
return doGetRequest(uri, ResultData.class);
}
/**
* Set tag to conversations
*/
public ResponseResult setConversationTag(TagConversationsModel tagConversations) throws Exception {
return setOrDeleteConversationTag(tagConversations,false);
}
/**
* Remove tag from conversations
*/
public ResponseResult removeConversationTag(TagConversationsModel tagConversations) throws Exception {
return setOrDeleteConversationTag(tagConversations,true);
}
private ResponseResult setOrDeleteConversationTag(TagConversationsModel tagConversations, boolean delete) throws Exception {
if (tagConversations == null) {
return required("tagConversations");
}
if (StringUtils.isBlank(tagConversations.getUserId())) {
return required("userId");
}
if (StringUtils.isBlank(tagConversations.getTagId())) {
return required("tagId");
}
if (tagConversations.getConversations() == null || tagConversations.getConversations().isEmpty()) {
return required("conversations");
}
for (TagConversationsModel.Conversation conversation : tagConversations.getConversations()) {
if (StringUtils.isBlank(conversation.getTargetId())) {
return required("targetId");
}
if (conversation.getConversationType() == null) {
return required("conversationType");
}
}
String uri = delete ? "/tag/conversation/del.json" : "/tag/conversation/set.json";
return doRequest(uri, GsonUtil.toJson(tagConversations, TagConversationsModel.class),
"", ResponseResult.class, CONTENT_TYPE_JSON);
}
/**
* Query Conversations By Tag.
*/
public ResultData<TagConversationsModel> queryTagConversations(String userId, String tagId) throws Exception {
if (StringUtils.isBlank(userId)) {
return new ResultData<>(PARAM_ERROR, String.format(ERR_REQUIRED_TEMPLATE, "userId"));
}
if (StringUtils.isBlank(tagId)) {
return new ResultData<>(PARAM_ERROR, String.format(ERR_REQUIRED_TEMPLATE, "tagId"));
}
String uri = "/tag/conversation/get.json?userId=" + URLEncoder.encode(userId, UTF8) + "&tagId=" + URLEncoder.encode(tagId, UTF8);
return doGetRequest(uri, ResultData.class);
}
/**
* Query Conversation Attribute.
*/
public ResultData<ConversationAttrModel> queryConversationAttribute(String userId, String targetId, Integer conversationType) throws Exception {
if (StringUtils.isBlank(userId)) {
return new ResultData<>(PARAM_ERROR, String.format(ERR_REQUIRED_TEMPLATE, "userId"));
}
if (StringUtils.isBlank(targetId)) {
return new ResultData<>(PARAM_ERROR, String.format(ERR_REQUIRED_TEMPLATE, "targetId"));
}
if (conversationType == null){
return new ResultData<>(PARAM_ERROR, String.format(ERR_REQUIRED_TEMPLATE, "conversationType"));
}
String uri = "/conversation/attribute/get.json?userId=" + URLEncoder.encode(userId, UTF8) + "&targetId=" + URLEncoder.encode(targetId, UTF8) + "&conversationType=" + conversationType;
return doGetRequest(uri, ResultData.class);
}
}