NIMSDK-AOS 10.4.0-beta
载入中...
搜索中...
未找到
V2NIMMessageConverter.java
浏览该文件的文档.
1package com.netease.nimlib.sdk.v2.message;
2
3import android.text.TextUtils;
4import com.netease.nimlib.SDKCacheUI;
5import com.netease.nimlib.biz.constant.ITalkService;
6import com.netease.nimlib.biz.constant.ITalkService.TagString;
7import com.netease.nimlib.log.NimLog;
8import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
9import com.netease.nimlib.sdk.v2.conversation.enums.V2NIMConversationType;
10import com.netease.nimlib.sdk.v2.message.config.V2NIMMessageAIConfig;
11import com.netease.nimlib.sdk.v2.message.config.V2NIMMessageAntispamConfig;
12import com.netease.nimlib.sdk.v2.message.config.V2NIMMessageAntispamConfig.V2NIMMessageAntispamConfigBuilder;
13import com.netease.nimlib.sdk.v2.message.config.V2NIMMessageConfig;
14import com.netease.nimlib.sdk.v2.message.config.V2NIMMessageConfig.V2NIMMessageConfigBuilder;
15import com.netease.nimlib.sdk.v2.message.config.V2NIMMessagePushConfig;
16import com.netease.nimlib.sdk.v2.message.config.V2NIMMessagePushConfig.V2NIMMessagePushConfigBuilder;
17import com.netease.nimlib.sdk.v2.message.config.V2NIMMessageRobotConfig;
18import com.netease.nimlib.sdk.v2.message.config.V2NIMMessageRobotConfig.V2NIMMessageRobotConfigBuilder;
19import com.netease.nimlib.sdk.v2.message.config.V2NIMMessageRouteConfig;
20import com.netease.nimlib.sdk.v2.message.config.V2NIMMessageRouteConfig.V2NIMMessageRouteConfigBuilder;
21import com.netease.nimlib.sdk.v2.message.enums.V2NIMMessageAIStatus;
22import com.netease.nimlib.sdk.v2.message.enums.V2NIMMessageType;
23import com.netease.nimlib.session.MsgHelper;
24import com.netease.nimlib.session.TimeConsumingStatisticsImpl;
25import com.netease.nimlib.v2.message.compat.V2MessageModelConverter;
26import com.netease.nimlib.v2.message.impl.V2NIMMessageImpl;
27import com.netease.nimlib.v2.message.impl.V2NIMMessageReferImpl;
28import java.util.List;
29import org.json.JSONException;
30import org.json.JSONObject;
31
33
34 private static final String TAG = "V2NIMMessageConverter";
35 /**
36 * 消息序列化为字符串
37 * @param message 消对象
38 * @return 序列化后的字符串
39 */
40 public static String messageSerialization(V2NIMMessage message) {
41 if(message == null) {
42 return null;
43 }
44 V2NIMMessageImpl messageImpl = (V2NIMMessageImpl) message;
45 JSONObject msgBody = new JSONObject();
46 try {
47 msgBody.put(ITalkService.TagString.ReceiverType, messageImpl.getConversationTypeV1().getValue());
48 msgBody.put(ITalkService.TagString.ReceiverAccount, messageImpl.getReceiverId());
49 msgBody.put(ITalkService.TagString.fromAccount, messageImpl.getSenderId());
50 msgBody.put(ITalkService.TagString.clientType, messageImpl.getFromClientType());
51 msgBody.put(ITalkService.TagString.fromNick, messageImpl.getFromNick());
52 msgBody.put(ITalkService.TagString.time, messageImpl.getCreateTime());
53 if (messageImpl.getMessageType() != null) {
54 msgBody.put(ITalkService.TagString.type, messageImpl.getMessageType().getValue());
55 }
56
57 msgBody.put(ITalkService.TagString.body, messageImpl.getText());
58
59 String attach = messageImpl.getAttachmentText();
60 if (!TextUtils.isEmpty(attach)) {
61 msgBody.put(ITalkService.TagString.attach, attach);
62 }
63
64 msgBody.put(ITalkService.TagString.msgid_client, messageImpl.getMessageClientId());
65 msgBody.put(ITalkService.TagString.msgid_server, messageImpl.getMessageServerIdValue());
66
67 msgBody.put(ITalkService.TagString.ext, messageImpl.getServerExtension());
68
69 V2NIMMessagePushConfig pushConfig = messageImpl.getPushConfig();
70 if(pushConfig != null){
71 msgBody.put(ITalkService.TagString.pushEnable, pushConfig.isPushEnabled() ? 1 : 0);
72 msgBody.put(ITalkService.TagString.pushPayload, pushConfig.getPushPayload());
73 msgBody.put(ITalkService.TagString.pushContent, pushConfig.getPushContent());
74 msgBody.put(ITalkService.TagString.needPushNick, pushConfig.isPushNickEnabled() ? 1 : 0);
75
76 List<String> forcePushAccountIds = pushConfig.getForcePushAccountIds();
77 if (forcePushAccountIds != null) {
78 String pushListStr = MsgHelper.getJsonStringFromList(forcePushAccountIds);
79 msgBody.put(ITalkService.TagString.forcePushList, pushListStr);
80 }
81 msgBody.put(ITalkService.TagString.forcePushContent, pushConfig.getForcePushContent());
82 msgBody.put(ITalkService.TagString.isForcePush, pushConfig.isForcePush() ? 1 : 0);
83 }
84
85
86 V2NIMMessageAntispamConfig antispamConfig = messageImpl.getAntispamConfig();
87 if (antispamConfig != null) {
88 if (antispamConfig.getAntispamCustomMessage() != null) {
89 msgBody.put(ITalkService.TagString.antiSpamContentCustom, 1);
90 msgBody.put(ITalkService.TagString.antiSpamContent, antispamConfig.getAntispamCustomMessage());
91 }else{
92 msgBody.put(ITalkService.TagString.antiSpamContentCustom, 0);
93 }
94
95 msgBody.put(ITalkService.TagString.antiSpamConfigId, antispamConfig.getAntispamBusinessId());
96 msgBody.put(ITalkService.TagString.antiSpamUsingYidun, antispamConfig.isAntispamEnabled() ? 1 : 0);
97 msgBody.put(ITalkService.TagString.yidunAntiCheating, antispamConfig.getAntispamCheating());
98 msgBody.put(ITalkService.TagString.yidunAntiSpamExt, antispamConfig.getAntispamExtension());
99 }
100
101 V2NIMMessageConfig messageConfig = messageImpl.getMessageConfig();
102 if(messageConfig != null){
103 if(messageConfig.isReadReceiptEnabled()){
104 msgBody.put(ITalkService.TagString.msgAck, 1);
105 }
106 msgBody.put(ITalkService.TagString.isSessionUpdate, messageConfig.isLastMessageUpdateEnabled() ? 1 : 0);
107 msgBody.put(ITalkService.TagString.historyEnable, messageConfig.isHistoryEnabled() ? 1 : 0);
108 msgBody.put(ITalkService.TagString.roamingEnable, messageConfig.isRoamingEnabled() ? 1 : 0);
109 msgBody.put(ITalkService.TagString.selfSyncEnable, messageConfig.isOnlineSyncEnabled() ? 1 : 0);
110 msgBody.put(ITalkService.TagString.persistEnable, messageConfig.isOfflineEnabled() ? 1 : 0);
111 msgBody.put(ITalkService.TagString.needBadge, messageConfig.isUnreadEnabled() ? 1 : 0);
112
113 }
114
115 V2NIMMessageReferImpl threadReply = (V2NIMMessageReferImpl) messageImpl.getThreadReply();
116 if(threadReply != null) {
117 msgBody.put(ITalkService.TagString.replyMsgFromAccount, threadReply.getSenderId());
118 msgBody.put(ITalkService.TagString.replyMsgToAccount, threadReply.getReceiverId());
119 msgBody.put(ITalkService.TagString.replyMsgTime, threadReply.getCreateTime());
120 msgBody.put(ITalkService.TagString.replyMsgIdServer, threadReply.getMessageServerIdValue());
121 msgBody.put(ITalkService.TagString.replyMsgIdClient, threadReply.getMessageClientId());
122 }
123
124 V2NIMMessageReferImpl threadRoot = (V2NIMMessageReferImpl) messageImpl.getThreadRoot();
125 if (threadRoot != null) {
126 msgBody.put(ITalkService.TagString.threadMsgFromAccount, threadRoot.getSenderId());
127 msgBody.put(ITalkService.TagString.threadMsgToAccount, threadRoot.getReceiverId());
128 msgBody.put(ITalkService.TagString.threadMsgTime, threadRoot.getCreateTime());
129 msgBody.put(ITalkService.TagString.threadMsgIdServer, threadRoot.getMessageServerIdValue());
130 msgBody.put(ITalkService.TagString.threadMsgIdClient, threadRoot.getMessageClientId());
131 }
132
133 msgBody.put(ITalkService.TagString.delete, messageImpl.isDeleted() ? 1 : 0);
134 msgBody.put(ITalkService.TagString.callbackExt, messageImpl.getCallbackExtension());
135 msgBody.put(ITalkService.TagString.subtype, messageImpl.getSubType());
136
137 TimeConsumingStatisticsImpl timeConsumingStatistics = messageImpl.getTimeConsumingStatistics();
138 if (timeConsumingStatistics != null) {
139 JSONObject jsonObject = timeConsumingStatistics.toJsonForClientExt();
140 msgBody.put(ITalkService.TagString.clientExt, jsonObject.toString());
141 }
142
143 V2NIMMessageRobotConfig robotConfig = messageImpl.getRobotConfig();
144 if (robotConfig != null) {
145 msgBody.put(ITalkService.TagString.function, robotConfig.getFunction());
146 msgBody.put(ITalkService.TagString.topic, robotConfig.getTopic());
147 msgBody.put(ITalkService.TagString.customContent, robotConfig.getCustomContent());
148 msgBody.put(ITalkService.TagString.account, robotConfig.getAccountId());
149 }
150
151 V2NIMMessageRouteConfig routeConfig = messageImpl.getRouteConfig();
152 if (routeConfig != null) {
153 msgBody.put(ITalkService.TagString.routeEnable, routeConfig.isRouteEnabled() ? 1 : 0);
154 msgBody.put(ITalkService.TagString.env, routeConfig.getRouteEnvironment());
155 }
156
157 msgBody.put(ITalkService.TagString.isBlackListed, messageImpl.isInBlackList() ? 1 : 0);
158
159 V2NIMMessageAIConfig aiConfig = messageImpl.getAIConfig();
160 if (aiConfig != null) {
161 msgBody.put(ITalkService.TagString.ai_agent_account, aiConfig.getAccountId());
162 V2NIMMessageAIStatus aiStatus = aiConfig.getAIStatus();
163 if(aiStatus != null){
164 msgBody.put(TagString.ai_agent_msg_direction, aiStatus.getValue());
165 }
166 }
167
168 if(messageImpl.getAIErrorCode() > 0){
169 msgBody.put(ITalkService.TagString.ai_agent_error_code, messageImpl.getAIErrorCode());
170 }
171
172 long modifyTime = messageImpl.getModifyTime();
173 if(modifyTime > 0){
174 msgBody.put(ITalkService.TagString.modifyTimetag, modifyTime);
175 }
176
177 String modifyAccountId = messageImpl.getModifyAccountId();
178 if(!TextUtils.isEmpty(modifyAccountId)){
179 msgBody.put(ITalkService.TagString.modifyAccid, modifyAccountId);
180 }
181
182 }
183 catch (JSONException e) {
184 NimLog.e(TAG, "messageSerialization JSONException: " + e.getMessage());
185 }
186
187 return msgBody.toString();
188 }
189
190 /**
191 * 字符串反序列化为消息对象
192 * @param msg messageSerialization方法序列化后的字符串
193 * @return 反序列化后的消息对象
194 */
195 public static V2NIMMessage messageDeserialization(String msg){
196 if(TextUtils.isEmpty(msg)){
197 return null;
198 }
199 JSONObject msgBody;
200 try {
201 msgBody = new JSONObject(msg);
202 }
203 catch (Throwable e) {
204 NimLog.e(TAG, "messageDeserialization exception, msg format error, msg = " + msg,e);
205 return null;
206 }
207
208 V2NIMMessageImpl message = new V2NIMMessageImpl();
209 int sessionType = -1;
210 V2NIMConversationType conversationType = null;
211 try {
212 if(msgBody.has(ITalkService.TagString.ReceiverType)){
213 sessionType = msgBody.getInt(ITalkService.TagString.ReceiverType);
214 conversationType = V2MessageModelConverter.toV2ConversationType(SessionTypeEnum.typeOfValue(sessionType));
215 message.setConversationType(conversationType);
216 }
217 }
218 catch (Throwable e) {
219 NimLog.e(TAG, "messageDeserialization exception, conversationType error, conversationType = " + msgBody.opt(ITalkService.TagString.ReceiverType),e);
220 return null;
221 }
222
223 String receiver = null;
224 try {
225 if(msgBody.has(ITalkService.TagString.ReceiverAccount)){
226 receiver = msgBody.getString(ITalkService.TagString.ReceiverAccount);
227 message.setReceiverId(receiver);
228 }
229 }
230 catch (Throwable e) {
231 NimLog.e(TAG, "messageDeserialization exception, receiverId error, receiverId = " + msgBody.opt(ITalkService.TagString.ReceiverAccount),e);
232 return null;
233 }
234
235 String fromId = null;
236 try {
237 if(msgBody.has(ITalkService.TagString.fromAccount)){
238 fromId = msgBody.getString(ITalkService.TagString.fromAccount);
239 message.setSenderId(fromId);
240 }
241 }
242 catch (Throwable e) {
243 NimLog.e(TAG, "messageDeserialization exception, senderId error,senderId = " + msgBody.opt(ITalkService.TagString.fromAccount),e);
244 return null;
245 }
246
247 String sessionId = sessionIdOfMsg(sessionType,fromId,receiver, SDKCacheUI.getAccount());
248 message.setSessionId(sessionId);
249
250 try {
251 if(msgBody.has(ITalkService.TagString.clientType)){
252 message.setFromClientType(msgBody.getInt(ITalkService.TagString.clientType));
253 }
254 }
255 catch (Throwable e) {
256 NimLog.e(TAG, "messageDeserialization exception, fromClientType error, fromClientType = "+msgBody.opt(ITalkService.TagString.clientType),e);
257 return null;
258 }
259
260 try {
261 if(msgBody.has(ITalkService.TagString.fromNick)){
262 message.setMsgFromNick(msgBody.getString(ITalkService.TagString.fromNick));
263 }
264 }
265 catch (Throwable e) {
266 NimLog.e(TAG, "messageDeserialization exception, fromNick error, fromNick = "+msgBody.opt(ITalkService.TagString.fromNick),e);
267 return null;
268 }
269
270 try {
271 if(msgBody.has(ITalkService.TagString.time)){
272 message.setCreateTime(msgBody.getLong(ITalkService.TagString.time));
273 }
274 }
275 catch (Throwable e) {
276 NimLog.e(TAG, "messageDeserialization exception, createTime error, createTime = "+msgBody.opt(ITalkService.TagString.time),e);
277 return null;
278 }
279 V2NIMMessageType messageType = null;
280 try {
281 if(msgBody.has(ITalkService.TagString.type)){
282 messageType = V2NIMMessageType.typeOfValue(msgBody.getInt(ITalkService.TagString.type));
283 message.setMessageType(messageType);
284 }
285 }
286 catch (Throwable e) {
287 NimLog.e(TAG, "messageDeserialization exception, messageType error, messageType = "+msgBody.opt(ITalkService.TagString.type),e);
288 return null;
289 }
290
291 try {
292 if(msgBody.has(ITalkService.TagString.body)){
293 message.setText(msgBody.getString(ITalkService.TagString.body));
294 }
295 }
296 catch (Throwable e) {
297 NimLog.e(TAG, "messageDeserialization exception, text error, text = "+msgBody.opt(ITalkService.TagString.body),e);
298 return null;
299 }
300
301 try {
302 if(msgBody.has(ITalkService.TagString.attach)){
303 String attachment = msgBody.getString(TagString.attach);
304 message.setAttachment(V2MessageModelConverter.parseV2Attachment(messageType,attachment));
305 }
306 }
307 catch (Throwable e) {
308 NimLog.e(TAG, "messageDeserialization exception, attachment error, attachment = "+msgBody.opt(ITalkService.TagString.attach),e);
309 return null;
310 }
311
312 try {
313 if(msgBody.has(ITalkService.TagString.msgid_client)){
314 message.setMessageClientId(msgBody.getString(ITalkService.TagString.msgid_client));
315 }
316 }
317 catch (Throwable e) {
318 NimLog.e(TAG, "messageDeserialization exception, messageClientId error, messageClientId = "+msgBody.opt(ITalkService.TagString.msgid_client),e);
319 return null;
320 }
321
322 try {
323 if(msgBody.has(ITalkService.TagString.msgid_server)){
324 message.setMessageServerId(msgBody.getLong(ITalkService.TagString.msgid_server));
325 }
326 }
327 catch (Throwable e) {
328 NimLog.e(TAG, "messageDeserialization exception, messageServerId error, messageServerId = "+msgBody.opt(ITalkService.TagString.msgid_server),e);
329 return null;
330 }
331
332 try {
333 if(msgBody.has(ITalkService.TagString.ext)){
334 message.setServerExtension(msgBody.getString(ITalkService.TagString.ext));
335 }
336 }
337 catch (Throwable e) {
338 NimLog.e(TAG, "messageDeserialization exception, serverExtension error, serverExtension = "+msgBody.opt(ITalkService.TagString.ext),e);
339 return null;
340 }
341
342 V2NIMMessagePushConfigBuilder pushConfigBuilder = V2NIMMessagePushConfigBuilder.builder();
343 try {
344 if(msgBody.has(ITalkService.TagString.pushEnable)){
345 pushConfigBuilder.withPushEnabled(msgBody.getInt(ITalkService.TagString.pushEnable) == 1);
346 }
347 }
348 catch (Throwable e) {
349 NimLog.e(TAG, "messageDeserialization exception, pushConfig error, pushEnable = "+msgBody.opt(ITalkService.TagString.pushEnable),e);
350 return null;
351 }
352
353 try {
354 if(msgBody.has(ITalkService.TagString.pushPayload)){
355 pushConfigBuilder.withPayload(msgBody.getString(ITalkService.TagString.pushPayload));
356 }
357 }
358 catch (Throwable e) {
359 NimLog.e(TAG, "messageDeserialization exception, pushConfig error, pushPayload = "+msgBody.opt(ITalkService.TagString.pushPayload),e);
360 return null;
361 }
362
363 try {
364 if(msgBody.has(ITalkService.TagString.pushContent)){
365 pushConfigBuilder.withContent(msgBody.getString(ITalkService.TagString.pushContent));
366 }
367 }
368 catch (Throwable e) {
369 NimLog.e(TAG, "messageDeserialization exception, pushConfig error, pushContent = "+msgBody.opt(ITalkService.TagString.pushContent),e);
370 return null;
371 }
372
373 try {
374 if(msgBody.has(ITalkService.TagString.needPushNick)){
375 pushConfigBuilder.withPushNickEnabled(msgBody.getInt(ITalkService.TagString.needPushNick) == 1);
376 }
377 }
378 catch (Throwable e) {
379 NimLog.e(TAG, "messageDeserialization exception, pushConfig error, needPushNick = "+msgBody.opt(ITalkService.TagString.needPushNick),e);
380 return null;
381 }
382
383 try {
384 if(msgBody.has(ITalkService.TagString.forcePushContent)){
385 pushConfigBuilder.withForcePushContent(msgBody.getString(ITalkService.TagString.forcePushContent));
386 }
387 }
388 catch (Throwable e) {
389 NimLog.e(TAG, "messageDeserialization exception, pushConfig error, forcePushContent = "+msgBody.opt(ITalkService.TagString.forcePushContent),e);
390 return null;
391 }
392
393 try {
394 if(msgBody.has(ITalkService.TagString.isForcePush)){
395 pushConfigBuilder.withForcePush(msgBody.getInt(ITalkService.TagString.isForcePush) == 1);
396 }
397 }
398 catch (Throwable e) {
399 NimLog.e(TAG, "messageDeserialization exception, pushConfig error, isForcePush = "+msgBody.opt(ITalkService.TagString.isForcePush),e);
400 return null;
401 }
402
403 try {
404 if(msgBody.has(ITalkService.TagString.forcePushList)){
405 String forcePushListStr = msgBody.optString(ITalkService.TagString.forcePushList);
406 if (!TextUtils.isEmpty(forcePushListStr)) {
407 pushConfigBuilder.withForcePushAccountIds(MsgHelper.getListFromJsonString(forcePushListStr));
408 }
409 }
410 }
411 catch (Throwable e) {
412 NimLog.e(TAG, "messageDeserialization exception, pushConfig error, forcePushList = "+msgBody.opt(ITalkService.TagString.forcePushList),e);
413 return null;
414 }
415
416 message.setPushConfig(pushConfigBuilder.build());
417
418 V2NIMMessageAntispamConfigBuilder antispamConfigBuilder = V2NIMMessageAntispamConfigBuilder.builder();
419 try {
420 if(msgBody.has(ITalkService.TagString.antiSpamContent)){
421 antispamConfigBuilder.withAntispamCustomMessage(msgBody.getString(ITalkService.TagString.antiSpamContent));
422 }
423 }
424 catch (Throwable e) {
425 NimLog.e(TAG, "messageDeserialization exception, antispamConfig error, antispamCustomMessage = "+msgBody.opt(ITalkService.TagString.antiSpamContent),e);
426 return null;
427 }
428
429 try {
430 if(msgBody.has(ITalkService.TagString.antiSpamUsingYidun)){
431 antispamConfigBuilder.withAntispamEnabled(msgBody.getInt(ITalkService.TagString.antiSpamUsingYidun) == 1);
432 }
433 }
434 catch (Throwable e) {
435 NimLog.e(TAG, "messageDeserialization exception, antispamConfig error, antispamEnabled = "+msgBody.opt(ITalkService.TagString.antiSpamUsingYidun),e);
436 return null;
437 }
438
439 try {
440 if(msgBody.has(ITalkService.TagString.antiSpamConfigId)){
441 antispamConfigBuilder.withAntispamBusinessId(msgBody.getString(ITalkService.TagString.antiSpamConfigId));
442 }
443 }
444 catch (Throwable e) {
445 NimLog.e(TAG, "messageDeserialization exception, antispamConfig error, antispamBusinessId = "+msgBody.opt(ITalkService.TagString.antiSpamConfigId),e);
446 return null;
447 }
448
449 try {
450 if(msgBody.has(ITalkService.TagString.yidunAntiCheating)){
451 antispamConfigBuilder.withAntispamCheating(msgBody.getString(ITalkService.TagString.yidunAntiCheating));
452 }
453 }
454 catch (Throwable e) {
455 NimLog.e(TAG, "messageDeserialization exception, antispamConfig error, antispamCheating = "+msgBody.opt(ITalkService.TagString.yidunAntiCheating),e);
456 return null;
457 }
458
459 try{
460 if(msgBody.has(ITalkService.TagString.yidunAntiSpamExt)){
461 antispamConfigBuilder.withAntispamExtension(msgBody.getString(ITalkService.TagString.yidunAntiSpamExt));
462 }
463 }
464 catch (Throwable e) {
465 NimLog.e(TAG, "messageDeserialization exception, antispamConfig error, antispamExtension = "+msgBody.opt(ITalkService.TagString.yidunAntiSpamExt),e);
466 return null;
467 }
468
469 message.setAntispamConfig(antispamConfigBuilder.build());
470
471 V2NIMMessageConfigBuilder messageConfigBuilder = V2NIMMessageConfigBuilder.builder();
472
473 try {
474 if(msgBody.has(ITalkService.TagString.msgAck)){
475 messageConfigBuilder.withReadReceiptEnabled(msgBody.getInt(ITalkService.TagString.msgAck) == 1);
476 }
477 }
478 catch (Throwable e) {
479 NimLog.e(TAG, "messageDeserialization exception, messageConfig error, readReceiptEnabled = "+msgBody.opt(ITalkService.TagString.msgAck),e);
480 return null;
481 }
482
483 try {
484 if(msgBody.has(ITalkService.TagString.isSessionUpdate)){
485 messageConfigBuilder.withLastMessageUpdateEnabled(msgBody.getInt(ITalkService.TagString.isSessionUpdate) == 1);
486 }
487 }
488 catch (Throwable e) {
489 NimLog.e(TAG, "messageDeserialization exception, messageConfig error, lastMessageUpdateEnabled = "+msgBody.opt(ITalkService.TagString.isSessionUpdate),e);
490 return null;
491 }
492
493 try {
494 if(msgBody.has(ITalkService.TagString.historyEnable)){
495 messageConfigBuilder.withHistoryEnabled(msgBody.getInt(ITalkService.TagString.historyEnable) == 1);
496 }
497 }
498 catch (Throwable e) {
499 NimLog.e(TAG, "messageDeserialization exception, messageConfig error, historyEnable = "+msgBody.opt(ITalkService.TagString.historyEnable),e);
500 return null;
501 }
502
503 try {
504 if(msgBody.has(ITalkService.TagString.roamingEnable)){
505 messageConfigBuilder.withRoamingEnabled(msgBody.getInt(ITalkService.TagString.roamingEnable) == 1);
506 }
507 }
508 catch (Throwable e) {
509 NimLog.e(TAG, "messageDeserialization exception, messageConfig error, roamingEnable = "+msgBody.opt(ITalkService.TagString.roamingEnable),e);
510 return null;
511 }
512
513 try {
514 if(msgBody.has(ITalkService.TagString.selfSyncEnable)){
515 messageConfigBuilder.withOnlineSyncEnabled(msgBody.getInt(ITalkService.TagString.selfSyncEnable) == 1);
516 }
517 }
518 catch (Throwable e) {
519 NimLog.e(TAG, "messageDeserialization exception, messageConfig error, onlineSyncEnabled = "+msgBody.opt(ITalkService.TagString.selfSyncEnable),e);
520 return null;
521 }
522
523 try {
524 if(msgBody.has(ITalkService.TagString.persistEnable)){
525 messageConfigBuilder.withOfflineEnabled(msgBody.getInt(ITalkService.TagString.persistEnable) == 1);
526 }
527 }
528 catch (Throwable e) {
529 NimLog.e(TAG, "messageDeserialization exception, messageConfig error, offlineEnabled = "+msgBody.opt(ITalkService.TagString.persistEnable),e);
530 return null;
531 }
532
533 try {
534 if(msgBody.has(ITalkService.TagString.needBadge)){
535 messageConfigBuilder.withUnreadEnabled(msgBody.getInt(ITalkService.TagString.needBadge) == 1);
536 }
537 }
538 catch (Throwable e) {
539 NimLog.e(TAG, "messageDeserialization exception, messageConfig error, unreadEnabled = "+msgBody.opt(ITalkService.TagString.needBadge),e);
540 return null;
541 }
542
543 message.setMessageConfig(messageConfigBuilder.build());
544
545 boolean hasThreadRoot = false;
546
547 String threadRootSenderId = null;
548 try {
549 if (msgBody.has(ITalkService.TagString.threadMsgFromAccount)) {
550 threadRootSenderId = msgBody.getString(ITalkService.TagString.threadMsgFromAccount);
551 hasThreadRoot = true;
552 }
553 }
554 catch (Throwable e) {
555 NimLog.e(TAG, "messageDeserialization exception, threadRootSenderId error, threadRoot senderId = "+msgBody.opt(ITalkService.TagString.threadMsgFromAccount),e);
556 return null;
557 }
558
559 String threadRootReceiverId = null;
560 try {
561 if (msgBody.has(ITalkService.TagString.threadMsgToAccount)) {
562 threadRootReceiverId = msgBody.getString(ITalkService.TagString.threadMsgToAccount);
563 hasThreadRoot = true;
564 }
565 }
566 catch (Throwable e) {
567 NimLog.e(TAG, "messageDeserialization exception, threadRootReceiverId error, threadRoot receiverId = "+msgBody.opt(ITalkService.TagString.threadMsgToAccount),e);
568 return null;
569 }
570
571 long threadRootCreateTime = 0;
572 try {
573 if (msgBody.has(ITalkService.TagString.threadMsgTime)) {
574 threadRootCreateTime = msgBody.getLong(ITalkService.TagString.threadMsgTime);
575 hasThreadRoot = true;
576 }
577 }
578 catch (Throwable e) {
579 NimLog.e(TAG, "messageDeserialization exception, threadRootCreateTime error, threadRoot createTime = "+msgBody.opt(ITalkService.TagString.threadMsgTime),e);
580 return null;
581 }
582
583 long threadRootMessageServerId = 0;
584 try {
585 if (msgBody.has(ITalkService.TagString.threadMsgIdServer)) {
586 threadRootMessageServerId = msgBody.getLong(ITalkService.TagString.threadMsgIdServer);
587 hasThreadRoot = true;
588 }
589 }
590 catch (Throwable e) {
591 NimLog.e(TAG, "messageDeserialization exception, threadRootMessageServerId error, threadRoot messageServerId = "+msgBody.opt(ITalkService.TagString.threadMsgIdServer),e);
592 return null;
593 }
594
595 String threadRootMessageClientId = null;
596 try {
597 if (msgBody.has(ITalkService.TagString.threadMsgIdClient)) {
598 threadRootMessageClientId = msgBody.getString(ITalkService.TagString.threadMsgIdClient);
599 hasThreadRoot = true;
600 }
601 }
602 catch (Throwable e) {
603 NimLog.e(TAG, "messageDeserialization exception, threadRootMessageClientId error, threadRoot messageClientId = "+msgBody.opt(ITalkService.TagString.threadMsgIdClient),e);
604 return null;
605 }
606
607 if(hasThreadRoot){
608 V2NIMMessageReferImpl threadRoot = new V2NIMMessageReferImpl(
609 threadRootSenderId,
610 threadRootReceiverId,
611 threadRootMessageClientId,
612 threadRootMessageServerId,
613 conversationType,
614 sessionId,
615 threadRootCreateTime);
616 message.setThreadRoot(threadRoot);
617 }
618
619 boolean hasThreadReply = false;
620
621 String threadReplySenderId = null;
622 try {
623 if (msgBody.has(ITalkService.TagString.replyMsgFromAccount)) {
624 threadReplySenderId = msgBody.getString(ITalkService.TagString.replyMsgFromAccount);
625 hasThreadReply = true;
626 }
627 }
628 catch (Throwable e) {
629 NimLog.e(TAG, "messageDeserialization exception, threadReplySenderId error, threadReply senderId = "+msgBody.opt(ITalkService.TagString.replyMsgFromAccount),e);
630 return null;
631 }
632
633 String threadReplyReceiverId = null;
634 try {
635 if (msgBody.has(ITalkService.TagString.replyMsgToAccount)) {
636 threadReplyReceiverId = msgBody.getString(ITalkService.TagString.replyMsgToAccount);
637 hasThreadReply = true;
638 }
639 }
640 catch (Throwable e) {
641 NimLog.e(TAG, "messageDeserialization exception, threadReplyReceiverId error, threadReply receiverId = "+msgBody.opt(ITalkService.TagString.replyMsgToAccount),e);
642 return null;
643 }
644
645 long threadReplyCreateTime = 0;
646 try {
647 if (msgBody.has(ITalkService.TagString.replyMsgTime)) {
648 threadReplyCreateTime = msgBody.getLong(ITalkService.TagString.replyMsgTime);
649 hasThreadReply = true;
650 }
651 }
652 catch (Throwable e) {
653 NimLog.e(TAG, "messageDeserialization exception, threadReplyCreateTime error, threadReply createTime = "+msgBody.opt(ITalkService.TagString.replyMsgTime),e);
654 return null;
655 }
656
657 long threadReplyMessageServerId = 0;
658 try {
659 if (msgBody.has(ITalkService.TagString.replyMsgIdServer)) {
660 threadReplyMessageServerId = msgBody.getLong(ITalkService.TagString.replyMsgIdServer);
661 hasThreadReply = true;
662 }
663 }
664 catch (Throwable e) {
665 NimLog.e(TAG, "messageDeserialization exception, threadReplyMessageServerId error, threadReply messageServerId = "+msgBody.opt(ITalkService.TagString.replyMsgIdServer),e);
666 return null;
667 }
668
669 String threadReplyMessageClientId = null;
670 try {
671 if (msgBody.has(ITalkService.TagString.replyMsgIdClient)) {
672 threadReplyMessageClientId = msgBody.getString(ITalkService.TagString.replyMsgIdClient);
673 hasThreadReply = true;
674 }
675 }
676 catch (Throwable e) {
677 NimLog.e(TAG, "messageDeserialization exception, threadReplyMessageClientId error, threadReply messageClientId = "+msgBody.opt(ITalkService.TagString.replyMsgIdClient),e);
678 return null;
679 }
680
681 if(hasThreadReply){
682 V2NIMMessageReferImpl threadReply = new V2NIMMessageReferImpl(
683 threadReplySenderId,
684 threadReplyReceiverId,
685 threadReplyMessageClientId,
686 threadReplyMessageServerId,
687 conversationType,
688 sessionId,
689 threadReplyCreateTime);
690 message.setThreadReply(threadReply);
691 }
692
693
694 try {
695 if (msgBody.has(ITalkService.TagString.delete)) {
696 message.setDeleted(msgBody.getInt(ITalkService.TagString.delete) == 1);
697 }
698 }
699 catch (Throwable e) {
700 NimLog.e(TAG, "messageDeserialization exception, delete error, delete = "+msgBody.opt(ITalkService.TagString.delete),e);
701 return null;
702 }
703
704 try {
705 if (msgBody.has(ITalkService.TagString.callbackExt)) {
706 message.setCallbackExtension(msgBody.getString(ITalkService.TagString.callbackExt));
707 }
708 }
709 catch (Throwable e) {
710 NimLog.e(TAG, "messageDeserialization exception, callbackExt error, callbackExt = "+msgBody.opt(ITalkService.TagString.callbackExt),e);
711 return null;
712 }
713
714
715 try {
716 if (msgBody.has(ITalkService.TagString.subtype)) {
717 message.setSubType(msgBody.getInt(ITalkService.TagString.subtype));
718 }
719 }
720 catch (Throwable e) {
721 NimLog.e(TAG, "messageDeserialization exception, subtype error, subtype = "+msgBody.opt(ITalkService.TagString.subtype),e);
722 return null;
723 }
724
725
726
727 try {
728 if (msgBody.has(ITalkService.TagString.clientExt)){
729 String clientExt = msgBody.getString(ITalkService.TagString.clientExt);
730 TimeConsumingStatisticsImpl timeConsumingStatistics = TimeConsumingStatisticsImpl.initWithClientExt(clientExt);
731 message.setTimeConsumingStatistics(timeConsumingStatistics);
732 }
733 }
734 catch (Throwable e) {
735 NimLog.e(TAG, "messageDeserialization exception, clientExt error, clientExt = "+msgBody.opt(ITalkService.TagString.clientExt),e);
736 return null;
737 }
738
739 boolean hasRobotConfig = false;
740
741 String robotConfigFunction = null;
742 try {
743 if(msgBody.has(ITalkService.TagString.function)){
744 robotConfigFunction = msgBody.getString(ITalkService.TagString.function);
745 hasRobotConfig = true;
746 }
747 }
748 catch (Throwable e) {
749 NimLog.e(TAG, "messageDeserialization exception, robotConfig error, function = "+msgBody.opt(ITalkService.TagString.function),e);
750 return null;
751 }
752
753 String robotConfigTopic = null;
754 try {
755 if(msgBody.has(ITalkService.TagString.topic)){
756 robotConfigTopic = msgBody.getString(ITalkService.TagString.topic);
757 hasRobotConfig = true;
758 }
759 }
760 catch (Throwable e) {
761 NimLog.e(TAG, "messageDeserialization exception, robotConfig error, topic = "+msgBody.opt(ITalkService.TagString.topic),e);
762 return null;
763 }
764
765 String robotConfigCustomContent = null;
766 try {
767 if(msgBody.has(ITalkService.TagString.customContent)){
768 robotConfigCustomContent = msgBody.getString(ITalkService.TagString.customContent);
769 hasRobotConfig = true;
770 }
771 }
772 catch (Throwable e) {
773 NimLog.e(TAG, "messageDeserialization exception, robotConfig error, customContent = "+msgBody.opt(ITalkService.TagString.customContent),e);
774 return null;
775 }
776
777 String robotConfigAccountId = null;
778 try {
779 if(msgBody.has(ITalkService.TagString.account)){
780 robotConfigAccountId = msgBody.getString(ITalkService.TagString.account);
781 }
782 }
783 catch (Throwable e) {
784 NimLog.e(TAG, "messageDeserialization exception, robotConfig error, accountId = "+msgBody.opt(ITalkService.TagString.account),e);
785 return null;
786 }
787
788 if(hasRobotConfig){
789 V2NIMMessageRobotConfigBuilder robotConfigBuilder = V2NIMMessageRobotConfigBuilder.builder();
790 robotConfigBuilder.withFunction(robotConfigFunction);
791 robotConfigBuilder.withTopic(robotConfigTopic);
792 robotConfigBuilder.withCustomContent(robotConfigCustomContent);
793 robotConfigBuilder.withAccountId(robotConfigAccountId);
794 message.setRobotConfig(robotConfigBuilder.build());
795 }
796
797 V2NIMMessageRouteConfigBuilder routeConfigBuilder = V2NIMMessageRouteConfigBuilder.builder();
798
799 try {
800 if(msgBody.has(ITalkService.TagString.routeEnable)){
801 routeConfigBuilder.withRouteEnabled(msgBody.getInt(ITalkService.TagString.routeEnable) == 1);
802 }
803 }
804 catch (Throwable e) {
805 NimLog.e(TAG, "messageDeserialization exception, routeConfig error, routeEnable = "+msgBody.opt(ITalkService.TagString.routeEnable),e);
806 return null;
807 }
808
809 try {
810 if(msgBody.has(ITalkService.TagString.env)){
811 routeConfigBuilder.withRouteEnvironment(msgBody.getString(ITalkService.TagString.env));
812 }
813 }
814 catch (Throwable e) {
815 NimLog.e(TAG, "messageDeserialization exception, routeConfig error, routeEnvironment = "+msgBody.opt(ITalkService.TagString.env),e);
816 return null;
817 }
818
819 message.setRouteConfig(routeConfigBuilder.build());
820
821 try {
822 if(msgBody.has(ITalkService.TagString.isBlackListed)){
823 message.setInBlackList(msgBody.getInt(ITalkService.TagString.isBlackListed) == 1);
824 }
825 }
826 catch (Throwable e) {
827 NimLog.e(TAG, "messageDeserialization exception, isBlackListed error, isBlackListed = "+msgBody.opt(ITalkService.TagString.isBlackListed),e);
828 return null;
829 }
830
831 try {
832 if(msgBody.has(ITalkService.TagString.ai_agent_account)){
834 aiConfig.setAccountId(msgBody.optString(ITalkService.TagString.ai_agent_account));
835 aiConfig.setAIStatus(V2NIMMessageAIStatus.typeOfValue(msgBody.optInt(TagString.ai_agent_msg_direction)));
836 message.setAIConfig(aiConfig);
837 }
838 }
839 catch (Throwable e) {
840 NimLog.e(TAG, "messageDeserialization exception, ai_agent_account error, ai_agent_account = "+msgBody.optString(ITalkService.TagString.ai_agent_account),e);
841 return null;
842 }
843
844 try {
845 if(msgBody.has(ITalkService.TagString.ai_agent_error_code)){
846 message.setAIErrorCode(msgBody.optInt(ITalkService.TagString.ai_agent_error_code));
847 }
848 }
849 catch (Throwable e) {
850 NimLog.e(TAG, "messageDeserialization exception, ai_agent_error_code error, ai_agent_error_code = "+msgBody.optInt(ITalkService.TagString.ai_agent_error_code),e);
851 return null;
852 }
853
854 try {
855 if(msgBody.has(ITalkService.TagString.modifyTimetag)){
856 message.setModifyTime(msgBody.optLong(ITalkService.TagString.modifyTimetag));
857 }
858 }
859 catch (Throwable e) {
860 NimLog.e(TAG, "messageDeserialization exception, modifyTimetag error, modifyTimetag = "+msgBody.optLong(ITalkService.TagString.modifyTimetag),e);
861 return null;
862 }
863
864 try {
865 if(msgBody.has(ITalkService.TagString.modifyAccid)){
866 message.setModifyAccountId(msgBody.optString(ITalkService.TagString.modifyAccid));
867 }
868 }
869 catch (Throwable e) {
870 NimLog.e(TAG, "messageDeserialization exception, modifyAccid error, modifyAccid = "+msgBody.optString(ITalkService.TagString.modifyAccid),e);
871 return null;
872 }
873
874
875 return message;
876 }
877
878 private static String sessionIdOfMsg(int sessionType,String fromId, String receiver, String currentAccount) {
879 //当前账号为空,不设置sessionId
880 if (TextUtils.isEmpty(currentAccount)) {
881 return null;
882 }
883 if (sessionType == ITalkService.SessionType.P2P || sessionType == ITalkService.SessionType.Ysf) {
884 //如果不是当前账号的消息,不设置sessionId
885 if(!currentAccount.equals(fromId) && !currentAccount.equals(receiver)) {
886 return null;
887 }
888 return TextUtils.equals(currentAccount, fromId) ? receiver : fromId;
889 } else {
890 return receiver;
891 }
892 }
893}
static String messageSerialization(V2NIMMessage message)
消息序列化为字符串
static V2NIMMessage messageDeserialization(String msg)
字符串反序列化为消息对象
void setAIStatus(V2NIMMessageAIStatus aiStatus)
设置该 AI 消息的询问和应答标识
V2NIMMessageAIStatus getAIStatus()
获取该 AI 消息的询问和应答标识
void setAccountId(String accountId)
设置数字人的 accountId