This repository has been archived by the owner on Jun 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
TencentImPlugin.java
1810 lines (1660 loc) · 72.7 KB
/
TencentImPlugin.java
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package top.huic.tencent_im_plugin;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.tencent.imsdk.TIMConversation;
import com.tencent.imsdk.TIMConversationType;
import com.tencent.imsdk.TIMElem;
import com.tencent.imsdk.TIMElemType;
import com.tencent.imsdk.TIMFriendshipManager;
import com.tencent.imsdk.TIMGroupAddOpt;
import com.tencent.imsdk.TIMGroupManager;
import com.tencent.imsdk.TIMGroupMemberInfo;
import com.tencent.imsdk.TIMGroupReceiveMessageOpt;
import com.tencent.imsdk.TIMManager;
import com.tencent.imsdk.TIMMessage;
import com.tencent.imsdk.TIMSdkConfig;
import com.tencent.imsdk.TIMSoundElem;
import com.tencent.imsdk.TIMUserConfig;
import com.tencent.imsdk.TIMUserProfile;
import com.tencent.imsdk.TIMVideoElem;
import com.tencent.imsdk.conversation.ProgressInfo;
import com.tencent.imsdk.ext.group.TIMGroupBaseInfo;
import com.tencent.imsdk.ext.group.TIMGroupDetailInfo;
import com.tencent.imsdk.ext.group.TIMGroupDetailInfoResult;
import com.tencent.imsdk.ext.group.TIMGroupMemberResult;
import com.tencent.imsdk.ext.group.TIMGroupPendencyGetParam;
import com.tencent.imsdk.ext.group.TIMGroupPendencyItem;
import com.tencent.imsdk.ext.group.TIMGroupPendencyListGetSucc;
import com.tencent.imsdk.friendship.TIMCheckFriendResult;
import com.tencent.imsdk.friendship.TIMFriend;
import com.tencent.imsdk.friendship.TIMFriendCheckInfo;
import com.tencent.imsdk.friendship.TIMFriendGroup;
import com.tencent.imsdk.friendship.TIMFriendPendencyItem;
import com.tencent.imsdk.friendship.TIMFriendPendencyRequest;
import com.tencent.imsdk.friendship.TIMFriendPendencyResponse;
import com.tencent.imsdk.friendship.TIMFriendRequest;
import com.tencent.imsdk.friendship.TIMFriendResponse;
import com.tencent.imsdk.friendship.TIMFriendResult;
import com.tencent.imsdk.session.SessionWrapper;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import top.huic.tencent_im_plugin.entity.GroupMemberEntity;
import top.huic.tencent_im_plugin.entity.GroupPendencyEntity;
import top.huic.tencent_im_plugin.entity.GroupPendencyPageEntiity;
import top.huic.tencent_im_plugin.entity.MessageEntity;
import top.huic.tencent_im_plugin.entity.PendencyEntity;
import top.huic.tencent_im_plugin.entity.PendencyPageEntiity;
import top.huic.tencent_im_plugin.entity.SessionEntity;
import top.huic.tencent_im_plugin.enums.ListenerTypeEnum;
import top.huic.tencent_im_plugin.enums.MessageNodeType;
import top.huic.tencent_im_plugin.listener.TencentImListener;
import top.huic.tencent_im_plugin.message.AbstractMessageNode;
import top.huic.tencent_im_plugin.message.entity.AbstractMessageEntity;
import top.huic.tencent_im_plugin.util.JsonUtil;
import top.huic.tencent_im_plugin.util.TencentImUtils;
/**
* TencentImPlugin
*/
public class TencentImPlugin implements FlutterPlugin, MethodCallHandler {
/**
* 日志签名
*/
public static String TAG = "TencentImPlugin";
/**
* 全局上下文
*/
public static Context context;
/**
* 与Flutter的通信管道
*/
private static MethodChannel channel;
public TencentImPlugin() {
}
private TencentImPlugin(Context context, MethodChannel channel) {
TencentImPlugin.context = context;
TencentImPlugin.channel = channel;
JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.DisableCircularReferenceDetect.mask;
}
@Override
public void onAttachedToEngine(FlutterPluginBinding flutterPluginBinding) {
final MethodChannel channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "tencent_im_plugin");
channel.setMethodCallHandler(new TencentImPlugin(flutterPluginBinding.getApplicationContext(), channel));
}
// This static function is optional and equivalent to onAttachedToEngine. It supports the old
// pre-Flutter-1.12 Android projects. You are encouraged to continue supporting
// plugin registration via this function while apps migrate to use the new Android APIs
// post-flutter-1.12 via https://flutter.dev/go/android-project-migration.
//
// It is encouraged to share logic between onAttachedToEngine and registerWith to keep
// them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called
// depending on the user's project. onAttachedToEngine or registerWith must both be defined
// in the same class.
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "tencent_im_plugin");
channel.setMethodCallHandler(new TencentImPlugin(registrar.context(), channel));
}
@Override
public void onMethodCall(MethodCall call, Result result) {
switch (call.method) {
case "init":
this.init(call, result);
break;
case "login":
this.login(call, result);
break;
case "logout":
this.logout(call, result);
break;
case "getLoginUser":
this.getLoginUser(call, result);
break;
case "initStorage":
this.initStorage(call, result);
break;
case "getConversationList":
getConversationList(call, result);
break;
case "getConversation":
getConversation(call, result);
break;
case "getGroupInfo":
this.getGroupInfo(call, result);
break;
case "getUserInfo":
this.getUserInfo(call, result);
break;
case "getMessages":
this.getMessages(call, result);
break;
case "getLocalMessages":
this.getLocalMessages(call, result);
break;
case "setRead":
this.setRead(call, result);
break;
case "sendMessage":
this.sendMessage(call, result);
break;
case "saveMessage":
this.saveMessage(call, result);
break;
case "getFriendList":
this.getFriendList(call, result);
break;
case "getGroupList":
this.getGroupList(call, result);
break;
case "addFriend":
this.addFriend(call, result);
break;
case "checkSingleFriends":
this.checkSingleFriends(call, result);
break;
case "getPendencyList":
this.getPendencyList(call, result);
break;
case "pendencyReport":
this.pendencyReport(call, result);
break;
case "deletePendency":
this.deletePendency(call, result);
break;
case "examinePendency":
this.examinePendency(call, result);
break;
case "deleteConversation":
this.deleteConversation(call, result);
break;
case "deleteLocalMessage":
this.deleteLocalMessage(call, result);
break;
case "createGroup":
this.createGroup(call, result);
break;
case "inviteGroupMember":
this.inviteGroupMember(call, result);
break;
case "applyJoinGroup":
this.applyJoinGroup(call, result);
break;
case "quitGroup":
this.quitGroup(call, result);
break;
case "deleteGroupMember":
this.deleteGroupMember(call, result);
break;
case "getGroupMembers":
this.getGroupMembers(call, result);
break;
case "deleteGroup":
this.deleteGroup(call, result);
break;
case "modifyGroupOwner":
this.modifyGroupOwner(call, result);
break;
case "modifyGroupInfo":
this.modifyGroupInfo(call, result);
break;
case "modifyMemberInfo":
this.modifyMemberInfo(call, result);
break;
case "getGroupPendencyList":
this.getGroupPendencyList(call, result);
break;
case "reportGroupPendency":
this.reportGroupPendency(call, result);
break;
case "groupPendencyAccept":
this.groupPendencyAccept(call, result);
break;
case "groupPendencyRefuse":
this.groupPendencyRefuse(call, result);
break;
case "getSelfProfile":
this.getSelfProfile(call, result);
break;
case "modifySelfProfile":
this.modifySelfProfile(call, result);
break;
case "modifyFriend":
this.modifyFriend(call, result);
break;
case "deleteFriends":
this.deleteFriends(call, result);
break;
case "addBlackList":
this.addBlackList(call, result);
break;
case "deleteBlackList":
this.deleteBlackList(call, result);
break;
case "getBlackList":
this.getBlackList(call, result);
break;
case "createFriendGroup":
this.createFriendGroup(call, result);
break;
case "deleteFriendGroup":
this.deleteFriendGroup(call, result);
break;
case "addFriendsToFriendGroup":
this.addFriendsToFriendGroup(call, result);
break;
case "deleteFriendsFromFriendGroup":
this.deleteFriendsFromFriendGroup(call, result);
break;
case "renameFriendGroup":
this.renameFriendGroup(call, result);
break;
case "getFriendGroups":
this.getFriendGroups(call, result);
break;
case "revokeMessage":
this.revokeMessage(call, result);
break;
case "removeMessage":
this.removeMessage(call, result);
break;
case "setMessageCustomInt":
this.setMessageCustomInt(call, result);
break;
case "setMessageCustomStr":
this.setMessageCustomStr(call, result);
break;
case "downloadVideoImage":
this.downloadVideoImage(call, result);
break;
case "downloadVideo":
this.downloadVideo(call, result);
break;
case "downloadSound":
this.downloadSound(call, result);
break;
case "findMessage":
this.findMessage(call, result);
break;
default:
result.notImplemented();
break;
}
}
@Override
public void onDetachedFromEngine(FlutterPluginBinding binding) {
}
/**
* 腾讯云Im初始化事件
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void init(MethodCall methodCall, Result result) {
// 应用appid
String appid = this.getParam(methodCall, result, "appid");
Boolean enabledLogPrint = this.getParam(methodCall, result, "enabledLogPrint");
Integer logPrintLevel = this.getParam(methodCall, result, "logPrintLevel");
// 主线程才初始化SDK
if (SessionWrapper.isMainProcess(context)) {
TencentImListener listener = new TencentImListener(TencentImPlugin.channel);
// 初始化 SDK
TIMManager.getInstance().init(context, new TIMSdkConfig(Integer.parseInt(appid))
.enableLogPrint(enabledLogPrint)
.setLogLevel(logPrintLevel)
);
// 基本用户配置
TIMUserConfig userConfig = new TIMUserConfig()
.setUserStatusListener(listener)
.setConnectionListener(listener)
.setGroupEventListener(listener)
.setRefreshListener(listener)
.setMessageRevokedListener(listener)
.setMessageReceiptListener(listener)
.enableReadReceipt(true)
.setUploadProgressListener(listener);
TIMManager.getInstance().setUserConfig(userConfig);
// 添加消息监听器
TIMManager.getInstance().addMessageListener(listener);
}
result.success(null);
}
/**
* 腾讯云 IM 登录
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void login(MethodCall methodCall, final Result result) {
// 用户ID和签名
final String identifier = this.getParam(methodCall, result, "identifier");
String userSig = this.getParam(methodCall, result, "userSig");
// 如果用户已登录,则不允许重复登录
if (!TextUtils.isEmpty(TIMManager.getInstance().getLoginUser())) {
result.error("login failed. ", "user is login", "user is already login ,you should login out first");
return;
}
// 登录操作
TIMManager.getInstance().login(identifier, userSig, new VoidCallBack(result));
}
/**
* 腾讯云 IM 退出登录
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void logout(MethodCall methodCall, final Result result) {
if (!TextUtils.isEmpty(TIMManager.getInstance().getLoginUser())) {
TIMManager.getInstance().logout(new VoidCallBack(result));
}
}
/**
* 腾讯云 获得当前登录用户
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void getLoginUser(MethodCall methodCall, final Result result) {
result.success(TIMManager.getInstance().getLoginUser());
}
/**
* 腾讯云 初始化本地存储
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void initStorage(MethodCall methodCall, final Result result) {
// 用户ID
String identifier = this.getParam(methodCall, result, "identifier");
//初始化本地存储
TIMManager.getInstance().initStorage(identifier, new VoidCallBack(result));
}
/**
* 腾讯云 获得当前登录用户会话列表
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void getConversationList(MethodCall methodCall, final Result result) {
TencentImUtils.getConversationInfo(new ValueCallBack<List<SessionEntity>>(result), TIMManager.getInstance().getConversationList());
}
/**
* 腾讯云 根据ID获得会话
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void getConversation(MethodCall methodCall, final Result result) {
// 会话ID
String sessionId = this.getParam(methodCall, result, "sessionId");
// 会话类型
String sessionTypeStr = this.getParam(methodCall, result, "sessionType");
// 获得会话信息
TIMConversation conversation = TencentImUtils.getSession(sessionId, sessionTypeStr);
TencentImUtils.getConversationInfo(new ValueCallBack<List<SessionEntity>>(result) {
@Override
public void onSuccess(List<SessionEntity> sessionEntities) {
result.success(JsonUtil.toJSONString(sessionEntities.get(0)));
}
}, Collections.singletonList(conversation));
}
/**
* 腾讯云 获得群信息
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void getGroupInfo(MethodCall methodCall, final Result result) {
// 群ID
String id = this.getParam(methodCall, result, "id");
TIMGroupDetailInfo groupDetailInfo = TIMGroupManager.getInstance().queryGroupInfo(id);
if (groupDetailInfo == null) {
TIMGroupManager.getInstance().getGroupInfo(Collections.singletonList(id), new ValueCallBack<List<TIMGroupDetailInfoResult>>(result) {
@Override
public void onSuccess(List<TIMGroupDetailInfoResult> timGroupDetailInfoResults) {
if (timGroupDetailInfoResults != null && timGroupDetailInfoResults.size() >= 1) {
result.success(JsonUtil.toJSONString(timGroupDetailInfoResults.get(0)));
} else {
result.success(null);
}
}
});
} else {
result.success(JsonUtil.toJSONString(groupDetailInfo));
}
}
/**
* 腾讯云 获得用户信息
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void getUserInfo(MethodCall methodCall, final Result result) {
// 用户ID
String id = this.getParam(methodCall, result, "id");
boolean forceUpdate = this.getParam(methodCall, result, "forceUpdate");
TIMFriendshipManager.getInstance().getUsersProfile(Collections.singletonList(id), forceUpdate, new ValueCallBack<List<TIMUserProfile>>(result) {
@Override
public void onSuccess(List<TIMUserProfile> timUserProfiles) {
if (timUserProfiles != null && timUserProfiles.size() >= 1) {
result.success(JsonUtil.toJSONString(timUserProfiles.get(0)));
} else {
result.success(null);
}
}
});
}
/**
* 腾讯云 设置会话消息为已读
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void setRead(MethodCall methodCall, final Result result) {
// 会话ID
String sessionId = this.getParam(methodCall, result, "sessionId");
// 会话类型
String sessionTypeStr = this.getParam(methodCall, result, "sessionType");
// 获得会话信息
TIMConversation conversation = TencentImUtils.getSession(sessionId, sessionTypeStr);
// 设置已读
conversation.setReadMessage(null, new VoidCallBack(result));
}
/**
* 腾讯云 获得消息列表
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void getMessages(MethodCall methodCall, final Result result) {
this.getMessages(methodCall, result, false);
}
/**
* 腾讯云 获得本地消息列表
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void getLocalMessages(MethodCall methodCall, final Result result) {
this.getMessages(methodCall, result, true);
}
/**
* 获得消息列表
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
* @param local 是否是获取本地消息
*/
private void getMessages(MethodCall methodCall, final Result result, final boolean local) {
// 会话IDgetMessage
String sessionId = this.getParam(methodCall, result, "sessionId");
// 会话类型
String sessionTypeStr = this.getParam(methodCall, result, "sessionType");
// 消息数量
final Integer number = this.getParam(methodCall, result, "number");
// 获得会话信息
final TIMConversation conversation = TencentImUtils.getSession(sessionId, sessionTypeStr);
TencentImUtils.getTimMessage(methodCall, result, "lastMessage", new ValueCallBack<TIMMessage>(result) {
@Override
public void onSuccess(TIMMessage message) {
// 获得聊天记录
if (local) {
conversation.getLocalMessage(number, message, new ValueCallBack<List<TIMMessage>>(result) {
@Override
public void onSuccess(List<TIMMessage> timMessages) {
if (timMessages == null || timMessages.size() == 0) {
result.success(JsonUtil.toJSONString(new ArrayList<>()));
return;
}
TencentImUtils.getMessageInfo(timMessages, new ValueCallBack<List<MessageEntity>>(result));
}
});
} else {
conversation.getMessage(number, message, new ValueCallBack<List<TIMMessage>>(result) {
@Override
public void onSuccess(List<TIMMessage> timMessages) {
if (timMessages == null || timMessages.size() == 0) {
result.success(JsonUtil.toJSONString(new ArrayList<>()));
return;
}
TencentImUtils.getMessageInfo(timMessages, new ValueCallBack<List<MessageEntity>>(result));
}
});
}
}
});
}
/**
* 发送消息
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void sendMessage(MethodCall methodCall, final Result result) {
String nodeStr = this.getParam(methodCall, result, "node");
Map node = JSON.parseObject(nodeStr, Map.class);
String sessionType = this.getParam(methodCall, result, "sessionType");
String sessionId = this.getParam(methodCall, result, "sessionId");
boolean ol = this.getParam(methodCall, result, "ol");
// 发送消息
AbstractMessageNode messageNode = MessageNodeType.valueOf(node.get("nodeType").toString()).getMessageNodeInterface();
AbstractMessageEntity messageEntity = (AbstractMessageEntity) JSON.parseObject(nodeStr, messageNode.getEntityClass());
messageNode.send(TencentImUtils.getSession(sessionId, sessionType), messageEntity, ol, new ValueCallBack<TIMMessage>(result) {
@Override
public void onSuccess(TIMMessage message) {
TencentImUtils.getMessageInfo(Collections.singletonList(message), new ValueCallBack<List<MessageEntity>>(result) {
@Override
public void onSuccess(List<MessageEntity> messageEntities) {
result.success(JsonUtil.toJSONString(messageEntities.get(0)));
}
});
}
});
}
/**
* 发送消息
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void saveMessage(MethodCall methodCall, final Result result) {
String nodeStr = this.getParam(methodCall, result, "node");
Map node = JSON.parseObject(nodeStr, Map.class);
String sessionType = this.getParam(methodCall, result, "sessionType");
String sessionId = this.getParam(methodCall, result, "sessionId");
String sender = this.getParam(methodCall, result, "sender");
Boolean isReaded = this.getParam(methodCall, result, "isReaded");
// 发送消息
AbstractMessageNode messageNode = MessageNodeType.valueOf(node.get("nodeType").toString()).getMessageNodeInterface();
AbstractMessageEntity messageEntity = (AbstractMessageEntity) JSON.parseObject(nodeStr, messageNode.getEntityClass());
TIMMessage message = messageNode.save(TencentImUtils.getSession(sessionId, sessionType), messageEntity, sender, isReaded);
TencentImUtils.getMessageInfo(Collections.singletonList(message), new ValueCallBack<List<MessageEntity>>(result) {
@Override
public void onSuccess(List<MessageEntity> messageEntities) {
result.success(JsonUtil.toJSONString(messageEntities.get(0)));
}
});
}
/**
* 腾讯云 获得好友列表
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void getFriendList(MethodCall methodCall, final Result result) {
TIMFriendshipManager.getInstance().getFriendList(new ValueCallBack<List<TIMFriend>>(result));
}
/**
* 腾讯云 获得群组列表
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void getGroupList(MethodCall methodCall, final Result result) {
TIMGroupManager.getInstance().getGroupList(new ValueCallBack<List<TIMGroupBaseInfo>>(result) {
@Override
public void onSuccess(List<TIMGroupBaseInfo> groupBaseInfos) {
List<String> ids = new ArrayList<>(groupBaseInfos.size());
for (TIMGroupBaseInfo groupBaseInfo : groupBaseInfos) {
ids.add(groupBaseInfo.getGroupId());
}
if (ids.size() == 0) {
result.success(JsonUtil.toJSONString(new ArrayList<>()));
return;
}
// 获得群资料
TIMGroupManager.getInstance().getGroupInfo(ids, new ValueCallBack<List<TIMGroupDetailInfoResult>>(result));
}
});
}
/**
* 腾讯云 添加好友
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void addFriend(MethodCall methodCall, final Result result) {
// 用户Id
String id = this.getParam(methodCall, result, "id");
// 添加类型
int addType = this.getParam(methodCall, result, "addType");
// 备注
String remark = methodCall.argument("remark");
if (remark == null) {
remark = "";
}
// 请求说明
String addWording = methodCall.argument("addWording");
if (addWording == null) {
addWording = "";
}
// 添加来源
String addSource = methodCall.argument("addSource");
if (addSource == null) {
addSource = "";
}
// 分组名
String friendGroup = methodCall.argument("friendGroup");
if (friendGroup == null) {
friendGroup = "";
}
TIMFriendRequest request = new TIMFriendRequest(id);
request.setRemark(remark);
request.setAddWording(addWording);
request.setAddSource(addSource);
request.setFriendGroup(friendGroup);
request.setAddType(addType);
// 添加好友
TIMFriendshipManager.getInstance().addFriend(request, new ValueCallBack<TIMFriendResult>(result));
}
/**
* 腾讯云 检测单个好友关系
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void checkSingleFriends(MethodCall methodCall, final Result result) {
// 用户Id
String id = this.getParam(methodCall, result, "id");
// 类型
int type = methodCall.argument("type");
TIMFriendCheckInfo checkInfo = new TIMFriendCheckInfo();
checkInfo.setUsers(Collections.singletonList(id));
checkInfo.setCheckType(type);
// 检测关系
TIMFriendshipManager.getInstance().checkFriends(checkInfo, new ValueCallBack<List<TIMCheckFriendResult>>(result) {
@Override
public void onSuccess(List<TIMCheckFriendResult> timCheckFriendResults) {
result.success(JsonUtil.toJSONString(timCheckFriendResults.get(0)));
}
});
}
/**
* 腾讯云 获得未决好友列表(申请中)
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void getPendencyList(MethodCall methodCall, final Result result) {
// 类型
int type = this.getParam(methodCall, result, "type");
// 未决列表序列号。建议客户端保存 seq 和未决列表,请求时填入 server 返回的 seq。如果 seq 是 server 最新的,则不返回数据
int seq = methodCall.argument("seq");
// 翻页时间戳,只用来翻页,server 返回0时表示没有更多数据,第一次请求填0
int timestamp = methodCall.argument("timestamp");
// 每页的数量,请求时有效
int numPerPage = methodCall.argument("numPerPage");
// 封装请求对象
TIMFriendPendencyRequest request = new TIMFriendPendencyRequest();
request.setTimPendencyGetType(type);
request.setSeq(seq);
request.setTimestamp(timestamp);
request.setNumPerPage(numPerPage);
TIMFriendshipManager.getInstance().getPendencyList(request, new ValueCallBack<TIMFriendPendencyResponse>(result) {
@Override
public void onSuccess(final TIMFriendPendencyResponse timFriendPendencyResponse) {
if (timFriendPendencyResponse.getItems().size() == 0) {
result.success(JsonUtil.toJSONString(new HashMap<>()));
return;
}
// 返回结果
final List<PendencyEntity> resultData = new ArrayList<>(timFriendPendencyResponse.getItems().size());
// 用户ID对应用户对象
final Map<String, PendencyEntity> map = new HashMap<>();
// 循环获得的列表,进行对象封装
for (TIMFriendPendencyItem item : timFriendPendencyResponse.getItems()) {
map.put(item.getIdentifier(), new PendencyEntity(item));
}
// 获得用户信息
TIMFriendshipManager.getInstance().getUsersProfile(Arrays.asList(map.keySet().toArray(new String[0])), true, new ValueCallBack<List<TIMUserProfile>>(result) {
@Override
public void onSuccess(List<TIMUserProfile> timUserProfiles) {
// 设置用户资料
for (TIMUserProfile timUserProfile : timUserProfiles) {
PendencyEntity data = map.get(timUserProfile.getIdentifier());
if (data != null) {
data.setUserProfile(timUserProfile);
resultData.add(data);
}
}
// 返回结果
result.success(JsonUtil.toJSONString(new PendencyPageEntiity(timFriendPendencyResponse, resultData)));
}
});
}
});
}
/**
* 腾讯云 未决已读上报
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void pendencyReport(MethodCall methodCall, final Result result) {
// 已读时间戳,此时间戳以前的消息都将置为已读
int timestamp = this.getParam(methodCall, result, "timestamp");
TIMFriendshipManager.getInstance().pendencyReport(timestamp, new VoidCallBack(result));
}
/**
* 腾讯云 未决删除
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void deletePendency(MethodCall methodCall, final Result result) {
// 类型
int type = this.getParam(methodCall, result, "type");
// 用户Id
String id = this.getParam(methodCall, result, "id");
// 删除未决
TIMFriendshipManager.getInstance().deletePendency(type, Collections.singletonList(id), new ValueCallBack<List<TIMFriendResult>>(result));
}
/**
* 腾讯云 未决审核
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void examinePendency(MethodCall methodCall, final Result result) {
// 类型
int type = this.getParam(methodCall, result, "type");
// 用户Id
String id = this.getParam(methodCall, result, "id");
// 好友备注
String remark = methodCall.argument("remark");
// 未决审核
TIMFriendResponse response = new TIMFriendResponse();
response.setIdentifier(id);
response.setResponseType(type);
response.setRemark(remark);
TIMFriendshipManager.getInstance().doResponse(response, new ValueCallBack<TIMFriendResult>(result));
}
/**
* 腾讯云 删除会话
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void deleteConversation(MethodCall methodCall, final Result result) {
// 会话ID
String sessionId = this.getParam(methodCall, result, "sessionId");
// 会话类型
String sessionTypeStr = this.getParam(methodCall, result, "sessionType");
TIMConversationType sessionType = TIMConversationType.valueOf(sessionTypeStr);
// 删除本地缓存(会话内的消息内容)
boolean removeCache = this.getParam(methodCall, result, "removeCache");
boolean res;
if (removeCache) {
res = TIMManager.getInstance().deleteConversationAndLocalMsgs(sessionType, sessionId);
} else {
res = TIMManager.getInstance().deleteConversation(sessionType, sessionId);
}
result.success(res);
}
/**
* 腾讯云 删除会话内的本地聊天记录
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void deleteLocalMessage(MethodCall methodCall, final Result result) {
// 会话ID
String sessionId = this.getParam(methodCall, result, "sessionId");
// 会话类型
String sessionTypeStr = this.getParam(methodCall, result, "sessionType");
// 获得会话信息
TIMConversation conversation = TencentImUtils.getSession(sessionId, sessionTypeStr);
conversation.deleteLocalMessage(new VoidCallBack(result));
}
/**
* 腾讯云 创建群组
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void createGroup(MethodCall methodCall, final Result result) {
// 群类型
String type = this.getParam(methodCall, result, "type");
// 群名称
String name = this.getParam(methodCall, result, "name");
// 群ID
String groupId = methodCall.argument("groupId");
// 群公告
String notification = methodCall.argument("notification");
// 群简介
String introduction = methodCall.argument("introduction");
// 群头像
String faceUrl = methodCall.argument("faceUrl");
// 加群选项
String addOption = methodCall.argument("addOption");
// 最大群成员数
Integer maxMemberNum = methodCall.argument("maxMemberNum");
// 默认群成员
List<TIMGroupMemberInfo> members = JSON.parseArray(this.getParam(methodCall, result, "members").toString(), TIMGroupMemberInfo.class);
// 创建参数对象
TIMGroupManager.CreateGroupParam param = new TIMGroupManager.CreateGroupParam(type, name);
param.setGroupId(groupId);
param.setNotification(notification);
param.setIntroduction(introduction);
param.setFaceUrl(faceUrl);
param.setMembers(members);
param.setAddOption(addOption != null ? TIMGroupAddOpt.valueOf(addOption) : null);
if (maxMemberNum != null) {
param.setMaxMemberNum(maxMemberNum);
}
// 创建群
TIMGroupManager.getInstance().createGroup(param, new ValueCallBack<String>(result) {
@Override
public void onSuccess(String s) {
result.success(s);
}
});
}
/**
* 腾讯云 邀请加入群组
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void inviteGroupMember(MethodCall methodCall, final Result result) {
// 群ID
String groupId = this.getParam(methodCall, result, "groupId");
// 用户ID集合
List<String> ids = Arrays.asList(this.getParam(methodCall, result, "ids").toString().split(","));
TIMGroupManager.getInstance().inviteGroupMember(groupId, ids, new ValueCallBack<List<TIMGroupMemberResult>>(result));
}
/**
* 腾讯云 申请加入群组
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void applyJoinGroup(MethodCall methodCall, final Result result) {
// 群ID
String groupId = this.getParam(methodCall, result, "groupId");
// 申请理由
String reason = methodCall.argument("reason");
TIMGroupManager.getInstance().applyJoinGroup(groupId, reason, new VoidCallBack(result));
}
/**
* 腾讯云 退出群组
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void quitGroup(MethodCall methodCall, final Result result) {
// 群ID
String groupId = this.getParam(methodCall, result, "groupId");
TIMGroupManager.getInstance().quitGroup(groupId, new VoidCallBack(result));
}
/**
* 腾讯云 删除群组成员
*
* @param methodCall 方法调用对象
* @param result 返回结果对象
*/
private void deleteGroupMember(MethodCall methodCall, final Result result) {
// 群ID
String groupId = this.getParam(methodCall, result, "groupId");
// 用户ID集合
List<String> ids = Arrays.asList(this.getParam(methodCall, result, "ids").toString().split(","));
// 删除理由