@onyx-p/imlib-web 1.4.2 → 1.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.esm.js CHANGED
@@ -26527,6 +26527,17 @@ function requireProtobufjs () {
26527
26527
  var protobufjsExports = requireProtobufjs();
26528
26528
  var protobuf = /*@__PURE__*/getDefaultExportFromCjs(protobufjsExports);
26529
26529
 
26530
+ class IMConfig {
26531
+ uploader = undefined;
26532
+ getUploader() {
26533
+ return this.uploader;
26534
+ }
26535
+ setUploader(uploader) {
26536
+ this.uploader = uploader;
26537
+ }
26538
+ }
26539
+ var imConfig = new IMConfig();
26540
+
26530
26541
  const transSentAttrs2IReceivedMessage = (message, options, sentStatus = SentStatus.SENDING) => ({
26531
26542
  conversationType: options.conversation.conversationType,
26532
26543
  targetId: options.conversation.targetId,
@@ -26558,10 +26569,381 @@ function generateMessageId() {
26558
26569
  return 230000000000000 + UniqueLocalId;
26559
26570
  }
26560
26571
 
26572
+ const getBlobUrl = blob => {
26573
+ const URL = window.URL || window.webkitURL;
26574
+ return URL ? URL.createObjectURL(blob) : '';
26575
+ };
26576
+ const getBlobExtension = blob => {
26577
+ const name = blob.name;
26578
+ if (notEmptyString(name)) {
26579
+ return name.substring(name.lastIndexOf('.') + 1);
26580
+ }
26581
+ return blob.type.indexOf('/') > 0 ? blob.type.substring(blob.type.lastIndexOf('/') + 1) : '';
26582
+ };
26583
+ const blob2ArrayBuffer = blob => {
26584
+ if (isPromise(blob.arrayBuffer)) {
26585
+ return blob.arrayBuffer();
26586
+ }
26587
+ return new Promise((resolve, reject) => {
26588
+ const reader = new FileReader();
26589
+ reader.addEventListener('load', event => {
26590
+ if (event.target) {
26591
+ resolve(event.target.result);
26592
+ } else {
26593
+ reject('null');
26594
+ }
26595
+ });
26596
+ reader.addEventListener('error', error => reject(error));
26597
+ reader.readAsArrayBuffer(blob);
26598
+ });
26599
+ };
26600
+ const revokeBlobUrl = url => {
26601
+ const URL = window.URL || window.webkitURL;
26602
+ URL.revokeObjectURL(url);
26603
+ };
26604
+ const dataURL2Blob = url => {
26605
+ const type = url.match(/data:([^;]+)/)[1];
26606
+ const base64 = url.replace(/^[^,]+,/, '');
26607
+ const bstr = window.atob(base64);
26608
+ let n = bstr.length;
26609
+ const u8arr = new Uint8Array(bstr.length);
26610
+ while (n--) {
26611
+ u8arr[n] = bstr.charCodeAt(n);
26612
+ }
26613
+ return new Blob([u8arr], {
26614
+ type
26615
+ });
26616
+ };
26617
+
26618
+ const COMPRESS_LENGTH_MAX = 1680;
26619
+ const COMPRESS_LENGTH_MIN = 160;
26620
+ const getImageObj = link => {
26621
+ return new Promise((resolve, reject) => {
26622
+ const handle = image => {
26623
+ if (image.width !== 0 || image.height !== 0) {
26624
+ resolve(image);
26625
+ } else {
26626
+ reject();
26627
+ }
26628
+ };
26629
+ const image = new Image();
26630
+ image.addEventListener('load', event => handle(event.target));
26631
+ image.addEventListener('error', reject);
26632
+ image.src = link;
26633
+ if (image.complete) {
26634
+ handle(image);
26635
+ return;
26636
+ }
26637
+ const intervalId = setInterval(() => {
26638
+ if (image.complete) {
26639
+ handle(image);
26640
+ clearInterval(intervalId);
26641
+ }
26642
+ }, 40);
26643
+ });
26644
+ };
26645
+ const compress = (image, quality = 1, mineType = 'image/jpeg') => {
26646
+ quality = Math.min(1, quality);
26647
+ const width = image.width;
26648
+ const height = image.height;
26649
+ let compressWidth = width;
26650
+ let compressHeight = height;
26651
+ if (quality < 1) {
26652
+ const isheight = width < height;
26653
+ const zoom = isheight ? height / COMPRESS_LENGTH_MAX : width / COMPRESS_LENGTH_MAX;
26654
+ if (width > COMPRESS_LENGTH_MAX && height > COMPRESS_LENGTH_MAX) {
26655
+ if (isheight) {
26656
+ compressHeight = COMPRESS_LENGTH_MAX;
26657
+ compressWidth = compressWidth / zoom;
26658
+ } else {
26659
+ compressWidth = COMPRESS_LENGTH_MAX;
26660
+ compressHeight = compressHeight / zoom;
26661
+ }
26662
+ } else if (width > COMPRESS_LENGTH_MAX && height > COMPRESS_LENGTH_MIN) {
26663
+ compressWidth = COMPRESS_LENGTH_MAX;
26664
+ compressHeight = compressHeight / zoom;
26665
+ if (compressHeight < COMPRESS_LENGTH_MIN) {
26666
+ compressHeight = COMPRESS_LENGTH_MIN;
26667
+ compressWidth = width / (height / COMPRESS_LENGTH_MIN);
26668
+ }
26669
+ } else if (height > COMPRESS_LENGTH_MAX && width > COMPRESS_LENGTH_MIN) {
26670
+ compressHeight = COMPRESS_LENGTH_MAX;
26671
+ compressWidth = compressWidth / zoom;
26672
+ if (compressWidth < COMPRESS_LENGTH_MIN) {
26673
+ compressWidth = COMPRESS_LENGTH_MIN;
26674
+ compressHeight = height / (width / COMPRESS_LENGTH_MIN);
26675
+ }
26676
+ }
26677
+ }
26678
+ compressWidth = Math.floor(compressWidth);
26679
+ compressHeight = Math.floor(compressHeight);
26680
+ const canvas = document.createElement('canvas');
26681
+ canvas.width = compressWidth;
26682
+ canvas.height = compressHeight;
26683
+ const ctx = canvas.getContext('2d');
26684
+ ctx.fillStyle = '#FFF';
26685
+ ctx.fillRect(0, 0, compressWidth, compressHeight);
26686
+ ctx.drawImage(image, 0, 0, width, height, 0, 0, compressWidth, compressHeight);
26687
+ if (!mineType || mineType == 'image/png') {
26688
+ mineType = 'image/jpeg';
26689
+ }
26690
+ return {
26691
+ imageFile: dataURL2Blob(canvas.toDataURL(mineType, Math.min(0.92, quality))),
26692
+ compressWidth,
26693
+ compressHeight
26694
+ };
26695
+ };
26696
+ const getThumbnail = (img, thumbnailConfig) => {
26697
+ const canvas = document.createElement('canvas');
26698
+ const context = canvas.getContext('2d');
26699
+ const pos = calcPosition(img.width, img.height, thumbnailConfig);
26700
+ canvas.width = pos.w > thumbnailConfig.maxWidth ? thumbnailConfig.maxWidth : pos.w;
26701
+ canvas.height = pos.h > thumbnailConfig.maxHeight ? thumbnailConfig.maxHeight : pos.h;
26702
+ context?.drawImage(img, pos.x, pos.y, pos.w, pos.h);
26703
+ try {
26704
+ let base64 = canvas.toDataURL('image/jpeg', thumbnailConfig.quality);
26705
+ return dataURL2Blob(base64);
26706
+ } catch (e) {
26707
+ throw new Error(e);
26708
+ }
26709
+ };
26710
+ function calcPosition(width, height, thumbnailConfig) {
26711
+ const isheight = width < height;
26712
+ const scale = isheight ? height / width : width / height;
26713
+ let zoom;
26714
+ let x = 0;
26715
+ let y = 0;
26716
+ let w;
26717
+ let h;
26718
+ const gtScale = function () {
26719
+ if (isheight) {
26720
+ zoom = width / 100;
26721
+ w = 100;
26722
+ h = height / zoom;
26723
+ y = (h - thumbnailConfig.maxHeight) / 2;
26724
+ } else {
26725
+ zoom = height / 100;
26726
+ h = 100;
26727
+ w = width / zoom;
26728
+ x = (w - thumbnailConfig.maxWidth) / 2;
26729
+ }
26730
+ return {
26731
+ w: w,
26732
+ h: h,
26733
+ x: -x,
26734
+ y: -y
26735
+ };
26736
+ };
26737
+ const ltScale = function () {
26738
+ if (isheight) {
26739
+ zoom = height / thumbnailConfig.maxHeight;
26740
+ h = thumbnailConfig.maxHeight;
26741
+ w = width / zoom;
26742
+ } else {
26743
+ zoom = width / thumbnailConfig.maxWidth;
26744
+ w = thumbnailConfig.maxWidth;
26745
+ h = height / zoom;
26746
+ }
26747
+ return {
26748
+ w: w,
26749
+ h: h,
26750
+ x: -x,
26751
+ y: -y
26752
+ };
26753
+ };
26754
+ return scale > thumbnailConfig.scale ? gtScale() : ltScale();
26755
+ }
26756
+
26757
+ class Img {
26758
+ async create(file) {
26759
+ try {
26760
+ const blobUrl = getBlobUrl(file);
26761
+ const image = await getImageObj(blobUrl);
26762
+ revokeBlobUrl(blobUrl);
26763
+ const {
26764
+ imageFile: sentImageFile,
26765
+ compressWidth,
26766
+ compressHeight
26767
+ } = compress(image, 0.85, file.type);
26768
+ const config = {
26769
+ maxHeight: 160,
26770
+ maxWidth: 160,
26771
+ quality: 0.6,
26772
+ scale: 2.4
26773
+ };
26774
+ const thumbnail = getThumbnail(image, config);
26775
+ return {
26776
+ success: true,
26777
+ message: new ImageMessage({
26778
+ thumbnailObjectKey: getBlobUrl(thumbnail),
26779
+ originalObjectKey: getBlobUrl(sentImageFile),
26780
+ width: compressWidth,
26781
+ height: compressHeight
26782
+ }),
26783
+ files: [thumbnail, sentImageFile]
26784
+ };
26785
+ } catch (error) {
26786
+ return {
26787
+ success: false
26788
+ };
26789
+ }
26790
+ }
26791
+ updateMessageRemoteUrlProperty(msgContent, uploadedResult) {
26792
+ let content = deepClone(msgContent);
26793
+ const {
26794
+ urls: [thumbObjectKey, originalObjectKey],
26795
+ encryptedKey
26796
+ } = uploadedResult;
26797
+ content.originalObjectKey = originalObjectKey;
26798
+ content.thumbnailObjectKey = thumbObjectKey;
26799
+ content.encryptKey = encryptedKey;
26800
+ return content;
26801
+ }
26802
+ }
26803
+ class GIF {
26804
+ async create(file) {
26805
+ if (file.size > 2 * 1024 * 1024) {
26806
+ return {
26807
+ success: false
26808
+ };
26809
+ }
26810
+ try {
26811
+ const blobUrl = getBlobUrl(file);
26812
+ const image = await getImageObj(blobUrl);
26813
+ return {
26814
+ success: true,
26815
+ message: new GIFMessage({
26816
+ originalObjectKey: blobUrl,
26817
+ extension: getBlobExtension(file),
26818
+ width: image.width,
26819
+ height: image.height
26820
+ }),
26821
+ files: [file]
26822
+ };
26823
+ } catch {
26824
+ return {
26825
+ success: false
26826
+ };
26827
+ }
26828
+ }
26829
+ updateMessageRemoteUrlProperty(messageContent, uploadedResult) {
26830
+ let content = deepClone(messageContent);
26831
+ const {
26832
+ urls: [originalObjectKey],
26833
+ encryptedKey
26834
+ } = uploadedResult;
26835
+ content.originalObjectKey = originalObjectKey;
26836
+ content.encryptKey = encryptedKey;
26837
+ return content;
26838
+ }
26839
+ }
26840
+ let audioCtx;
26841
+ class Audio {
26842
+ create(file) {
26843
+ return blob2ArrayBuffer(file).then(this.getAudioInfo).then(info => {
26844
+ return {
26845
+ success: true,
26846
+ message: new VoiceMessage({
26847
+ audioObjectKey: getBlobUrl(file),
26848
+ length: info.duration,
26849
+ extension: getBlobExtension(file)
26850
+ }),
26851
+ files: [file]
26852
+ };
26853
+ }).catch(() => {
26854
+ return {
26855
+ success: false
26856
+ };
26857
+ });
26858
+ }
26859
+ updateMessageRemoteUrlProperty(messageContent, uploadedResult) {
26860
+ let content = deepClone(messageContent);
26861
+ const {
26862
+ urls: [audioObjectKey],
26863
+ encryptedKey
26864
+ } = uploadedResult;
26865
+ content.audioObjectKey = audioObjectKey;
26866
+ content.encryptKey = encryptedKey;
26867
+ return content;
26868
+ }
26869
+ getAudioInfo(buffer) {
26870
+ const ctx = audioCtx || new AudioContext();
26871
+ audioCtx = ctx;
26872
+ return new Promise((resolve, reject) => {
26873
+ ctx.decodeAudioData(buffer, function (audioBuffer) {
26874
+ resolve({
26875
+ duration: audioBuffer.duration,
26876
+ length: audioBuffer.length
26877
+ });
26878
+ }, reject);
26879
+ });
26880
+ }
26881
+ }
26882
+ class File {
26883
+ async create(file) {
26884
+ if (file.size > 100 * 1024 * 1024) {
26885
+ return {
26886
+ success: false
26887
+ };
26888
+ }
26889
+ return {
26890
+ success: true,
26891
+ message: new FileMessage({
26892
+ fileKey: '',
26893
+ title: file.name,
26894
+ extension: getBlobExtension(file),
26895
+ size: file.size
26896
+ }),
26897
+ files: [file]
26898
+ };
26899
+ }
26900
+ updateMessageRemoteUrlProperty(messageContent, uploadedResult) {
26901
+ let content = deepClone(messageContent);
26902
+ const {
26903
+ urls: [fileKey],
26904
+ encryptedKey
26905
+ } = uploadedResult;
26906
+ content.fileKey = fileKey;
26907
+ content.encryptKey = encryptedKey;
26908
+ return content;
26909
+ }
26910
+ }
26911
+ var FileType;
26912
+ (function (FileType) {
26913
+ FileType[FileType["IMAGE"] = 1] = "IMAGE";
26914
+ FileType[FileType["GIF"] = 2] = "GIF";
26915
+ FileType[FileType["AUDIO"] = 3] = "AUDIO";
26916
+ FileType[FileType["VIDEO"] = 4] = "VIDEO";
26917
+ FileType[FileType["FILE"] = 5] = "FILE";
26918
+ FileType[FileType["SIGHT"] = 6] = "SIGHT";
26919
+ })(FileType || (FileType = {}));
26920
+ const getMediaMessageCreator = fileType => {
26921
+ let creator;
26922
+ switch (fileType) {
26923
+ case FileType.IMAGE:
26924
+ creator = new Img();
26925
+ break;
26926
+ case FileType.AUDIO:
26927
+ creator = new Audio();
26928
+ break;
26929
+ case FileType.GIF:
26930
+ creator = new GIF();
26931
+ break;
26932
+ case FileType.FILE:
26933
+ creator = new File();
26934
+ break;
26935
+ }
26936
+ return creator;
26937
+ };
26938
+
26561
26939
  const MAX_MESSAGE_CONTENT_BYTES = 80 * 1024;
26562
26940
  async function sendMessage$1(conversation, message, options) {
26563
26941
  return internal_sendMessage(conversation, message, options);
26564
26942
  }
26943
+ const sendImageMessage$1 = createSendFunction.bind(null, FileType.IMAGE);
26944
+ const sendGIFMessage$1 = createSendFunction.bind(null, FileType.GIF);
26945
+ const sendHQVoiceMessage$1 = createSendFunction.bind(null, FileType.AUDIO);
26946
+ const sendFileMessage$1 = createSendFunction.bind(null, FileType.FILE);
26565
26947
  const sendRecallMessage = async (conversation, options) => {
26566
26948
  const dialogId = getFullDialogId(conversation);
26567
26949
  RecallMessageStore.addMessage(options.messageUId);
@@ -26587,6 +26969,47 @@ const sendRecallMessage = async (conversation, options) => {
26587
26969
  data: receivedMessage
26588
26970
  };
26589
26971
  };
26972
+ async function createSendFunction(fileType, conversation, msgBody, hooks, sendOptions) {
26973
+ const mediaMessageCreator = getMediaMessageCreator(fileType);
26974
+ if (!mediaMessageCreator) return {
26975
+ code: ErrorCode.NOT_SUPPORT
26976
+ };
26977
+ const {
26978
+ success,
26979
+ message: message1,
26980
+ files
26981
+ } = await mediaMessageCreator.create(msgBody.file);
26982
+ if (!success || !message1 || !files) {
26983
+ return {
26984
+ code: ErrorCode.MEDIA_EXCEPTION
26985
+ };
26986
+ }
26987
+ message1.content.user = msgBody.user;
26988
+ message1.content.extra = msgBody.extra;
26989
+ return internal_sendMessage(conversation, message1, sendOptions, {
26990
+ files,
26991
+ task: (message2, toBeUploadedFiles) => {
26992
+ return new Promise(resolve => {
26993
+ imConfig.getUploader().upload(toBeUploadedFiles, {
26994
+ onProgress: progress => hooks?.onProgress?.(progress),
26995
+ onError: () => {
26996
+ resolve({
26997
+ finished: false,
26998
+ message: message2
26999
+ });
27000
+ },
27001
+ onCompleted: result => {
27002
+ message2.content = mediaMessageCreator.updateMessageRemoteUrlProperty(message2.content, result);
27003
+ resolve({
27004
+ finished: true,
27005
+ message: hooks?.onComplete?.(result) ?? message2
27006
+ });
27007
+ }
27008
+ });
27009
+ });
27010
+ }
27011
+ });
27012
+ }
26590
27013
  async function internal_sendMessage(conversation, message, options, uploadOptions) {
26591
27014
  const checkResult = beforeSend(conversation, message, options);
26592
27015
  if (checkResult.code !== ErrorCode.SUCCESS || !checkResult.message || !checkResult.sentArgs) {
@@ -26599,7 +27022,7 @@ async function internal_sendMessage(conversation, message, options, uploadOption
26599
27022
  sentArgs
26600
27023
  } = checkResult;
26601
27024
  if (isDef(uploadOptions)) {
26602
- const uploadResult = await uploadOptions.task(sentMessage, uploadOptions.file);
27025
+ const uploadResult = await uploadOptions.task(sentMessage, uploadOptions.files);
26603
27026
  sentMessage = uploadResult.message;
26604
27027
  if (!uploadResult.finished) {
26605
27028
  return {
@@ -26734,6 +27157,9 @@ class IMClient extends EventEmitter {
26734
27157
  logger.setLogStdout(options.logStdout);
26735
27158
  }
26736
27159
  logger.debug('IMClient.init -> ');
27160
+ if (isDef(options.uploader)) {
27161
+ imConfig.setUploader(options.uploader);
27162
+ }
26737
27163
  this.libLoader = LibLoader.init(options);
26738
27164
  this.watch();
26739
27165
  IMClient.imClient = this;
@@ -26796,6 +27222,10 @@ class IMClient extends EventEmitter {
26796
27222
  }
26797
27223
  getServerTime = getServerTime$1;
26798
27224
  sendMessage = sendMessage$1;
27225
+ sendImageMessage = sendImageMessage$1;
27226
+ sendGIFMessage = sendGIFMessage$1;
27227
+ sendHQVoiceMessage = sendHQVoiceMessage$1;
27228
+ sendFileMessage = sendFileMessage$1;
26799
27229
  recallMsg = sendRecallMessage;
26800
27230
  async getRemoteHistoryMessages(conversation, options) {
26801
27231
  const dialogId = getFullDialogId(conversation);
@@ -28118,6 +28548,48 @@ const sendTextMessage = (conversation, messageBody, options) => {
28118
28548
  const message = new TextMessage(messageBody);
28119
28549
  return sendMessage(conversation, message, options);
28120
28550
  };
28551
+ const sendImageMessage = async (conversation, msgBody, hooks, sendOptions) => {
28552
+ assert('conversation', conversation, AssertRules.CONVERSATION, true);
28553
+ assert('uploader', imConfig.getUploader(), AssertRules.OBJECT, true);
28554
+ assert('msgBody.file', msgBody.file, file => file instanceof Blob && file.type.startsWith('image/'), true);
28555
+ const paramsStr = 'conversationType:' + conversation.conversationType + ',targetId:' + conversation.targetId;
28556
+ logger.debug('send message ->' + paramsStr);
28557
+ _logSendBefore(conversation);
28558
+ const response = await imClient.sendImageMessage(conversation, msgBody, hooks, sendOptions);
28559
+ _logSendError(conversation, response.code);
28560
+ return response;
28561
+ };
28562
+ const sendGIFMessage = async (conversation, msgBody, hooks, sendOptions) => {
28563
+ assert('conversation', conversation, AssertRules.CONVERSATION, true);
28564
+ assert('uploader', imConfig.getUploader(), AssertRules.OBJECT, true);
28565
+ assert('msgBody.file', msgBody.file, file => file instanceof Blob && file.type.endsWith('image/gif'), true);
28566
+ const paramsStr = 'conversationType:' + conversation.conversationType + ',targetId:' + conversation.targetId;
28567
+ logger.debug('send message ->' + paramsStr);
28568
+ _logSendBefore(conversation);
28569
+ const response = await imClient.sendGIFMessage(conversation, msgBody, hooks, sendOptions);
28570
+ _logSendError(conversation, response.code);
28571
+ return response;
28572
+ };
28573
+ const sendHQVoiceMessage = async (conversation, msgBody, hooks, sendOptions) => {
28574
+ assert('conversation', conversation, AssertRules.CONVERSATION, true);
28575
+ assert('uploader', imConfig.getUploader(), AssertRules.OBJECT, true);
28576
+ assert('msgBody.file', msgBody.file, file => file instanceof Blob, true);
28577
+ _logSendBefore(conversation);
28578
+ const response = await imClient.sendHQVoiceMessage(conversation, msgBody, hooks, sendOptions);
28579
+ _logSendError(conversation, response.code);
28580
+ return response;
28581
+ };
28582
+ const sendFileMessage = async (conversation, msgBody, hooks, sendOptions) => {
28583
+ assert('conversation', conversation, AssertRules.CONVERSATION, true);
28584
+ assert('uploader', imConfig.getUploader(), AssertRules.OBJECT, true);
28585
+ assert('msgBody.file', msgBody.file, file => file instanceof Blob, true);
28586
+ const paramsStr = 'conversationType:' + conversation.conversationType + ',targetId:' + conversation.targetId;
28587
+ logger.debug('send message ->' + paramsStr);
28588
+ _logSendBefore(conversation);
28589
+ const response = await imClient.sendFileMessage(conversation, msgBody, hooks, sendOptions);
28590
+ _logSendError(conversation, response.code);
28591
+ return response;
28592
+ };
28121
28593
  const recallMessage = async (conversation, options) => {
28122
28594
  assert('options.messageUId', options.messageUId, AssertRules.STRING, true);
28123
28595
  assert('options.sentTime', options.sentTime, AssertRules.NUMBER, true);
@@ -28217,4 +28689,4 @@ const _logSendError = (conversation, errorCode) => {
28217
28689
  }
28218
28690
  };
28219
28691
 
28220
- export { ConnectionStatus, ConversationType, ErrorCode, ErrorDesc, Events, FileMessage, GIFMessage, VoiceMessage as HQVoiceMessage, ImageMessage, LogLevel, MentionedType, MessageDirection, MessageTypes, NotificationLevel, NotificationStatus, ReceivedStatus, SentStatus, TextMessage, VideoMessage, addEventListener, clearAllMessagesUnreadStatus, clearHistoryMessages, clearMessagesUnreadStatus, clearTextMessageDraft, connect, deleteMessages, disconnect, getAllConversationState, getAllUnreadMentionedCount, getBlockedConversationList, getConnectionStatus, getConversation, getConversationList, getConversationNotificationLevel, getConversationNotificationStatus, getHistoryMessages, getRemoteHistoryMessages, getServerTime, getTextMessageDraft, getTopConversationList, getTotalUnreadCount, getUnreadCount, getUnreadMentionedCount, init, logOut, mockLogin, onceEventListener, recallMessage, registerMessageType, removeConversation, removeEventListener, request, saveTextMessageDraft, sendMessage, sendTextMessage, setConversationNotificationStatus, setConversationToTop, setUserLogged };
28692
+ export { ConnectionStatus, ConversationType, ErrorCode, ErrorDesc, Events, FileMessage, GIFMessage, VoiceMessage as HQVoiceMessage, ImageMessage, LogLevel, MentionedType, MessageDirection, MessageTypes, NotificationLevel, NotificationStatus, ReceivedStatus, SentStatus, TextMessage, VideoMessage, addEventListener, clearAllMessagesUnreadStatus, clearHistoryMessages, clearMessagesUnreadStatus, clearTextMessageDraft, connect, deleteMessages, disconnect, getAllConversationState, getAllUnreadMentionedCount, getBlockedConversationList, getConnectionStatus, getConversation, getConversationList, getConversationNotificationLevel, getConversationNotificationStatus, getHistoryMessages, getRemoteHistoryMessages, getServerTime, getTextMessageDraft, getTopConversationList, getTotalUnreadCount, getUnreadCount, getUnreadMentionedCount, init, logOut, mockLogin, onceEventListener, recallMessage, registerMessageType, removeConversation, removeEventListener, request, saveTextMessageDraft, sendFileMessage, sendGIFMessage, sendHQVoiceMessage, sendImageMessage, sendMessage, sendTextMessage, setConversationNotificationStatus, setConversationToTop, setUserLogged };
package/index.umd.js CHANGED
@@ -26533,6 +26533,17 @@
26533
26533
  var protobufjsExports = requireProtobufjs();
26534
26534
  var protobuf = /*@__PURE__*/getDefaultExportFromCjs(protobufjsExports);
26535
26535
 
26536
+ class IMConfig {
26537
+ uploader = undefined;
26538
+ getUploader() {
26539
+ return this.uploader;
26540
+ }
26541
+ setUploader(uploader) {
26542
+ this.uploader = uploader;
26543
+ }
26544
+ }
26545
+ var imConfig = new IMConfig();
26546
+
26536
26547
  const transSentAttrs2IReceivedMessage = (message, options, sentStatus = exports.SentStatus.SENDING) => ({
26537
26548
  conversationType: options.conversation.conversationType,
26538
26549
  targetId: options.conversation.targetId,
@@ -26564,10 +26575,381 @@
26564
26575
  return 230000000000000 + UniqueLocalId;
26565
26576
  }
26566
26577
 
26578
+ const getBlobUrl = blob => {
26579
+ const URL = window.URL || window.webkitURL;
26580
+ return URL ? URL.createObjectURL(blob) : '';
26581
+ };
26582
+ const getBlobExtension = blob => {
26583
+ const name = blob.name;
26584
+ if (notEmptyString(name)) {
26585
+ return name.substring(name.lastIndexOf('.') + 1);
26586
+ }
26587
+ return blob.type.indexOf('/') > 0 ? blob.type.substring(blob.type.lastIndexOf('/') + 1) : '';
26588
+ };
26589
+ const blob2ArrayBuffer = blob => {
26590
+ if (isPromise(blob.arrayBuffer)) {
26591
+ return blob.arrayBuffer();
26592
+ }
26593
+ return new Promise((resolve, reject) => {
26594
+ const reader = new FileReader();
26595
+ reader.addEventListener('load', event => {
26596
+ if (event.target) {
26597
+ resolve(event.target.result);
26598
+ } else {
26599
+ reject('null');
26600
+ }
26601
+ });
26602
+ reader.addEventListener('error', error => reject(error));
26603
+ reader.readAsArrayBuffer(blob);
26604
+ });
26605
+ };
26606
+ const revokeBlobUrl = url => {
26607
+ const URL = window.URL || window.webkitURL;
26608
+ URL.revokeObjectURL(url);
26609
+ };
26610
+ const dataURL2Blob = url => {
26611
+ const type = url.match(/data:([^;]+)/)[1];
26612
+ const base64 = url.replace(/^[^,]+,/, '');
26613
+ const bstr = window.atob(base64);
26614
+ let n = bstr.length;
26615
+ const u8arr = new Uint8Array(bstr.length);
26616
+ while (n--) {
26617
+ u8arr[n] = bstr.charCodeAt(n);
26618
+ }
26619
+ return new Blob([u8arr], {
26620
+ type
26621
+ });
26622
+ };
26623
+
26624
+ const COMPRESS_LENGTH_MAX = 1680;
26625
+ const COMPRESS_LENGTH_MIN = 160;
26626
+ const getImageObj = link => {
26627
+ return new Promise((resolve, reject) => {
26628
+ const handle = image => {
26629
+ if (image.width !== 0 || image.height !== 0) {
26630
+ resolve(image);
26631
+ } else {
26632
+ reject();
26633
+ }
26634
+ };
26635
+ const image = new Image();
26636
+ image.addEventListener('load', event => handle(event.target));
26637
+ image.addEventListener('error', reject);
26638
+ image.src = link;
26639
+ if (image.complete) {
26640
+ handle(image);
26641
+ return;
26642
+ }
26643
+ const intervalId = setInterval(() => {
26644
+ if (image.complete) {
26645
+ handle(image);
26646
+ clearInterval(intervalId);
26647
+ }
26648
+ }, 40);
26649
+ });
26650
+ };
26651
+ const compress = (image, quality = 1, mineType = 'image/jpeg') => {
26652
+ quality = Math.min(1, quality);
26653
+ const width = image.width;
26654
+ const height = image.height;
26655
+ let compressWidth = width;
26656
+ let compressHeight = height;
26657
+ if (quality < 1) {
26658
+ const isheight = width < height;
26659
+ const zoom = isheight ? height / COMPRESS_LENGTH_MAX : width / COMPRESS_LENGTH_MAX;
26660
+ if (width > COMPRESS_LENGTH_MAX && height > COMPRESS_LENGTH_MAX) {
26661
+ if (isheight) {
26662
+ compressHeight = COMPRESS_LENGTH_MAX;
26663
+ compressWidth = compressWidth / zoom;
26664
+ } else {
26665
+ compressWidth = COMPRESS_LENGTH_MAX;
26666
+ compressHeight = compressHeight / zoom;
26667
+ }
26668
+ } else if (width > COMPRESS_LENGTH_MAX && height > COMPRESS_LENGTH_MIN) {
26669
+ compressWidth = COMPRESS_LENGTH_MAX;
26670
+ compressHeight = compressHeight / zoom;
26671
+ if (compressHeight < COMPRESS_LENGTH_MIN) {
26672
+ compressHeight = COMPRESS_LENGTH_MIN;
26673
+ compressWidth = width / (height / COMPRESS_LENGTH_MIN);
26674
+ }
26675
+ } else if (height > COMPRESS_LENGTH_MAX && width > COMPRESS_LENGTH_MIN) {
26676
+ compressHeight = COMPRESS_LENGTH_MAX;
26677
+ compressWidth = compressWidth / zoom;
26678
+ if (compressWidth < COMPRESS_LENGTH_MIN) {
26679
+ compressWidth = COMPRESS_LENGTH_MIN;
26680
+ compressHeight = height / (width / COMPRESS_LENGTH_MIN);
26681
+ }
26682
+ }
26683
+ }
26684
+ compressWidth = Math.floor(compressWidth);
26685
+ compressHeight = Math.floor(compressHeight);
26686
+ const canvas = document.createElement('canvas');
26687
+ canvas.width = compressWidth;
26688
+ canvas.height = compressHeight;
26689
+ const ctx = canvas.getContext('2d');
26690
+ ctx.fillStyle = '#FFF';
26691
+ ctx.fillRect(0, 0, compressWidth, compressHeight);
26692
+ ctx.drawImage(image, 0, 0, width, height, 0, 0, compressWidth, compressHeight);
26693
+ if (!mineType || mineType == 'image/png') {
26694
+ mineType = 'image/jpeg';
26695
+ }
26696
+ return {
26697
+ imageFile: dataURL2Blob(canvas.toDataURL(mineType, Math.min(0.92, quality))),
26698
+ compressWidth,
26699
+ compressHeight
26700
+ };
26701
+ };
26702
+ const getThumbnail = (img, thumbnailConfig) => {
26703
+ const canvas = document.createElement('canvas');
26704
+ const context = canvas.getContext('2d');
26705
+ const pos = calcPosition(img.width, img.height, thumbnailConfig);
26706
+ canvas.width = pos.w > thumbnailConfig.maxWidth ? thumbnailConfig.maxWidth : pos.w;
26707
+ canvas.height = pos.h > thumbnailConfig.maxHeight ? thumbnailConfig.maxHeight : pos.h;
26708
+ context?.drawImage(img, pos.x, pos.y, pos.w, pos.h);
26709
+ try {
26710
+ let base64 = canvas.toDataURL('image/jpeg', thumbnailConfig.quality);
26711
+ return dataURL2Blob(base64);
26712
+ } catch (e) {
26713
+ throw new Error(e);
26714
+ }
26715
+ };
26716
+ function calcPosition(width, height, thumbnailConfig) {
26717
+ const isheight = width < height;
26718
+ const scale = isheight ? height / width : width / height;
26719
+ let zoom;
26720
+ let x = 0;
26721
+ let y = 0;
26722
+ let w;
26723
+ let h;
26724
+ const gtScale = function () {
26725
+ if (isheight) {
26726
+ zoom = width / 100;
26727
+ w = 100;
26728
+ h = height / zoom;
26729
+ y = (h - thumbnailConfig.maxHeight) / 2;
26730
+ } else {
26731
+ zoom = height / 100;
26732
+ h = 100;
26733
+ w = width / zoom;
26734
+ x = (w - thumbnailConfig.maxWidth) / 2;
26735
+ }
26736
+ return {
26737
+ w: w,
26738
+ h: h,
26739
+ x: -x,
26740
+ y: -y
26741
+ };
26742
+ };
26743
+ const ltScale = function () {
26744
+ if (isheight) {
26745
+ zoom = height / thumbnailConfig.maxHeight;
26746
+ h = thumbnailConfig.maxHeight;
26747
+ w = width / zoom;
26748
+ } else {
26749
+ zoom = width / thumbnailConfig.maxWidth;
26750
+ w = thumbnailConfig.maxWidth;
26751
+ h = height / zoom;
26752
+ }
26753
+ return {
26754
+ w: w,
26755
+ h: h,
26756
+ x: -x,
26757
+ y: -y
26758
+ };
26759
+ };
26760
+ return scale > thumbnailConfig.scale ? gtScale() : ltScale();
26761
+ }
26762
+
26763
+ class Img {
26764
+ async create(file) {
26765
+ try {
26766
+ const blobUrl = getBlobUrl(file);
26767
+ const image = await getImageObj(blobUrl);
26768
+ revokeBlobUrl(blobUrl);
26769
+ const {
26770
+ imageFile: sentImageFile,
26771
+ compressWidth,
26772
+ compressHeight
26773
+ } = compress(image, 0.85, file.type);
26774
+ const config = {
26775
+ maxHeight: 160,
26776
+ maxWidth: 160,
26777
+ quality: 0.6,
26778
+ scale: 2.4
26779
+ };
26780
+ const thumbnail = getThumbnail(image, config);
26781
+ return {
26782
+ success: true,
26783
+ message: new ImageMessage({
26784
+ thumbnailObjectKey: getBlobUrl(thumbnail),
26785
+ originalObjectKey: getBlobUrl(sentImageFile),
26786
+ width: compressWidth,
26787
+ height: compressHeight
26788
+ }),
26789
+ files: [thumbnail, sentImageFile]
26790
+ };
26791
+ } catch (error) {
26792
+ return {
26793
+ success: false
26794
+ };
26795
+ }
26796
+ }
26797
+ updateMessageRemoteUrlProperty(msgContent, uploadedResult) {
26798
+ let content = deepClone(msgContent);
26799
+ const {
26800
+ urls: [thumbObjectKey, originalObjectKey],
26801
+ encryptedKey
26802
+ } = uploadedResult;
26803
+ content.originalObjectKey = originalObjectKey;
26804
+ content.thumbnailObjectKey = thumbObjectKey;
26805
+ content.encryptKey = encryptedKey;
26806
+ return content;
26807
+ }
26808
+ }
26809
+ class GIF {
26810
+ async create(file) {
26811
+ if (file.size > 2 * 1024 * 1024) {
26812
+ return {
26813
+ success: false
26814
+ };
26815
+ }
26816
+ try {
26817
+ const blobUrl = getBlobUrl(file);
26818
+ const image = await getImageObj(blobUrl);
26819
+ return {
26820
+ success: true,
26821
+ message: new GIFMessage({
26822
+ originalObjectKey: blobUrl,
26823
+ extension: getBlobExtension(file),
26824
+ width: image.width,
26825
+ height: image.height
26826
+ }),
26827
+ files: [file]
26828
+ };
26829
+ } catch {
26830
+ return {
26831
+ success: false
26832
+ };
26833
+ }
26834
+ }
26835
+ updateMessageRemoteUrlProperty(messageContent, uploadedResult) {
26836
+ let content = deepClone(messageContent);
26837
+ const {
26838
+ urls: [originalObjectKey],
26839
+ encryptedKey
26840
+ } = uploadedResult;
26841
+ content.originalObjectKey = originalObjectKey;
26842
+ content.encryptKey = encryptedKey;
26843
+ return content;
26844
+ }
26845
+ }
26846
+ let audioCtx;
26847
+ class Audio {
26848
+ create(file) {
26849
+ return blob2ArrayBuffer(file).then(this.getAudioInfo).then(info => {
26850
+ return {
26851
+ success: true,
26852
+ message: new VoiceMessage({
26853
+ audioObjectKey: getBlobUrl(file),
26854
+ length: info.duration,
26855
+ extension: getBlobExtension(file)
26856
+ }),
26857
+ files: [file]
26858
+ };
26859
+ }).catch(() => {
26860
+ return {
26861
+ success: false
26862
+ };
26863
+ });
26864
+ }
26865
+ updateMessageRemoteUrlProperty(messageContent, uploadedResult) {
26866
+ let content = deepClone(messageContent);
26867
+ const {
26868
+ urls: [audioObjectKey],
26869
+ encryptedKey
26870
+ } = uploadedResult;
26871
+ content.audioObjectKey = audioObjectKey;
26872
+ content.encryptKey = encryptedKey;
26873
+ return content;
26874
+ }
26875
+ getAudioInfo(buffer) {
26876
+ const ctx = audioCtx || new AudioContext();
26877
+ audioCtx = ctx;
26878
+ return new Promise((resolve, reject) => {
26879
+ ctx.decodeAudioData(buffer, function (audioBuffer) {
26880
+ resolve({
26881
+ duration: audioBuffer.duration,
26882
+ length: audioBuffer.length
26883
+ });
26884
+ }, reject);
26885
+ });
26886
+ }
26887
+ }
26888
+ class File {
26889
+ async create(file) {
26890
+ if (file.size > 100 * 1024 * 1024) {
26891
+ return {
26892
+ success: false
26893
+ };
26894
+ }
26895
+ return {
26896
+ success: true,
26897
+ message: new FileMessage({
26898
+ fileKey: '',
26899
+ title: file.name,
26900
+ extension: getBlobExtension(file),
26901
+ size: file.size
26902
+ }),
26903
+ files: [file]
26904
+ };
26905
+ }
26906
+ updateMessageRemoteUrlProperty(messageContent, uploadedResult) {
26907
+ let content = deepClone(messageContent);
26908
+ const {
26909
+ urls: [fileKey],
26910
+ encryptedKey
26911
+ } = uploadedResult;
26912
+ content.fileKey = fileKey;
26913
+ content.encryptKey = encryptedKey;
26914
+ return content;
26915
+ }
26916
+ }
26917
+ var FileType;
26918
+ (function (FileType) {
26919
+ FileType[FileType["IMAGE"] = 1] = "IMAGE";
26920
+ FileType[FileType["GIF"] = 2] = "GIF";
26921
+ FileType[FileType["AUDIO"] = 3] = "AUDIO";
26922
+ FileType[FileType["VIDEO"] = 4] = "VIDEO";
26923
+ FileType[FileType["FILE"] = 5] = "FILE";
26924
+ FileType[FileType["SIGHT"] = 6] = "SIGHT";
26925
+ })(FileType || (FileType = {}));
26926
+ const getMediaMessageCreator = fileType => {
26927
+ let creator;
26928
+ switch (fileType) {
26929
+ case FileType.IMAGE:
26930
+ creator = new Img();
26931
+ break;
26932
+ case FileType.AUDIO:
26933
+ creator = new Audio();
26934
+ break;
26935
+ case FileType.GIF:
26936
+ creator = new GIF();
26937
+ break;
26938
+ case FileType.FILE:
26939
+ creator = new File();
26940
+ break;
26941
+ }
26942
+ return creator;
26943
+ };
26944
+
26567
26945
  const MAX_MESSAGE_CONTENT_BYTES = 80 * 1024;
26568
26946
  async function sendMessage$1(conversation, message, options) {
26569
26947
  return internal_sendMessage(conversation, message, options);
26570
26948
  }
26949
+ const sendImageMessage$1 = createSendFunction.bind(null, FileType.IMAGE);
26950
+ const sendGIFMessage$1 = createSendFunction.bind(null, FileType.GIF);
26951
+ const sendHQVoiceMessage$1 = createSendFunction.bind(null, FileType.AUDIO);
26952
+ const sendFileMessage$1 = createSendFunction.bind(null, FileType.FILE);
26571
26953
  const sendRecallMessage = async (conversation, options) => {
26572
26954
  const dialogId = getFullDialogId(conversation);
26573
26955
  RecallMessageStore.addMessage(options.messageUId);
@@ -26593,6 +26975,47 @@
26593
26975
  data: receivedMessage
26594
26976
  };
26595
26977
  };
26978
+ async function createSendFunction(fileType, conversation, msgBody, hooks, sendOptions) {
26979
+ const mediaMessageCreator = getMediaMessageCreator(fileType);
26980
+ if (!mediaMessageCreator) return {
26981
+ code: exports.ErrorCode.NOT_SUPPORT
26982
+ };
26983
+ const {
26984
+ success,
26985
+ message: message1,
26986
+ files
26987
+ } = await mediaMessageCreator.create(msgBody.file);
26988
+ if (!success || !message1 || !files) {
26989
+ return {
26990
+ code: exports.ErrorCode.MEDIA_EXCEPTION
26991
+ };
26992
+ }
26993
+ message1.content.user = msgBody.user;
26994
+ message1.content.extra = msgBody.extra;
26995
+ return internal_sendMessage(conversation, message1, sendOptions, {
26996
+ files,
26997
+ task: (message2, toBeUploadedFiles) => {
26998
+ return new Promise(resolve => {
26999
+ imConfig.getUploader().upload(toBeUploadedFiles, {
27000
+ onProgress: progress => hooks?.onProgress?.(progress),
27001
+ onError: () => {
27002
+ resolve({
27003
+ finished: false,
27004
+ message: message2
27005
+ });
27006
+ },
27007
+ onCompleted: result => {
27008
+ message2.content = mediaMessageCreator.updateMessageRemoteUrlProperty(message2.content, result);
27009
+ resolve({
27010
+ finished: true,
27011
+ message: hooks?.onComplete?.(result) ?? message2
27012
+ });
27013
+ }
27014
+ });
27015
+ });
27016
+ }
27017
+ });
27018
+ }
26596
27019
  async function internal_sendMessage(conversation, message, options, uploadOptions) {
26597
27020
  const checkResult = beforeSend(conversation, message, options);
26598
27021
  if (checkResult.code !== exports.ErrorCode.SUCCESS || !checkResult.message || !checkResult.sentArgs) {
@@ -26605,7 +27028,7 @@
26605
27028
  sentArgs
26606
27029
  } = checkResult;
26607
27030
  if (isDef(uploadOptions)) {
26608
- const uploadResult = await uploadOptions.task(sentMessage, uploadOptions.file);
27031
+ const uploadResult = await uploadOptions.task(sentMessage, uploadOptions.files);
26609
27032
  sentMessage = uploadResult.message;
26610
27033
  if (!uploadResult.finished) {
26611
27034
  return {
@@ -26740,6 +27163,9 @@
26740
27163
  logger.setLogStdout(options.logStdout);
26741
27164
  }
26742
27165
  logger.debug('IMClient.init -> ');
27166
+ if (isDef(options.uploader)) {
27167
+ imConfig.setUploader(options.uploader);
27168
+ }
26743
27169
  this.libLoader = LibLoader.init(options);
26744
27170
  this.watch();
26745
27171
  IMClient.imClient = this;
@@ -26802,6 +27228,10 @@
26802
27228
  }
26803
27229
  getServerTime = getServerTime$1;
26804
27230
  sendMessage = sendMessage$1;
27231
+ sendImageMessage = sendImageMessage$1;
27232
+ sendGIFMessage = sendGIFMessage$1;
27233
+ sendHQVoiceMessage = sendHQVoiceMessage$1;
27234
+ sendFileMessage = sendFileMessage$1;
26805
27235
  recallMsg = sendRecallMessage;
26806
27236
  async getRemoteHistoryMessages(conversation, options) {
26807
27237
  const dialogId = getFullDialogId(conversation);
@@ -28124,6 +28554,48 @@
28124
28554
  const message = new TextMessage(messageBody);
28125
28555
  return sendMessage(conversation, message, options);
28126
28556
  };
28557
+ const sendImageMessage = async (conversation, msgBody, hooks, sendOptions) => {
28558
+ assert('conversation', conversation, AssertRules.CONVERSATION, true);
28559
+ assert('uploader', imConfig.getUploader(), AssertRules.OBJECT, true);
28560
+ assert('msgBody.file', msgBody.file, file => file instanceof Blob && file.type.startsWith('image/'), true);
28561
+ const paramsStr = 'conversationType:' + conversation.conversationType + ',targetId:' + conversation.targetId;
28562
+ logger.debug('send message ->' + paramsStr);
28563
+ _logSendBefore(conversation);
28564
+ const response = await imClient.sendImageMessage(conversation, msgBody, hooks, sendOptions);
28565
+ _logSendError(conversation, response.code);
28566
+ return response;
28567
+ };
28568
+ const sendGIFMessage = async (conversation, msgBody, hooks, sendOptions) => {
28569
+ assert('conversation', conversation, AssertRules.CONVERSATION, true);
28570
+ assert('uploader', imConfig.getUploader(), AssertRules.OBJECT, true);
28571
+ assert('msgBody.file', msgBody.file, file => file instanceof Blob && file.type.endsWith('image/gif'), true);
28572
+ const paramsStr = 'conversationType:' + conversation.conversationType + ',targetId:' + conversation.targetId;
28573
+ logger.debug('send message ->' + paramsStr);
28574
+ _logSendBefore(conversation);
28575
+ const response = await imClient.sendGIFMessage(conversation, msgBody, hooks, sendOptions);
28576
+ _logSendError(conversation, response.code);
28577
+ return response;
28578
+ };
28579
+ const sendHQVoiceMessage = async (conversation, msgBody, hooks, sendOptions) => {
28580
+ assert('conversation', conversation, AssertRules.CONVERSATION, true);
28581
+ assert('uploader', imConfig.getUploader(), AssertRules.OBJECT, true);
28582
+ assert('msgBody.file', msgBody.file, file => file instanceof Blob, true);
28583
+ _logSendBefore(conversation);
28584
+ const response = await imClient.sendHQVoiceMessage(conversation, msgBody, hooks, sendOptions);
28585
+ _logSendError(conversation, response.code);
28586
+ return response;
28587
+ };
28588
+ const sendFileMessage = async (conversation, msgBody, hooks, sendOptions) => {
28589
+ assert('conversation', conversation, AssertRules.CONVERSATION, true);
28590
+ assert('uploader', imConfig.getUploader(), AssertRules.OBJECT, true);
28591
+ assert('msgBody.file', msgBody.file, file => file instanceof Blob, true);
28592
+ const paramsStr = 'conversationType:' + conversation.conversationType + ',targetId:' + conversation.targetId;
28593
+ logger.debug('send message ->' + paramsStr);
28594
+ _logSendBefore(conversation);
28595
+ const response = await imClient.sendFileMessage(conversation, msgBody, hooks, sendOptions);
28596
+ _logSendError(conversation, response.code);
28597
+ return response;
28598
+ };
28127
28599
  const recallMessage = async (conversation, options) => {
28128
28600
  assert('options.messageUId', options.messageUId, AssertRules.STRING, true);
28129
28601
  assert('options.sentTime', options.sentTime, AssertRules.NUMBER, true);
@@ -28265,6 +28737,10 @@
28265
28737
  exports.removeEventListener = removeEventListener;
28266
28738
  exports.request = request;
28267
28739
  exports.saveTextMessageDraft = saveTextMessageDraft;
28740
+ exports.sendFileMessage = sendFileMessage;
28741
+ exports.sendGIFMessage = sendGIFMessage;
28742
+ exports.sendHQVoiceMessage = sendHQVoiceMessage;
28743
+ exports.sendImageMessage = sendImageMessage;
28268
28744
  exports.sendMessage = sendMessage;
28269
28745
  exports.sendTextMessage = sendTextMessage;
28270
28746
  exports.setConversationNotificationStatus = setConversationNotificationStatus;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onyx-p/imlib-web",
3
- "version": "1.4.2",
3
+ "version": "1.4.3",
4
4
  "main": "index.umd.js",
5
5
  "module": "index.esm.js",
6
6
  "types": "types/index.d.ts",
package/types/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { ConnectionStatus, ConversationType, NotificationLevel, NotificationStatus } from './model/statusTypes';
2
- import { EventListeners, Events, GetHistoryMessageOption, GetHistoryMessageResult, IBaseConversationInfo, IConversationOption, IConversationState, IDeleteMessageOptions, IGetConversationListOptions, IInitOption, IPromiseResult, IRecallMessageOptions, ISendMessageOptions, ProfileInfo } from './types';
2
+ import { EventListeners, Events, GetHistoryMessageOption, GetHistoryMessageResult, IBaseConversationInfo, IConversationOption, IConversationState, IDeleteMessageOptions, IGetConversationListOptions, IImageMessageOption, IInitOption, IPromiseResult, IRecallMessageOptions, ISendBody, ISendMessageOptions, IUploadHooks, IUploadMessageOption, ProfileInfo } from './types';
3
3
  import { BaseMessage, IBaseMessageBody } from './model/baseMessage';
4
4
  import { ITextMessageBody } from './model/messages/textMessage';
5
5
  import IReceivedMessage from './model/iReceivedMessage';
@@ -182,15 +182,19 @@ export declare const sendTextMessage: (conversation: IConversationOption, messag
182
182
  /**
183
183
  * 发送图片消息
184
184
  */
185
+ export declare const sendImageMessage: (conversation: IConversationOption, msgBody: ISendBody, hooks?: IUploadHooks, sendOptions?: IImageMessageOption) => IPromiseResult<IReceivedMessage>;
185
186
  /**
186
187
  * 发送GIF消息
187
188
  */
189
+ export declare const sendGIFMessage: (conversation: IConversationOption, msgBody: ISendBody, hooks?: IUploadHooks, sendOptions?: IUploadMessageOption) => IPromiseResult<IReceivedMessage>;
188
190
  /**
189
191
  * 发送高清语音消息
190
192
  */
193
+ export declare const sendHQVoiceMessage: (conversation: IConversationOption, msgBody: ISendBody, hooks?: IUploadHooks | undefined, sendOptions?: IUploadMessageOption | undefined) => IPromiseResult<IReceivedMessage>;
191
194
  /**
192
195
  * 发送文件消息
193
196
  */
197
+ export declare const sendFileMessage: (conversation: IConversationOption, msgBody: ISendBody, hooks?: IUploadHooks, sendOptions?: IUploadMessageOption) => IPromiseResult<IReceivedMessage>;
194
198
  /**
195
199
  * 撤回消息
196
200
  * @param options
package/types/types.d.ts CHANGED
@@ -21,6 +21,7 @@ export type IInitOption = {
21
21
  */
22
22
  logStdout?: (logLevel: LogLevel, content: string) => void;
23
23
  serverInfo?: string;
24
+ uploader?: IUploader;
24
25
  };
25
26
  export interface ProfileInfo {
26
27
  deviceId: string;
@@ -140,9 +141,20 @@ export interface IUploadHooks {
140
141
  /**
141
142
  * 文件上传完成回调,可通过修改返回值以修改待发布的消息内容,如实现自定义消息
142
143
  */
143
- onComplete?: (fileInfo: {
144
- url: string;
145
- }) => void | BaseMessage;
144
+ onComplete?: (fileInfo: IUploadCompletedResult) => void | BaseMessage;
145
+ }
146
+ export type IUploadCompletedResult = {
147
+ urls: string[];
148
+ encryptedKey?: string;
149
+ };
150
+ export type IUploadCallback = {
151
+ onProgress?: (progress: number) => void;
152
+ onCompleted?: (result: IUploadCompletedResult) => void;
153
+ onError?: (reason: string) => void;
154
+ };
155
+ export interface IUploader {
156
+ upload(file: Blob[], callback: IUploadCallback): void;
157
+ cancel(): void;
146
158
  }
147
159
  /**
148
160
  * 文件消息配置