/
ChatMessage.ts
1589 lines (1521 loc) · 42 KB
/
ChatMessage.ts
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
import { generateMessageId, getNowTimestamp } from '../__internal__/Utils';
import { ChatClient } from '../ChatClient';
import type { ChatSearchDirection } from './ChatConversation';
import { ChatError } from './ChatError';
import type { ChatMessageReaction } from './ChatMessageReaction';
import type { ChatMessageThread } from './ChatMessageThread';
/**
* 会话类型枚举。
*/
export enum ChatMessageChatType {
/**
* 单聊。
*/
PeerChat = 0,
/**
* 群聊。
*/
GroupChat = 1,
/**
* 聊天室。
*/
ChatRoom = 2,
}
/**
* 消息的方向枚举。
*/
export enum ChatMessageDirection {
/**
* 该消息是当前用户发送出去的。
*/
SEND = 'send',
/**
* 该消息是当前用户接收到的。
*/
RECEIVE = 'rec',
}
/**
* 消息的发送状态枚举类。
*/
export enum ChatMessageStatus {
/**
* 消息已创建待发送。
*/
CREATE = 0,
/**
* 正在发送。
*/
PROGRESS = 1,
/**
* 发送成功。
*/
SUCCESS = 2,
/**
* 发送失败。
*/
FAIL = 3,
}
/**
* 消息附件下载状态枚举。
*/
export enum ChatDownloadStatus {
/**
* 等待下载。
*/
PENDING = -1,
/**
* 正在下载。
*/
DOWNLOADING = 0,
/**
* 下载成功。
*/
SUCCESS = 1,
/**
* 下载失败。
*/
FAILED = 2,
}
/**
* 消息类型。
*/
export enum ChatMessageType {
/**
* 文本消息。
*/
TXT = 'txt',
/**
* 图片消息。
*/
IMAGE = 'img',
/**
* 视频消息。
*/
VIDEO = 'video',
/**
* 位置消息。
*/
LOCATION = 'loc',
/**
* 语音消息。
*/
VOICE = 'voice',
/**
* 文件消息。
*/
FILE = 'file',
/**
* 命令消息。
*/
CMD = 'cmd',
/**
* 自定义消息。
*/
CUSTOM = 'custom',
/**
* 合并消息。
*/
COMBINE = 'combine',
}
/**
* 聊天室消息优先级。
*/
export enum ChatRoomMessagePriority {
/**
* 高
*/
PriorityHigh = 0,
/**
* 普通。默认优先级为 `普通`。
*/
PriorityNormal,
/**
* 低。
*/
PriorityLow,
}
/**
* 将会话类型由整型转换为字符串类型。
*
* @param params 整型的会话类型。
* @returns 字符串类型的会话类型。
*/
export function ChatMessageChatTypeFromNumber(
params: number
): ChatMessageChatType {
switch (params) {
case 2:
return ChatMessageChatType.ChatRoom;
case 1:
return ChatMessageChatType.GroupChat;
default:
return ChatMessageChatType.PeerChat;
}
}
/**
* 将消息方向由字符串类型转换为枚举类型。
*
* @param params 字符串类型的消息方向。
* @returns 枚举类型的消息方向。
*/
export function ChatMessageDirectionFromString(
params: string
): ChatMessageDirection {
switch (params) {
case 'send':
return ChatMessageDirection.SEND;
default:
return ChatMessageDirection.RECEIVE;
}
}
/**
* 将消息状态由整型转换为枚举类型。
*
* @param params 整型的消息状态。
* @returns 枚举类型的消息状态。
*/
export function ChatMessageStatusFromNumber(params: number): ChatMessageStatus {
switch (params) {
case 3:
return ChatMessageStatus.FAIL;
case 2:
return ChatMessageStatus.SUCCESS;
case 1:
return ChatMessageStatus.PROGRESS;
default:
return ChatMessageStatus.CREATE;
}
}
/**
* 将消息状态由枚举类型转换为字符串类型。
*
* @param params 枚举类型的消息状态。
* @returns 字符串类型的消息状态。
*/
export function ChatMessageStatusToString(params: ChatMessageStatus): string {
return ChatMessageStatus[params]!;
}
/**
* 将消息下载状态由整型转换为字符串类型。
*
* @param params 整型的消息下载状态。
* @returns 字符串类型的消息下载状态。
*/
export function ChatDownloadStatusFromNumber(
params: number
): ChatDownloadStatus {
switch (params) {
case 0:
return ChatDownloadStatus.DOWNLOADING;
case 1:
return ChatDownloadStatus.SUCCESS;
case 2:
return ChatDownloadStatus.FAILED;
default:
return ChatDownloadStatus.PENDING;
}
}
/**
* 将消息附件下载状态由整型转换为字符串类型。
*
* @param params 整型的消息附件下载状态。
* @returns 枚举类型的消息附件下载状态。
*/
export function ChatDownloadStatusToString(params: ChatDownloadStatus): string {
return ChatDownloadStatus[params]!;
}
/**
* 将消息类型由字符串类型转换为枚举类型。
*
* @param params 字符串类型的消息类型。
* @returns 枚举类型的消息类型。
*/
export function ChatMessageTypeFromString(params: string): ChatMessageType {
switch (params) {
case 'txt':
return ChatMessageType.TXT;
case 'loc':
return ChatMessageType.LOCATION;
case 'cmd':
return ChatMessageType.CMD;
case 'custom':
return ChatMessageType.CUSTOM;
case 'file':
return ChatMessageType.FILE;
case 'img':
return ChatMessageType.IMAGE;
case 'video':
return ChatMessageType.VIDEO;
case 'voice':
return ChatMessageType.VOICE;
case 'combine':
return ChatMessageType.COMBINE;
default:
throw new ChatError({
code: 1,
description: `This type is not supported. ` + params,
});
}
}
/**
* 消息状态监听器接口。
*/
export interface ChatMessageStatusCallback {
/**
* 消息上传或下载进度回调。
*
* @param progress 消息上传或下载的进度值,取值范围为 [0,100]。
*/
onProgress(localMsgId: string, progress: number): void;
/**
* 消息错误回调。
*
* @param error 错误码和错误描述,详见 {@link ChatError}。
*/
onError(localMsgId: string, error: ChatError): void;
/**
* 消息发送成功回调。
* @param message 发送成功的消息。
*/
onSuccess(message: ChatMessage): void;
}
/**
* 消息类,用于定义一条要发送或接收的消息。
*
* 例如,构造一条文本消息:
*
* ```typescript
* let msg = ChatMessage.createTextMessage(
* 'asteriskhx2',
* Date.now().toString(),
* ChatMessageChatType.PeerChat
* );
* ```
*/
export class ChatMessage {
static TAG = 'ChatMessage';
/**
* 服务器生成的消息 ID。
*/
msgId: string = generateMessageId();
/**
* 本地消息 ID。
*/
localMsgId: string = '';
/**
* 会话 ID。
*/
conversationId: string = '';
/**
* 消息发送者的用户 ID。
*/
from: string = '';
/**
* 消息接收方的用户 ID:
*
* - 单聊:消息接收方的用户 ID;
* - 群聊:群组 ID;
* - 聊天室:聊天室 ID;
* - 子区:子区 ID。
*/
to: string = '';
/**
* 消息创建的本地 Unix 时间戳。单位为毫秒。
*/
localTime: number = getNowTimestamp();
/**
* 服务器收到该消息的 Unix 时间戳。单位为毫秒。
*/
serverTime: number = getNowTimestamp();
/**
* 单聊时,发送的消息是否送达至接收方。要使该参数生效,初始化时需接收方设置 {@link ChatOptions.requireDeliveryAck} 为 `true`。群消息不支持送达回执。
*
* - `true`:是。
* - (默认)`false`:否。
*/
hasDeliverAck: boolean = false;
/**
* 单聊时,发送方是否收到了接收方的已读回执。接收方阅读消息后会调用 {@link ChatManager.sendMessageReadAck} 或者 {@link ChatManager.sendConversationReadAck} 发送已读回执。若需要已读回执,SDK 初始化时需设置 {@link ChatOptions.requireAck} 为 `true`。
*
* - `true`:是。
* - (默认)`false`:否。
*/
hasReadAck: boolean = false;
/**
* 群聊时,是否需要消息已读回执。
*
* - `true`:是。
* - (默认)`false`:否。
*/
needGroupAck: boolean = false;
/**
* 群聊时,已阅读消息的群成员数量。群成员已读消息后调用 {@link ChatManager.sendGroupMessageReadAck} 或者 {@link ChatManager.sendConversationReadAck} 发送已读回执。若需要开启已读回执功能,初始化时需设置 {@link ChatOptions.requireAck} 为 `true`, 并且发送消息时设置 {@link isNeedGroupAck} 为 `true`。
*/
groupAckCount: number = 0;
/**
* 单聊或群聊时,接收方是否已读了消息。该参数的值影响会话的未读消息数。阅读消息后,接收方可以调用 {@link ChatManager.markMessageAsRead} 或者 {@link ChatManager.markAllMessagesAsRead} 将消息设置为已读。
*
* - `true`:是。
* - (默认)`false`:否。
*/
hasRead: boolean = false;
/**
* 会话类型,包括单聊,群聊和聊天室。详见 {@link ChatType}。
*/
chatType: ChatMessageChatType = ChatMessageChatType.ChatRoom;
/**
* 消息方向,详见 {@link ChatMessageDirection}。
*/
direction: ChatMessageDirection = ChatMessageDirection.SEND;
/**
* 消息发送状态,详见 {@link ChatMessageStatus}。
*/
status: ChatMessageStatus = ChatMessageStatus.CREATE;
/**
* 消息的扩展属性。
*/
attributes: Record<string, any>;
/**
* 消息体实例,详见 {@link ChatMessageBody}。
*/
body: ChatMessageBody;
/**
* 消息是否为子区消息:
*
* - `true`:是。你需将消息接收方的用户 ID 设置为子区 ID。详见 {@link to}。
* - `false`:否。
*
* **注意**
* 该参数仅对群聊有效。
*/
isChatThread: boolean;
/**
* 消息是否为在线时收到的消息。
*
* - `true`: 是。这种情况下,如果应用在后台运行可以弹出消息提示框。
* - `false`: 否,离线时收到的消息。
*/
isOnline: boolean;
/**
* 聊天室消息的优先级。
* **Note** 该属性仅适用于聊天室会话。
*/
private priority?: ChatRoomMessagePriority;
/**
* 消息是否只投递给在线用户:
* - `true`:只有消息接收方在线时才能投递成功。若接收方离线,则消息会被丢弃。
* - (默认)`false`:如果用户在线,则直接投递;如果用户离线,消息会在用户上线时投递。
*/
deliverOnlineOnly: boolean;
/**
* 消息接收对象列表。
*
* 默认为 `undefined`,表示群组或聊天室中的所有成员均能收到该消息。
*
* 该属性只在群组或者聊天室中使用。
*/
receiverList?: string[];
/**
* 是否是广播消息。
*/
isBroadcast: boolean;
/**
* 构造消息。
*/
public constructor(params: {
msgId?: string;
localMsgId?: string;
conversationId?: string;
from?: string;
to?: string;
localTime?: number;
serverTime?: number;
hasDeliverAck?: boolean;
hasReadAck?: boolean;
needGroupAck?: boolean;
groupAckCount?: number;
hasRead?: boolean;
chatType?: number;
direction?: string;
status?: number;
attributes?: any;
body: any;
isChatThread?: boolean;
isOnline?: boolean;
deliverOnlineOnly?: boolean;
receiverList?: string[];
isBroadcast?: boolean;
}) {
this.msgId = params.msgId ?? generateMessageId();
this.conversationId = params.conversationId ?? '';
this.from = params.from ?? '';
this.to = params.to ?? '';
this.localTime = params.localTime ?? getNowTimestamp();
this.serverTime = params.serverTime ?? getNowTimestamp();
this.hasDeliverAck = params.hasDeliverAck ?? false;
this.hasReadAck = params.hasReadAck ?? false;
this.needGroupAck = params.needGroupAck ?? false;
this.groupAckCount = params.groupAckCount ?? 0;
this.hasRead = params.hasRead ?? false;
this.chatType = ChatMessageChatTypeFromNumber(params.chatType ?? 0);
this.direction = ChatMessageDirectionFromString(params.direction ?? 'send');
this.status = ChatMessageStatusFromNumber(params.status ?? 0);
this.attributes = {};
this.fromAttributes(params.attributes);
this.body = ChatMessage.getBody(params.body);
this.localMsgId = this.localTime.toString();
this.isChatThread = params.isChatThread ?? false;
this.isOnline = params.isOnline ?? true;
this.deliverOnlineOnly = params.deliverOnlineOnly ?? false;
this.receiverList = params.receiverList;
this.isBroadcast = params.isBroadcast ?? false;
}
private fromAttributes(attributes: any) {
if (attributes) {
const keys = Object.getOwnPropertyNames(attributes);
for (const key of keys) {
const v = attributes[key];
if (typeof v === 'object') {
this.attributes[key] = v;
} else if (typeof v === 'function') {
this.attributes[key] = v;
} else if (typeof v === 'symbol' || typeof v === 'undefined') {
this.attributes[key] = v;
} else if (typeof v === 'string') {
// !!! maybe json string
try {
this.attributes[key] = JSON.parse(v);
} catch (error) {
this.attributes[key] = v;
}
} else {
this.attributes[key] = v;
}
}
}
}
private static getBody(params: any): ChatMessageBody {
let type = ChatMessageTypeFromString(params.type as string);
switch (type) {
case ChatMessageType.TXT:
return new ChatTextMessageBody(params);
case ChatMessageType.LOCATION:
return new ChatLocationMessageBody(params);
case ChatMessageType.CMD:
return new ChatCmdMessageBody(params);
case ChatMessageType.CUSTOM:
return new ChatCustomMessageBody(params);
case ChatMessageType.FILE:
return new ChatFileMessageBody(params);
case ChatMessageType.IMAGE:
return new ChatImageMessageBody(params);
case ChatMessageType.VIDEO:
return new ChatVideoMessageBody(params);
case ChatMessageType.VOICE:
return new ChatVoiceMessageBody(params);
case ChatMessageType.COMBINE:
return new ChatCombineMessageBody(params);
default:
throw new ChatError({
code: 1,
description: `This type is not supported. ` + type,
});
}
}
private static createSendMessage(params: {
body: ChatMessageBody;
targetId: string;
chatType: ChatMessageChatType;
isChatThread?: boolean;
isOnline?: boolean;
deliverOnlineOnly?: boolean;
receiverList?: string[];
}): ChatMessage {
let r = new ChatMessage({
from: ChatClient.getInstance().currentUserName ?? '',
body: params.body,
direction: 'send',
to: params.targetId,
hasRead: true,
chatType: params.chatType,
isChatThread: params.isChatThread,
conversationId: params.targetId,
isOnline: params.isOnline,
deliverOnlineOnly: params.deliverOnlineOnly,
receiverList: params.receiverList,
});
return r;
}
/**
* 创建一条待发送的文本消息。
*
* @param targetId 消息接收方的用户 ID。
* - 单聊时是接收方的用户 ID。
* - 群聊时是群组 ID。
* - 聊天室则是聊天室 ID。
* @param content 文本消息内容。
* @param chatType 会话类型。详见 {@link ChatType}。
* @params opt 消息扩展参数。
* - isChatThread: 是否是子区消息。默认不是子区消息。
* - isOnline: 是否为在线时收到的消息。
* - deliverOnlineOnly: 消息是否只投递给在线用户。
*
* @returns 消息实例。
*/
public static createTextMessage(
targetId: string,
content: string,
chatType: ChatMessageChatType = ChatMessageChatType.PeerChat,
opt?: {
isChatThread?: boolean;
targetLanguageCodes?: Array<string>;
isOnline?: boolean;
deliverOnlineOnly?: boolean;
receiverList?: string[];
}
): ChatMessage {
return ChatMessage.createSendMessage({
body: new ChatTextMessageBody({
content: content,
targetLanguageCodes: opt?.targetLanguageCodes,
}),
targetId: targetId,
chatType: chatType,
isChatThread: opt?.isChatThread,
isOnline: opt?.isOnline,
deliverOnlineOnly: opt?.deliverOnlineOnly,
receiverList: opt?.receiverList,
});
}
/**
* 创建一条待发送的文件类型消息。
*
* @param targetId 消息接收方的用户 ID。
* - 单聊时为接收方的用户 ID。
* - 群聊时为群组 ID。
* - 聊天室则为聊天室 ID。
* @param filePath 文件路径。
* @param chatType 会话类型。详见 {@link ChatType}。
* @params opt 消息扩展参数。
* - isChatThread: 是否是子区消息。默认不是子区消息。
* - isOnline: 是否为在线时收到的消息。
* - deliverOnlineOnly: 消息是否只投递给在线用户。
*
* @returns 消息实例。
*/
public static createFileMessage(
targetId: string,
filePath: string,
chatType: ChatMessageChatType = ChatMessageChatType.PeerChat,
opt?: {
displayName: string;
isChatThread?: boolean;
fileSize?: number;
isOnline?: boolean;
deliverOnlineOnly?: boolean;
receiverList?: string[];
}
): ChatMessage {
return ChatMessage.createSendMessage({
body: new ChatFileMessageBody({
localPath: filePath,
displayName: opt?.displayName ?? '',
fileSize: opt?.fileSize,
}),
targetId: targetId,
chatType: chatType,
isChatThread: opt?.isChatThread,
isOnline: opt?.isOnline,
deliverOnlineOnly: opt?.deliverOnlineOnly,
receiverList: opt?.receiverList,
});
}
/**
* 创建一条待发送的图片消息。
*
* @param targetId 消息接收方的用户 ID。
* - 单聊:接收方的用户 ID。
* - 群聊:群组 ID。
* - 聊天室:聊天室 ID。
* @param filePath 图片路径。
* @param chatType 会话类型。
* @params opt 消息扩展参数。
* - isChatThread: 是否是子区消息。默认不是子区消息。
* - isOnline: 是否为在线时收到的消息。
* - deliverOnlineOnly: 消息是否只投递给在线用户。
* - displayName: 图片名称。
* - thumbnailLocalPath: 缩略图本地地址。
* - sendOriginalImage: 是否发送原图。
* - width: 图片宽度。
* - height: 图片高度。
* - fileSize: 图片大小。
*
* @returns 消息实例。
*/
public static createImageMessage(
targetId: string,
filePath: string,
chatType: ChatMessageChatType = ChatMessageChatType.PeerChat,
opt?: {
displayName: string;
thumbnailLocalPath?: string;
sendOriginalImage?: boolean;
width: number;
height: number;
isChatThread?: boolean;
fileSize?: number;
isOnline?: boolean;
deliverOnlineOnly?: boolean;
receiverList?: string[];
}
): ChatMessage {
return ChatMessage.createSendMessage({
body: new ChatImageMessageBody({
localPath: filePath,
displayName: opt?.displayName ?? filePath,
thumbnailLocalPath: opt?.thumbnailLocalPath,
sendOriginalImage: opt?.sendOriginalImage ?? false,
width: opt?.width,
height: opt?.height,
fileSize: opt?.fileSize,
}),
targetId: targetId,
chatType: chatType,
isChatThread: opt?.isChatThread,
isOnline: opt?.isOnline,
deliverOnlineOnly: opt?.deliverOnlineOnly,
receiverList: opt?.receiverList,
});
}
/**
* 创建一条待发送的视频消息。
*
* @param targetId 消息接收方的用户 ID。
* - 单聊时是接收方的用户 ID。
* - 群聊时是群组 ID。
* - 聊天室则是聊天室 ID。
* @param filePath 视频文件路径。
* @param chatType 会话类型。
* @params opt 消息扩展参数。
* - isChatThread: 是否是子区消息。默认不是子区消息。
* - isOnline: 是否为在线时收到的消息。
* - deliverOnlineOnly: 消息是否只投递给在线用户。
* - displayName: 文件名称。
* - thumbnailLocalPath: 缩略图本地地址。
* - duration: 视频时长。
* - width: 视频宽度。
* - height: 视频高度。
* - fileSize: 文件大小。
*
* @returns 消息实例。
*/
public static createVideoMessage(
targetId: string,
filePath: string,
chatType: ChatMessageChatType = ChatMessageChatType.PeerChat,
opt?: {
displayName: string;
thumbnailLocalPath: string;
duration: number;
width: number;
height: number;
isChatThread?: boolean;
fileSize?: number;
isOnline?: boolean;
deliverOnlineOnly?: boolean;
receiverList?: string[];
}
): ChatMessage {
return ChatMessage.createSendMessage({
body: new ChatVideoMessageBody({
localPath: filePath,
displayName: opt?.displayName ?? '',
thumbnailLocalPath: opt?.thumbnailLocalPath,
duration: opt?.duration,
width: opt?.width,
height: opt?.height,
fileSize: opt?.fileSize,
}),
targetId: targetId,
chatType: chatType,
isChatThread: opt?.isChatThread,
isOnline: opt?.isOnline,
deliverOnlineOnly: opt?.deliverOnlineOnly,
receiverList: opt?.receiverList,
});
}
/**
* 创建一条待发送的语音消息。
*
* @param targetId 消息接收方 ID。
* - 单聊时是接收方的用户 ID。
* - 群聊时是群组 ID。
* - 聊天室则是聊天室 ID。
* @param filePath 语音文件路径。
* @param chatType 会话类型。
* @params opt 消息扩展参数。
* - isChatThread: 是否是子区消息。默认不是子区消息。
* - isOnline: 是否为在线时收到的消息。
* - deliverOnlineOnly: 消息是否只投递给在线用户。
* - displayName: 文件名称。
* - thumbnailLocalPath: 缩略图本地地址。
* - duration: 语音时长。
* - fileSize: 文件大小。
*
* @returns 消息实例。
*/
public static createVoiceMessage(
targetId: string,
filePath: string,
chatType: ChatMessageChatType = ChatMessageChatType.PeerChat,
opt?: {
displayName?: string;
duration: number;
isChatThread?: boolean;
fileSize?: number;
isOnline?: boolean;
deliverOnlineOnly?: boolean;
receiverList?: string[];
}
): ChatMessage {
return ChatMessage.createSendMessage({
body: new ChatVoiceMessageBody({
localPath: filePath,
displayName: opt?.displayName ?? '',
duration: opt?.duration,
fileSize: opt?.fileSize,
}),
targetId: targetId,
chatType: chatType,
isChatThread: opt?.isChatThread,
isOnline: opt?.isOnline,
deliverOnlineOnly: opt?.deliverOnlineOnly,
receiverList: opt?.receiverList,
});
}
/**
* 创建合并类型消息体。
*
* @param targetId 消息接收方。
* - 单聊时是接收方的用户 ID。
* - 群聊时是群组 ID。
* - 聊天室则是聊天室 ID。
* @param messageIdList 合并的消息列表 ID。
* @param chatType 会话类型。 详见 {@link ChatType}.
* @params opt 消息扩展参数。
* - isChatThread: 是否是子区消息。
* - `true`:是;
* - (默认)`false`:否。
* - isOnline: 是否为在线时收到的消息。
* - `true`:是;
* - `false`:否。
* - deliverOnlineOnly: 消息是否只投递给在线用户。
* - `true`:是。只有消息接收方在线时才能投递成功。若接收方离线,则消息会被丢弃。
* - (默认)`false`:否。如果用户在线,则直接投递;如果用户离线,消息会在用户上线时投递。
* - title: 合并消息的标题。
* - summary: 合并消息的概要。
* - compatibleText: 合并消息的兼容信息。该字段用于需要兼容不支持合并转发消息的版本。
*
* @returns The message instance.
*/
public static createCombineMessage(
targetId: string,
messageIdList: string[],
chatType: ChatMessageChatType = ChatMessageChatType.PeerChat,
opt?: {
title?: string;
summary?: string;
compatibleText?: string;
isChatThread?: boolean;
isOnline?: boolean;
deliverOnlineOnly?: boolean;
receiverList?: string[];
}
): ChatMessage {
return ChatMessage.createSendMessage({
body: new ChatCombineMessageBody({
localPath: '',
title: opt?.title,
summary: opt?.summary,
compatibleText: opt?.compatibleText,
messageIdList: messageIdList,
}),
targetId: targetId,
chatType: chatType,
isChatThread: opt?.isChatThread,
isOnline: opt?.isOnline,
deliverOnlineOnly: opt?.deliverOnlineOnly,
receiverList: opt?.receiverList,
});
}
/**
* 创建一条待发送的位置消息。
*
* @param targetId 消息接收方的用户 ID。
* - 单聊:接收方的用户 ID。
* - 群聊:群组 ID。
* - 聊天室:聊天室 ID。
* @param latitude 纬度。
* @param longitude 经度。
* @param chatType 会话类型。
* @params opt 消息扩展参数。
* - isChatThread: 是否是子区消息。默认不是子区消息。
* - isOnline: 是否为在线时收到的消息。
* - deliverOnlineOnly: 消息是否只投递给在线用户。
* - address: 地址信息。
*
* @returns 消息实例。
*/
public static createLocationMessage(
targetId: string,
latitude: string,
longitude: string,
chatType: ChatMessageChatType = ChatMessageChatType.PeerChat,
opt?: {
address: string;
isChatThread?: boolean;
isOnline?: boolean;
deliverOnlineOnly?: boolean;
receiverList?: string[];
}
): ChatMessage {
return ChatMessage.createSendMessage({
body: new ChatLocationMessageBody({
latitude: latitude,
longitude: longitude,
address: opt?.address ?? '',
}),
targetId: targetId,
chatType: chatType,
isChatThread: opt?.isChatThread,
isOnline: opt?.isOnline,
deliverOnlineOnly: opt?.deliverOnlineOnly,
receiverList: opt?.receiverList,
});
}
/**
* 创建一条待发送的命令消息。
*
* @param targetId 消息接收方的用户 ID。
* - 单聊:接收方的用户 ID。
* - 群聊:群组 ID。
* - 聊天室:聊天室 ID。
* @param action 命令内容。
* @param chatType 会话类型。
* @params opt 消息扩展参数。
* - isChatThread: 是否是子区消息。默认不是子区消息。
* - isOnline: 是否为在线时收到的消息。
* - deliverOnlineOnly: 消息是否只投递给在线用户。
*
* @returns 消息实例。
*/
public static createCmdMessage(
targetId: string,
action: string,
chatType: ChatMessageChatType = ChatMessageChatType.PeerChat,
opt?: {
isChatThread?: boolean;
isOnline?: boolean;
deliverOnlineOnly?: boolean;
receiverList?: string[];
}
): ChatMessage {
return ChatMessage.createSendMessage({
body: new ChatCmdMessageBody({
action: action,
}),
targetId: targetId,
chatType: chatType,
isChatThread: opt?.isChatThread,
isOnline: opt?.isOnline,
deliverOnlineOnly: opt?.deliverOnlineOnly,
receiverList: opt?.receiverList,
});
}
/**
* 创建一条待发送的自定义类型消息。
*
* @param targetId 消息接收方的用户 ID。
* - 单聊:接收方的用户 ID。
* - 群聊:群组 ID。
* - 聊天室:聊天室 ID。
* @param event 消息触发的自定义事件。
* @param chatType 会话类型。
* @params opt 消息扩展参数。
* - isChatThread: 是否是子区消息。默认不是子区消息。
* - isOnline: 是否为在线时收到的消息。
* - deliverOnlineOnly: 消息是否只投递给在线用户。
* - params: 自定义参数。key-value格式。
*
* @returns 消息实例。
*/
public static createCustomMessage(
targetId: string,
event: string,